Fawkes API  Fawkes Development Version
avahi_thread.h
1 
2 /***************************************************************************
3  * avahi_thread.h - Avahi Thread
4  *
5  * Created: Wed Nov 08 11:17:06 2006
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __NETCOMM_DNSSD_AVAHI_THREAD_H_
25 #define __NETCOMM_DNSSD_AVAHI_THREAD_H_
26 
27 #include <core/threading/thread.h>
28 #include <netcomm/service_discovery/service_publisher.h>
29 #include <netcomm/service_discovery/service_browser.h>
30 
31 #include <core/utils/lock_map.h>
32 #include <core/utils/lock_list.h>
33 #include <core/utils/lock_queue.h>
34 
35 #include <avahi-client/client.h>
36 #include <string>
37 #include <utility>
38 #include <netinet/in.h>
39 
40 struct AvahiEntryGroup;
41 struct AvahiSimplePoll;
42 struct AvahiServiceBrowser;
43 struct AvahiServiceResolver;
44 struct AvahiHostNameResolver;
45 struct AvahiAddressResolver;
46 
47 namespace fawkes {
48 
49 class ServiceBrowseHandler;
50 class NetworkService;
51 class WaitCondition;
52 class AvahiResolverHandler;
53 
55 : public Thread,
56  public ServicePublisher,
57  public ServiceBrowser
58 {
59  public:
60  AvahiThread(bool enable_ipv4 = true, bool enable_ipv6 = true);
61  ~AvahiThread();
62 
63  void wait_initialized();
64 
65  virtual void loop();
66 
67  /* Service publisher entry methods */
68  void publish_service(NetworkService *service);
69  void unpublish_service(NetworkService *service);
70 
71  /* Service browser methods */
72  void watch_service(const char *service_type, ServiceBrowseHandler *h);
73  void unwatch_service(const char *service_type, ServiceBrowseHandler *h);
74 
75  /* Resolver methods */
76  void resolve_name(const char *name, AvahiResolverHandler *handler);
77  void resolve_address(struct sockaddr *addr, socklen_t addrlen,
78  AvahiResolverHandler *handler);
79 
80  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
81  protected: virtual void run() { Thread::run(); }
82 
83  private:
84  /* Callbacks */
85  static void client_callback(AvahiClient *c, AvahiClientState state, void *instance);
86 
87  static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
88  void *instance);
89 
90  static void browse_callback( AvahiServiceBrowser *b,
91  AvahiIfIndex interface,
92  AvahiProtocol protocol,
93  AvahiBrowserEvent event,
94  const char *name,
95  const char *type,
96  const char *domain,
97  AvahiLookupResultFlags flags,
98  void *instance);
99 
100  static void resolve_callback( AvahiServiceResolver *r,
101  AVAHI_GCC_UNUSED AvahiIfIndex interface,
102  AVAHI_GCC_UNUSED AvahiProtocol protocol,
103  AvahiResolverEvent event,
104  const char *name,
105  const char *type,
106  const char *domain,
107  const char *host_name,
108  const AvahiAddress *address,
109  uint16_t port,
110  AvahiStringList *txt,
111  AvahiLookupResultFlags flags,
112  void *instance);
113 
114  static void host_name_resolver_callback(AvahiHostNameResolver *r,
115  AvahiIfIndex interface,
116  AvahiProtocol protocol,
117  AvahiResolverEvent event,
118  const char *name,
119  const AvahiAddress *a,
120  AvahiLookupResultFlags flags,
121  void *userdata);
122 
123  static void address_resolver_callback(AvahiAddressResolver *r,
124  AvahiIfIndex interface,
125  AvahiProtocol protocol,
126  AvahiResolverEvent event,
127  const AvahiAddress *a,
128  const char *name,
129  AvahiLookupResultFlags flags,
130  void *userdata);
131 
132 
133  void call_handler_service_removed( const char *name,
134  const char *type,
135  const char *domain);
136  void call_handler_service_added( const char *name,
137  const char *type,
138  const char *domain,
139  const char *host_name,
140  const AvahiIfIndex interface,
141  const AvahiAddress *address,
142  uint16_t port,
143  std::list<std::string> &txt,
144  AvahiLookupResultFlags flags);
145  void call_handler_failed( const char *name,
146  const char *type,
147  const char *domain);
148 
149  void call_handler_all_for_now(const char *type);
150  void call_handler_cache_exhausted(const char *type);
151 
152 
153  void create_browser(const char *service_type);
154  void create_browsers();
155  void erase_browsers();
156  void recreate_browsers();
157  void create_pending_browsers();
158  void remove_pending_browsers();
159 
160  /* general private methods */
161  void init_done();
162  void recover();
163  void wake_poller();
164 
165  /* publisher private methods */
166  AvahiEntryGroup * create_service(const NetworkService &service,
167  AvahiEntryGroup *exgroup);
168  void group_reset(AvahiEntryGroup *g);
169  void group_erase(AvahiEntryGroup *g);
170  void name_collision(AvahiEntryGroup *g);
171  void erase_groups();
172  void reset_groups();
173  void create_pending_services();
174  void remove_pending_services();
175  void recreate_services();
176 
177  /* resolver */
178  /** Internal type to pass data to callbacks for resolve methods */
179  typedef std::pair<AvahiThread *, AvahiResolverHandler *> AvahiResolverCallbackData;
180 
181  void remove_hostname_resolver(AvahiHostNameResolver *r);
182  void remove_address_resolver(AvahiAddressResolver *r);
183  void start_address_resolvers();
184  void start_hostname_resolvers();
185  void start_hostname_resolver(const char *name, AvahiResolverCallbackData *data);
186  void start_address_resolver(const struct sockaddr_storage *in_addr, AvahiResolverCallbackData *data);
187 
188  bool enable_ipv4;
189  bool enable_ipv6;
190 
191  bool need_recover;
192  bool do_erase_browsers;
193  bool do_reset_groups;
194 
195  AvahiSimplePoll *simple_poll;
196  AvahiClient *client;
197  AvahiClientState client_state;
198  AvahiProtocol service_protocol;
199 
200  WaitCondition *init_wc;
201 
204  LockQueue<NetworkService> __pending_services;
205  LockQueue<NetworkService> __pending_remove_services;
206 
209  LockQueue<std::string> __pending_browsers;
210  LockQueue<std::string> __pending_browser_removes;
211 
212  LockList<AvahiHostNameResolver *> __running_hostname_resolvers;
213  LockList<AvahiAddressResolver *> __running_address_resolvers;
214 
215  LockMap<std::string, AvahiResolverCallbackData * > __pending_hostname_resolves;
217 };
218 
219 } // end namespace fawkes
220 
221 #endif
Service browser.
Service publisher interface.
Wait until a given condition holds.
~AvahiThread()
Destructor.
virtual void loop()
Avahi thread loop.
Fawkes library namespace.
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:939
Thread class encapsulation of pthreads.
Definition: thread.h:42
Interface for class that process browse results.
Map with a lock.
Definition: lock_map.h:37
Avahi resolver handler interface.
void publish_service(NetworkService *service)
Publish service.
void resolve_address(struct sockaddr *addr, socklen_t addrlen, AvahiResolverHandler *handler)
Order address resolution.
void resolve_name(const char *name, AvahiResolverHandler *handler)
Order name resolution.
List with a lock.
Definition: thread.h:40
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: avahi_thread.h:81
void watch_service(const char *service_type, ServiceBrowseHandler *h)
Add a result handler.
const char * name() const
Get name of thread.
Definition: thread.h:95
Avahi main thread.
Definition: avahi_thread.h:54
Queue with a lock.
Definition: lock_queue.h:43
Representation of a service announced or found via service discovery (i.e.
Definition: service.h:37
AvahiThread(bool enable_ipv4=true, bool enable_ipv6=true)
Constructor.
void unpublish_service(NetworkService *service)
Revoke service publication.
void wait_initialized()
Waits for the AvahiThread to be initialized.
void unwatch_service(const char *service_type, ServiceBrowseHandler *h)
Remove a handler.