Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalyster committed Mar 30, 2023
1 parent e55d3e1 commit 51d3a12
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 176 deletions.
8 changes: 4 additions & 4 deletions backends/libcommuni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ add_executable(spectrum2_libcommuni_backend ${SRC})

if(NOT WIN32)
if(ENABLE_QT4)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport pthread)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin pthread)
else()
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport pthread)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin pthread)
endif()
else()
if(ENABLE_QT4)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin)
else()
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin)
endif()
endif()

Expand Down
4 changes: 2 additions & 2 deletions backends/swiften/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ file(GLOB SRC *.cpp)
add_executable(spectrum2_swiften_backend ${SRC})

if(NOT WIN32)
target_link_libraries(spectrum2_swiften_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_swiften_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
else()
target_link_libraries(spectrum2_swiften_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_swiften_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
endif()

install(TARGETS spectrum2_swiften_backend RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})
30 changes: 3 additions & 27 deletions backends/swiften/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,15 @@ class SwiftenPlugin : public NetworkPlugin, Swift::XMPPParserClient {
Swift::XMPPParser *m_xmppParser;
Swift::FullPayloadParserFactoryCollection m_collection2;

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

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(std::shared_ptr<Swift::SafeByteArray> data) {
if (m_firstPing) {
m_firstPing = false;
NetworkPlugin::PluginConfig cfg;
cfg.setRawXML(true);
cfg.setNeedRegistration(false);
sendConfig(cfg);
}
std::string d(data->begin(), data->end());
handleDataRead(d);
connect(host, std::to_string(port));
}

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

Logging::initBackendLogging(cfg);

Swift::SimpleEventLoop eventLoop;
loop_ = &eventLoop;
np = new SwiftenPlugin(cfg, &eventLoop, host, port);
loop_->run();
np = new SwiftenPlugin(cfg, host, port);

return 0;
}
6 changes: 3 additions & 3 deletions backends/template/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ add_executable(spectrum2_template_backend ${SRC})

if(CMAKE_COMPILER_IS_GNUCXX)
if(NOT WIN32)
target_link_libraries(spectrum2_template_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_template_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
else()
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_template_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
endif()
else()
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_template_backend transport-plugin ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
endif()

#install(TARGETS spectrum2_template_backend RUNTIME DESTINATION bin)
8 changes: 2 additions & 6 deletions backends/template/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include "transport/NetworkPlugin.h"
#include "transport/Logging.h"

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

#ifndef _WIN32
// for signal handler
#include "unistd.h"
Expand All @@ -17,6 +14,7 @@
#endif
// Boost
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace Transport;
Expand Down Expand Up @@ -60,9 +58,7 @@ int main (int argc, char* argv[]) {

Logging::initBackendLogging(cfg);

Swift::SimpleEventLoop eventLoop;
Plugin p(cfg, &eventLoop, host, port);
eventLoop.run();
Plugin p(cfg, host, port);

return 0;
}
23 changes: 3 additions & 20 deletions backends/template/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,19 @@
#include "transport/NetworkPlugin.h"
#include "transport/Logging.h"

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

// Boost
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.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() {
Plugin::Plugin(Config *config, 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::fromString(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(std::shared_ptr<Swift::SafeByteArray> data) {
std::string d(data->begin(), data->end());
handleDataRead(d);
connect(host, std::to_string(port));
}

void Plugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password, const std::map<std::string, std::string> &settings) {
Expand Down
11 changes: 1 addition & 10 deletions backends/template/plugin.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#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);
Plugin(Transport::Config *config, const std::string &host, int port);

// NetworkPlugin uses this method to send the data to networkplugin server
void sendData(const std::string &string);
Expand All @@ -23,12 +21,5 @@ class Plugin : public Transport::NetworkPlugin {
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(std::shared_ptr<Swift::SafeByteArray> data);

private:
Swift::BoostNetworkFactories *m_factories;
Swift::BoostIOServiceThread m_boostIOServiceThread;
std::shared_ptr<Swift::Connection> m_conn;
Transport::Config *config;
};
2 changes: 1 addition & 1 deletion backends/twitter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ find_package(CURL)

if(CURL_FOUND)
message(STATUS "Using curl ${CURL_VERSION_STRING}: ${CURL_INCLUDE_DIRS} ${CURL_LIBRARIES}")
target_link_libraries(spectrum2_twitter_backend transport JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_twitter_backend transport-plugin JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
else()
message(FATAL_ERROR "curl not found")
endif()
Expand Down
48 changes: 7 additions & 41 deletions backends/twitter/TwitterPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
#include "Requests/DestroyFriendRequest.h"
#include "Requests/RetweetRequest.h"
#include "Requests/ProfileImageRequest.h"
#include "Swiften/StringCodecs/Hexify.h"

DEFINE_LOGGER(logger, "Twitter Backend");

TwitterPlugin *np = NULL;
Swift::SimpleEventLoop *loop_; // Event Loop

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


TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
TwitterPlugin::TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
{
this->config = config;
this->storagebackend = storagebackend;
Expand Down Expand Up @@ -68,56 +66,24 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
OAUTH_SECRET = "twitter_oauth_secret";
MODE = "mode";

m_factories = new Swift::BoostNetworkFactories(loop);
m_conn = m_factories->getConnectionFactory()->createConnection();
m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));

tp = new ThreadPool(loop_, 10);

LOG4CXX_INFO(logger, "Fetch timeout is set to " << CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
tweet_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
message_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
io = std::make_unique<boost::asio::io_service>();
tweet_timer = std::make_unique<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));
message_timer = std::make_unique<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));

tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
//message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));

tweet_timer->start();
message_timer->start();
cryptoProvider = std::shared_ptr<Swift::CryptoProvider>(Swift::PlatformCryptoProvider::create());
tweet_timer->async_wait(boost::bind(&TwitterPlugin::pollForTweets, this));
//message_timer->async_wait(boost::bind(&TwitterPlugin::pollForDirectMessages, this));


LOG4CXX_INFO(logger, "Starting the plugin.");
connect(host, std::to_string(port));
}

TwitterPlugin::~TwitterPlugin()
{
delete storagebackend;
std::set<std::string>::iterator it;
for (it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions;
delete tp;
}

// Send data to NetworkPlugin server
void TwitterPlugin::sendData(const std::string &string)
{
m_conn->write(Swift::createSafeByteArray(string));
}

// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
void TwitterPlugin::_handleDataRead(std::shared_ptr<Swift::SafeByteArray> data)
{
if (m_firstPing) {
m_firstPing = false;
// Users can join the network without registering if we allow
// one user to connect multiple IRC networks.
NetworkPlugin::PluginConfig cfg;
cfg.setNeedPassword(false);
sendConfig(cfg);
}

std::string d(data->begin(), data->end());
handleDataRead(d);
}

// User trying to login into his twitter account
Expand Down
22 changes: 6 additions & 16 deletions backends/twitter/TwitterPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
#include "transport/MySQLBackend.h"
#include "transport/PQXXBackend.h"
#include "transport/StorageBackend.h"
#include "transport/ThreadPool.h"

#include <Swiften/Swiften.h>
#ifndef _WIN32
#include "unistd.h"
#include "signal.h"
#include "sys/wait.h"
#include "sys/signal.h"
#endif
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>

Expand All @@ -30,8 +29,7 @@
#include <queue>
#include <set>
#include <cstdio>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>

using namespace boost::filesystem;
using namespace boost::program_options;
using namespace Transport;
Expand All @@ -40,27 +38,20 @@ using namespace Transport;

class TwitterPlugin;
extern TwitterPlugin *np;
extern Swift::SimpleEventLoop *loop_; // Event Loop


class TwitterPlugin : public NetworkPlugin {
public:
Swift::BoostNetworkFactories *m_factories;
Swift::BoostIOServiceThread m_boostIOServiceThread;
std::shared_ptr<Swift::Connection> m_conn;
std::shared_ptr<Swift::CryptoProvider> cryptoProvider;
Swift::Timer::ref tweet_timer;
Swift::Timer::ref message_timer;
std::unique_ptr<boost::asio::deadline_timer> tweet_timer;
std::unique_ptr<boost::asio::deadline_timer> message_timer;
std::unique_ptr<boost::asio::io_service> io;
StorageBackend *storagebackend;

TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port);
TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port);
~TwitterPlugin();

// Send data to NetworkPlugin server
void sendData(const std::string &string);

// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data);

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

boost::mutex dblock, userlock;

ThreadPool *tp;
std::set<std::string> onlineUsers;
struct UserData
{
Expand Down
6 changes: 1 addition & 5 deletions backends/twitter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ int main (int argc, char* argv[]) {
LOG4CXX_ERROR(logger, "Can't connect to database!");
return -1;
}

Swift::SimpleEventLoop eventLoop;
loop_ = &eventLoop;
np = new TwitterPlugin(cfg, &eventLoop, storagebackend, host, port);
loop_->run();
np = new TwitterPlugin(cfg, storagebackend, host, port);

return 0;
}
Loading

0 comments on commit 51d3a12

Please sign in to comment.