service_discovery
ServiceDiscovery.hpp
Go to the documentation of this file.
1 #ifndef _SERVICEDISCOVERY_CORE_H_
2 #define _SERVICEDISCOVERY_CORE_H_
3 
4 #include <string>
5 #include <vector>
6 #include <boost/thread.hpp>
7 #include <service_discovery/impl/avahi/Client.hpp>
8 #include <service_discovery/impl/avahi/ServiceBrowser.hpp>
9 #include <service_discovery/impl/avahi/ServiceEvent.hpp>
10 #include <service_discovery/impl/avahi/ServicePattern.hpp>
11 #include <service_discovery/impl/avahi/LocalService.hpp>
12 
13 namespace servicediscovery {
14 namespace avahi {
15 
19 };
20 
77 class ServiceDiscovery : public sigc::trackable, public ClientObserver
78 {
79 
80 public:
82 
87  virtual ~ServiceDiscovery();
88 
93  void start(const ServiceConfiguration& conf);
94 
100  void listenOn(const std::vector<std::string>& types);
101 
106  void update(const ServiceDescription& desc);
107 
114  virtual void update(const ClientObserver::Event& event);
115 
119  void stop();
120 
126  static bool update(const std::string& servicename, const ServiceDescription& description);
127 
132  static ServiceDescription getServiceDescription(const std::string& servicename);
133 
138  static std::vector<ServiceDescription> getUpdateableServices();
139 
145  std::vector<ServiceDescription> findServices( const SearchPattern& pattern = SearchPattern()) const;
146 
150  std::vector<ServiceDescription> findServices(const ServicePattern& pattern,
151  const std::string& name_space = "*") const;
152 
156  std::vector<std::string> getServiceNames();
157 
161  bool isRunning() const;
162 
163 
172 
173  void addedComponentConnect(const sigc::slot<void, ServiceEvent>& slot) {
174  boost::unique_lock<boost::mutex> lock(mAddedComponentMutex);
175  ServiceAddedSignal.connect(slot);
176  }
177 
178  void removedComponentConnect(const sigc::slot<void, ServiceEvent>& slot) {
179  boost::unique_lock<boost::mutex> lock(mRemovedComponentMutex);
180  ServiceRemovedSignal.connect(slot);
181  }
182 
188  static std::map<std::string, ServiceDescription> getVisibleServices(const SearchPattern& pattern = SearchPattern(""));
189 
193  static void registerCallbacks(const std::string& serviceName, const sigc::slot<void, ServiceEvent>& serviceAdded, const sigc::slot<void, ServiceEvent>& serviceRemoved);
194 
195 private:
196 
197  // The internal modes for the service discovery, reflect the
198  // usage as service publisher or listener only, i.e.
199  // to allow search one or more avahi domains, e.g.
200  // _test._tcp or similar
201  enum Mode { NONE = 0, LISTEN_ONLY = 1, PUBLISH = 2};
202 
206  void addedService(const RemoteService& service);
207 
211  void removedService(const RemoteService& service);
212 
216  void updatedService(const RemoteService& service);
217 
221  std::vector<ServiceDescription> _findServices(const SearchPattern& pattern) const;
222 
228  void _start(const ServiceConfiguration& conf);
229 
237  void _stop(bool lock_client, bool wait_until_unpublished = true);
238 
245  void _listenOn(const std::vector<std::string>& types);
246 
250  void reconnect();
251 
255  void cleanupBrowsers();
256 
257  sigc::signal<void, ServiceEvent> ServiceAddedSignal;
258  sigc::signal<void, ServiceEvent> ServiceRemovedSignal;
259 
260  mutable boost::mutex mAddedComponentMutex;
261  mutable boost::mutex mRemovedComponentMutex;
262  mutable boost::mutex mUpdatedComponentMutex;
263 
264  List<ServiceDescription> mServices;
265  static boost::mutex mServicesMutex;
266 
267  // Flag whether service is published or not
268  bool mPublished;
269 
270  // Seems to allow better thread safety and handling of the dbus if
271  // every browser is started with the same client object
272  //
273  // Map service type such as _myservice._tcp to a corresponding browser
274  std::map<std::string, ServiceBrowser*> mBrowsers;
275 
276  // There can be only a single local service
277  LocalService* mLocalService;
278  // Configuration of the local service
279  ServiceConfiguration mLocalServiceConfiguration;
280 
281  // When in listing mode, the list of service types this instance
282  // is listening to
283  std::vector<std::string> mListenTypes;
284 
285  // Mode of the
286  Mode mMode;
287 
288  // Global list of service discovery instances
289  static std::vector<ServiceDiscovery*> msServiceDiscoveries;
290 
291 };
292 
293 } // end namespace avahi
294 } // end namespace servicediscovery
295 #endif // _SERVICEDISCOVERY_CORE_H_
static std::map< std::string, ServiceDescription > getVisibleServices(const SearchPattern &pattern=SearchPattern(""))
Definition: ServiceDiscovery.cpp:137
class for handling service that are found and resolved by the service browsers
Definition: RemoteService.hpp:31
void start(const ServiceConfiguration &conf)
Definition: ServiceDiscovery.cpp:184
std::vector< std::string > getServiceNames()
Definition: ServiceDiscovery.cpp:318
Definition: Client.cpp:7
static void registerCallbacks(const std::string &serviceName, const sigc::slot< void, ServiceEvent > &serviceAdded, const sigc::slot< void, ServiceEvent > &serviceRemoved)
Definition: ServiceDiscovery.cpp:106
Description and parameterization of a service.
Definition: ServiceConfiguration.hpp:21
A wrapper class for the avahi service discovery.
Definition: ServiceDiscovery.hpp:77
void listenOn(const std::vector< std::string > &types)
Definition: ServiceDiscovery.cpp:201
static std::vector< ServiceDescription > getUpdateableServices()
Definition: ServiceDiscovery.cpp:163
void update(const ServiceDescription &desc)
Definition: ServiceDiscovery.cpp:209
Description and parameterization of a service instance.
Definition: ServiceDescription.hpp:21
void addedComponentConnect(const sigc::slot< void, ServiceEvent > &slot)
Definition: ServiceDiscovery.hpp:173
Definition: List.hpp:13
ServiceConfiguration getConfiguration() const
Definition: ServiceDiscovery.cpp:341
Definition: ServiceDiscovery.hpp:18
void stop()
Definition: ServiceDiscovery.cpp:482
virtual ~ServiceDiscovery()
Definition: ServiceDiscovery.cpp:28
std::vector< ServiceDescription > findServices(const SearchPattern &pattern=SearchPattern()) const
Definition: ServiceDiscovery.cpp:351
Definition: ServiceDiscovery.hpp:17
void removedComponentConnect(const sigc::slot< void, ServiceEvent > &slot)
Definition: ServiceDiscovery.hpp:178
ServiceDiscovery()
Definition: ServiceDiscovery.cpp:13
bool isRunning() const
Definition: ServiceDiscovery.cpp:331
LocalService for service objects that are meant to be published. Should be not used directly unless y...
Definition: LocalService.hpp:20
SDException
Definition: ServiceDiscovery.hpp:16
static ServiceDescription getServiceDescription(const std::string &servicename)
Definition: ServiceDiscovery.cpp:71