Changeset - eb8f7ddad957
[Not reviewed]
0 4 0
Jan Kaluza - 9 years ago 2016-02-01 08:17:16
jkaluza@redhat.com
Slack: Better HTTPRequests logging
4 files changed with 16 insertions and 7 deletions:
0 comments (0 inline, 0 general)
include/transport/HTTPRequest.h
Show inline comments
 
@@ -30,24 +30,28 @@ class HTTPRequest : public Thread {
 

	
 
		void setProxy(std::string, std::string, std::string, std::string);
 
		bool execute();
 
		bool execute(rapidjson::Document &json);
 
		std::string getError() {return std::string(curl_errorbuffer);}
 
		const std::string &getRawData() {
 
			return m_data;
 
		}
 

	
 
		void run();
 
		void finalize();
 

	
 
		const std::string &getURL() {
 
			return m_url;
 
		}
 

	
 
		boost::signal<void ()> onRequestFinished;
 

	
 
	private:
 
		bool init();
 
		bool GET(std::string url, std::string &output);
 
		bool GET(std::string url, rapidjson::Document &json);
 

	
 

	
 
		CURL *curlhandle;
 
		char curl_errorbuffer[1024];
 
		std::string error;
 
		std::string callbackdata;
include/transport/HTTPRequestQueue.h
Show inline comments
 
@@ -9,32 +9,33 @@
 
#include <string.h>
 
#include "rapidjson/document.h"
 

	
 
#include "Swiften/Network/Timer.h"
 

	
 
namespace Transport {
 

	
 
class HTTPRequest;
 
class Component;
 

	
 
class HTTPRequestQueue {
 
	public:
 
		HTTPRequestQueue(Component *component, int delayBetweenRequests = 1);
 
		HTTPRequestQueue(Component *component, const std::string &user, int delayBetweenRequests = 1);
 

	
 
		virtual ~HTTPRequestQueue();
 

	
 
		void queueRequest(HTTPRequest *req);
 

	
 
		void sendNextRequest();
 

	
 
	private:
 
		void handleRequestFinished();
 

	
 
	private:
 
		int m_delay;
 
		std::queue<HTTPRequest *> m_queue;
 
		HTTPRequest *m_req;
 
		Swift::Timer::ref m_queueTimer;
 
		std::string m_user;
 
};
 

	
 
}
 

	
libtransport/HTTPRequestQueue.cpp
Show inline comments
 
#include "transport/HTTPRequestQueue.h"
 
#include "transport/HTTPRequest.h"
 
#include "transport/Transport.h"
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "HTTPRequestQueue")
 

	
 
HTTPRequestQueue::HTTPRequestQueue(Component *component, int delay) {
 
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, "queue is empty");
 
		LOG4CXX_INFO(logger, m_user << ": Queue is empty.");
 
		m_req = NULL;
 
		m_queueTimer->stop();
 
		return;
 
	}
 

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

	
 
	LOG4CXX_INFO(logger, "Starting new request");
 
	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);
 

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

	
 

	
 
}
spectrum/src/frontends/slack/SlackAPI.cpp
Show inline comments
 
@@ -62,25 +62,25 @@ DEFINE_LOGGER(logger, "SlackAPI");
 
#define GET_OBJECT(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \
 
	if (!NAME.IsObject()) { \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \
 
		return; \
 
	}
 

	
 
#define STORE_STRING_OPTIONAL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
	std::string NAME; \
 
	if (NAME##_tmp.IsString()) {  \
 
		 NAME = NAME##_tmp.GetString(); \
 
	}
 

	
 
SlackAPI::SlackAPI(Component *component, SlackIdManager *idManager, const std::string &token, const std::string &domain) : HTTPRequestQueue(component) {
 
SlackAPI::SlackAPI(Component *component, SlackIdManager *idManager, const std::string &token, const std::string &domain) : HTTPRequestQueue(component, domain) {
 
	m_component = component;
 
	m_token = token;
 
	m_idManager = idManager;
 
	m_domain = domain;
 
}
 

	
 
SlackAPI::~SlackAPI() {
 
}
 

	
 
void SlackAPI::handleSendMessage(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	LOG4CXX_INFO(logger, data);
 
}
0 comments (0 inline, 0 general)