Files
@ 629e93e34b42
Branch filter:
Location: libtransport.git/include/Swiften/Server/Server.cpp
629e93e34b42
5.0 KiB
text/x-c++hdr
Fixed crash in removig XMPPLayer
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | /*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "Swiften/Server/Server.h"
#include <string>
#include <boost/bind.hpp>
#include "Swiften/Base/String.h"
#include "Swiften/Base/foreach.h"
#include "Swiften/Network/Connection.h"
#include "Swiften/Network/ConnectionServer.h"
#include "Swiften/Network/ConnectionServerFactory.h"
#include "Swiften/Elements/Element.h"
#include "Swiften/Elements/Presence.h"
#include "Swiften/Elements/RosterPayload.h"
#include "Swiften/Network/NetworkFactories.h"
#include "Swiften/Session/SessionTracer.h"
#include "Swiften/Elements/IQ.h"
#include "Swiften/Elements/VCard.h"
#include "Swiften/Server/UserRegistry.h"
#include <string>
#include "Swiften/Network/ConnectionServer.h"
#include "Swiften/Network/ConnectionFactory.h"
#include "Swiften/Server/ServerFromClientSession.h"
#include "Swiften/Server/ServerStanzaChannel.h"
#include "Swiften/Queries/IQRouter.h"
#include <iostream>
namespace Swift {
Server::Server(
EventLoop* eventLoop,
NetworkFactories* networkFactories,
UserRegistry *userRegistry,
const JID& jid,
int port) :
userRegistry_(userRegistry),
port_(port),
eventLoop(eventLoop),
networkFactories_(networkFactories),
stopping(false),
selfJID(jid),
stanzaChannel_(){
stanzaChannel_ = new ServerStanzaChannel();
iqRouter_ = new IQRouter(stanzaChannel_);
tlsFactory = NULL;
}
Server::~Server() {
stop();
delete iqRouter_;
delete stanzaChannel_;
}
void Server::start() {
if (serverFromClientConnectionServer) {
return;
}
serverFromClientConnectionServer = networkFactories_->getConnectionServerFactory()->createConnectionServer(port_);
serverFromClientConnectionServerSignalConnections.push_back(
serverFromClientConnectionServer->onNewConnection.connect(
boost::bind(&Server::handleNewClientConnection, this, _1)));
// serverFromClientConnectionServerSignalConnections.push_back(
// serverFromClientConnectionServer->onStopped.connect(
// boost::bind(&Server::handleClientConnectionServerStopped, this, _1)));
serverFromClientConnectionServer->start();
}
void Server::stop() {
if (stopping) {
return;
}
stopping = true;
foreach(boost::shared_ptr<ServerFromClientSession> session, serverFromClientSessions) {
session->finishSession();
}
serverFromClientSessions.clear();
if (serverFromClientConnectionServer) {
serverFromClientConnectionServer->stop();
foreach(boost::bsignals::connection& connection, serverFromClientConnectionServerSignalConnections) {
connection.disconnect();
}
serverFromClientConnectionServerSignalConnections.clear();
serverFromClientConnectionServer.reset();
}
stopping = false;
// onStopped(e);
}
void Server::handleNewClientConnection(boost::shared_ptr<Connection> connection) {
boost::shared_ptr<ServerFromClientSession> serverFromClientSession = boost::shared_ptr<ServerFromClientSession>(
new ServerFromClientSession(idGenerator.generateID(), connection,
getPayloadParserFactories(), getPayloadSerializers(), userRegistry_));
//serverFromClientSession->setAllowSASLEXTERNAL();
serverFromClientSession->onSessionStarted.connect(
boost::bind(&Server::handleSessionStarted, this, serverFromClientSession));
serverFromClientSession->onSessionFinished.connect(
boost::bind(&Server::handleSessionFinished, this,
serverFromClientSession));
serverFromClientSession->onDataRead.connect(boost::bind(&Server::handleDataRead, this, _1));
serverFromClientSession->onDataWritten.connect(boost::bind(&Server::handleDataWritten, this, _1));
if (tlsFactory) {
serverFromClientSession->addTLSEncryption(tlsFactory, cert);
}
serverFromClientSession->startSession();
serverFromClientSessions.push_back(serverFromClientSession);
}
void Server::handleDataRead(const SafeByteArray& data) {
onDataRead(data);
}
void Server::handleDataWritten(const SafeByteArray& data) {
onDataWritten(data);
}
void Server::handleSessionStarted(boost::shared_ptr<ServerFromClientSession> session) {
dynamic_cast<ServerStanzaChannel *>(stanzaChannel_)->addSession(session);
}
void Server::handleSessionFinished(boost::shared_ptr<ServerFromClientSession> session) {
if (!session->getRemoteJID().isValid()) {
Swift::Presence::ref presence = Swift::Presence::create();
presence->setFrom(session->getBareJID());
presence->setType(Swift::Presence::Unavailable);
dynamic_cast<ServerStanzaChannel *>(stanzaChannel_)->onPresenceReceived(presence);
}
serverFromClientSessions.erase(std::remove(serverFromClientSessions.begin(), serverFromClientSessions.end(), session), serverFromClientSessions.end());
std::cout << "FINISH SESSION2 " << serverFromClientSessions.size() << "\n";
session->onSessionStarted.disconnect(
boost::bind(&Server::handleSessionStarted, this, session));
session->onSessionFinished.disconnect(
boost::bind(&Server::handleSessionFinished, this, session));
}
void Server::addTLSEncryption(TLSServerContextFactory* tlsContextFactory, const PKCS12Certificate& cert) {
tlsFactory = tlsContextFactory;
this->cert = cert;
}
}
|