summaryrefslogtreecommitdiffstats
blob: b7ccca80e3c347bc19566d7d706ce586e8b895a3 (plain)
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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Limber/Server/ServerStanzaRouter.h>

#include <algorithm>
#include <cassert>

#include <Swiften/Base/Algorithm.h>

#include <Limber/Server/ServerSession.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(std::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);
}

}