Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 struct sockaddr_in;
47 
48 namespace fawkes {
49 
50 class ServiceBrowseHandler;
51 class NetworkService;
52 class WaitCondition;
53 class AvahiResolverHandler;
54 
56 : public Thread,
57  public ServicePublisher,
58  public ServiceBrowser
59 {
60  public:
61  AvahiThread();
62  ~AvahiThread();
63 
64  void wait_initialized();
65 
66  virtual void loop();
67 
68  /* Service publisher entry methods */
69  void publish_service(NetworkService *service);
70  void unpublish_service(NetworkService *service);
71 
72  /* Service browser methods */
73  void watch_service(const char *service_type, ServiceBrowseHandler *h);
74  void unwatch_service(const char *service_type, ServiceBrowseHandler *h);
75 
76  /* Resolver methods */
77  void resolve_name(const char *name, AvahiResolverHandler *handler);
78  void resolve_address(struct sockaddr *addr, socklen_t addrlen,
79  AvahiResolverHandler *handler);
80 
81  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
82  protected: virtual void run() { Thread::run(); }
83 
84  private:
85  /* Callbacks */
86  static void client_callback(AvahiClient *c, AvahiClientState state, void *instance);
87 
88  static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
89  void *instance);
90 
91  static void browse_callback( AvahiServiceBrowser *b,
92  AvahiIfIndex interface,
93  AvahiProtocol protocol,
94  AvahiBrowserEvent event,
95  const char *name,
96  const char *type,
97  const char *domain,
98  AvahiLookupResultFlags flags,
99  void *instance);
100 
101  static void resolve_callback( AvahiServiceResolver *r,
102  AVAHI_GCC_UNUSED AvahiIfIndex interface,
103  AVAHI_GCC_UNUSED AvahiProtocol protocol,
104  AvahiResolverEvent event,
105  const char *name,
106  const char *type,
107  const char *domain,
108  const char *host_name,
109  const AvahiAddress *address,
110  uint16_t port,
111  AvahiStringList *txt,
112  AvahiLookupResultFlags flags,
113  void *instance);
114 
115  static void host_name_resolver_callback(AvahiHostNameResolver *r,
116  AvahiIfIndex interface,
117  AvahiProtocol protocol,
118  AvahiResolverEvent event,
119  const char *name,
120  const AvahiAddress *a,
121  AvahiLookupResultFlags flags,
122  void *userdata);
123 
124  static void address_resolver_callback(AvahiAddressResolver *r,
125  AvahiIfIndex interface,
126  AvahiProtocol protocol,
127  AvahiResolverEvent event,
128  const AvahiAddress *a,
129  const char *name,
130  AvahiLookupResultFlags flags,
131  void *userdata);
132 
133 
134  void call_handler_service_removed( const char *name,
135  const char *type,
136  const char *domain);
137  void call_handler_service_added( const char *name,
138  const char *type,
139  const char *domain,
140  const char *host_name,
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(struct sockaddr_in *in_addr, AvahiResolverCallbackData *data);
187 
188 
189  bool need_recover;
190  bool do_erase_browsers;
191  bool do_reset_groups;
192 
193  AvahiSimplePoll *simple_poll;
194  AvahiClient *client;
195  AvahiClientState client_state;
196 
197  WaitCondition *init_wc;
198 
201  LockQueue<NetworkService> __pending_services;
202  LockQueue<NetworkService> __pending_remove_services;
203 
206  LockQueue<std::string> __pending_browsers;
207  LockQueue<std::string> __pending_browser_removes;
208 
209  LockList<AvahiHostNameResolver *> __running_hostname_resolvers;
210  LockList<AvahiAddressResolver *> __running_address_resolvers;
211 
212  LockMap<std::string, AvahiResolverCallbackData * > __pending_hostname_resolves;
216 };
217 
218 } // end namespace fawkes
219 
220 #endif