]> git.meshlink.io Git - catta/blob - avahi-common/dbus-watch-glue.c
* Disable debug output of avahi-client
[catta] / avahi-common / dbus-watch-glue.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <assert.h>
23 #include <stdio.h>
24
25 #include <avahi-common/malloc.h>
26
27 #include "dbus-watch-glue.h"
28
29 static AvahiWatchEvent translate_dbus_to_avahi(unsigned int f) {
30     AvahiWatchEvent e = 0;
31
32     if (f & DBUS_WATCH_READABLE)
33         e |= AVAHI_WATCH_IN;
34     if (f & DBUS_WATCH_WRITABLE)
35         e |= AVAHI_WATCH_OUT;
36     if (f & DBUS_WATCH_ERROR)
37         e |= AVAHI_WATCH_ERR;
38     if (f & DBUS_WATCH_HANGUP)
39         e |= AVAHI_WATCH_HUP;
40
41     return e;
42 }
43
44 static unsigned int translate_avahi_to_dbus(AvahiWatchEvent e) {
45     unsigned int f = 0;
46
47     if (e & AVAHI_WATCH_IN)
48         f |= DBUS_WATCH_READABLE;
49     if (e & AVAHI_WATCH_OUT)
50         f |= DBUS_WATCH_WRITABLE;
51     if (e & AVAHI_WATCH_ERR)
52         f |= DBUS_WATCH_ERROR;
53     if (e & AVAHI_WATCH_HUP)
54         f |= DBUS_WATCH_HANGUP;
55
56     return f;
57 }
58
59 typedef struct {
60     DBusConnection *connection;
61     const AvahiPoll *poll_api;
62     AvahiTimeout *dispatch_timeout;
63     int ref;
64 } ConnectionData;
65
66 static ConnectionData *connection_data_ref(ConnectionData *d) {
67     assert(d);
68     assert(d->ref >= 1);
69
70     d->ref++;
71     return d;
72 }
73
74 static void connection_data_unref(ConnectionData *d) {
75     assert(d);
76     assert(d->ref >= 1);
77
78     if (--d->ref <= 0) {
79         d->poll_api->timeout_free(d->dispatch_timeout);
80         avahi_free(d);
81     }
82 }
83
84 static void request_dispatch(ConnectionData *d) {
85     static const struct timeval tv = { 0, 0 };
86     assert(d);
87
88     assert(dbus_connection_get_dispatch_status(d->connection) == DBUS_DISPATCH_DATA_REMAINS);
89
90     d->poll_api->timeout_update(d->dispatch_timeout, &tv);
91 }
92
93 static void dispatch_timeout_callback(AvahiTimeout *t, void *userdata) {
94     ConnectionData *d = userdata;
95     assert(t);
96     assert(d);
97
98     connection_data_ref(d);
99     dbus_connection_ref(d->connection);
100
101     if (dbus_connection_dispatch(d->connection) == DBUS_DISPATCH_DATA_REMAINS)
102         /* If there's still data, request that this handler is called again */
103         request_dispatch(d);
104
105     dbus_connection_unref(d->connection);
106     connection_data_unref(d);
107 }
108
109 static void watch_callback(AvahiWatch *avahi_watch, int fd, AvahiWatchEvent events, void *userdata) {
110     DBusWatch *dbus_watch = userdata;
111     assert(avahi_watch);
112     assert(dbus_watch);
113
114     dbus_watch_handle(dbus_watch, translate_avahi_to_dbus(events));
115     /* Ignore the return value */
116 }
117
118 static dbus_bool_t update_watch(const AvahiPoll *poll_api, DBusWatch *dbus_watch) {
119     AvahiWatch *avahi_watch;
120     dbus_bool_t b;
121     
122     assert(dbus_watch);
123
124     avahi_watch = dbus_watch_get_data(dbus_watch);
125
126     b = dbus_watch_get_enabled(dbus_watch);
127     
128     if (b && !avahi_watch) {
129
130         if (!(avahi_watch = poll_api->watch_new(
131                   poll_api,
132                   dbus_watch_get_fd(dbus_watch),
133                   translate_dbus_to_avahi(dbus_watch_get_flags(dbus_watch)),
134                   watch_callback,
135                   dbus_watch)))
136             return FALSE;
137
138         dbus_watch_set_data(dbus_watch, avahi_watch, NULL);
139         
140     } else if (!b && avahi_watch) {
141         
142         poll_api->watch_free(avahi_watch);
143         dbus_watch_set_data(dbus_watch, NULL, NULL);
144
145     } else if (avahi_watch) {
146         
147         /* Update flags */
148         poll_api->watch_update(avahi_watch, dbus_watch_get_flags(dbus_watch));
149     }
150
151     return TRUE;
152 }
153
154 static dbus_bool_t add_watch(DBusWatch *dbus_watch, void *userdata) {
155     ConnectionData *d = userdata;
156     
157     assert(dbus_watch);
158     assert(d);
159
160     return update_watch(d->poll_api, dbus_watch);
161 }
162
163 static void remove_watch(DBusWatch *dbus_watch, void *userdata) {
164     ConnectionData *d = userdata;
165     AvahiWatch *avahi_watch;
166     
167     assert(dbus_watch);
168     assert(d);
169
170     if ((avahi_watch = dbus_watch_get_data(dbus_watch))) {
171         d->poll_api->watch_free(avahi_watch);
172         dbus_watch_set_data(dbus_watch, NULL, NULL);
173     }
174 }
175
176 static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
177     ConnectionData *d = userdata;
178     
179     assert(dbus_watch);
180     assert(d);
181
182     update_watch(d->poll_api, dbus_watch);
183 }
184
185 typedef struct TimeoutData {
186     const AvahiPoll *poll_api;
187     AvahiTimeout *avahi_timeout;
188     DBusTimeout *dbus_timeout;
189     int ref;
190 } TimeoutData;
191
192 static TimeoutData* timeout_data_ref(TimeoutData *t) {
193     assert(t);
194     assert(t->ref >= 1);
195
196     t->ref++;
197     return t;
198 }
199
200 static void timeout_data_unref(TimeoutData *t) {
201     assert(t);
202     assert(t->ref >= 1);
203
204     if (--t->ref <= 0) {
205         if (t->avahi_timeout)
206             t->poll_api->timeout_free(t->avahi_timeout);
207         
208         avahi_free(t);
209     }
210 }
211
212 static void update_timeout(TimeoutData *timeout) {
213     assert(timeout);
214     assert(timeout->ref >= 1);
215     
216     if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
217         struct timeval tv;
218         avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
219         timeout->poll_api->timeout_update(timeout->
220                                       avahi_timeout, &tv);
221     } else
222         timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
223
224 }
225
226 static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
227     TimeoutData *timeout = userdata;
228     
229     assert(avahi_timeout);
230     assert(timeout);
231
232     timeout_data_ref(timeout);
233
234     dbus_timeout_handle(timeout->dbus_timeout);
235     /* Ignore the return value */
236
237     if (timeout->avahi_timeout)
238         update_timeout(timeout);
239     
240     timeout_data_unref(timeout);
241 }
242
243 static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
244     TimeoutData *timeout;
245     ConnectionData *d = userdata;
246     struct timeval tv;
247     dbus_bool_t b;
248
249     assert(dbus_timeout);
250     assert(d);
251
252     if (!(timeout = avahi_new(TimeoutData, 1)))
253         return FALSE;
254
255     timeout->dbus_timeout = dbus_timeout;
256     timeout->poll_api = d->poll_api;
257     timeout->ref = 1;
258
259     if ((b = dbus_timeout_get_enabled(dbus_timeout)))
260         avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
261     
262     if (!(timeout->avahi_timeout = d->poll_api->timeout_new(
263               d->poll_api,
264               b ? &tv : NULL,
265               timeout_callback,
266               timeout))) {
267         avahi_free(timeout);
268         return FALSE;
269     }
270
271     dbus_timeout_set_data(dbus_timeout, timeout, (DBusFreeFunction) timeout_data_unref);
272     return TRUE;
273 }
274
275 static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
276     ConnectionData *d = userdata;
277     TimeoutData *timeout;
278
279     assert(dbus_timeout);
280     assert(d);
281
282     timeout = dbus_timeout_get_data(dbus_timeout);
283     assert(timeout);
284
285     d->poll_api->timeout_free(timeout->avahi_timeout);
286     timeout->avahi_timeout = NULL;
287 }
288
289 static void timeout_toggled(DBusTimeout *dbus_timeout, void *userdata) {
290     TimeoutData *timeout;
291
292     assert(dbus_timeout);
293     timeout = dbus_timeout_get_data(dbus_timeout);
294     assert(timeout);
295
296     update_timeout(timeout);
297 }
298
299 static void dispatch_status(DBusConnection *connection, DBusDispatchStatus new_status, void *userdata) {
300     ConnectionData *d = userdata;
301     
302     if (new_status == DBUS_DISPATCH_DATA_REMAINS)
303         request_dispatch(d);
304  }
305
306 int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
307     ConnectionData *d = NULL;
308     
309     assert(c);
310     assert(poll_api);
311
312     if (!(d = avahi_new(ConnectionData, 1)))
313         goto fail;;
314
315     d->poll_api = poll_api;
316     d->connection = c;
317     d->ref = 1;
318     
319     if (!(d->dispatch_timeout = poll_api->timeout_new(poll_api, NULL, dispatch_timeout_callback, d)))
320         goto fail;
321     
322     if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
323         goto fail;
324
325     if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
326         goto fail;
327
328     dbus_connection_set_dispatch_status_function(c, dispatch_status, connection_data_ref(d), (DBusFreeFunction)connection_data_unref);
329
330     if (dbus_connection_get_dispatch_status(c) == DBUS_DISPATCH_DATA_REMAINS)
331         request_dispatch(d);
332
333     connection_data_unref(d);
334     
335     return 0;
336
337 fail:
338
339     if (d) {
340         d->poll_api->timeout_free(d->dispatch_timeout);
341
342         avahi_free(d);
343     }
344
345     return -1;
346 }