Config and Transport class + first example

This commit is contained in:
Jan Kaluza 2011-02-10 13:12:10 +01:00
parent 80822e3c77
commit e678a46286
11 changed files with 309 additions and 0 deletions

View file

@ -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)

1
examples/CMakeLists.txt Normal file
View file

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

View file

@ -0,0 +1,6 @@
FILE(GLOB SRC *.cpp)
ADD_EXECUTABLE(transport_server_connect ${SRC})
TARGET_LINK_LIBRARIES(transport_server_connect transport)

View file

@ -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);
}

View file

@ -0,0 +1,6 @@
[service]
jid = icq.localhost
password = secret
server = localhost
port = 5347

View file

@ -0,0 +1,3 @@
FILE(GLOB HEADERS *.h)
INSTALL(FILES ${HEADERS} DESTINATION include/transport COMPONENT headers)

View file

@ -0,0 +1,37 @@
/**
* 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>
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);
}
}

View file

@ -0,0 +1,82 @@
/**
* 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 <vector>
#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<Swift::DiscoInfo> info, const boost::optional<Swift::ErrorPayload>& 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;
};
}

View file

@ -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)

54
src/config.cpp Normal file
View file

@ -0,0 +1,54 @@
/**
* 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 "transport/config.h"
#include <fstream>
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<std::string>(), "set compression level")
("service.server", value<std::string>(), "set compression level")
("service.password", value<std::string>(), "set compression level")
("service.port", value<int>(), "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);
}
}
}

View file

@ -0,0 +1,100 @@
/**
* 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 "transport/transport.h"
#include <boost/bind.hpp>
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<std::string>());
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<std::string>());
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<std::string>(), m_config["service.port"].as<int>());
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();
}
}