service_discovery
ServiceBrowser.hpp
Go to the documentation of this file.
1 #ifndef _SERVICE_DISCOVERY_SERVICEBROWSER_H_
2 #define _SERVICE_DISCOVERY_SERVICEBROWSER_H_
3 
4 namespace servicediscovery {
5 namespace avahi {
6 
7 struct ResolveData;
8 class ServiceBrowser;
9 
10 }}
11 
12 #include <iostream>
13 #include <map>
14 #include <list>
15 #include <string>
16 #include <boost/thread.hpp>
17 #include <sigc++/sigc++.h>
18 #include <avahi-client/client.h>
19 #include <service_discovery/impl/avahi/RemoteService.hpp>
20 #include <service_discovery/impl/avahi/Client.hpp>
21 #include <service_discovery/utils/List.hpp>
22 #include <service_discovery/impl/avahi/Service.hpp>
23 
24 namespace servicediscovery {
25 namespace avahi {
26 
31 struct ResolveData {
33  int count;
34 };
35 
36 
43 private:
45  Client* client;
47  std::string type;
49  AvahiIfIndex interface;
51  AvahiProtocol protocol;
53  std::string domain;
55  AvahiLookupFlags flags;
56 
57  void *data;
58 
59  AvahiServiceBrowser *browser;
60 
61  void bootstrap();
62 
63  List<RemoteService> services;
64 
65  boost::mutex mServicesMutex;
66 
67  const char* getInType() {
68  return type.c_str();
69  }
70 
71  const char* getInDomain() {
72  if (domain == "") {
73  return NULL; //if empty string then the default value NULL (.local) domain is used
74  } else {
75  return domain.c_str();
76  }
77  }
78 
79 
81  sigc::signal<void, RemoteService> ServiceAdded;
82 
84  sigc::signal<void, RemoteService> ServiceRemoved;
85 
87  sigc::signal<void, RemoteService> ServiceUpdated;
88 
89  boost::mutex mServiceAddedMutex;
90  boost::mutex mServiceRemovedMutex;
91  boost::mutex mServiceUpdateMutex;
92 
93 public:
94 
98  const int static MAX_RESOLVER_TRIES = 5;
99 
106  Client *client,
107  std::string type);
119  Client* client,
120  AvahiIfIndex interface, // In most cases pass AVAHI_IF_UNSPEC here
121  AvahiProtocol protocol, // In most cases pass AVAHI_PROTO_UNSPEC here
122  std::string type, // A service type such as "_http._tcp"
123  std::string domain, // A domain to browse in. In most cases you want to pass NULL here for the default domain (usually ".local")
124  AvahiLookupFlags flags,
125  void* data);
126 
127  virtual ~ServiceBrowser();
128 
133  void cleanup();
134 
136 // sigc::signal<void,
137 // ServiceBrowser*,
138 // AvahiBrowserEvent,
139 // RemoteService*,
140 // void*> ServiceBrowserSignal;
141 
142 
143  void serviceAddedConnect(const sigc::slot<void, RemoteService>& slot_) {
144  boost::unique_lock<boost::mutex> lock(mServiceAddedMutex);
145  ServiceAdded.connect(slot_);
146  }
147 
149  boost::unique_lock<boost::mutex> lock(mServiceAddedMutex);
150  ServiceAdded.emit(rms);
151  }
152 
153 
154  void serviceRemovedConnect(const sigc::slot<void, RemoteService>& slot_) {
155  boost::unique_lock<boost::mutex> lock(mServiceRemovedMutex);
156  ServiceRemoved.connect(slot_);
157  }
158 
160  boost::unique_lock<boost::mutex> lock(mServiceRemovedMutex);
161  ServiceRemoved.emit(rms);
162  }
163 
164  void serviceUpdatedConnect(const sigc::slot<void, RemoteService>& slot_) {
165  boost::unique_lock<boost::mutex> lock(mServiceUpdateMutex);
166  ServiceUpdated.connect(slot_);
167  }
168 
170  boost::unique_lock<boost::mutex> lock(mServiceUpdateMutex);
171  ServiceUpdated.emit(rms);
172  }
173 
175  {
176  boost::unique_lock<boost::mutex> lock(mServicesMutex);
177  List<RemoteService> tlist = services;
178  return tlist;
179  }
180 
181  //to be used only by the static callbacks. TODO: how to avoid this to be public
183  {
184  return &services;
185  }
186  //to be used only by the static callbacks. TODO: how to avoid this to be public
187  boost::mutex& getServicesMutex()
188  {
189  return mServicesMutex;
190  }
191 
192 
193 
194  AvahiServiceBrowser* getAvahiBrowser()
195  {
196  return browser;
197  }
198 
200  {
201  return client;
202  }
203 
204 
205  static void browseCallback(AvahiServiceBrowser *sb, AvahiIfIndex interface, AvahiProtocol protocol,
206  AvahiBrowserEvent event, const char *name, const char *type, const char *domain,
207  AvahiLookupResultFlags flags, void *userdata);
208 
209  static void resolveCallback(AvahiServiceResolver *sr, AvahiIfIndex interface, AvahiProtocol protocol,
210  AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host,
211  const AvahiAddress *address, uint16_t port, AvahiStringList *list, AvahiLookupResultFlags flags, void *userdata);
212 
213 };
214 
215 } // end namespace avahi
216 } // end namespace servicediscovery
217 #endif /* _SERVICE_DISCOVERYSERVICEBROWSER_H_ */
List< RemoteService > getServices()
Definition: ServiceBrowser.hpp:174
Client represent a wrapper to access the avahi_client (a singleton). Every avahi program must set up ...
Definition: Client.hpp:64
ServiceBrowser * serviceBrowser
Definition: ServiceBrowser.hpp:32
virtual ~ServiceBrowser()
Definition: ServiceBrowser.cpp:70
class for handling service that are found and resolved by the service browsers
Definition: RemoteService.hpp:31
ServiceBrowser(Client *client, std::string type)
Definition: ServiceBrowser.cpp:42
struct used to count failed attempts of service resolution
Definition: ServiceBrowser.hpp:31
const static int MAX_RESOLVER_TRIES
Definition: ServiceBrowser.hpp:98
Definition: List.hpp:13
Client * getClient()
Definition: ServiceBrowser.hpp:199
AvahiServiceBrowser * getAvahiBrowser()
Definition: ServiceBrowser.hpp:194
void serviceAddedConnect(const sigc::slot< void, RemoteService > &slot_)
Definition: ServiceBrowser.hpp:143
List< RemoteService > * getInternalServices()
Definition: ServiceBrowser.hpp:182
void serviceRemovedEmit(RemoteService rms)
Definition: ServiceBrowser.hpp:159
Definition: ServiceBrowser.hpp:42
void serviceAddedEmit(RemoteService rms)
Definition: ServiceBrowser.hpp:148
void serviceUpdatedEmit(RemoteService rms)
Definition: ServiceBrowser.hpp:169
static void browseCallback(AvahiServiceBrowser *sb, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void *userdata)
Definition: ServiceBrowser.cpp:157
static void resolveCallback(AvahiServiceResolver *sr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host, const AvahiAddress *address, uint16_t port, AvahiStringList *list, AvahiLookupResultFlags flags, void *userdata)
Definition: ServiceBrowser.cpp:76
void serviceUpdatedConnect(const sigc::slot< void, RemoteService > &slot_)
Definition: ServiceBrowser.hpp:164
boost::mutex & getServicesMutex()
Definition: ServiceBrowser.hpp:187
int count
Definition: ServiceBrowser.hpp:33
void cleanup()
Definition: ServiceBrowser.cpp:53
void serviceRemovedConnect(const sigc::slot< void, RemoteService > &slot_)
Definition: ServiceBrowser.hpp:154