Changeset - 8b6068588621
[Not reviewed]
0 1 2
HanzZ - 13 years ago 2012-09-12 10:18:51
hanzz.k@gmail.com
Split Template backend into more files
3 files changed with 100 insertions and 68 deletions:
0 comments (0 inline, 0 general)
backends/template/main.cpp
Show inline comments
 
#include "plugin.h"
 

	
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
@@ -19,71 +21,6 @@ using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
DEFINE_LOGGER(logger, "Backend Template");
 

	
 
// eventloop
 
Swift::SimpleEventLoop *loop_;
 

	
 
// Plugin
 
class TemplatePlugin;
 
TemplatePlugin * np = NULL;
 

	
 
class TemplatePlugin : public NetworkPlugin {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 

	
 
		TemplatePlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
			m_factories = new Swift::BoostNetworkFactories(loop);
 
			m_conn = m_factories->getConnectionFactory()->createConnection();
 
			m_conn->onDataRead.connect(boost::bind(&TemplatePlugin::_handleDataRead, this, _1));
 
			m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
			LOG4CXX_INFO(logger, "Starting the plugin.");
 
		}
 

	
 
		// NetworkPlugin uses this method to send the data to networkplugin server
 
		void sendData(const std::string &string) {
 
			m_conn->write(Swift::createSafeByteArray(string));
 
		}
 

	
 
		// This method has to call handleDataRead with all received data from network plugin server
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) {
 
			std::string d(data->begin(), data->end());
 
			handleDataRead(d);
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			handleConnected(user);
 
			LOG4CXX_INFO(logger, user << ": Added buddy - Echo.");
 
			handleBuddyChanged(user, "echo", "Echo", std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
		}
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") {
 
			LOG4CXX_INFO(logger, "Sending message from " << user << " to " << legacyName << ".");
 
			if (legacyName == "echo") {
 
				handleMessage(user, legacyName, message);
 
			}
 
		}
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
			LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
			handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
 
		}
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
 

	
 
		}
 

	
 
	private:
 
		Config *config;
 
};
 

	
 
#ifndef _WIN32
 

	
 
static void spectrum_sigchld_handler(int sig)
 
@@ -124,9 +61,8 @@ int main (int argc, char* argv[]) {
 
	Logging::initBackendLogging(cfg);
 

	
 
	Swift::SimpleEventLoop eventLoop;
 
	loop_ = &eventLoop;
 
	np = new TemplatePlugin(cfg, &eventLoop, host, port);
 
	loop_->run();
 
	Plugin p(cfg, &eventLoop, host, port);
 
	eventLoop.run();
 

	
 
	return 0;
 
}
backends/template/plugin.cpp
Show inline comments
 
new file 100644
 
#include "plugin.h"
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 

	
 
// Swiften
 
#include "Swiften/Swiften.h"
 

	
 
// Boost
 
#include <boost/algorithm/string.hpp>
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
DEFINE_LOGGER(logger, "Backend Template");
 

	
 
Plugin::Plugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
	this->config = config;
 
	m_factories = new Swift::BoostNetworkFactories(loop);
 
	m_conn = m_factories->getConnectionFactory()->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&Plugin::_handleDataRead, this, _1));
 
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
	LOG4CXX_INFO(logger, "Starting the plugin.");
 
}
 

	
 
// NetworkPlugin uses this method to send the data to networkplugin server
 
void Plugin::sendData(const std::string &string) {
 
	m_conn->write(Swift::createSafeByteArray(string));
 
}
 

	
 
// This method has to call handleDataRead with all received data from network plugin server
 
void Plugin::_handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) {
 
	std::string d(data->begin(), data->end());
 
	handleDataRead(d);
 
}
 

	
 
void Plugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
	handleConnected(user);
 
	LOG4CXX_INFO(logger, user << ": Added buddy - Echo.");
 
	handleBuddyChanged(user, "echo", "Echo", std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
}
 

	
 
void Plugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
}
 

	
 
void Plugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml) {
 
	LOG4CXX_INFO(logger, "Sending message from " << user << " to " << legacyName << ".");
 
	if (legacyName == "echo") {
 
		handleMessage(user, legacyName, message);
 
	}
 
}
 

	
 
void Plugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
	LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
	handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
 
}
 

	
 
void Plugin::handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
 

	
 
}
backends/template/plugin.h
Show inline comments
 
new file 100644
 
#pragma once
 

	
 
#include "Swiften/Swiften.h"
 

	
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 

	
 
class Plugin : public Transport::NetworkPlugin {
 
	public:
 
		Plugin(Transport::Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port);
 

	
 
		// NetworkPlugin uses this method to send the data to networkplugin server
 
		void sendData(const std::string &string);
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password);
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName);
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "");
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups);
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups);
 

	
 
	private:
 
		// This method has to call handleDataRead with all received data from network plugin server
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data);
 

	
 
	private:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
		Transport::Config *config;
 
};
0 comments (0 inline, 0 general)