4 This file is part of avahi.
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.
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.
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
29 struct AvahiRecordBrowser {
38 AvahiTimeEvent *time_event;
40 AvahiRecordBrowserCallback callback;
42 guint scan_idle_source;
44 AVAHI_LLIST_FIELDS(AvahiRecordBrowser, browser);
45 AVAHI_LLIST_FIELDS(AvahiRecordBrowser, by_key);
48 static void elapse(AvahiTimeEvent *e, void *userdata) {
49 AvahiRecordBrowser *s = userdata;
55 avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
59 if (s->sec_delay >= 60*60) /* 1h */
62 /* avahi_log_debug("%i. Continuous querying for %s", s->n_query, t = avahi_key_to_string(s->key)); */
65 avahi_elapse_time(&tv, s->sec_delay*1000, 0);
66 avahi_time_event_queue_update(s->server->time_event_queue, s->time_event, &tv);
70 AvahiRecordBrowser *record_browser;
71 AvahiInterface *interface;
74 static gpointer scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) {
75 struct cbdata *cbdata = userdata;
82 if (cbdata->record_browser->dead)
85 cbdata->record_browser->callback(
86 cbdata->record_browser,
87 cbdata->interface->hardware->index,
88 cbdata->interface->protocol,
91 cbdata->record_browser->userdata);
96 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
97 AvahiRecordBrowser *s = userdata;
98 struct cbdata cbdata = { s, i };
104 avahi_cache_walk(i->cache, s->key, scan_cache_callback, &cbdata);
107 gboolean scan_idle_callback(gpointer data) {
108 AvahiRecordBrowser *b = data;
111 /* Scan the caches */
112 avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b);
113 b->scan_idle_source = (guint) -1;
118 AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, gint interface, guchar protocol, AvahiKey *key, AvahiRecordBrowserCallback callback, gpointer userdata) {
119 AvahiRecordBrowser *b, *t;
126 g_assert(!avahi_key_is_pattern(key));
128 b = g_new(AvahiRecordBrowser, 1);
131 b->key = avahi_key_ref(key);
132 b->interface = interface;
133 b->protocol = protocol;
134 b->callback = callback;
135 b->userdata = userdata;
138 avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
140 avahi_elapse_time(&tv, b->sec_delay*1000, 0);
141 b->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, elapse, b);
143 AVAHI_LLIST_PREPEND(AvahiRecordBrowser, browser, server->record_browsers, b);
145 /* Add the new entry to the record_browser hash table */
146 t = g_hash_table_lookup(server->record_browser_hashtable, key);
147 AVAHI_LLIST_PREPEND(AvahiRecordBrowser, by_key, t, b);
148 g_hash_table_replace(server->record_browser_hashtable, key, t);
150 /* The currenlty cached entries are scanned a bit later */
151 b->scan_idle_source = g_idle_add_full(G_PRIORITY_HIGH, scan_idle_callback, b, NULL);
155 void avahi_record_browser_free(AvahiRecordBrowser *b) {
160 b->server->need_browser_cleanup = TRUE;
163 avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event);
164 b->time_event = NULL;
166 if (b->scan_idle_source != (guint) -1) {
167 g_source_remove(b->scan_idle_source);
168 b->scan_idle_source = (guint) -1;
174 void avahi_record_browser_destroy(AvahiRecordBrowser *b) {
175 AvahiRecordBrowser *t;
179 AVAHI_LLIST_REMOVE(AvahiRecordBrowser, browser, b->server->record_browsers, b);
181 t = g_hash_table_lookup(b->server->record_browser_hashtable, b->key);
182 AVAHI_LLIST_REMOVE(AvahiRecordBrowser, by_key, t, b);
184 g_hash_table_replace(b->server->record_browser_hashtable, t->key, t);
186 g_hash_table_remove(b->server->record_browser_hashtable, b->key);
189 avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event);
190 avahi_key_unref(b->key);
192 if (b->scan_idle_source != (guint) -1)
193 g_source_remove(b->scan_idle_source);
198 void avahi_browser_cleanup(AvahiServer *server) {
199 AvahiRecordBrowser *b;
200 AvahiRecordBrowser *n;
204 for (b = server->record_browsers; b; b = n) {
208 avahi_record_browser_destroy(b);
211 server->need_browser_cleanup = FALSE;
214 void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
215 AvahiRecordBrowser *b;
220 for (b = g_hash_table_lookup(server->record_browser_hashtable, record->key); b; b = b->by_key_next)
221 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
222 b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata);
225 gboolean avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) {
226 AvahiRecordBrowser *b;
230 for (b = g_hash_table_lookup(server->record_browser_hashtable, k); b; b = b->by_key_next)
231 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
237 void avahi_browser_new_interface(AvahiServer*s, AvahiInterface *i) {
238 AvahiRecordBrowser *b;
243 for (b = s->record_browsers; b; b = b->browser_next)
244 if (avahi_interface_match(i, b->interface, b->protocol))
245 avahi_interface_post_query(i, b->key, FALSE);