From e678a46286f44c4d96950e8a34d42dbacd524d39 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Thu, 10 Feb 2011 13:12:10 +0100 Subject: [PATCH] Config and Transport class + first example --- CMakeLists.txt | 2 + examples/CMakeLists.txt | 1 + examples/server_connect/CMakeLists.txt | 6 ++ examples/server_connect/main.cpp | 17 +++++ examples/server_connect/sample.cfg | 6 ++ include/transport/CMakeLists.txt | 3 + include/transport/config.h | 37 +++++++++ include/transport/transport.h | 82 ++++++++++++++++++++ src/CMakeLists.txt | 1 + src/config.cpp | 54 +++++++++++++ src/transport.cpp | 100 +++++++++++++++++++++++++ 11 files changed, 309 insertions(+) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/server_connect/CMakeLists.txt create mode 100644 examples/server_connect/main.cpp create mode 100644 examples/server_connect/sample.cfg create mode 100644 include/transport/config.h create mode 100644 include/transport/transport.h create mode 100644 src/config.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c09fae71..a8cb73df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,8 @@ if(CMAKE_BUILD_TYPE MATCHES Debug) endif(CMAKE_BUILD_TYPE MATCHES Debug) SET(TRANSPORT_VERSION 2.0) +include_directories(include) ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(include) +ADD_SUBDIRECTORY(examples) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..a97a3918 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(server_connect) diff --git a/examples/server_connect/CMakeLists.txt b/examples/server_connect/CMakeLists.txt new file mode 100644 index 00000000..70b9962d --- /dev/null +++ b/examples/server_connect/CMakeLists.txt @@ -0,0 +1,6 @@ +FILE(GLOB SRC *.cpp) + +ADD_EXECUTABLE(transport_server_connect ${SRC}) + +TARGET_LINK_LIBRARIES(transport_server_connect transport) + diff --git a/examples/server_connect/main.cpp b/examples/server_connect/main.cpp new file mode 100644 index 00000000..e7ae3cb0 --- /dev/null +++ b/examples/server_connect/main.cpp @@ -0,0 +1,17 @@ +#include "transport/config.h" +#include "transport/transport.h" +#include "Swiften/EventLoop/SimpleEventLoop.h" + +using namespace Transport; + +int main(void) +{ + Config::Variables config; + if (!Config::load("sample.cfg", config)) { + std::cout << "Can't open sample.cfg configuration file.\n"; + return 1; + } + + Swift::SimpleEventLoop eventLoop; + Transport::Transport transport(&eventLoop, config); +} diff --git a/examples/server_connect/sample.cfg b/examples/server_connect/sample.cfg new file mode 100644 index 00000000..0af1c23f --- /dev/null +++ b/examples/server_connect/sample.cfg @@ -0,0 +1,6 @@ +[service] +jid = icq.localhost +password = secret +server = localhost +port = 5347 + diff --git a/include/transport/CMakeLists.txt b/include/transport/CMakeLists.txt index e69de29b..0dac9b52 100644 --- a/include/transport/CMakeLists.txt +++ b/include/transport/CMakeLists.txt @@ -0,0 +1,3 @@ +FILE(GLOB HEADERS *.h) + +INSTALL(FILES ${HEADERS} DESTINATION include/transport COMPONENT headers) \ No newline at end of file diff --git a/include/transport/config.h b/include/transport/config.h new file mode 100644 index 00000000..5d61e86b --- /dev/null +++ b/include/transport/config.h @@ -0,0 +1,37 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * 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 +#include +#include +#include +#include + +namespace Transport { + namespace Config { + typedef boost::program_options::variables_map Variables; + + bool load(const std::string &configfile, Variables &variables, boost::program_options::options_description &opts); + bool load(const std::string &configfile, Variables &variables); + + } +} diff --git a/include/transport/transport.h b/include/transport/transport.h new file mode 100644 index 00000000..d14a0040 --- /dev/null +++ b/include/transport/transport.h @@ -0,0 +1,82 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * 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 +#include "Swiften/Swiften.h" +#include "Swiften/Disco/GetDiscoInfoRequest.h" +#include "Swiften/Disco/EntityCapsManager.h" +#include "Swiften/Disco/CapsManager.h" +#include "Swiften/Disco/CapsMemoryStorage.h" +#include "Swiften/Presence/PresenceOracle.h" +#include "Swiften/Network/BoostTimerFactory.h" +#include "Swiften/Network/BoostIOServiceThread.h" +#include "transport/config.h" + +namespace Transport { + // typedef enum { CLIENT_FEATURE_ROSTERX = 2, + // CLIENT_FEATURE_XHTML_IM = 4, + // CLIENT_FEATURE_FILETRANSFER = 8, + // CLIENT_FEATURE_CHATSTATES = 16 + // } SpectrumImportantFeatures; + // + // class SpectrumDiscoInfoResponder; + // class SpectrumRegisterHandler; + + class Transport { + public: + Transport(Swift::EventLoop *loop, Config::Variables &config); + ~Transport(); + + // Connect to server + void connect(); + + Swift::Component *getComponent() { return m_component; } + + private: + void handleConnected(); + void handleConnectionError(const Swift::ComponentError &error); +// void handlePresenceReceived(Swift::Presence::ref presence); +// void handleMessageReceived(Swift::Message::ref message); +// void handlePresence(Swift::Presence::ref presence); +// void handleSubscription(Swift::Presence::ref presence); +// void handleProbePresence(Swift::Presence::ref presence); +// void handleDataRead(const Swift::String &data); +// void handleDataWritten(const Swift::String &data); +// +// void handleDiscoInfoResponse(boost::shared_ptr info, const boost::optional& error, const Swift::JID& jid); +// void handleCapsChanged(const Swift::JID& jid); + + Swift::BoostNetworkFactories *m_factories; + Swift::Component *m_component; + Swift::Timer::ref m_reconnectTimer; + Swift::BoostIOServiceThread m_boostIOServiceThread; + Swift::EntityCapsManager *m_entityCapsManager; + Swift::CapsManager *m_capsManager; + Swift::CapsMemoryStorage *m_capsMemoryStorage; + Swift::PresenceOracle *m_presenceOracle; +// SpectrumDiscoInfoResponder *m_discoInfoResponder; +// SpectrumRegisterHandler *m_registerHandler; + int m_reconnectCount; + Config::Variables m_config; + Swift::JID m_jid; + }; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 99cb8f64..aff7bf95 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ FILE(GLOB HEADERS ../../include/transport/*.h) # SOURCE_GROUP(headers FILES ${HEADERS}) ADD_LIBRARY(transport SHARED ${HEADERS} ${SRC}) +ADD_DEFINITIONS(-fPIC) TARGET_LINK_LIBRARIES(transport -lSwiften -lresolv -lidn -lz -lpthread -lexpat -lidn -lboost_date_time -lboost_system -lboost_filesystem -lboost_program_options -lboost_regex -lboost_thread-mt -lboost_signals -lz -lssl -lcrypto -lexpat -lresolv -lc -lxml2 -export-dynamic) diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 00000000..03c2f341 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,54 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * 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 "transport/config.h" +#include + +using namespace boost::program_options; + +namespace Transport { +namespace Config { + +bool load(const std::string &configfile, Variables &variables, boost::program_options::options_description &opts) { + std::ifstream ifs(configfile.c_str()); + if (!ifs.is_open()) + return false; + + opts.add_options() + ("service.jid", value(), "set compression level") + ("service.server", value(), "set compression level") + ("service.password", value(), "set compression level") + ("service.port", value(), "set compression level") + ; + + + store(parse_config_file(ifs, opts), variables); + notify(variables); + + return true; +} + +bool load(const std::string &configfile, Variables &variables) { + options_description opts("Transport options"); + return load(configfile, variables, opts); +} + +} +} diff --git a/src/transport.cpp b/src/transport.cpp index e69de29b..a7d012f8 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -0,0 +1,100 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * 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 "transport/transport.h" +#include + +using namespace Swift; +using namespace boost; + +namespace Transport { + +Transport::Transport(Swift::EventLoop *loop, Config::Variables &config) { + m_reconnectCount = 0; + m_config = config; + + for (Config::Variables::iterator i = config.begin() ; i != config.end() ; ++i ) + { + std::cout << (*i).first << "\n"; + } + + m_jid = Swift::JID(m_config["service.jid"].as()); + + m_factories = new BoostNetworkFactories(loop); + + m_reconnectTimer = m_factories->getTimerFactory()->createTimer(1000); + m_reconnectTimer->onTick.connect(bind(&Transport::connect, this)); + + m_component = new Component(loop, m_factories, m_jid, m_config["service.password"].as()); + m_component->setSoftwareVersion("", ""); + m_component->onConnected.connect(bind(&Transport::handleConnected, this)); + m_component->onError.connect(bind(&Transport::handleConnectionError, this, _1)); +// m_component->onDataRead.connect(bind(&Transport::handleDataRead, this, _1)); +// m_component->onDataWritten.connect(bind(&Transport::handleDataWritten, this, _1)); +// m_component->onPresenceReceived.connect(bind(&Transport::handlePresenceReceived, this, _1)); +// m_component->onMessageReceived.connect(bind(&Transport::handleMessageReceived, this, _1)); + + m_capsMemoryStorage = new CapsMemoryStorage(); + m_capsManager = new CapsManager(m_capsMemoryStorage, m_component->getStanzaChannel(), m_component->getIQRouter()); + m_entityCapsManager = new EntityCapsManager(m_capsManager, m_component->getStanzaChannel()); +// m_entityCapsManager->onCapsChanged.connect(boost::bind(&Transport::handleCapsChanged, this, _1)); + + m_presenceOracle = new PresenceOracle(m_component->getStanzaChannel()); +// m_presenceOracle->onPresenceChange.connect(bind(&Transport::handlePresence, this, _1)); + +// m_discoInfoResponder = new SpectrumDiscoInfoResponder(m_component->getIQRouter()); +// m_discoInfoResponder->start(); +// +// m_registerHandler = new SpectrumRegisterHandler(m_component); +// m_registerHandler->start(); +} + +Transport::~Transport() { + delete m_presenceOracle; + delete m_entityCapsManager; + delete m_capsManager; + delete m_capsMemoryStorage; +// delete m_discoInfoResponder; +// delete m_registerHandler; + delete m_component; + delete m_factories; +} + +void Transport::connect() { + m_reconnectCount++; + m_component->connect(m_config["service.server"].as(), m_config["service.port"].as()); + m_reconnectTimer->stop(); +} + +void Transport::handleConnected() { + std::cout <<"Transport" << " CONNECTED!\n"; + m_reconnectCount = 0; +} + +void Transport::handleConnectionError(const ComponentError &error) { + std::cout << "Transport" << " Disconnected from Jabber server!\n"; + +// if (m_reconnectCount == 2) +// Transport::instance()->userManager()->removeAllUsers(); + + m_reconnectTimer->start(); +} + +}