]> git.meshlink.io Git - catta/blob - libavahi-core/subscribe.c
df8ee0517912ca540c1a92a0bc4ff940cdbb1e09
[catta] / libavahi-core / subscribe.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 #include "subscribe.h"
23 #include "util.h"
24
25 static void elapse(AvahiTimeEvent *e, void *userdata) {
26     AvahiSubscription *s = userdata;
27     GTimeVal tv;
28     gchar *t;
29     
30     g_assert(s);
31
32     avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
33
34     if (s->n_query++ <= 8)
35         s->sec_delay *= 2;
36
37     g_message("%i. Continuous querying for %s", s->n_query, t = avahi_key_to_string(s->key));
38     g_free(t);
39     
40     avahi_elapse_time(&tv, s->sec_delay*1000, 0);
41     avahi_time_event_queue_update(s->server->time_event_queue, s->time_event, &tv);
42 }
43
44 struct cbdata {
45     AvahiSubscription *subscription;
46     AvahiInterface *interface;
47 };
48
49 static gpointer scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) {
50     struct cbdata *cbdata = userdata;
51
52     g_assert(c);
53     g_assert(pattern);
54     g_assert(e);
55     g_assert(cbdata);
56
57     cbdata->subscription->callback(
58         cbdata->subscription,
59         e->record,
60         cbdata->interface->hardware->index,
61         cbdata->interface->protocol,
62         AVAHI_SUBSCRIPTION_NEW,
63         cbdata->subscription->userdata);
64
65     return NULL;
66 }
67
68 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
69     AvahiSubscription *s = userdata;
70     struct cbdata cbdata = { s, i };
71
72     g_assert(m);
73     g_assert(i);
74     g_assert(s);
75
76     avahi_cache_walk(i->cache, s->key, scan_cache_callback, &cbdata);
77 }
78
79 AvahiSubscription *avahi_subscription_new(AvahiServer *server, AvahiKey *key, gint interface, guchar protocol, AvahiSubscriptionCallback callback, gpointer userdata) {
80     AvahiSubscription *s, *t;
81     GTimeVal tv;
82
83     g_assert(server);
84     g_assert(key);
85     g_assert(callback);
86
87     g_assert(!avahi_key_is_pattern(key));
88     
89     s = g_new(AvahiSubscription, 1);
90     s->server = server;
91     s->key = avahi_key_ref(key);
92     s->interface = interface;
93     s->protocol = protocol;
94     s->callback = callback;
95     s->userdata = userdata;
96     s->n_query = 1;
97     s->sec_delay = 1;
98
99     avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
100     
101     avahi_elapse_time(&tv, s->sec_delay*1000, 0);
102     s->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, elapse, s);
103
104     AVAHI_LLIST_PREPEND(AvahiSubscription, subscriptions, server->subscriptions, s);
105
106     /* Add the new entry to the subscription hash table */
107     t = g_hash_table_lookup(server->subscription_hashtable, key);
108     AVAHI_LLIST_PREPEND(AvahiSubscription, by_key, t, s);
109     g_hash_table_replace(server->subscription_hashtable, key, t);
110
111     /* Scan the caches */
112     avahi_interface_monitor_walk(s->server->monitor, s->interface, s->protocol, scan_interface_callback, s);
113     
114     return s;
115 }
116
117 void avahi_subscription_free(AvahiSubscription *s) {
118     AvahiSubscription *t;
119     
120     g_assert(s);
121
122     AVAHI_LLIST_REMOVE(AvahiSubscription, subscriptions, s->server->subscriptions, s);
123
124     t = g_hash_table_lookup(s->server->subscription_hashtable, s->key);
125     AVAHI_LLIST_REMOVE(AvahiSubscription, by_key, t, s);
126     if (t)
127         g_hash_table_replace(s->server->subscription_hashtable, t->key, t);
128     else
129         g_hash_table_remove(s->server->subscription_hashtable, s->key);
130     
131     avahi_time_event_queue_remove(s->server->time_event_queue, s->time_event);
132     avahi_key_unref(s->key);
133
134     
135     g_free(s);
136 }
137
138 void avahi_subscription_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiSubscriptionEvent event) {
139     AvahiSubscription *s;
140     
141     g_assert(server);
142     g_assert(record);
143
144     for (s = g_hash_table_lookup(server->subscription_hashtable, record->key); s; s = s->by_key_next)
145         if (avahi_interface_match(i, s->interface, s->protocol))
146             s->callback(s, record, i->hardware->index, i->protocol, event, s->userdata);
147 }
148
149 gboolean avahi_is_subscribed(AvahiServer *server, AvahiKey *k) {
150     g_assert(server);
151     g_assert(k);
152
153     return !!g_hash_table_lookup(server->subscription_hashtable, k);
154 }