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 <chrono>
37#include <string>
38#include <utility>
39
40struct AvahiEntryGroup;
41struct AvahiSimplePoll;
42struct AvahiServiceBrowser;
43struct AvahiServiceResolver;
44struct AvahiHostNameResolver;
45struct AvahiAddressResolver;
46
47namespace fawkes {
48
49class ServiceBrowseHandler;
50class NetworkService;
51class WaitCondition;
52class AvahiResolverHandler;
53
54class AvahiThread : public Thread, public ServicePublisher, public ServiceBrowser
55{
56public:
57 AvahiThread(bool enable_ipv4 = true, bool enable_ipv6 = true);
59
60 void wait_initialized();
61
62 virtual void loop();
63
64 /* Service publisher entry methods */
65 void publish_service(NetworkService *service);
66 void unpublish_service(NetworkService *service);
67
68 /* Service browser methods */
69 void watch_service(const char *service_type, ServiceBrowseHandler *h);
70 void unwatch_service(const char *service_type, ServiceBrowseHandler *h);
71
72 /* Resolver methods */
73 void resolve_name(const char *name, AvahiResolverHandler *handler);
74 void resolve_address(struct sockaddr *addr, socklen_t addrlen, AvahiResolverHandler *handler);
75
76 /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
77protected:
78 virtual void
80 {
82 }
83
84private:
85 /* Callbacks */
86 static void client_callback(AvahiClient *c, AvahiClientState state, void *instance);
87
88 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, 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 void call_handler_service_removed(const char *name, const char *type, const char *domain);
133 void call_handler_service_added(const char * name,
134 const char * type,
135 const char * domain,
136 const char * host_name,
137 const AvahiIfIndex interface,
138 const AvahiAddress * address,
139 uint16_t port,
140 std::list<std::string> &txt,
141 AvahiLookupResultFlags flags);
142 void call_handler_failed(const char *name, const char *type, const char *domain);
143
144 void call_handler_all_for_now(const char *type);
145 void call_handler_cache_exhausted(const char *type);
146
147 void create_browser(const char *service_type);
148 void create_browsers();
149 void erase_browsers();
150 void recreate_browsers();
151 void create_pending_browsers();
152 void remove_pending_browsers();
153
154 /* general private methods */
155 void init_done();
156 void recover();
157 void wake_poller();
158
159 /* publisher private methods */
160 AvahiEntryGroup *create_service(const NetworkService &service, AvahiEntryGroup *exgroup);
161 void group_reset(AvahiEntryGroup *g);
162 void group_erase(AvahiEntryGroup *g);
163 void name_collision(AvahiEntryGroup *g);
164 void erase_groups();
165 void reset_groups();
166 void create_pending_services();
167 void remove_pending_services();
168 void recreate_services();
169
170 /* resolver */
171 /** Internal type to pass data to callbacks for resolve methods */
172 typedef std::pair<AvahiThread *, AvahiResolverHandler *> AvahiResolverCallbackData;
173
174 void remove_hostname_resolver(AvahiHostNameResolver *r);
175 void remove_address_resolver(AvahiAddressResolver *r);
176 void start_address_resolvers();
177 void start_hostname_resolvers();
178 void start_hostname_resolver(const char *name, AvahiResolverCallbackData *data);
179 void start_address_resolver(const struct sockaddr_storage *in_addr,
180 AvahiResolverCallbackData * data);
181
182 bool enable_ipv4;
183 bool enable_ipv6;
184
185 bool need_recover;
186 bool do_erase_browsers;
187 bool do_reset_groups;
188
189 AvahiSimplePoll * simple_poll;
190 AvahiClient * client;
191 AvahiClientState client_state;
192 AvahiProtocol service_protocol;
193 const static std::chrono::seconds wait_on_init_failure;
194
195 WaitCondition *init_wc;
196
199 LockQueue<NetworkService> pending_services_;
200 LockQueue<NetworkService> pending_remove_services_;
201
204 LockQueue<std::string> pending_browsers_;
205 LockQueue<std::string> pending_browser_removes_;
206
207 LockList<AvahiHostNameResolver *> running_hostname_resolvers_;
208 LockList<AvahiAddressResolver *> running_address_resolvers_;
209
210 LockMap<std::string, AvahiResolverCallbackData *> pending_hostname_resolves_;
212};
213
214} // end namespace fawkes
215
216#endif
Avahi resolver handler interface.
Avahi main thread.
Definition: avahi_thread.h:55
void resolve_name(const char *name, AvahiResolverHandler *handler)
Order name resolution.
void watch_service(const char *service_type, ServiceBrowseHandler *h)
Add a result handler.
~AvahiThread()
Destructor.
void publish_service(NetworkService *service)
Publish service.
void unwatch_service(const char *service_type, ServiceBrowseHandler *h)
Remove a handler.
void unpublish_service(NetworkService *service)
Revoke service publication.
AvahiThread(bool enable_ipv4=true, bool enable_ipv6=true)
Constructor.
void wait_initialized()
Waits for the AvahiThread to be initialized.
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: avahi_thread.h:79
virtual void loop()
Avahi thread loop.
void resolve_address(struct sockaddr *addr, socklen_t addrlen, AvahiResolverHandler *handler)
Order address resolution.
Map with a lock.
Definition: lock_map.h:36
Queue with a lock.
Definition: lock_queue.h:45
Representation of a service announced or found via service discovery (i.e.
Definition: service.h:38
Interface for class that process browse results.
Service browser.
Service publisher interface.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:918
Wait until a given condition holds.
Fawkes library namespace.