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 "subscribe.h"
29 static void elapse(AvahiTimeEvent *e, void *userdata) {
30 AvahiSubscription *s = userdata;
36 avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
38 if (s->n_query++ <= 8)
41 g_message("%i. Continuous querying for %s", s->n_query, t = avahi_key_to_string(s->key));
44 avahi_elapse_time(&tv, s->sec_delay*1000, 0);
45 avahi_time_event_queue_update(s->server->time_event_queue, s->time_event, &tv);
49 AvahiSubscription *subscription;
50 AvahiInterface *interface;
53 static gpointer scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) {
54 struct cbdata *cbdata = userdata;
61 cbdata->subscription->callback(
64 cbdata->interface->hardware->index,
65 cbdata->interface->protocol,
66 AVAHI_SUBSCRIPTION_NEW,
67 cbdata->subscription->userdata);
72 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
73 AvahiSubscription *s = userdata;
74 struct cbdata cbdata = { s, i };
80 avahi_cache_walk(i->cache, s->key, scan_cache_callback, &cbdata);
83 AvahiSubscription *avahi_subscription_new(AvahiServer *server, AvahiKey *key, gint interface, guchar protocol, AvahiSubscriptionCallback callback, gpointer userdata) {
84 AvahiSubscription *s, *t;
91 g_assert(!avahi_key_is_pattern(key));
93 s = g_new(AvahiSubscription, 1);
95 s->key = avahi_key_ref(key);
96 s->interface = interface;
97 s->protocol = protocol;
98 s->callback = callback;
99 s->userdata = userdata;
103 avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
105 avahi_elapse_time(&tv, s->sec_delay*1000, 0);
106 s->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, elapse, s);
108 AVAHI_LLIST_PREPEND(AvahiSubscription, subscriptions, server->subscriptions, s);
110 /* Add the new entry to the subscription hash table */
111 t = g_hash_table_lookup(server->subscription_hashtable, key);
112 AVAHI_LLIST_PREPEND(AvahiSubscription, by_key, t, s);
113 g_hash_table_replace(server->subscription_hashtable, key, t);
115 /* Scan the caches */
116 avahi_interface_monitor_walk(s->server->monitor, s->interface, s->protocol, scan_interface_callback, s);
121 void avahi_subscription_free(AvahiSubscription *s) {
122 AvahiSubscription *t;
126 AVAHI_LLIST_REMOVE(AvahiSubscription, subscriptions, s->server->subscriptions, s);
128 t = g_hash_table_lookup(s->server->subscription_hashtable, s->key);
129 AVAHI_LLIST_REMOVE(AvahiSubscription, by_key, t, s);
131 g_hash_table_replace(s->server->subscription_hashtable, t->key, t);
133 g_hash_table_remove(s->server->subscription_hashtable, s->key);
135 avahi_time_event_queue_remove(s->server->time_event_queue, s->time_event);
136 avahi_key_unref(s->key);
142 void avahi_subscription_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiSubscriptionEvent event) {
143 AvahiSubscription *s;
148 for (s = g_hash_table_lookup(server->subscription_hashtable, record->key); s; s = s->by_key_next)
149 if (avahi_interface_match(i, s->interface, s->protocol))
150 s->callback(s, record, i->hardware->index, i->protocol, event, s->userdata);
153 gboolean avahi_is_subscribed(AvahiServer *server, AvahiKey *k) {
157 return !!g_hash_table_lookup(server->subscription_hashtable, k);