Skip to content

Commit 51d3a12

Browse files
committed
WIP
1 parent e55d3e1 commit 51d3a12

20 files changed

+70
-176
lines changed

backends/libcommuni/CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ add_executable(spectrum2_libcommuni_backend ${SRC})
1313

1414
if(NOT WIN32)
1515
if(ENABLE_QT4)
16-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport pthread)
16+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin pthread)
1717
else()
18-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport pthread)
18+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin pthread)
1919
endif()
2020
else()
2121
if(ENABLE_QT4)
22-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport)
22+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin)
2323
else()
24-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport)
24+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin)
2525
endif()
2626
endif()
2727

backends/swiften/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ file(GLOB SRC *.cpp)
33
add_executable(spectrum2_swiften_backend ${SRC})
44

55
if(NOT WIN32)
6-
target_link_libraries(spectrum2_swiften_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
6+
target_link_libraries(spectrum2_swiften_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
77
else()
8-
target_link_libraries(spectrum2_swiften_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
8+
target_link_libraries(spectrum2_swiften_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
99
endif()
1010

1111
install(TARGETS spectrum2_swiften_backend RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})

backends/swiften/main.cpp

+3-27
Original file line numberDiff line numberDiff line change
@@ -80,36 +80,15 @@ class SwiftenPlugin : public NetworkPlugin, Swift::XMPPParserClient {
8080
Swift::XMPPParser *m_xmppParser;
8181
Swift::FullPayloadParserFactoryCollection m_collection2;
8282

83-
SwiftenPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
83+
SwiftenPlugin(Config *config, const std::string &host, int port) : NetworkPlugin() {
8484
this->config = config;
8585
m_firstPing = true;
86-
m_factories = new Swift::BoostNetworkFactories(loop);
87-
m_conn = m_factories->getConnectionFactory()->createConnection();
88-
m_conn->onDataRead.connect(boost::bind(&SwiftenPlugin::_handleDataRead, this, _1));
89-
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));
9086
serializer = new Swift::XMPPSerializer(&collection, Swift::ClientStreamType, false);
9187
m_xmppParser = new Swift::XMPPParser(this, &m_collection2, m_factories->getXMLParserFactory());
9288
m_xmppParser->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
9389

9490
LOG4CXX_INFO(logger, "Starting the plugin.");
95-
}
96-
97-
// NetworkPlugin uses this method to send the data to networkplugin server
98-
void sendData(const std::string &string) {
99-
m_conn->write(Swift::createSafeByteArray(string));
100-
}
101-
102-
// This method has to call handleDataRead with all received data from network plugin server
103-
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data) {
104-
if (m_firstPing) {
105-
m_firstPing = false;
106-
NetworkPlugin::PluginConfig cfg;
107-
cfg.setRawXML(true);
108-
cfg.setNeedRegistration(false);
109-
sendConfig(cfg);
110-
}
111-
std::string d(data->begin(), data->end());
112-
handleDataRead(d);
91+
connect(host, std::to_string(port));
11392
}
11493

11594
void handleStreamStart(const Swift::ProtocolHeader&) {}
@@ -446,10 +425,7 @@ int main (int argc, char* argv[]) {
446425

447426
Logging::initBackendLogging(cfg);
448427

449-
Swift::SimpleEventLoop eventLoop;
450-
loop_ = &eventLoop;
451-
np = new SwiftenPlugin(cfg, &eventLoop, host, port);
452-
loop_->run();
428+
np = new SwiftenPlugin(cfg, host, port);
453429

454430
return 0;
455431
}

backends/template/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ add_executable(spectrum2_template_backend ${SRC})
44

55
if(CMAKE_COMPILER_IS_GNUCXX)
66
if(NOT WIN32)
7-
target_link_libraries(spectrum2_template_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
7+
target_link_libraries(spectrum2_template_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
88
else()
9-
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
9+
target_link_libraries(spectrum2_template_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
1010
endif()
1111
else()
12-
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
12+
target_link_libraries(spectrum2_template_backend transport-plugin ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
1313
endif()
1414

1515
#install(TARGETS spectrum2_template_backend RUNTIME DESTINATION bin)

backends/template/main.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include "transport/NetworkPlugin.h"
66
#include "transport/Logging.h"
77

8-
// Swiften
9-
#include "Swiften/Swiften.h"
10-
118
#ifndef _WIN32
129
// for signal handler
1310
#include "unistd.h"
@@ -17,6 +14,7 @@
1714
#endif
1815
// Boost
1916
#include <boost/algorithm/string.hpp>
17+
#include <boost/filesystem.hpp>
2018
using namespace boost::filesystem;
2119
using namespace boost::program_options;
2220
using namespace Transport;
@@ -60,9 +58,7 @@ int main (int argc, char* argv[]) {
6058

6159
Logging::initBackendLogging(cfg);
6260

63-
Swift::SimpleEventLoop eventLoop;
64-
Plugin p(cfg, &eventLoop, host, port);
65-
eventLoop.run();
61+
Plugin p(cfg, host, port);
6662

6763
return 0;
6864
}

backends/template/plugin.cpp

+3-20
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,19 @@
44
#include "transport/NetworkPlugin.h"
55
#include "transport/Logging.h"
66

7-
// Swiften
8-
#include "Swiften/Swiften.h"
9-
107
// Boost
118
#include <boost/algorithm/string.hpp>
9+
#include <boost/filesystem.hpp>
1210
using namespace boost::filesystem;
1311
using namespace boost::program_options;
1412
using namespace Transport;
1513

1614
DEFINE_LOGGER(logger, "Backend Template");
1715

18-
Plugin::Plugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
16+
Plugin::Plugin(Config *config, const std::string &host, int port) : NetworkPlugin() {
1917
this->config = config;
20-
m_factories = new Swift::BoostNetworkFactories(loop);
21-
m_conn = m_factories->getConnectionFactory()->createConnection();
22-
m_conn->onDataRead.connect(boost::bind(&Plugin::_handleDataRead, this, _1));
23-
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));
24-
2518
LOG4CXX_INFO(logger, "Starting the plugin.");
26-
}
27-
28-
// NetworkPlugin uses this method to send the data to networkplugin server
29-
void Plugin::sendData(const std::string &string) {
30-
m_conn->write(Swift::createSafeByteArray(string));
31-
}
32-
33-
// This method has to call handleDataRead with all received data from network plugin server
34-
void Plugin::_handleDataRead(std::shared_ptr<Swift::SafeByteArray> data) {
35-
std::string d(data->begin(), data->end());
36-
handleDataRead(d);
19+
connect(host, std::to_string(port));
3720
}
3821

3922
void Plugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password, const std::map<std::string, std::string> &settings) {

backends/template/plugin.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#pragma once
22

3-
#include "Swiften/Swiften.h"
4-
53
#include "transport/Config.h"
64
#include "transport/NetworkPlugin.h"
75

86
class Plugin : public Transport::NetworkPlugin {
97
public:
10-
Plugin(Transport::Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port);
8+
Plugin(Transport::Config *config, const std::string &host, int port);
119

1210
// NetworkPlugin uses this method to send the data to networkplugin server
1311
void sendData(const std::string &string);
@@ -23,12 +21,5 @@ class Plugin : public Transport::NetworkPlugin {
2321
void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups);
2422

2523
private:
26-
// This method has to call handleDataRead with all received data from network plugin server
27-
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data);
28-
29-
private:
30-
Swift::BoostNetworkFactories *m_factories;
31-
Swift::BoostIOServiceThread m_boostIOServiceThread;
32-
std::shared_ptr<Swift::Connection> m_conn;
3324
Transport::Config *config;
3425
};

backends/twitter/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ find_package(CURL)
88

99
if(CURL_FOUND)
1010
message(STATUS "Using curl ${CURL_VERSION_STRING}: ${CURL_INCLUDE_DIRS} ${CURL_LIBRARIES}")
11-
target_link_libraries(spectrum2_twitter_backend transport JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
11+
target_link_libraries(spectrum2_twitter_backend transport-plugin JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
1212
else()
1313
message(FATAL_ERROR "curl not found")
1414
endif()

backends/twitter/TwitterPlugin.cpp

+7-41
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
#include "Requests/DestroyFriendRequest.h"
1111
#include "Requests/RetweetRequest.h"
1212
#include "Requests/ProfileImageRequest.h"
13-
#include "Swiften/StringCodecs/Hexify.h"
1413

1514
DEFINE_LOGGER(logger, "Twitter Backend");
1615

1716
TwitterPlugin *np = NULL;
18-
Swift::SimpleEventLoop *loop_; // Event Loop
1917

2018
const std::string OLD_APP_KEY = "PCWAdQpyyR12ezp2fVwEhw";
2119
const std::string OLD_APP_SECRET = "EveLmCXJIg2R7BTCpm6OWV8YyX49nI0pxnYXh7JMvDg";
@@ -35,7 +33,7 @@ static int cmp(std::string a, std::string b)
3533
}
3634

3735

38-
TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
36+
TwitterPlugin::TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
3937
{
4038
this->config = config;
4139
this->storagebackend = storagebackend;
@@ -68,56 +66,24 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
6866
OAUTH_SECRET = "twitter_oauth_secret";
6967
MODE = "mode";
7068

71-
m_factories = new Swift::BoostNetworkFactories(loop);
72-
m_conn = m_factories->getConnectionFactory()->createConnection();
73-
m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
74-
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));
75-
76-
tp = new ThreadPool(loop_, 10);
77-
7869
LOG4CXX_INFO(logger, "Fetch timeout is set to " << CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
79-
tweet_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
80-
message_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
70+
io = std::make_unique<boost::asio::io_service>();
71+
tweet_timer = std::make_unique<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));
72+
message_timer = std::make_unique<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));
8173

82-
tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
83-
//message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
84-
85-
tweet_timer->start();
86-
message_timer->start();
87-
cryptoProvider = std::shared_ptr<Swift::CryptoProvider>(Swift::PlatformCryptoProvider::create());
74+
tweet_timer->async_wait(boost::bind(&TwitterPlugin::pollForTweets, this));
75+
//message_timer->async_wait(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
8876

8977

9078
LOG4CXX_INFO(logger, "Starting the plugin.");
79+
connect(host, std::to_string(port));
9180
}
9281

9382
TwitterPlugin::~TwitterPlugin()
9483
{
9584
delete storagebackend;
9685
std::set<std::string>::iterator it;
9786
for (it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions;
98-
delete tp;
99-
}
100-
101-
// Send data to NetworkPlugin server
102-
void TwitterPlugin::sendData(const std::string &string)
103-
{
104-
m_conn->write(Swift::createSafeByteArray(string));
105-
}
106-
107-
// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
108-
void TwitterPlugin::_handleDataRead(std::shared_ptr<Swift::SafeByteArray> data)
109-
{
110-
if (m_firstPing) {
111-
m_firstPing = false;
112-
// Users can join the network without registering if we allow
113-
// one user to connect multiple IRC networks.
114-
NetworkPlugin::PluginConfig cfg;
115-
cfg.setNeedPassword(false);
116-
sendConfig(cfg);
117-
}
118-
119-
std::string d(data->begin(), data->end());
120-
handleDataRead(d);
12187
}
12288

12389
// User trying to login into his twitter account

backends/twitter/TwitterPlugin.h

+6-16
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77
#include "transport/MySQLBackend.h"
88
#include "transport/PQXXBackend.h"
99
#include "transport/StorageBackend.h"
10-
#include "transport/ThreadPool.h"
1110

12-
#include <Swiften/Swiften.h>
1311
#ifndef _WIN32
1412
#include "unistd.h"
1513
#include "signal.h"
1614
#include "sys/wait.h"
1715
#include "sys/signal.h"
1816
#endif
1917
#include <boost/algorithm/string.hpp>
18+
#include <boost/filesystem.hpp>
2019
#include <boost/thread.hpp>
2120
#include <boost/thread/mutex.hpp>
2221

@@ -30,8 +29,7 @@
3029
#include <queue>
3130
#include <set>
3231
#include <cstdio>
33-
#include <Swiften/Crypto/CryptoProvider.h>
34-
#include <Swiften/Crypto/PlatformCryptoProvider.h>
32+
3533
using namespace boost::filesystem;
3634
using namespace boost::program_options;
3735
using namespace Transport;
@@ -40,27 +38,20 @@ using namespace Transport;
4038

4139
class TwitterPlugin;
4240
extern TwitterPlugin *np;
43-
extern Swift::SimpleEventLoop *loop_; // Event Loop
4441

4542

4643
class TwitterPlugin : public NetworkPlugin {
4744
public:
48-
Swift::BoostNetworkFactories *m_factories;
49-
Swift::BoostIOServiceThread m_boostIOServiceThread;
50-
std::shared_ptr<Swift::Connection> m_conn;
51-
std::shared_ptr<Swift::CryptoProvider> cryptoProvider;
52-
Swift::Timer::ref tweet_timer;
53-
Swift::Timer::ref message_timer;
45+
std::unique_ptr<boost::asio::deadline_timer> tweet_timer;
46+
std::unique_ptr<boost::asio::deadline_timer> message_timer;
47+
std::unique_ptr<boost::asio::io_service> io;
5448
StorageBackend *storagebackend;
5549

56-
TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port);
50+
TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port);
5751
~TwitterPlugin();
5852

5953
// Send data to NetworkPlugin server
6054
void sendData(const std::string &string);
61-
62-
// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
63-
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data);
6455

6556
// User trying to login into his twitter account
6657
void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password, const std::map<std::string, std::string> &settings);
@@ -153,7 +144,6 @@ class TwitterPlugin : public NetworkPlugin {
153144

154145
boost::mutex dblock, userlock;
155146

156-
ThreadPool *tp;
157147
std::set<std::string> onlineUsers;
158148
struct UserData
159149
{

backends/twitter/main.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ int main (int argc, char* argv[]) {
5252
LOG4CXX_ERROR(logger, "Can't connect to database!");
5353
return -1;
5454
}
55-
56-
Swift::SimpleEventLoop eventLoop;
57-
loop_ = &eventLoop;
58-
np = new TwitterPlugin(cfg, &eventLoop, storagebackend, host, port);
59-
loop_->run();
55+
np = new TwitterPlugin(cfg, storagebackend, host, port);
6056

6157
return 0;
6258
}

0 commit comments

Comments
 (0)