Skip to content

Commit 7f01140

Browse files
committed
First version of spectrum_manager
1 parent 9c294ea commit 7f01140

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ ADD_SUBDIRECTORY(include)
123123
ADD_SUBDIRECTORY(spectrum)
124124
ADD_SUBDIRECTORY(backends)
125125
#ADD_SUBDIRECTORY(tests)
126+
ADD_SUBDIRECTORY(spectrum_manager)
126127

127128
if(DOXYGEN_FOUND)
128129
message("Docs : yes")

include/transport/config.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define CONFIG_INT(PTR, KEY) (*PTR)[KEY].as<int>()
3434
#define CONFIG_BOOL(PTR, KEY) (*PTR)[KEY].as<bool>()
3535
#define CONFIG_LIST(PTR, KEY) (*PTR)[KEY].as<std::list<std::string> >()
36+
#define CONFIG_VECTOR(PTR, KEY) (*PTR)[KEY].as<std::vector<std::string> >()
3637

3738
namespace Transport {
3839

spectrum_manager/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADD_SUBDIRECTORY(src)

spectrum_manager/src/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
FILE(GLOB SRC *.cpp)
3+
4+
ADD_EXECUTABLE(spectrum_manager ${SRC})
5+
6+
target_link_libraries(spectrum_manager transport)
7+
8+
INSTALL(TARGETS spectrum_manager RUNTIME DESTINATION bin)
9+
10+
INSTALL(FILES
11+
spectrum_manager.cfg
12+
DESTINATION /etc/spectrum2
13+
)

spectrum_manager/src/main.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "managerconfig.h"
2+
#include "transport/transport.h"
3+
#include "transport/usermanager.h"
4+
#include "transport/logger.h"
5+
#include "transport/sqlite3backend.h"
6+
#include "transport/userregistration.h"
7+
#include "transport/networkpluginserver.h"
8+
#include "Swiften/EventLoop/SimpleEventLoop.h"
9+
10+
using namespace Transport;
11+
12+
static int finished;
13+
14+
static void handleDisconnected(Swift::Client *client, const boost::optional<Swift::ClientError> &) {
15+
std::cout << "[ DISCONNECTED ] " << client->getJID().getDomain() << "\n";
16+
if (--finished == 0) {
17+
exit(0);
18+
}
19+
}
20+
21+
static void handleConnected(Swift::Client *client) {
22+
std::cout << "[ OK ] " << client->getJID().getDomain() << "\n";
23+
if (--finished == 0) {
24+
exit(0);
25+
}
26+
}
27+
28+
int main(int argc, char **argv)
29+
{
30+
ManagerConfig config;
31+
32+
boost::program_options::options_description desc("Usage: spectrum_manager <config_file.cfg>\nAllowed options");
33+
desc.add_options()
34+
("help,h", "help")
35+
;
36+
try
37+
{
38+
boost::program_options::variables_map vm;
39+
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
40+
boost::program_options::notify(vm);
41+
if(vm.count("help"))
42+
{
43+
std::cout << desc << "\n";
44+
return 1;
45+
}
46+
}
47+
catch (std::runtime_error& e)
48+
{
49+
std::cout << desc << "\n";
50+
return 1;
51+
}
52+
catch (...)
53+
{
54+
std::cout << desc << "\n";
55+
return 1;
56+
}
57+
58+
if (argc != 2) {
59+
std::cout << desc << "\n";
60+
return 1;
61+
}
62+
63+
64+
if (!config.load(argv[1])) {
65+
std::cerr << "Can't load configuration file.\n";
66+
return 1;
67+
}
68+
69+
Swift::SimpleEventLoop eventLoop;
70+
Swift::BoostNetworkFactories networkFactories(&eventLoop);
71+
72+
std::vector<std::string> servers = CONFIG_VECTOR(&config, "servers.server");
73+
for (std::vector<std::string>::const_iterator it = servers.begin(); it != servers.end(); it++) {
74+
finished++;
75+
Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + (*it), CONFIG_STRING(&config, "service.admin_password"), &networkFactories);
76+
client->setAlwaysTrustCertificates();
77+
// client->onConnected.connect(bind(&handleConnected, client));
78+
client->onDisconnected.connect(bind(&handleDisconnected, client, _1));
79+
// client->onMessageReceived.connect(bind(&handleMessageReceived, _1));
80+
Swift::ClientOptions opt;
81+
opt.allowPLAINWithoutTLS = true;
82+
client->connect(opt);
83+
// std::cout << *it << "\n";
84+
}
85+
86+
eventLoop.run();
87+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* libtransport -- C++ library for easy XMPP Transports development
3+
*
4+
* Copyright (C) 2011, Jan Kaluza <[email protected]>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19+
*/
20+
21+
#include "managerconfig.h"
22+
#include <fstream>
23+
24+
using namespace boost::program_options;
25+
26+
bool ManagerConfig::load(const std::string &configfile, boost::program_options::options_description &opts) {
27+
std::ifstream ifs(configfile.c_str());
28+
if (!ifs.is_open())
29+
return false;
30+
31+
opts.add_options()
32+
("service.admin_username", value<std::string>()->default_value(""), "Administrator username.")
33+
("service.admin_password", value<std::string>()->default_value(""), "Administrator password.")
34+
("servers.server", value<std::vector<std::string> >()->multitoken(), "Server.")
35+
;
36+
37+
store(parse_config_file(ifs, opts), m_variables);
38+
notify(m_variables);
39+
40+
m_file = configfile;
41+
42+
onManagerConfigReloaded();
43+
44+
return true;
45+
}
46+
47+
bool ManagerConfig::load(const std::string &configfile) {
48+
options_description opts("Transport options");
49+
return load(configfile, opts);
50+
}

spectrum_manager/src/managerconfig.h

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* libtransport -- C++ library for easy XMPP Transports development
3+
*
4+
* Copyright (C) 2011, Jan Kaluza <[email protected]>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19+
*/
20+
21+
#pragma once
22+
23+
#include <boost/program_options.hpp>
24+
#include <boost/foreach.hpp>
25+
#include <boost/format.hpp>
26+
#include <boost/algorithm/string.hpp>
27+
#include <boost/assign.hpp>
28+
#include <boost/bind.hpp>
29+
#include <boost/signal.hpp>
30+
31+
/// Represents variable:value pairs.
32+
typedef boost::program_options::variables_map Variables;
33+
34+
/// Represents config file.
35+
36+
/// It's used to load config file and allows others parts of libtransport to be configured
37+
/// properly. ManagerConfig files are text files which use "ini" format. Variables are divided into multiple
38+
/// sections. Every class is configurable with some variables which change its behavior. Check particular
39+
/// class documentation to get a list of all relevant variables for that class.
40+
class ManagerConfig {
41+
public:
42+
/// Constructor.
43+
ManagerConfig() {}
44+
45+
/// Destructor
46+
virtual ~ManagerConfig() {}
47+
48+
/// Loads data from config file.
49+
50+
/// You can pass your extra options which will be recognized by
51+
/// the parser using opts parameter.
52+
/// \param configfile path to config file
53+
/// \param opts extra options which will be recognized by a parser
54+
bool load(const std::string &configfile, boost::program_options::options_description &opts);
55+
56+
/// Loads data from config file.
57+
58+
/// This function loads only config variables needed by libtransport.
59+
/// \see load(const std::string &, boost::program_options::options_description &)
60+
/// \param configfile path to config file
61+
bool load(const std::string &configfile);
62+
63+
/// Returns value of variable defined by key.
64+
65+
/// For variables in sections you can use "section.variable" key format.
66+
/// \param key config variable name
67+
const boost::program_options::variable_value &operator[] (const std::string &key) {
68+
return m_variables[key];
69+
}
70+
71+
/// Returns path to config file from which data were loaded.
72+
const std::string &getManagerConfigFile() { return m_file; }
73+
74+
/// This signal is emitted when config is loaded/reloaded.
75+
boost::signal<void ()> onManagerConfigReloaded;
76+
77+
private:
78+
Variables m_variables;
79+
std::string m_file;
80+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[service]
2+
admin_username=admin
3+
admin_password=test
4+
5+
[servers]
6+
server=localhost
7+
server=icq.spectrum.im

0 commit comments

Comments
 (0)