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 <avahi-client/client.h>
28 #include <core/threading/thread.h>
29 #include <core/utils/lock_list.h>
30 #include <core/utils/lock_map.h>
31 #include <core/utils/lock_queue.h>
32 #include <netcomm/service_discovery/service_browser.h>
33 #include <netcomm/service_discovery/service_publisher.h>
34 #include <netinet/in.h>
35 
36 #include <string>
37 #include <utility>
38 
39 struct AvahiEntryGroup;
40 struct AvahiSimplePoll;
41 struct AvahiServiceBrowser;
42 struct AvahiServiceResolver;
43 struct AvahiHostNameResolver;
44 struct AvahiAddressResolver;
45 
46 namespace fawkes {
47 
48 class ServiceBrowseHandler;
49 class NetworkService;
50 class WaitCondition;
51 class AvahiResolverHandler;
52 
53 class AvahiThread : public Thread, public ServicePublisher, public ServiceBrowser
54 {
55 public:
56  AvahiThread(bool enable_ipv4 = true, bool enable_ipv6 = true);
57  ~AvahiThread();
58 
59  void wait_initialized();
60 
61  virtual void loop();
62 
63  /* Service publisher entry methods */
64  void publish_service(NetworkService *service);
65  void unpublish_service(NetworkService *service);
66 
67  /* Service browser methods */
68  void watch_service(const char *service_type, ServiceBrowseHandler *h);
69  void unwatch_service(const char *service_type, ServiceBrowseHandler *h);
70 
71  /* Resolver methods */
72  void resolve_name(const char *name, AvahiResolverHandler *handler);
73  void resolve_address(struct sockaddr *addr, socklen_t addrlen, AvahiResolverHandler *handler);
74 
75  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
76 protected:
77  virtual void
78  run()
79  {
80  Thread::run();
81  }
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, void *instance);
88 
89  static void browse_callback(AvahiServiceBrowser * b,
90  AvahiIfIndex interface,
91  AvahiProtocol protocol,
92  AvahiBrowserEvent event,
93  const char * name,
94  const char * type,
95  const char * domain,
96  AvahiLookupResultFlags flags,
97  void * instance);
98 
99  static void resolve_callback(AvahiServiceResolver *r,
100  AVAHI_GCC_UNUSED AvahiIfIndex interface,
101  AVAHI_GCC_UNUSED AvahiProtocol protocol,
102  AvahiResolverEvent event,
103  const char * name,
104  const char * type,
105  const char * domain,
106  const char * host_name,
107  const AvahiAddress * address,
108  uint16_t port,
109  AvahiStringList * txt,
110  AvahiLookupResultFlags flags,
111  void * instance);
112 
113  static void host_name_resolver_callback(AvahiHostNameResolver *r,
114  AvahiIfIndex interface,
115  AvahiProtocol protocol,
116  AvahiResolverEvent event,
117  const char * name,
118  const AvahiAddress * a,
119  AvahiLookupResultFlags flags,
120  void * userdata);
121 
122  static void address_resolver_callback(AvahiAddressResolver * r,
123  AvahiIfIndex interface,
124  AvahiProtocol protocol,
125  AvahiResolverEvent event,
126  const AvahiAddress * a,
127  const char * name,
128  AvahiLookupResultFlags flags,
129  void * userdata);
130 
131  void call_handler_service_removed(const char *name, const char *type, const char *domain);
132  void call_handler_service_added(const char * name,
133  const char * type,
134  const char * domain,
135  const char * host_name,
136  const AvahiIfIndex interface,
137  const AvahiAddress * address,
138  uint16_t port,
139  std::list<std::string> &txt,
140  AvahiLookupResultFlags flags);
141  void call_handler_failed(const char *name, const char *type, const char *domain);
142 
143  void call_handler_all_for_now(const char *type);
144  void call_handler_cache_exhausted(const char *type);
145 
146  void create_browser(const char *service_type);
147  void create_browsers();
148  void erase_browsers();
149  void recreate_browsers();
150  void create_pending_browsers();
151  void remove_pending_browsers();
152 
153  /* general private methods */
154  void init_done();
155  void recover();
156  void wake_poller();
157 
158  /* publisher private methods */
159  AvahiEntryGroup *create_service(const NetworkService &service, AvahiEntryGroup *exgroup);
160  void group_reset(AvahiEntryGroup *g);
161  void group_erase(AvahiEntryGroup *g);
162  void name_collision(AvahiEntryGroup *g);
163  void erase_groups();
164  void reset_groups();
165  void create_pending_services();
166  void remove_pending_services();
167  void recreate_services();
168 
169  /* resolver */
170  /** Internal type to pass data to callbacks for resolve methods */
171  typedef std::pair<AvahiThread *, AvahiResolverHandler *> AvahiResolverCallbackData;
172 
173  void remove_hostname_resolver(AvahiHostNameResolver *r);
174  void remove_address_resolver(AvahiAddressResolver *r);
175  void start_address_resolvers();
176  void start_hostname_resolvers();
177  void start_hostname_resolver(const char *name, AvahiResolverCallbackData *data);
178  void start_address_resolver(const struct sockaddr_storage *in_addr,
179  AvahiResolverCallbackData * data);
180 
181  bool enable_ipv4;
182  bool enable_ipv6;
183 
184  bool need_recover;
185  bool do_erase_browsers;
186  bool do_reset_groups;
187 
188  AvahiSimplePoll *simple_poll;
189  AvahiClient * client;
190  AvahiClientState client_state;
191  AvahiProtocol service_protocol;
192 
193  WaitCondition *init_wc;
194 
197  LockQueue<NetworkService> pending_services_;
198  LockQueue<NetworkService> pending_remove_services_;
199 
202  LockQueue<std::string> pending_browsers_;
203  LockQueue<std::string> pending_browser_removes_;
204 
205  LockList<AvahiHostNameResolver *> running_hostname_resolvers_;
206  LockList<AvahiAddressResolver *> running_address_resolvers_;
207 
208  LockMap<std::string, AvahiResolverCallbackData *> pending_hostname_resolves_;
210 };
211 
212 } // end namespace fawkes
213 
214 #endif
fawkes::AvahiThread::resolve_name
void resolve_name(const char *name, AvahiResolverHandler *handler)
Order name resolution.
Definition: avahi_thread.cpp:886
fawkes::LockMap
Definition: lock_map.h:41
fawkes::WaitCondition
Definition: wait_condition.h:42
fawkes::ServiceBrowser
Definition: service_browser.h:38
fawkes::AvahiThread::unpublish_service
void unpublish_service(NetworkService *service)
Definition: avahi_thread.cpp:264
fawkes::LockList< AvahiHostNameResolver * >
fawkes::AvahiThread::watch_service
void watch_service(const char *service_type, ServiceBrowseHandler *h)
Add a result handler.
Definition: avahi_thread.cpp:484
fawkes::AvahiThread::run
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: avahi_thread.h:78
fawkes::Thread::name
const char * name() const
Definition: thread.h:100
fawkes::AvahiThread::~AvahiThread
~AvahiThread()
Destructor.
Definition: avahi_thread.cpp:99
fawkes::AvahiThread::publish_service
void publish_service(NetworkService *service)
Publish service.
Definition: avahi_thread.cpp:252
fawkes::AvahiThread::AvahiThread
AvahiThread(bool enable_ipv4=true, bool enable_ipv6=true)
Constructor.
Definition: avahi_thread.cpp:74
fawkes::AvahiThread::resolve_address
void resolve_address(struct sockaddr *addr, socklen_t addrlen, AvahiResolverHandler *handler)
Order address resolution.
Definition: avahi_thread.cpp:948
fawkes::AvahiThread
Definition: avahi_thread.h:53
fawkes::NetworkService
Definition: service.h:43
fawkes
fawkes::ServicePublisher
Definition: service_publisher.h:37
fawkes::AvahiResolverHandler
Definition: avahi_resolver_handler.h:37
fawkes::AvahiThread::wait_initialized
void wait_initialized()
Waits for the AvahiThread to be initialized.
Definition: avahi_thread.cpp:1132
fawkes::Thread::run
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:924
fawkes::LockQueue
Definition: lock_queue.h:50
fawkes::Thread
Definition: thread.h:45
fawkes::AvahiThread::unwatch_service
void unwatch_service(const char *service_type, ServiceBrowseHandler *h)
Remove a handler.
Definition: avahi_thread.cpp:499
fawkes::AvahiThread::loop
virtual void loop()
Avahi thread loop.
Definition: avahi_thread.cpp:121
fawkes::ServiceBrowseHandler
Definition: browse_handler.h:52