Changeset - d3ccb9f195c8
[Not reviewed]
0 3 0
HanzZ - 14 years ago 2011-06-26 23:00:05
hanzz.k@gmail.com
Better spectrum_manager
3 files changed with 30 insertions and 5 deletions:
0 comments (0 inline, 0 general)
spectrum/src/main.cpp
Show inline comments
 
#include "transport/config.h"
 
#include "transport/transport.h"
 
#include "transport/usermanager.h"
 
#include "transport/logger.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/userregistration.h"
 
#include "transport/networkpluginserver.h"
 
#include "transport/admininterface.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
 
using namespace Transport;
 
 
int main(int argc, char **argv)
 
{
 
	Config config;
 
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
 
	desc.add_options()
 
		("help,h", "help")
 
		("no-daemonize,n", "Do not run spectrum as daemon")
 
@@ -63,16 +64,18 @@ int main(int argc, char **argv)
 
		if (!storageBackend->connect()) {
 
			std::cerr << "Can't connect to database.\n";
 
		}
 
	}
 
 
	UserManager userManager(&transport, storageBackend);
 
	if (storageBackend) {
 
		UserRegistration userRegistration(&transport, &userManager, storageBackend);
 
		logger.setUserRegistration(&userRegistration);
 
	}
 
	logger.setUserManager(&userManager);
 
 
	AdminInterface adminInterface(&transport, storageBackend);
 
 
	NetworkPluginServer plugin(&transport, &config, &userManager);
 
 
	eventLoop.run();
 
}
spectrum_manager/src/main.cpp
Show inline comments
 
#include "managerconfig.h"
 
#include "transport/transport.h"
 
#include "transport/usermanager.h"
 
#include "transport/logger.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/userregistration.h"
 
#include "transport/networkpluginserver.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
 
using namespace Transport;
 
 
static int finished;
 
static std::string *m;
 
 
static void handleDisconnected(Swift::Client *client, const boost::optional<Swift::ClientError> &) {
 
	std::cout << "[ DISCONNECTED ] " << client->getJID().getDomain() << "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
 
static void handleConnected(Swift::Client *client) {
 
	std::cout << "[      OK      ] " << client->getJID().getDomain() << "\n";
 
	boost::shared_ptr<Swift::Message> message(new Swift::Message());
 
	message->setTo(client->getJID().getDomain());
 
	message->setFrom(client->getJID());
 
	message->setBody(*m);
 
 
	client->sendMessage(message);
 
}
 
 
static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message) {
 
	std::cout << "[      OK      ] " << client->getJID().getDomain() << ": " << message->getBody() <<  "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
 
int main(int argc, char **argv)
 
{
 
	ManagerConfig config;
 
 
	boost::program_options::options_description desc("Usage: spectrum_manager <config_file.cfg>\nAllowed options");
 
	boost::program_options::options_description desc("Usage: spectrum_manager <config_file.cfg> <command>\nAllowed options");
 
	desc.add_options()
 
		("help,h", "help")
 
		;
 
	try
 
	{
 
		boost::program_options::variables_map vm;
 
		boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
 
		boost::program_options::notify(vm);
 
		if(vm.count("help"))
 
		{
 
			std::cout << desc << "\n";
 
			return 1;
 
@@ -46,42 +56,45 @@ int main(int argc, char **argv)
 
	}
 
	catch (std::runtime_error& e)
 
	{
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
	catch (...)
 
	{
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
 
	if (argc != 2) {
 
	if (argc != 3) {
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
 
 
	if (!config.load(argv[1])) {
 
		std::cerr << "Can't load configuration file.\n";
 
		return 1;
 
	}
 
 
	Swift::SimpleEventLoop eventLoop;
 
	Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
 
	std::string message = argv[2];
 
	m = &message;
 
 
	std::vector<std::string> servers = CONFIG_VECTOR(&config, "servers.server");
 
	for (std::vector<std::string>::const_iterator it = servers.begin(); it != servers.end(); it++) {
 
		finished++;
 
		Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + (*it), CONFIG_STRING(&config, "service.admin_password"), &networkFactories);
 
		client->setAlwaysTrustCertificates();
 
 		client->onConnected.connect(boost::bind(&handleConnected, client));
 
		client->onDisconnected.connect(bind(&handleDisconnected, client, _1));
 
// 		client->onMessageReceived.connect(bind(&handleMessageReceived, _1));
 
		client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1));
 
		Swift::ClientOptions opt;
 
		opt.allowPLAINWithoutTLS = true;
 
		client->connect(opt);
 
// 		std::cout << *it << "\n";
 
	}
 
 
	eventLoop.run();
 
}
src/admininterface.cpp
Show inline comments
 
@@ -37,19 +37,28 @@ AdminInterface::AdminInterface(Component *component, StorageBackend *storageBack
 
	m_component = component;
 
	m_storageBackend = storageBackend;
 

	
 
	m_component->getStanzaChannel()->onMessageReceived.connect(bind(&AdminInterface::handleMessageReceived, this, _1));
 
}
 

	
 
AdminInterface::~AdminInterface() {
 
}
 

	
 
void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
 
	if (!message->getTo().getNode().empty())
 
		return;
 
	if (message->getFrom().getNode() != CONFIG_STRING(m_component->getConfig(), "service.admin_username"))
 

	
 
	if (message->getFrom().getNode() != CONFIG_STRING(m_component->getConfig(), "service.admin_username")) {
 
		LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().getNode());
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Message from admin received");
 
	message->setTo(message->getFrom());
 
	message->setFrom(m_component->getJID());
 

	
 
	message->setBody("Unknown command");
 

	
 
	m_component->getStanzaChannel()->sendMessage(message);
 
}
 

	
 
}
0 comments (0 inline, 0 general)