summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-06-18 10:34:41 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-06-18 10:41:11 (GMT)
commit362b2d147e4050e997104c24b2ed2e818adab5ed (patch)
tree63340cf49072e2cc0d9d876859249eb22fd540d3 /Limber/Server/ServerStanzaRouter.cpp
parent0328d98d29b383ecfcb5ffb66df0a9b7b4d85654 (diff)
parent7af8a078c57d94ff63eb81f26de2f55eca6b5c00 (diff)
downloadswift-contrib-362b2d147e4050e997104c24b2ed2e818adab5ed.zip
swift-contrib-362b2d147e4050e997104c24b2ed2e818adab5ed.tar.bz2
Merge branch 'swift-1.x'
* swift-1.x: Moving unused server code out of Swiften into Limber. Conflicts: Limber/Server/ServerFromClientSession.cpp Limber/Server/ServerSession.cpp Limber/Server/ServerStanzaRouter.cpp Limber/Server/SimpleUserRegistry.cpp Limber/Server/SimpleUserRegistry.h Limber/Server/UnitTest/ServerStanzaRouterTest.cpp Limber/Server/UserRegistry.cpp Limber/main.cpp Slimber/Server.cpp Slimber/Server.h
Diffstat (limited to 'Limber/Server/ServerStanzaRouter.cpp')
-rw-r--r--Limber/Server/ServerStanzaRouter.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/Limber/Server/ServerStanzaRouter.cpp b/Limber/Server/ServerStanzaRouter.cpp
new file mode 100644
index 0000000..ea695e3
--- /dev/null
+++ b/Limber/Server/ServerStanzaRouter.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Limber/Server/ServerStanzaRouter.h"
+#include "Limber/Server/ServerSession.h"
+
+#include <cassert>
+#include <algorithm>
+#include <Swiften/Base/Algorithm.h>
+
+namespace Swift {
+
+namespace {
+ struct PriorityLessThan {
+ bool operator()(const ServerSession* s1, const ServerSession* s2) const {
+ return s1->getPriority() < s2->getPriority();
+ }
+ };
+
+ struct HasJID {
+ HasJID(const JID& jid) : jid(jid) {}
+ bool operator()(const ServerSession* session) const {
+ return session->getJID().equals(jid, JID::WithResource);
+ }
+ JID jid;
+ };
+}
+
+ServerStanzaRouter::ServerStanzaRouter() {
+}
+
+bool ServerStanzaRouter::routeStanza(boost::shared_ptr<Stanza> stanza) {
+ JID to = stanza->getTo();
+ assert(to.isValid());
+
+ // For a full JID, first try to route to a session with the full JID
+ if (!to.isBare()) {
+ std::vector<ServerSession*>::const_iterator i = std::find_if(clientSessions_.begin(), clientSessions_.end(), HasJID(to));
+ if (i != clientSessions_.end()) {
+ (*i)->sendStanza(stanza);
+ return true;
+ }
+ }
+
+ // Look for candidate sessions
+ to = to.toBare();
+ std::vector<ServerSession*> candidateSessions;
+ for (std::vector<ServerSession*>::const_iterator i = clientSessions_.begin(); i != clientSessions_.end(); ++i) {
+ if ((*i)->getJID().equals(to, JID::WithoutResource) && (*i)->getPriority() >= 0) {
+ candidateSessions.push_back(*i);
+ }
+ }
+ if (candidateSessions.empty()) {
+ return false;
+ }
+
+ // Find the session with the highest priority
+ std::vector<ServerSession*>::const_iterator i = std::max_element(clientSessions_.begin(), clientSessions_.end(), PriorityLessThan());
+ (*i)->sendStanza(stanza);
+ return true;
+}
+
+void ServerStanzaRouter::addClientSession(ServerSession* clientSession) {
+ clientSessions_.push_back(clientSession);
+}
+
+void ServerStanzaRouter::removeClientSession(ServerSession* clientSession) {
+ erase(clientSessions_, clientSession);
+}
+
+}