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
26 #include <avahi-common/timeval.h>
30 struct AvahiRecordBrowser {
35 AvahiIfIndex interface;
36 AvahiProtocol protocol;
39 AvahiTimeEvent *time_event;
41 AvahiRecordBrowserCallback callback;
43 guint scan_idle_source;
45 AVAHI_LLIST_FIELDS(AvahiRecordBrowser, browser);
46 AVAHI_LLIST_FIELDS(AvahiRecordBrowser, by_key);
49 static void elapse(AvahiTimeEvent *e, void *userdata) {
50 AvahiRecordBrowser *s = userdata;
56 avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
60 if (s->sec_delay >= 60*60) /* 1h */
63 /* avahi_log_debug("Continuous querying for %s (%i)", t = avahi_key_to_string(s->key), s->sec_delay); */
66 avahi_elapse_time(&tv, s->sec_delay*1000, 0);
67 avahi_time_event_queue_update(s->server->time_event_queue, s->time_event, &tv);
71 AvahiRecordBrowser *record_browser;
72 AvahiInterface *interface;
75 static gpointer scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) {
76 struct cbdata *cbdata = userdata;
83 if (cbdata->record_browser->dead)
86 cbdata->record_browser->callback(
87 cbdata->record_browser,
88 cbdata->interface->hardware->index,
89 cbdata->interface->protocol,
92 cbdata->record_browser->userdata);
97 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
98 AvahiRecordBrowser *s = userdata;
99 struct cbdata cbdata = { s, i };
105 avahi_cache_walk(i->cache, s->key, scan_cache_callback, &cbdata);
108 static gboolean scan_idle_callback(gpointer data) {
109 AvahiRecordBrowser *b = data;
112 /* Scan the caches */
113 avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b);
114 b->scan_idle_source = (guint) -1;
119 AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiRecordBrowserCallback callback, gpointer userdata) {
120 AvahiRecordBrowser *b, *t;
127 if (avahi_key_is_pattern(key)) {
128 avahi_server_set_errno(server, AVAHI_ERR_IS_PATTERN);
132 if (!avahi_key_valid(key)) {
133 avahi_server_set_errno(server, AVAHI_ERR_INVALID_KEY);
137 b = g_new(AvahiRecordBrowser, 1);
140 b->key = avahi_key_ref(key);
141 b->interface = interface;
142 b->protocol = protocol;
143 b->callback = callback;
144 b->userdata = userdata;
147 avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
149 avahi_elapse_time(&tv, b->sec_delay*1000, 0);
150 b->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, elapse, b);
152 AVAHI_LLIST_PREPEND(AvahiRecordBrowser, browser, server->record_browsers, b);
154 /* Add the new entry to the record_browser hash table */
155 t = g_hash_table_lookup(server->record_browser_hashtable, key);
156 AVAHI_LLIST_PREPEND(AvahiRecordBrowser, by_key, t, b);
157 g_hash_table_replace(server->record_browser_hashtable, key, t);
159 /* The currenlty cached entries are scanned a bit later */
160 b->scan_idle_source = g_idle_add_full(G_PRIORITY_HIGH, scan_idle_callback, b, NULL);
164 void avahi_record_browser_free(AvahiRecordBrowser *b) {
169 b->server->need_browser_cleanup = TRUE;
172 avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event);
173 b->time_event = NULL;
175 if (b->scan_idle_source != (guint) -1) {
176 g_source_remove(b->scan_idle_source);
177 b->scan_idle_source = (guint) -1;
183 void avahi_record_browser_destroy(AvahiRecordBrowser *b) {
184 AvahiRecordBrowser *t;
188 AVAHI_LLIST_REMOVE(AvahiRecordBrowser, browser, b->server->record_browsers, b);
190 t = g_hash_table_lookup(b->server->record_browser_hashtable, b->key);
191 AVAHI_LLIST_REMOVE(AvahiRecordBrowser, by_key, t, b);
193 g_hash_table_replace(b->server->record_browser_hashtable, t->key, t);
195 g_hash_table_remove(b->server->record_browser_hashtable, b->key);
198 avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event);
199 avahi_key_unref(b->key);
201 if (b->scan_idle_source != (guint) -1)
202 g_source_remove(b->scan_idle_source);
207 void avahi_browser_cleanup(AvahiServer *server) {
208 AvahiRecordBrowser *b;
209 AvahiRecordBrowser *n;
213 for (b = server->record_browsers; b; b = n) {
217 avahi_record_browser_destroy(b);
220 server->need_browser_cleanup = FALSE;
223 void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
224 AvahiRecordBrowser *b;
229 for (b = g_hash_table_lookup(server->record_browser_hashtable, record->key); b; b = b->by_key_next)
230 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
231 b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata);
234 gboolean avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) {
235 AvahiRecordBrowser *b;
239 for (b = g_hash_table_lookup(server->record_browser_hashtable, k); b; b = b->by_key_next)
240 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
246 void avahi_browser_new_interface(AvahiServer*s, AvahiInterface *i) {
247 AvahiRecordBrowser *b;
252 for (b = s->record_browsers; b; b = b->browser_next)
253 if (avahi_interface_match(i, b->interface, b->protocol))
254 avahi_interface_post_query(i, b->key, FALSE);