zeroconf.hpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10  ** Includes
11  *****************************************************************************/
12 
13 #include <set>
14 #include <string>
15 
16 #include <avahi-client/publish.h>
17 #include <avahi-client/client.h>
18 #include <avahi-client/lookup.h>
19 #include <avahi-common/malloc.h>
20 #include <avahi-common/error.h>
21 #include <avahi-common/thread-watch.h>
22 
23 #include <boost/thread/mutex.hpp>
24 #include <boost/bimap/bimap.hpp>
25 #include <boost/function.hpp>
26 #include <boost/shared_ptr.hpp>
27 
28 #include <zeroconf_msgs/PublishedService.h>
29 #include <zeroconf_msgs/DiscoveredService.h>
30 
31 /*****************************************************************************
32  ** Namespaces
33  *****************************************************************************/
34 
35 namespace zeroconf_avahi
36 {
37 
38 /*****************************************************************************
39  ** Utilities
40  *****************************************************************************/
51 {
52  bool operator()(const zeroconf_msgs::PublishedService &a, const zeroconf_msgs::PublishedService &b) const
53  {
54  if (a.name != b.name)
55  {
56  return a.name < b.name;
57  }
58  else if (a.type != b.type)
59  {
60  return a.type < b.type;
61  }
62  else
63  {
64  return a.port < b.port;
65  }
66  }
67 };
68 
79 {
80 public:
82  resolver(NULL)
83  {
84  }
85  DiscoveredAvahiService(zeroconf_msgs::DiscoveredService &discovered_service, AvahiServiceResolver *new_resolver,
86  int protocol, int hardware_interface) :
87  service(discovered_service), protocol(protocol), hardware_interface(hardware_interface), resolver(new_resolver)
88  {
89 
90  }
92  {
93  if (resolver)
94  {
95  avahi_service_resolver_free(resolver);
96  }
97  }
98  zeroconf_msgs::DiscoveredService service;
99  int protocol;
101  AvahiServiceResolver *resolver;
102 };
103 
111 {
113  const boost::shared_ptr<DiscoveredAvahiService> avahi_service_b) const
114  {
115  const zeroconf_msgs::DiscoveredService &a = avahi_service_a->service;
116  const zeroconf_msgs::DiscoveredService &b = avahi_service_b->service;
117  if (a.name != b.name)
118  {
119  return a.name < b.name;
120  }
121  else if (a.type != b.type)
122  {
123  return a.type < b.type;
124  }
125  else if (a.domain != b.domain)
126  {
127  return a.domain < b.domain;
128  }
129  else if (avahi_service_a->hardware_interface != avahi_service_b->hardware_interface)
130  {
131  return avahi_service_a->hardware_interface < avahi_service_b->hardware_interface;
132  }
133  else
134  {
135  return avahi_service_a->protocol < avahi_service_b->protocol;
136  }
137  }
138 };
139 
140 /*****************************************************************************
141  ** Interfaces
142  *****************************************************************************/
162 class Zeroconf
163 {
164 private:
165  typedef zeroconf_msgs::PublishedService PublishedService;
166  typedef boost::bimaps::bimap<AvahiEntryGroup*, boost::bimaps::set_of<PublishedService, PublishedServiceCompare> > service_bimap;
167  typedef boost::bimaps::bimap<AvahiServiceBrowser*, boost::bimaps::set_of<std::string> > discovery_bimap;
168  typedef std::set<boost::shared_ptr<DiscoveredAvahiService>, DiscoveredAvahiServiceCompare> discovered_service_set;
169  typedef std::pair<AvahiEntryGroup*, PublishedService> service_map_pair;
170  typedef boost::function<void(zeroconf_msgs::DiscoveredService)> connection_signal_cb;
171 
172 public:
173  Zeroconf();
174  ~Zeroconf();
175  bool add_service(PublishedService &service);
176  bool remove_service(const PublishedService &service);
177  // Todo : would be useful to be able to remove services in various other ways
178  // bool remove_services_by_name(const std::string& service_name);
179  // bool remove_services_by_type(const std::string& service_type);
180  // bool remove_services() <-- remove *all* services
181  bool add_listener(std::string &service_type);
182  bool remove_listener(const std::string &service_type);
183  bool is_alive() const { return !invalid_object; }
184  void list_discovered_services(const std::string &service_type, std::vector<zeroconf_msgs::DiscoveredService> &list);
185  void list_published_services(const std::string &service_type, std::vector<zeroconf_msgs::PublishedService> &list);
186 
187  void connect_signal_callbacks(connection_signal_cb new_connections, connection_signal_cb lost_connections)
188  {
189  new_connection_signal = new_connections;
190  lost_connection_signal = lost_connections;
191  }
192 
193  void spin();
194 
195 private:
197  AvahiThreadedPoll *threaded_poll;
198  AvahiClient *client;
199  service_bimap committed_services;
200  service_bimap established_services;
201  discovery_bimap discovery_service_types;
202  discovered_service_set discovered_services;
203  boost::mutex service_mutex;
204  const int interface;
206  connection_signal_cb new_connection_signal, lost_connection_signal;
207 
208  /*********************
209  ** Utilitiies
210  **********************/
211  int ros_to_avahi_protocol(const int &protocol);
212  std::string ros_to_txt_protocol(const int &protocol);
213  int avahi_to_ros_protocol(const int &protocol);
214  std::string avahi_to_txt_protocol(const int &protocol);
215  discovered_service_set::iterator find_discovered_service(zeroconf_msgs::DiscoveredService &service);
216 
217  /*********************
218  ** Mechanics
219  **********************/
220  bool add_service_non_threaded(PublishedService &service);
221  void fail()
222  {
223  avahi_threaded_poll_quit(threaded_poll);
224  invalid_object = true;
225  }
226 
227  /*********************
228  ** Callbacks
229  **********************/
230  static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata);
231  static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata);
232  static void discovery_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol,
233  AvahiBrowserEvent event, const char *name, const char *type, const char *domain,
234  AvahiLookupResultFlags flags, void* userdata);
235  static void resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol,
236  AvahiResolverEvent event, const char *name, const char *type, const char *domain,
237  const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt,
238  AvahiLookupResultFlags flags, void* userdata);
239  static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata);
240 };
241 
242 } // namespace zeroconf_avahi
std::set< boost::shared_ptr< DiscoveredAvahiService >, DiscoveredAvahiServiceCompare > discovered_service_set
Definition: zeroconf.hpp:168
bool is_alive() const
Definition: zeroconf.hpp:183
Internal storage for linking discovered service info with active avahi resolvers. ...
Definition: zeroconf.hpp:78
bool operator()(const zeroconf_msgs::PublishedService &a, const zeroconf_msgs::PublishedService &b) const
Definition: zeroconf.hpp:52
boost::mutex service_mutex
Definition: zeroconf.hpp:203
boost::bimaps::bimap< AvahiEntryGroup *, boost::bimaps::set_of< PublishedService, PublishedServiceCompare > > service_bimap
Definition: zeroconf.hpp:166
Internal comparison function for use with ordered sets.
Definition: zeroconf.hpp:50
AvahiThreadedPoll * threaded_poll
Definition: zeroconf.hpp:197
bool operator()(const boost::shared_ptr< DiscoveredAvahiService > avahi_service_a, const boost::shared_ptr< DiscoveredAvahiService > avahi_service_b) const
Definition: zeroconf.hpp:112
connection_signal_cb new_connection_signal
Definition: zeroconf.hpp:206
discovery_bimap discovery_service_types
Definition: zeroconf.hpp:201
const int permitted_protocols
Definition: zeroconf.hpp:205
service_bimap committed_services
Definition: zeroconf.hpp:199
zeroconf_msgs::DiscoveredService service
Definition: zeroconf.hpp:98
AvahiClient * client
Definition: zeroconf.hpp:198
Ros interface to linux&#39;s avahi daemon.
Definition: zeroconf.hpp:162
ROSCPP_DECL void spin()
zeroconf_msgs::PublishedService PublishedService
Definition: zeroconf.hpp:165
std::pair< AvahiEntryGroup *, PublishedService > service_map_pair
Definition: zeroconf.hpp:169
void connect_signal_callbacks(connection_signal_cb new_connections, connection_signal_cb lost_connections)
Definition: zeroconf.hpp:187
AvahiServiceResolver * resolver
Definition: zeroconf.hpp:101
DiscoveredAvahiService(zeroconf_msgs::DiscoveredService &discovered_service, AvahiServiceResolver *new_resolver, int protocol, int hardware_interface)
Definition: zeroconf.hpp:85
boost::bimaps::bimap< AvahiServiceBrowser *, boost::bimaps::set_of< std::string > > discovery_bimap
Definition: zeroconf.hpp:167
boost::function< void(zeroconf_msgs::DiscoveredService)> connection_signal_cb
Definition: zeroconf.hpp:170
service_bimap established_services
Definition: zeroconf.hpp:200
discovered_service_set discovered_services
Definition: zeroconf.hpp:202
Comparison functor for the discovered services set.
Definition: zeroconf.hpp:110


zeroconf_avahi
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 15:49:04