Better server performance + preparation for handling multiple resources/clients

This commit is contained in:
Jan Kaluza 2011-06-28 13:16:23 +02:00
parent bc97b0fb77
commit 6aa3e63520
3 changed files with 12 additions and 11 deletions

View file

@ -111,8 +111,6 @@ void Server::handleNewClientConnection(boost::shared_ptr<Connection> connection)
serverFromClientSession->onDataRead.connect(boost::bind(&Server::handleDataRead, this, _1)); serverFromClientSession->onDataRead.connect(boost::bind(&Server::handleDataRead, this, _1));
serverFromClientSession->onDataWritten.connect(boost::bind(&Server::handleDataWritten, this, _1)); serverFromClientSession->onDataWritten.connect(boost::bind(&Server::handleDataWritten, this, _1));
dynamic_cast<ServerStanzaChannel *>(stanzaChannel_)->addSession(serverFromClientSession);
if (tlsFactory) { if (tlsFactory) {
serverFromClientSession->addTLSEncryption(tlsFactory, cert); serverFromClientSession->addTLSEncryption(tlsFactory, cert);
} }
@ -130,8 +128,8 @@ void Server::handleDataWritten(const SafeByteArray& data) {
onDataWritten(data); onDataWritten(data);
} }
void Server::handleSessionStarted(boost::shared_ptr<ServerFromClientSession>) { void Server::handleSessionStarted(boost::shared_ptr<ServerFromClientSession> session) {
// onSelfConnected(true); dynamic_cast<ServerStanzaChannel *>(stanzaChannel_)->addSession(session);
} }
void Server::handleSessionFinished(boost::shared_ptr<ServerFromClientSession> session) { void Server::handleSessionFinished(boost::shared_ptr<ServerFromClientSession> session) {

View file

@ -28,7 +28,7 @@ namespace {
} }
void ServerStanzaChannel::addSession(boost::shared_ptr<ServerFromClientSession> session) { void ServerStanzaChannel::addSession(boost::shared_ptr<ServerFromClientSession> session) {
sessions.push_back(session); sessions[session->getRemoteJID().toBare().toString()].push_back(session);
session->onSessionFinished.connect(boost::bind(&ServerStanzaChannel::handleSessionFinished, this, _1, session)); session->onSessionFinished.connect(boost::bind(&ServerStanzaChannel::handleSessionFinished, this, _1, session));
session->onElementReceived.connect(boost::bind(&ServerStanzaChannel::handleElement, this, _1, session)); session->onElementReceived.connect(boost::bind(&ServerStanzaChannel::handleElement, this, _1, session));
} }
@ -36,7 +36,8 @@ void ServerStanzaChannel::addSession(boost::shared_ptr<ServerFromClientSession>
void ServerStanzaChannel::removeSession(boost::shared_ptr<ServerFromClientSession> session) { void ServerStanzaChannel::removeSession(boost::shared_ptr<ServerFromClientSession> session) {
session->onSessionFinished.disconnect(boost::bind(&ServerStanzaChannel::handleSessionFinished, this, _1, session)); session->onSessionFinished.disconnect(boost::bind(&ServerStanzaChannel::handleSessionFinished, this, _1, session));
session->onElementReceived.disconnect(boost::bind(&ServerStanzaChannel::handleElement, this, _1, session)); session->onElementReceived.disconnect(boost::bind(&ServerStanzaChannel::handleElement, this, _1, session));
sessions.erase(std::remove(sessions.begin(), sessions.end(), session), sessions.end()); std::list<boost::shared_ptr<ServerFromClientSession> > &lst = sessions[session->getRemoteJID().toBare().toString()];
lst.erase(std::remove(lst.begin(), lst.end(), session), lst.end());
} }
void ServerStanzaChannel::sendIQ(boost::shared_ptr<IQ> iq) { void ServerStanzaChannel::sendIQ(boost::shared_ptr<IQ> iq) {
@ -53,7 +54,7 @@ void ServerStanzaChannel::sendPresence(boost::shared_ptr<Presence> presence) {
void ServerStanzaChannel::finishSession(const JID& to, boost::shared_ptr<Element> element) { void ServerStanzaChannel::finishSession(const JID& to, boost::shared_ptr<Element> element) {
std::vector<boost::shared_ptr<ServerFromClientSession> > candidateSessions; std::vector<boost::shared_ptr<ServerFromClientSession> > candidateSessions;
for (std::list<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = sessions.begin(); i != sessions.end(); ++i) { for (std::list<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = sessions[to.toBare().toString()].begin(); i != sessions[to.toBare().toString()].end(); ++i) {
if ((*i)->getRemoteJID().equals(to, JID::WithoutResource)) { if ((*i)->getRemoteJID().equals(to, JID::WithoutResource)) {
(*i)->sendElement(element); (*i)->sendElement(element);
candidateSessions.push_back(*i); candidateSessions.push_back(*i);
@ -62,6 +63,7 @@ void ServerStanzaChannel::finishSession(const JID& to, boost::shared_ptr<Element
for (std::vector<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = candidateSessions.begin(); i != candidateSessions.end(); ++i) { for (std::vector<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = candidateSessions.begin(); i != candidateSessions.end(); ++i) {
(*i)->finishSession(); (*i)->finishSession();
sessions[to.toBare().toString()].remove(*i);
} }
} }
@ -75,8 +77,8 @@ void ServerStanzaChannel::send(boost::shared_ptr<Stanza> stanza) {
// For a full JID, first try to route to a session with the full JID // For a full JID, first try to route to a session with the full JID
if (!to.isBare()) { if (!to.isBare()) {
std::list<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = std::find_if(sessions.begin(), sessions.end(), HasJID(to)); std::list<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = std::find_if(sessions[stanza->getTo().toBare().toString()].begin(), sessions[stanza->getTo().toBare().toString()].end(), HasJID(to));
if (i != sessions.end()) { if (i != sessions[stanza->getTo().toBare().toString()].end()) {
(*i)->sendElement(stanza); (*i)->sendElement(stanza);
return; return;
} }
@ -85,7 +87,7 @@ void ServerStanzaChannel::send(boost::shared_ptr<Stanza> stanza) {
// Look for candidate sessions // Look for candidate sessions
to = to.toBare(); to = to.toBare();
std::vector<boost::shared_ptr<ServerFromClientSession> > candidateSessions; std::vector<boost::shared_ptr<ServerFromClientSession> > candidateSessions;
for (std::list<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = sessions.begin(); i != sessions.end(); ++i) { for (std::list<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = sessions[stanza->getTo().toBare().toString()].begin(); i != sessions[stanza->getTo().toBare().toString()].end(); ++i) {
if ((*i)->getRemoteJID().equals(to, JID::WithoutResource)) { if ((*i)->getRemoteJID().equals(to, JID::WithoutResource)) {
candidateSessions.push_back(*i); candidateSessions.push_back(*i);
(*i)->sendElement(stanza); (*i)->sendElement(stanza);

View file

@ -45,7 +45,8 @@ namespace Swift {
private: private:
IDGenerator idGenerator; IDGenerator idGenerator;
std::list<boost::shared_ptr<ServerFromClientSession> > sessions; // [JID][resources][ServerFromClientSession]
std::map<std::string, std::list<boost::shared_ptr<ServerFromClientSession> > > sessions;
}; };
} }