summaryrefslogtreecommitdiffstats
blob: 3bee9072e9f50195b2fdaa1987a43d4b9f48b9cf (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <boost/bind.hpp>

#include "Swiften/LinkLocal/DNSSD/Avahi/AvahiQuery.h"
#include "Swiften/LinkLocal/DNSSD/DNSSDBrowseQuery.h"
#include "Swiften/EventLoop/EventLoop.h"

namespace Swift {
	class AvahiQuerier;

	class AvahiBrowseQuery : public DNSSDBrowseQuery, public AvahiQuery {
		public:	
			AvahiBrowseQuery(boost::shared_ptr<AvahiQuerier> q) : AvahiQuery(q), browser(NULL) {
			}

			void startBrowsing() {
				std::cout << "Start browsing" << std::endl;
				assert(!browser);
				avahi_threaded_poll_lock(querier->getThreadedPoll());
				browser = avahi_service_browser_new(querier->getClient(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_presence._tcp", NULL, static_cast<AvahiLookupFlags>(0), &handleServiceDiscoveredStatic, this);
				if (!browser) {
					std::cout << "Error" << std::endl;
					eventLoop->postEvent(boost::bind(boost::ref(onError)), shared_from_this());
				}
				avahi_threaded_poll_unlock(querier->getThreadedPoll());
			}

			void stopBrowsing() {
				std::cout << "Stop browsing" << std::endl;
				avahi_threaded_poll_lock(querier->getThreadedPoll());
				avahi_service_browser_free(browser);
				browser = NULL;
				avahi_threaded_poll_unlock(querier->getThreadedPoll());
			}

		private:
			static void handleServiceDiscoveredStatic(AvahiServiceBrowser *b, AvahiIfIndex interfaceIndex, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* context) {
				static_cast<AvahiBrowseQuery*>(context)->handleServiceDiscovered(b, interfaceIndex, protocol, event, name, type, domain, flags);
			}

			void handleServiceDiscovered(AvahiServiceBrowser *, AvahiIfIndex interfaceIndex, AvahiProtocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags) {
				switch (event) {
					case AVAHI_BROWSER_FAILURE:	
						std::cout << "Service browse error" << std::endl;
						eventLoop->postEvent(boost::bind(boost::ref(onError)), shared_from_this());
						break;
					case AVAHI_BROWSER_NEW: {
						DNSSDServiceID service(name, domain, type, interfaceIndex);
						std::cout << "Service discovered " << name << " " << domain << " " << type << " " << interfaceIndex << std::endl;
						eventLoop->postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this());
						break;
					}
					case AVAHI_BROWSER_REMOVE: {
						std::cout << "Service went away " << name << " " << domain << " " << type << " " << interfaceIndex << std::endl;
						DNSSDServiceID service(name, domain, type, interfaceIndex);
						eventLoop->postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this());
						break;
					}
					case AVAHI_BROWSER_ALL_FOR_NOW:
					case AVAHI_BROWSER_CACHE_EXHAUSTED:
						break;
				}
			}

		private:
			AvahiServiceBrowser* browser;
			
	};
}