Files @ daeb6dfc9a9f
Branch filter:

Location: libtransport.git/libtransport/HTTPRequestQueue.cpp

Conrad Kostecki
Enable support for Qt5

Since Qt4 is EOL, we should also support libcommuni build with Qt5.
In order not to break Fedora docker builds, -DENABLE_QT4 is introduced.

When set to 'ON', support for Qt4 is being enabled,
otherwise support for Qt5 is enabled.

Signed-off-by: Conrad Kostecki <conrad@kostecki.com>
#include "transport/HTTPRequestQueue.h"
#include "transport/HTTPRequest.h"
#include "transport/Transport.h"

namespace Transport {

DEFINE_LOGGER(logger, "HTTPRequestQueue")

HTTPRequestQueue::HTTPRequestQueue(Component *component, const std::string &user, int delay) {
	m_delay = delay;
	m_req = NULL;
	m_user = user;

	m_queueTimer = component->getNetworkFactories()->getTimerFactory()->createTimer(500);
	m_queueTimer->onTick.connect(boost::bind(&HTTPRequestQueue::sendNextRequest, this));
}

HTTPRequestQueue::~HTTPRequestQueue() {
	m_queueTimer->stop();

	if (m_req) {
		m_req->onRequestFinished.disconnect(boost::bind(&HTTPRequestQueue::handleRequestFinished, this));
	}
}

void HTTPRequestQueue::handleRequestFinished() {
	m_req = NULL;
	m_queueTimer->start();
}

void HTTPRequestQueue::sendNextRequest() {
	if (m_queue.empty()) {
		LOG4CXX_INFO(logger, m_user << ": Queue is empty.");
		m_req = NULL;
		m_queueTimer->stop();
		return;
	}

	if (m_req) {
		LOG4CXX_INFO(logger, m_user << ": There is already a request being handled.");
		return;
	}

	m_req = m_queue.front();
	m_queue.pop();

	LOG4CXX_INFO(logger, m_user << ": Starting request '" << m_req->getURL() << "'.");
	m_req->onRequestFinished.connect(boost::bind(&HTTPRequestQueue::handleRequestFinished, this));
	m_req->execute();
}

void HTTPRequestQueue::queueRequest(HTTPRequest *req) {
	m_queue.push(req);

	if (!m_req) {
		sendNextRequest();
	}
	else {
		LOG4CXX_INFO(logger, m_user << ": Request '" << req->getURL() << "' queued.");
	}
}


}