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>
27 #include <avahi-common/malloc.h>
32 struct AvahiSRecordBrowser {
37 AvahiIfIndex interface;
38 AvahiProtocol protocol;
41 AvahiTimeEvent *query_time_event;
42 AvahiTimeEvent *scan_time_event;
44 AvahiSRecordBrowserCallback callback;
47 AVAHI_LLIST_FIELDS(AvahiSRecordBrowser, browser);
48 AVAHI_LLIST_FIELDS(AvahiSRecordBrowser, by_key);
51 static void elapse_callback(AvahiTimeEvent *e, void *userdata) {
52 AvahiSRecordBrowser *s = userdata;
58 avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
62 if (s->sec_delay >= 60*60) /* 1h */
65 /* avahi_log_debug("Continuous querying for %s (%i)", t = avahi_key_to_string(s->key), s->sec_delay); */
68 avahi_elapse_time(&tv, s->sec_delay*1000, 0);
69 avahi_time_event_update(s->query_time_event, &tv);
73 AvahiSRecordBrowser *record_browser;
74 AvahiInterface *interface;
77 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
78 struct cbdata *cbdata = userdata;
85 if (cbdata->record_browser->dead)
88 cbdata->record_browser->callback(
89 cbdata->record_browser,
90 cbdata->interface->hardware->index,
91 cbdata->interface->protocol,
94 cbdata->record_browser->userdata);
99 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
100 AvahiSRecordBrowser *b = userdata;
101 struct cbdata cbdata;
103 cbdata.record_browser = b;
104 cbdata.interface = i;
111 avahi_cache_walk(i->cache, b->key, scan_cache_callback, &cbdata);
114 static void scan_callback(AvahiTimeEvent *e, void *userdata) {
115 AvahiSRecordBrowser *b = userdata;
118 /* Scan the caches */
120 avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b);
122 if (b->scan_time_event) {
123 avahi_time_event_free(b->scan_time_event);
124 b->scan_time_event = NULL;
128 AvahiSRecordBrowser *avahi_s_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiSRecordBrowserCallback callback, void* userdata) {
129 AvahiSRecordBrowser *b, *t;
136 if (avahi_key_is_pattern(key)) {
137 avahi_server_set_errno(server, AVAHI_ERR_IS_PATTERN);
141 if (!avahi_key_is_valid(key)) {
142 avahi_server_set_errno(server, AVAHI_ERR_INVALID_KEY);
146 if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
147 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
153 b->key = avahi_key_ref(key);
154 b->interface = interface;
155 b->protocol = protocol;
156 b->callback = callback;
157 b->userdata = userdata;
160 avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
162 avahi_elapse_time(&tv, b->sec_delay*1000, 0);
163 b->query_time_event = avahi_time_event_new(server->time_event_queue, &tv, elapse_callback, b);
165 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
167 /* Add the new entry to the record_browser hash table */
168 t = avahi_hashmap_lookup(server->record_browser_hashmap, key);
169 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, by_key, t, b);
170 avahi_hashmap_replace(server->record_browser_hashmap, key, t);
172 /* The currenlty cached entries are scanned a bit later */
173 b->scan_time_event = avahi_time_event_new(server->time_event_queue, NULL, scan_callback, b);
174 assert(b->scan_time_event);
178 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
183 b->server->need_browser_cleanup = 1;
185 if (b->query_time_event) {
186 avahi_time_event_free(b->query_time_event);
187 b->query_time_event = NULL;
190 if (b->scan_time_event) {
191 avahi_time_event_free(b->scan_time_event);
192 b->scan_time_event = NULL;
196 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
197 AvahiSRecordBrowser *t;
201 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
203 t = avahi_hashmap_lookup(b->server->record_browser_hashmap, b->key);
204 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, by_key, t, b);
206 avahi_hashmap_replace(b->server->record_browser_hashmap, t->key, t);
208 avahi_hashmap_remove(b->server->record_browser_hashmap, b->key);
210 if (b->query_time_event)
211 avahi_time_event_free(b->query_time_event);
212 if (b->scan_time_event)
213 avahi_time_event_free(b->scan_time_event);
215 avahi_key_unref(b->key);
220 void avahi_browser_cleanup(AvahiServer *server) {
221 AvahiSRecordBrowser *b;
222 AvahiSRecordBrowser *n;
226 for (b = server->record_browsers; b; b = n) {
230 avahi_s_record_browser_destroy(b);
233 server->need_browser_cleanup = 0;
236 void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
237 AvahiSRecordBrowser *b;
242 for (b = avahi_hashmap_lookup(server->record_browser_hashmap, record->key); b; b = b->by_key_next)
243 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
244 b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata);
247 int avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) {
248 AvahiSRecordBrowser *b;
252 for (b = avahi_hashmap_lookup(server->record_browser_hashmap, k); b; b = b->by_key_next)
253 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
259 void avahi_browser_new_interface(AvahiServer*s, AvahiInterface *i) {
260 AvahiSRecordBrowser *b;
265 for (b = s->record_browsers; b; b = b->browser_next)
266 if (avahi_interface_match(i, b->interface, b->protocol))
267 avahi_interface_post_query(i, b->key, 0);