summaryrefslogtreecommitdiffstats
blob: 18b5d6aa496d2d2ca91df6b47970d9e8294ae263 (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
#include "Swiften/LinkLocal/LinkLocalConnector.h"

#include <boost/bind.hpp>

#include "Swiften/Network/Connection.h"
#include "Swiften/Network/ConnectionFactory.h"
#include "Swiften/Network/HostAddress.h"
#include "Swiften/Network/HostAddressPort.h"
#include "Swiften/LinkLocal/DNSSD/DNSSDQuerier.h"
#include "Swiften/LinkLocal/DNSSD/DNSSDResolveHostnameQuery.h"

namespace Swift {

LinkLocalConnector::LinkLocalConnector(
		const JID& remoteJID,
		const String& hostname,
		int interfaceIndex,
		int port,
		boost::shared_ptr<DNSSDQuerier> querier,
		boost::shared_ptr<Connection> connection) : 
			remoteJID(remoteJID),
			hostname(hostname),
			interfaceIndex(interfaceIndex),
			port(port),
			querier(querier),
			connection(connection) {
}

void LinkLocalConnector::connect() {
	resolveQuery = querier->createResolveHostnameQuery(hostname, interfaceIndex);
	resolveQuery->onHostnameResolved.connect(boost::bind(
			&LinkLocalConnector::handleHostnameResolved, 
			boost::dynamic_pointer_cast<LinkLocalConnector>(shared_from_this()), 
			_1));
	resolveQuery->run();
}

void LinkLocalConnector::handleHostnameResolved(const boost::optional<HostAddress>& address) {
	if (address) {
		resolveQuery->finish();
		resolveQuery.reset();
		connection->onConnectFinished.connect(
				boost::bind(boost::ref(onConnectFinished), _1));
		connection->connect(HostAddressPort(*address, port));
	}
	else {
		onConnectFinished(false);
	}
}

void LinkLocalConnector::handleConnected(bool error) {
	onConnectFinished(error);
}

void LinkLocalConnector::queueElement(boost::shared_ptr<Element> element) {
	queuedElements.push_back(element);
}


}