summaryrefslogtreecommitdiffstats
blob: f32bcdde6c6c1e4d20c4496e5de66a7f476822a2 (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
// TODO: Send back errors if we can't make a connection

#include "Swiften/LinkLocal/OutgoingLinkLocalSession.h"

#include <boost/bind.hpp>

#include "Swiften/Elements/ProtocolHeader.h"
#include "Swiften/Network/Connection.h"
#include "Swiften/Network/ConnectionFactory.h"
#include "Swiften/Network/HostAddress.h"
#include "Swiften/Network/HostAddressPort.h"
#include "Swiften/StreamStack/StreamStack.h"
#include "Swiften/LinkLocal/DNSSDService.h"
#include "Swiften/StreamStack/ConnectionLayer.h"
#include "Swiften/StreamStack/XMPPLayer.h"
#include "Swiften/Elements/StreamFeatures.h"
#include "Swiften/Elements/IQ.h"
#include "Swiften/SASL/PLAINMessage.h"

namespace Swift {

OutgoingLinkLocalSession::OutgoingLinkLocalSession(
		const JID& localJID,
		const JID& remoteJID,
		const String& hostname,
		int port,
		boost::shared_ptr<DNSSDService> resolver,
		PayloadParserFactoryCollection* payloadParserFactories, 
		PayloadSerializerCollection* payloadSerializers,
		ConnectionFactory* connectionFactory) :
			LinkLocalSession(
				localJID, 
				boost::shared_ptr<Connection>(), 
				payloadParserFactories, 
				payloadSerializers),
			resolving_(false),
			remoteJID_(remoteJID),
			hostname_(hostname),
			port_(port),
			resolver_(resolver),
			connectionFactory_(connectionFactory) {
}

void OutgoingLinkLocalSession::start() {
	resolving_ = true;
	resolver_->onHostnameResolved.connect(boost::bind(&OutgoingLinkLocalSession::handleHostnameResolved, boost::dynamic_pointer_cast<OutgoingLinkLocalSession>(shared_from_this()), _1, _2));
	resolver_->resolveHostname(hostname_);
}

void OutgoingLinkLocalSession::handleHostnameResolved(const String& hostname, const boost::optional<HostAddress>& address) {
	if (resolving_) {
		if (hostname == hostname_) {
			resolving_ = false;
			if (address) {
				boost::shared_ptr<Connection> connection = connectionFactory_->createConnection();
				setConnection(connection);
				initializeStreamStack();
				connection->onConnected.connect(boost::bind(&OutgoingLinkLocalSession::handleConnected, boost::dynamic_pointer_cast<OutgoingLinkLocalSession>(shared_from_this())));
				connection->connect(HostAddressPort(*address, port_));
			}
			else {
				onSessionFinished(boost::optional<Error>(UnknownError));
			}
		}
	}
}

void OutgoingLinkLocalSession::handleConnected() {
	ProtocolHeader header;
	header.setFrom(getLocalJID());
	getXMPPLayer()->writeHeader(header);
}

void OutgoingLinkLocalSession::handleStreamStart(const ProtocolHeader&) {
	foreach(const boost::shared_ptr<Stanza>& stanza, queuedStanzas_) {
		LinkLocalSession::sendStanza(stanza);
	}
	queuedStanzas_.clear();
	setInitialized();
}

void OutgoingLinkLocalSession::handleElement(boost::shared_ptr<Element> element) {
	if (isInitialized()) {
		boost::shared_ptr<Stanza> stanza = boost::dynamic_pointer_cast<Stanza>(element);
		if (stanza) {
			onStanzaReceived(stanza);
		}
	}
}

void OutgoingLinkLocalSession::sendStanza(boost::shared_ptr<Stanza> stanza) {
	if (isInitialized()) {
		LinkLocalSession::sendStanza(stanza);
	}
	else {
		queuedStanzas_.push_back(stanza);
	}
}


}