summaryrefslogtreecommitdiffstats
blob: 66973d5165421bf38f43ff0a8b1e855cca323cb6 (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
#pragma once

#include <boost/signal.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
#include <map>
#include <vector>

#include "Swiften/Base/String.h"
#include "Swiften/LinkLocal/DNSSD/DNSSDQuerier.h"
#include "Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h"
#include "Swiften/LinkLocal/DNSSD/DNSSDRegisterQuery.h"
#include "Swiften/LinkLocal/DNSSD/DNSSDServiceID.h"
#include "Swiften/LinkLocal/LinkLocalService.h"
#include "Swiften/LinkLocal/LinkLocalServiceInfo.h"

namespace Swift {
	class LinkLocalServiceBrowser {
		public:
			LinkLocalServiceBrowser(boost::shared_ptr<DNSSDQuerier> querier);
			~LinkLocalServiceBrowser();

			void start();
			void stop();
			bool isRunning() const;
			bool hasError() const;

			void registerService(
					const String& name, 
					int port, 
					const LinkLocalServiceInfo& info = LinkLocalServiceInfo());
			void updateService(
					const LinkLocalServiceInfo& info = LinkLocalServiceInfo());
			void unregisterService();
			bool isRegistered() const;

			std::vector<LinkLocalService> getServices() const;

			// FIXME: Ugly that we need this
			boost::shared_ptr<DNSSDQuerier> getQuerier() const {
				return querier;
			}

			boost::signal<void (const LinkLocalService&)> onServiceAdded;
			boost::signal<void (const LinkLocalService&)> onServiceChanged;
			boost::signal<void (const LinkLocalService&)> onServiceRemoved;
			boost::signal<void (const DNSSDServiceID&)> onServiceRegistered;
			boost::signal<void (bool)> onStopped;

		private:
			void handleServiceAdded(const DNSSDServiceID&);
			void handleServiceRemoved(const DNSSDServiceID&);
			void handleServiceResolved(const DNSSDServiceID& service, const boost::optional<DNSSDResolveServiceQuery::Result>& result);
			void handleRegisterFinished(const boost::optional<DNSSDServiceID>&);
			void handleBrowseError();

		private:
			boost::shared_ptr<DNSSDQuerier> querier;
			boost::optional<DNSSDServiceID> selfService;
			boost::shared_ptr<DNSSDBrowseQuery> browseQuery;
			boost::shared_ptr<DNSSDRegisterQuery> registerQuery;
			typedef std::map<DNSSDServiceID, boost::shared_ptr<DNSSDResolveServiceQuery> > ResolveQueryMap;
			ResolveQueryMap resolveQueries;
			typedef std::map<DNSSDServiceID, DNSSDResolveServiceQuery::Result> ServiceMap;
			ServiceMap services;
			bool haveError;
	};
}