Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HTTPRequestQueue.cpp « cpp « plugin - github.com/SpectrumIM/spectrum2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de298806a70ca2186f9e335f66cae79fdc73ba64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "transport/HTTPRequestQueue.h"
#include "transport/HTTPRequest.h"
#include "transport/Transport.h"

namespace Transport {

DEFINE_LOGGER(httpRequestQueueLogger, "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(httpRequestQueueLogger, m_user << ": Queue is empty.");
		m_req = NULL;
		m_queueTimer->stop();
		return;
	}

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

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

	LOG4CXX_INFO(httpRequestQueueLogger, 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(httpRequestQueueLogger, m_user << ": Request '" << req->getURL() << "' queued.");
	}
}


}