summaryrefslogtreecommitdiffstats
blob: 69ac06dbcc779059ba6da69cde77962e91c7b750 (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
#pragma once

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

#include "Swiften/EventLoop/MainEventLoop.h"
#include "Swiften/LinkLocal/DNSSDService.h"
#include "Swiften/LinkLocal/DNSSDServiceFactory.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 removeService(const LinkLocalServiceID& id) {
				serviceInfo.erase(id);
				MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), id));
			}

			void setServiceInfo(const LinkLocalServiceID& id, const DNSSDService::ResolveResult& info) {
				std::pair<ServiceInfoMap::iterator, bool> r = serviceInfo.insert(std::make_pair(id, info));
				if (!r.second) {
					r.first->second = 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;
	};

  class MockDNSSDServiceFactory : public DNSSDServiceFactory {
    public:
      boost::shared_ptr<DNSSDService> createDNSSDService() {
        boost::shared_ptr<MockDNSSDService> result(new MockDNSSDService());   
        services.push_back(result);
        return result;
      }

      bool canCreate() const {
        return true;
      }

      std::vector<boost::shared_ptr<MockDNSSDService> > services;
  };

}