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

#include <Swiften/Client/ClientSessionStanzaChannel.h>

#include <iostream>

#include <boost/bind.hpp>

namespace Swift {

ClientSessionStanzaChannel::~ClientSessionStanzaChannel() {
    if (session) {
        session->onFinished.disconnect(boost::bind(&ClientSessionStanzaChannel::handleSessionFinished, this, _1));
        session->onStanzaReceived.disconnect(boost::bind(&ClientSessionStanzaChannel::handleStanza, this, _1));
        session->onStanzaAcked.disconnect(boost::bind(&ClientSessionStanzaChannel::handleStanzaAcked, this, _1));
        session->onInitialized.disconnect(boost::bind(&ClientSessionStanzaChannel::handleSessionInitialized, this));
        session.reset();
    }
}

void ClientSessionStanzaChannel::setSession(std::shared_ptr<ClientSession> session) {
    assert(!this->session);
    this->session = session;
    session->onInitialized.connect(boost::bind(&ClientSessionStanzaChannel::handleSessionInitialized, this));
    session->onFinished.connect(boost::bind(&ClientSessionStanzaChannel::handleSessionFinished, this, _1));
    session->onStanzaReceived.connect(boost::bind(&ClientSessionStanzaChannel::handleStanza, this, _1));
    session->onStanzaAcked.connect(boost::bind(&ClientSessionStanzaChannel::handleStanzaAcked, this, _1));
}

void ClientSessionStanzaChannel::sendIQ(std::shared_ptr<IQ> iq) {
    send(iq);
}

void ClientSessionStanzaChannel::sendMessage(std::shared_ptr<Message> message) {
    send(message);
}

void ClientSessionStanzaChannel::sendPresence(std::shared_ptr<Presence> presence) {
    send(presence);
}

std::string ClientSessionStanzaChannel::getNewIQID() {
    return idGenerator.generateID();
}

void ClientSessionStanzaChannel::send(std::shared_ptr<Stanza> stanza) {
    if (!isAvailable()) {
        std::cerr << "Warning: Client: Trying to send a stanza while disconnected." << std::endl;
        return;
    }
    session->sendStanza(stanza);
}

void ClientSessionStanzaChannel::handleSessionFinished(std::shared_ptr<Error>) {
    session->onFinished.disconnect(boost::bind(&ClientSessionStanzaChannel::handleSessionFinished, this, _1));
    session->onStanzaReceived.disconnect(boost::bind(&ClientSessionStanzaChannel::handleStanza, this, _1));
    session->onStanzaAcked.disconnect(boost::bind(&ClientSessionStanzaChannel::handleStanzaAcked, this, _1));
    session->onInitialized.disconnect(boost::bind(&ClientSessionStanzaChannel::handleSessionInitialized, this));
    session.reset();

    onAvailableChanged(false);
}

void ClientSessionStanzaChannel::handleStanza(std::shared_ptr<Stanza> stanza) {
    std::shared_ptr<Message> message = std::dynamic_pointer_cast<Message>(stanza);
    if (message) {
        onMessageReceived(message);
        return;
    }

    std::shared_ptr<Presence> presence = std::dynamic_pointer_cast<Presence>(stanza);
    if (presence) {
        onPresenceReceived(presence);
        return;
    }

    std::shared_ptr<IQ> iq = std::dynamic_pointer_cast<IQ>(stanza);
    if (iq) {
        onIQReceived(iq);
        return;
    }
}


bool ClientSessionStanzaChannel::getStreamManagementEnabled() const {
    if (session) {
        return session->getStreamManagementEnabled();
    }
    return false;
}

std::vector<Certificate::ref> ClientSessionStanzaChannel::getPeerCertificateChain() const {
    if (session) {
        return session->getPeerCertificateChain();
    }
    return std::vector<Certificate::ref>();
}

void ClientSessionStanzaChannel::handleStanzaAcked(std::shared_ptr<Stanza> stanza) {
    onStanzaAcked(stanza);
}


void ClientSessionStanzaChannel::handleSessionInitialized() {
    onAvailableChanged(true);
}

}