summaryrefslogtreecommitdiffstats
blob: 4b68a338ac6b6c8ed5caedeab36779c530045023 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <boost/bind.hpp>
#include <iostream>

#include <Swiften/LinkLocal/LinkLocalServiceBrowser.h>
#include <Swiften/LinkLocal/DNSSD/DNSSDBrowseQuery.h>
#include <Swiften/Network/HostAddress.h>

namespace Swift {

LinkLocalServiceBrowser::LinkLocalServiceBrowser(boost::shared_ptr<DNSSDQuerier> querier) : querier(querier), haveError(false) {
}

LinkLocalServiceBrowser::~LinkLocalServiceBrowser() {
	if (isRunning()) {
		std::cerr << "WARNING: LinkLocalServiceBrowser still running on destruction" << std::endl;
	}
}


void LinkLocalServiceBrowser::start() {
	assert(!isRunning());
	haveError = false;
	browseQuery = querier->createBrowseQuery();
	browseQuery->onServiceAdded.connect(
			boost::bind(&LinkLocalServiceBrowser::handleServiceAdded, this, _1));
	browseQuery->onServiceRemoved.connect(
			boost::bind(&LinkLocalServiceBrowser::handleServiceRemoved, this, _1));
	browseQuery->onError.connect(
			boost::bind(&LinkLocalServiceBrowser::handleBrowseError, this));
	browseQuery->startBrowsing();
}

void LinkLocalServiceBrowser::stop() {
	assert(isRunning());
	if (isRegistered()) {
		unregisterService();
	}
	for (ResolveQueryMap::const_iterator i = resolveQueries.begin(); i != resolveQueries.end(); ++i) {
		i->second->stop();
	}
	resolveQueries.clear();
	services.clear();
	browseQuery->stopBrowsing();
	browseQuery.reset();
	onStopped(haveError);
}

bool LinkLocalServiceBrowser::isRunning() const {
	return browseQuery;
}

bool LinkLocalServiceBrowser::hasError() const {
	return haveError;
}

bool LinkLocalServiceBrowser::isRegistered() const {
	return registerQuery;
}

void LinkLocalServiceBrowser::registerService(const std::string& name, int port, const LinkLocalServiceInfo& info) {
	assert(!registerQuery);
	registerQuery = querier->createRegisterQuery(name, port, info.toTXTRecord());
	registerQuery->onRegisterFinished.connect(
		boost::bind(&LinkLocalServiceBrowser::handleRegisterFinished, this, _1));
	registerQuery->registerService();
}

void LinkLocalServiceBrowser::updateService(const LinkLocalServiceInfo& info) {
	assert(registerQuery);
	registerQuery->updateServiceInfo(info.toTXTRecord());
}

void LinkLocalServiceBrowser::unregisterService() {
	assert(registerQuery);
	registerQuery->unregisterService();
	registerQuery.reset();
	selfService.reset();
}

std::vector<LinkLocalService> LinkLocalServiceBrowser::getServices() const {
	std::vector<LinkLocalService> result;
	for (ServiceMap::const_iterator i = services.begin(); i != services.end(); ++i) {
		result.push_back(LinkLocalService(i->first, i->second));
	}
	return result;
}

void LinkLocalServiceBrowser::handleServiceAdded(const DNSSDServiceID& service) {
	if (selfService && service == *selfService) {
		return;
	}

	std::pair<ResolveQueryMap::iterator, bool> r = resolveQueries.insert(std::make_pair(service, boost::shared_ptr<DNSSDResolveServiceQuery>()));
	if (r.second) {
		// There was no existing query yet. Start a new query.
		boost::shared_ptr<DNSSDResolveServiceQuery> resolveQuery = querier->createResolveServiceQuery(service);
		resolveQuery->onServiceResolved.connect(
			boost::bind(&LinkLocalServiceBrowser::handleServiceResolved, this, service, _1));
		r.first->second = resolveQuery;
		resolveQuery->start();
	}
}

void LinkLocalServiceBrowser::handleServiceRemoved(const DNSSDServiceID& service) {
	ResolveQueryMap::iterator i = resolveQueries.find(service);
	if (i == resolveQueries.end()) {
		// Can happen after an unregister(), when getting the old 'self' 
		// service remove notification.
		return;
	}
	i->second->stop();
	resolveQueries.erase(i);
	ServiceMap::iterator j = services.find(service);
	assert(j != services.end());
	LinkLocalService linkLocalService(j->first, j->second);
	services.erase(j);
	onServiceRemoved(linkLocalService);
}

void LinkLocalServiceBrowser::handleServiceResolved(const DNSSDServiceID& service, const boost::optional<DNSSDResolveServiceQuery::Result>& result) {
	if (result) {
		std::pair<ServiceMap::iterator, bool> r = services.insert(std::make_pair(service, *result));
		if (r.second) {
			onServiceAdded(LinkLocalService(r.first->first, r.first->second));
		}
		else {
			r.first->second = *result;
			onServiceChanged(LinkLocalService(r.first->first, r.first->second));
		}
	}
}

void LinkLocalServiceBrowser::handleRegisterFinished(const boost::optional<DNSSDServiceID>& result) {
	if (result) {
		selfService = result;
		onServiceRegistered(*result);
	}
	else {
		haveError = true;
		stop();
	}
}

void LinkLocalServiceBrowser::handleBrowseError() {
	haveError = true;
	stop();
}

}