]> git.meshlink.io Git - catta/blob - subscribe.c
* add todo list
[catta] / subscribe.c
1 #include "subscribe.h"
2 #include "util.h"
3
4 static void elapse(flxTimeEvent *e, void *userdata) {
5     flxSubscription *s = userdata;
6     GTimeVal tv;
7     gchar *t;
8     
9     g_assert(s);
10
11     flx_server_post_query(s->server, s->interface, s->protocol, s->key);
12
13     if (s->n_query++ <= 8)
14         s->sec_delay *= 2;
15
16     g_message("%i. Continuous querying for %s", s->n_query, t = flx_key_to_string(s->key));
17     g_free(t);
18     
19     flx_elapse_time(&tv, s->sec_delay*1000, 0);
20     flx_time_event_queue_update(s->server->time_event_queue, s->time_event, &tv);
21 }
22
23 static void scan_cache_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
24     flxSubscription *s = userdata;
25     flxCacheEntry *e;
26
27     g_assert(m);
28     g_assert(i);
29     g_assert(s);
30
31     for (e = flx_cache_lookup_key(i->cache, s->key); e; e = e->by_name_next)
32         s->callback(s, e->record, i->hardware->index, i->protocol, FLX_SUBSCRIPTION_NEW, s->userdata);
33 }
34
35 flxSubscription *flx_subscription_new(flxServer *server, flxKey *key, gint interface, guchar protocol, flxSubscriptionCallback callback, gpointer userdata) {
36     flxSubscription *s, *t;
37     GTimeVal tv;
38
39     g_assert(server);
40     g_assert(key);
41     g_assert(callback);
42
43     s = g_new(flxSubscription, 1);
44     s->server = server;
45     s->key = flx_key_ref(key);
46     s->interface = interface;
47     s->protocol = protocol;
48     s->callback = callback;
49     s->userdata = userdata;
50     s->n_query = 1;
51     s->sec_delay = 1;
52
53     flx_server_post_query(s->server, s->interface, s->protocol, s->key);
54     
55     flx_elapse_time(&tv, s->sec_delay*1000, 0);
56     s->time_event = flx_time_event_queue_add(server->time_event_queue, &tv, elapse, s);
57
58     FLX_LLIST_PREPEND(flxSubscription, subscriptions, server->subscriptions, s);
59
60     /* Add the new entry to the subscription hash table */
61     t = g_hash_table_lookup(server->subscription_hashtable, key);
62     FLX_LLIST_PREPEND(flxSubscription, by_key, t, s);
63     g_hash_table_replace(server->subscription_hashtable, key, t);
64
65     /* Scan the caches */
66     flx_interface_monitor_walk(s->server->monitor, s->interface, s->protocol, scan_cache_callback, s);
67     
68     return s;
69 }
70
71 void flx_subscription_free(flxSubscription *s) {
72     flxSubscription *t;
73     
74     g_assert(s);
75
76     FLX_LLIST_REMOVE(flxSubscription, subscriptions, s->server->subscriptions, s);
77
78     t = g_hash_table_lookup(s->server->subscription_hashtable, s->key);
79     FLX_LLIST_REMOVE(flxSubscription, by_key, t, s);
80     if (t)
81         g_hash_table_replace(s->server->subscription_hashtable, t->key, t);
82     else
83         g_hash_table_remove(s->server->subscription_hashtable, s->key);
84     
85     flx_time_event_queue_remove(s->server->time_event_queue, s->time_event);
86     flx_key_unref(s->key);
87
88     
89     g_free(s);
90 }
91
92 void flx_subscription_notify(flxServer *server, flxInterface *i, flxRecord *record, flxSubscriptionEvent event) {
93     flxSubscription *s;
94     
95     g_assert(server);
96     g_assert(record);
97
98     for (s = g_hash_table_lookup(server->subscription_hashtable, record->key); s; s = s->by_key_next)
99         if (flx_interface_match(i, s->interface, s->protocol))
100             s->callback(s, record, i->hardware->index, i->protocol, event, s->userdata);
101     
102 }