Files
@ e886f1597f5d
Branch filter:
Location: libtransport.git/src/transport.cpp
e886f1597f5d
3.7 KiB
text/x-c++hdr
Use Ubuntu Trusty in travis CI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | /**
* 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 "transport/logging.h"
#include "transport/frontend.h"
#include "transport/presenceoracle.h"
#include "transport/config.h"
#include <boost/bind.hpp>
#include <boost/algorithm/string/predicate.hpp>
using namespace Swift;
namespace Transport {
DEFINE_LOGGER(logger, "Component");
DEFINE_LOGGER(logger_xml, "Component.RAW");
Component::Component(Frontend *frontend, Swift::EventLoop *loop, Swift::NetworkFactories *factories, Config *config, Factory *factory, Transport::UserRegistry *userRegistry) {
m_frontend = frontend;
m_userRegistry = NULL;
m_reconnectCount = 0;
m_config = config;
m_factory = factory;
m_loop = loop;
m_userRegistry = userRegistry;
m_rawXML = false;
m_jid = Swift::JID(CONFIG_STRING(m_config, "service.jid"));
m_factories = factories;
m_reconnectTimer = m_factories->getTimerFactory()->createTimer(3000);
m_reconnectTimer->onTick.connect(boost::bind(&Component::start, this));
m_presenceOracle = new Transport::PresenceOracle(frontend);
m_presenceOracle->onPresenceChange.connect(boost::bind(&Component::handlePresence, this, _1));
m_frontend->init(this, loop, factories, config, userRegistry);
}
Component::~Component() {
delete m_presenceOracle;
}
Transport::PresenceOracle *Component::getPresenceOracle() {
return m_presenceOracle;
}
bool Component::inServerMode() {
return CONFIG_BOOL_DEFAULTED(m_config, "service.server_mode", true);
}
void Component::start() {
m_frontend->connectToServer();
m_reconnectCount++;
m_reconnectTimer->stop();
}
void Component::stop() {
m_reconnectCount = 0;
m_frontend->disconnectFromServer();
m_reconnectTimer->stop();
}
void Component::handleConnected() {
onConnected();
m_reconnectCount = 0;
m_reconnectTimer->stop();
LOG4CXX_INFO(logger, "Connected to Frontend server.");
}
void Component::handleConnectionError(const std::string &error) {
onConnectionError(error);
LOG4CXX_INFO(logger, "Disconnected from Frontend server. Error: " << error);
m_reconnectTimer->start();
}
void Component::handleDataRead(const std::string &data) {
if (!boost::starts_with(data, "<auth")) {
LOG4CXX_INFO(logger_xml, "RAW DATA IN " << data);
}
}
void Component::handleDataWritten(const std::string &data) {
LOG4CXX_INFO(logger_xml, "RAW DATA OUT " << data);
}
void Component::handlePresence(Swift::Presence::ref presence) {
// filter out login/logout presence spam
if (!presence->getTo().getNode().empty())
return;
// filter out bad presences
if (!presence->getFrom().isValid()) {
return;
}
switch (presence->getType()) {
case Presence::Error:
case Presence::Subscribe:
case Presence::Subscribed:
case Presence::Unsubscribe:
case Presence::Unsubscribed:
return;
default:
break;
};
// check if we have this client's capabilities and ask for them
if (presence->getType() != Swift::Presence::Unavailable) {
m_frontend->sendCapabilitiesRequest(presence->getFrom());
}
onUserPresenceReceived(presence);
}
}
|