First version of spectrum_manager

This commit is contained in:
HanzZ 2011-06-26 17:38:31 +02:00
parent 9c294ea319
commit 7f01140b4f
8 changed files with 240 additions and 0 deletions

View file

@ -123,6 +123,7 @@ ADD_SUBDIRECTORY(include)
ADD_SUBDIRECTORY(spectrum)
ADD_SUBDIRECTORY(backends)
#ADD_SUBDIRECTORY(tests)
ADD_SUBDIRECTORY(spectrum_manager)
if(DOXYGEN_FOUND)
message("Docs : yes")

View file

@ -33,6 +33,7 @@
#define CONFIG_INT(PTR, KEY) (*PTR)[KEY].as<int>()
#define CONFIG_BOOL(PTR, KEY) (*PTR)[KEY].as<bool>()
#define CONFIG_LIST(PTR, KEY) (*PTR)[KEY].as<std::list<std::string> >()
#define CONFIG_VECTOR(PTR, KEY) (*PTR)[KEY].as<std::vector<std::string> >()
namespace Transport {

View file

@ -0,0 +1 @@
ADD_SUBDIRECTORY(src)

View file

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 2.6)
FILE(GLOB SRC *.cpp)
ADD_EXECUTABLE(spectrum_manager ${SRC})
target_link_libraries(spectrum_manager transport)
INSTALL(TARGETS spectrum_manager RUNTIME DESTINATION bin)
INSTALL(FILES
spectrum_manager.cfg
DESTINATION /etc/spectrum2
)

View file

@ -0,0 +1,87 @@
#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 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";
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");
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;
}
}
catch (std::runtime_error& e)
{
std::cout << desc << "\n";
return 1;
}
catch (...)
{
std::cout << desc << "\n";
return 1;
}
if (argc != 2) {
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::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(bind(&handleConnected, client));
client->onDisconnected.connect(bind(&handleDisconnected, client, _1));
// client->onMessageReceived.connect(bind(&handleMessageReceived, _1));
Swift::ClientOptions opt;
opt.allowPLAINWithoutTLS = true;
client->connect(opt);
// std::cout << *it << "\n";
}
eventLoop.run();
}

View file

@ -0,0 +1,50 @@
/**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "managerconfig.h"
#include <fstream>
using namespace boost::program_options;
bool ManagerConfig::load(const std::string &configfile, boost::program_options::options_description &opts) {
std::ifstream ifs(configfile.c_str());
if (!ifs.is_open())
return false;
opts.add_options()
("service.admin_username", value<std::string>()->default_value(""), "Administrator username.")
("service.admin_password", value<std::string>()->default_value(""), "Administrator password.")
("servers.server", value<std::vector<std::string> >()->multitoken(), "Server.")
;
store(parse_config_file(ifs, opts), m_variables);
notify(m_variables);
m_file = configfile;
onManagerConfigReloaded();
return true;
}
bool ManagerConfig::load(const std::string &configfile) {
options_description opts("Transport options");
return load(configfile, opts);
}

View file

@ -0,0 +1,80 @@
/**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#pragma once
#include <boost/program_options.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/signal.hpp>
/// Represents variable:value pairs.
typedef boost::program_options::variables_map Variables;
/// Represents config file.
/// It's used to load config file and allows others parts of libtransport to be configured
/// properly. ManagerConfig files are text files which use "ini" format. Variables are divided into multiple
/// sections. Every class is configurable with some variables which change its behavior. Check particular
/// class documentation to get a list of all relevant variables for that class.
class ManagerConfig {
public:
/// Constructor.
ManagerConfig() {}
/// Destructor
virtual ~ManagerConfig() {}
/// Loads data from config file.
/// You can pass your extra options which will be recognized by
/// the parser using opts parameter.
/// \param configfile path to config file
/// \param opts extra options which will be recognized by a parser
bool load(const std::string &configfile, boost::program_options::options_description &opts);
/// Loads data from config file.
/// This function loads only config variables needed by libtransport.
/// \see load(const std::string &, boost::program_options::options_description &)
/// \param configfile path to config file
bool load(const std::string &configfile);
/// Returns value of variable defined by key.
/// For variables in sections you can use "section.variable" key format.
/// \param key config variable name
const boost::program_options::variable_value &operator[] (const std::string &key) {
return m_variables[key];
}
/// Returns path to config file from which data were loaded.
const std::string &getManagerConfigFile() { return m_file; }
/// This signal is emitted when config is loaded/reloaded.
boost::signal<void ()> onManagerConfigReloaded;
private:
Variables m_variables;
std::string m_file;
};

View file

@ -0,0 +1,7 @@
[service]
admin_username=admin
admin_password=test
[servers]
server=localhost
server=icq.spectrum.im