]> git.meshlink.io Git - catta/blob - avahi-glib/glib-watch.c
a99cb4ddecb96c6a9bb57538af00b865a2f66f5a
[catta] / avahi-glib / glib-watch.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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <avahi-common/llist.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/timeval.h>
29
30 #include "glib-watch.h"
31
32 struct AvahiWatch {
33     AvahiGLibPoll *glib_poll;
34     int dead;
35     
36     GPollFD pollfd;
37     int pollfd_added;
38     
39     AvahiWatchCallback callback;
40     void *userdata;
41
42     AVAHI_LLIST_FIELDS(AvahiWatch, watches);
43 };
44
45 struct AvahiTimeout {
46     AvahiGLibPoll *glib_poll;
47     gboolean dead;
48
49     gboolean enabled;
50     struct timeval expiry;
51
52     AvahiTimeoutCallback callback;
53     void  *userdata;
54     
55     AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
56 };
57
58 struct AvahiGLibPoll {
59     GSource source;
60     AvahiPoll api;
61     GMainContext *context;
62
63     gboolean timeout_req_cleanup;
64     gboolean watch_req_cleanup;
65     
66     AVAHI_LLIST_HEAD(AvahiWatch, watches);
67     AVAHI_LLIST_HEAD(AvahiTimeout, timeouts);
68 };
69
70 static void destroy_watch(AvahiWatch *w) {
71     assert(w);
72
73     if (w->pollfd_added)
74         g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
75
76     AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->glib_poll->watches, w);
77
78     avahi_free(w);
79 }
80
81 static void cleanup_watches(AvahiGLibPoll *g, int all) {
82     AvahiWatch *w, *next;
83     assert(g);
84
85     for (w = g->watches; w; w = next) {
86         next = w->watches_next;
87
88         if (all || w->dead)
89             destroy_watch(w);
90     }
91
92     g->watch_req_cleanup = 0;
93 }
94
95 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
96     AvahiWatch *w;
97     AvahiGLibPoll *g;
98     
99     assert(api);
100     assert(fd >= 0);
101     assert(callback);
102
103     g = api->userdata;
104     assert(g);
105
106     if (!(w = avahi_new(AvahiWatch, 1)))
107         return NULL;
108     
109     w->glib_poll = g;
110     w->pollfd.fd = fd;
111     w->pollfd.events =
112         (event & AVAHI_WATCH_IN ? G_IO_IN : 0) |
113         (event & AVAHI_WATCH_OUT ? G_IO_OUT : 0) |
114         (event & AVAHI_WATCH_ERR ? G_IO_ERR : 0) |
115         (event & AVAHI_WATCH_HUP ? G_IO_HUP : 0);
116     w->pollfd.revents = 0;
117     w->callback = callback;
118     w->userdata = userdata;
119     w->dead = FALSE;
120
121     g_source_add_poll(&g->source, &w->pollfd);
122     w->pollfd_added = TRUE;
123
124     AVAHI_LLIST_PREPEND(AvahiWatch, watches, g->watches, w);
125
126     return w;
127 }
128
129 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
130     assert(w);
131     assert(!w->dead);
132
133     w->pollfd.events = events;
134 }
135
136 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
137     assert(w);
138     assert(!w->dead);
139
140     return w->pollfd.revents;
141 }
142
143 static void watch_free(AvahiWatch *w) {
144     assert(w);
145     assert(!w->dead);
146
147     if (w->pollfd_added) {
148         g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
149         w->pollfd_added = TRUE;
150     }
151     
152     w->dead = TRUE;
153     w->glib_poll->timeout_req_cleanup = TRUE;
154 }
155
156 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
157     AvahiTimeout *t;
158     AvahiGLibPoll *g;
159     
160     assert(api);
161     assert(callback);
162
163     g = api->userdata;
164     assert(g);
165
166     if (!(t = avahi_new(AvahiTimeout, 1)))
167         return NULL;
168     
169     t->glib_poll = g;
170     t->dead = FALSE;
171
172     if ((t->enabled = !!tv))
173         t->expiry = *tv;
174         
175     t->callback = callback;
176     t->userdata = userdata;
177
178     AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, g->timeouts, t);
179
180     return t;
181 }
182
183 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
184     assert(t);
185     assert(!t->dead);
186     
187     if ((t->enabled = !!tv))
188         t->expiry = *tv;
189 }
190
191 static void timeout_free(AvahiTimeout *t) {
192     assert(t);
193     assert(!t->dead);
194
195     t->dead = TRUE;
196     t->glib_poll->timeout_req_cleanup = TRUE;
197 }
198
199 static void destroy_timeout(AvahiTimeout *t) {
200     assert(t);
201
202     AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->glib_poll->timeouts, t);
203     avahi_free(t);
204 }
205
206 static void cleanup_timeouts(AvahiGLibPoll *g, int all) {
207     AvahiTimeout *t, *next;
208     assert(g);
209
210     for (t = g->timeouts; t; t = next) {
211         next = t->timeouts_next;
212
213         if (all || t->dead)
214             destroy_timeout(t);
215     }
216
217     g->timeout_req_cleanup = FALSE;
218 }
219
220 static AvahiTimeout* find_next_timeout(AvahiGLibPoll *g) {
221     AvahiTimeout *t, *n = NULL;
222     assert(g);
223
224     for (t = g->timeouts; t; t = t->timeouts_next) {
225         
226         if (t->dead || !t->enabled)
227             continue;
228         
229         if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
230             n = t;
231     }
232
233     return n;
234 }
235
236 static void start_timeout_callback(AvahiTimeout *t) {
237     assert(t);
238     assert(!t->dead);
239     assert(t->enabled);
240
241     t->enabled = 0;
242     t->callback(t, t->userdata);
243 }
244
245 static gboolean prepare_func(GSource *source, gint *timeout) {
246     AvahiGLibPoll *g = (AvahiGLibPoll*) source;
247     AvahiTimeout *next_timeout;
248
249     g_assert(g);
250     g_assert(timeout);
251
252     if (g->watch_req_cleanup)
253         cleanup_watches(g, 0);
254
255     if (g->timeout_req_cleanup)
256         cleanup_timeouts(g, 0);
257     
258     if ((next_timeout = find_next_timeout(g))) {
259         GTimeVal now;
260         struct timeval tvnow;
261         AvahiUsec usec;
262
263         g_source_get_current_time(source, &now);
264         tvnow.tv_sec = now.tv_sec;
265         tvnow.tv_usec = now.tv_usec;
266     
267         usec = avahi_timeval_diff(&next_timeout->expiry, &tvnow);
268
269         if (usec <= 0)
270             return TRUE;
271
272         *timeout = (gint) (usec / 1000);
273     }
274         
275     return FALSE;
276 }
277
278 static gboolean check_func(GSource *source) {
279     AvahiGLibPoll *g = (AvahiGLibPoll*) source;
280     AvahiWatch *w;
281     AvahiTimeout *next_timeout;
282
283     g_assert(g);
284
285     if ((next_timeout = find_next_timeout(g))) {
286         GTimeVal now;
287         struct timeval tvnow;
288         g_source_get_current_time(source, &now);
289         tvnow.tv_sec = now.tv_sec;
290         tvnow.tv_usec = now.tv_usec;
291         
292         if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) < 0)
293             return TRUE;
294     }
295
296     for (w = g->watches; w; w = w->watches_next)
297         if (w->pollfd.revents > 0)
298             return TRUE;
299     
300     return FALSE;
301 }
302
303 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer userdata) {
304     AvahiGLibPoll* g = (AvahiGLibPoll*) source;
305     AvahiWatch *w;
306     AvahiTimeout *next_timeout;
307     
308     g_assert(g);
309
310     if ((next_timeout = find_next_timeout(g))) {
311         GTimeVal now;
312         struct timeval tvnow;
313         g_source_get_current_time(source, &now);
314         tvnow.tv_sec = now.tv_sec;
315         tvnow.tv_usec = now.tv_usec;
316         
317         if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) < 0) {
318             start_timeout_callback(next_timeout);
319             return TRUE;
320         }
321     }
322     
323     for (w = g->watches; w; w = w->watches_next)
324         if (w->pollfd.revents > 0) {
325             assert(w->callback);
326             w->callback(w, w->pollfd.fd, w->pollfd.revents, w->userdata);
327             w->pollfd.revents = 0;
328             return TRUE;
329         }
330
331     return TRUE;
332 }
333
334 AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context, gint priority) {
335     AvahiGLibPoll *g;
336     
337     static GSourceFuncs source_funcs = {
338         prepare_func,
339         check_func,
340         dispatch_func,
341         NULL,
342         NULL,
343         NULL
344     };
345
346     g = (AvahiGLibPoll*) g_source_new(&source_funcs, sizeof(AvahiGLibPoll));
347     g_main_context_ref(g->context = context ? context : g_main_context_default());
348
349     g->api.userdata = g;
350     
351     g->api.watch_new = watch_new;
352     g->api.watch_free = watch_free;
353     g->api.watch_update = watch_update;
354     g->api.watch_get_events = watch_get_events;
355     
356     g->api.timeout_new = timeout_new;
357     g->api.timeout_free = timeout_free;
358     g->api.timeout_update = timeout_update;
359
360     g->watch_req_cleanup = FALSE;
361     g->timeout_req_cleanup = FALSE;
362     
363     AVAHI_LLIST_HEAD_INIT(AvahiWatch, g->watches);
364     AVAHI_LLIST_HEAD_INIT(AvahiTimeout, g->timeouts);
365     
366     g_source_attach(&g->source, g->context);
367     g_source_set_priority(&g->source, priority);
368
369     return g;
370 }
371
372 void avahi_glib_poll_free(AvahiGLibPoll *g) {
373     GSource *s = &g->source;
374     assert(g);
375
376     cleanup_watches(g, 1);
377     cleanup_timeouts(g, 1);
378
379     g_main_context_unref(g->context);
380     g_source_destroy(s);
381     g_source_unref(s);
382 }
383
384 const AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g) {
385     assert(g);
386
387     return &g->api;
388 }