summaryrefslogtreecommitdiffstats
blob: 5d857fb42fda8be119bcbd2bb26234157244ce73 (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
#pragma once

#include <vector>
#include <map>
#include <boost/bind.hpp>

#include "Swiften/EventLoop/MainEventLoop.h"
#include "Swiften/LinkLocal/DNSSDService.h"

namespace Swift {
	class MockDNSSDService : public DNSSDService {
		public:
			MockDNSSDService() {
			}
			
			void start() {
			}

			void stop() {
			}

			virtual void registerService(const String&, int, const LinkLocalServiceInfo&) {
				assert(false);
			}

			virtual void updateService(const LinkLocalServiceInfo&) {
				assert(false);
			}

			virtual void unregisterService() {
				assert(false);
			}

			virtual void startResolvingService(const LinkLocalServiceID& id) {
				resolvingServices.push_back(id);
				broadcastServiceInfo(id);
			}

			virtual void stopResolvingService(const LinkLocalServiceID& id) {
				resolvingServices.erase(std::remove(resolvingServices.begin(), resolvingServices.end(), id), resolvingServices.end());
			}
			
			virtual void resolveHostname(const String&, int) {
				assert(false);
			}

			void addService(const LinkLocalServiceID& id) {
				MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), id));
			}

			void setServiceInfo(const LinkLocalServiceID& id, const DNSSDService::ResolveResult& info) {
				serviceInfo.insert(std::make_pair(id, info));
				broadcastServiceInfo(id);
			}

		private:
			void broadcastServiceInfo(const LinkLocalServiceID& id) {
				if (std::find(resolvingServices.begin(), resolvingServices.end(), id) != resolvingServices.end()) {
					ServiceInfoMap::const_iterator i = serviceInfo.find(id);
					if (i != serviceInfo.end()) {
						MainEventLoop::postEvent(
								boost::bind(boost::ref(onServiceResolved), id, i->second));
					}
				}
			}

		private:
			typedef std::map<LinkLocalServiceID,DNSSDService::ResolveResult> ServiceInfoMap;
			ServiceInfoMap serviceInfo;
			std::vector<LinkLocalServiceID> resolvingServices;
	};


}