2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <catta/timeval.h>
27 #include <catta/malloc.h>
28 #include <catta/error.h>
29 #include <catta/domain.h>
32 #include <catta/log.h>
35 CattaInterface *iface;
42 CattaTimeEvent *time_event;
44 struct timeval creation_time;
49 CATTA_LLIST_FIELDS(CattaQuerier, queriers);
52 void catta_querier_free(CattaQuerier *q) {
55 CATTA_LLIST_REMOVE(CattaQuerier, queriers, q->iface->queriers, q);
56 catta_hashmap_remove(q->iface->queriers_by_key, q->key);
58 catta_key_unref(q->key);
59 catta_time_event_free(q->time_event);
64 static void querier_elapse_callback(CATTA_GCC_UNUSED CattaTimeEvent *e, void *userdata) {
65 CattaQuerier *q = userdata;
72 /* We are not referenced by anyone anymore, so let's free
73 * ourselves. We should not send out any further queries from
74 * this querier object anymore. */
76 catta_querier_free(q);
80 if (catta_interface_post_query(q->iface, q->key, 0, &q->post_id)) {
82 /* The queue accepted our query. We store the query id here,
83 * that allows us to drop the query at a later point if the
84 * query is very short-lived. */
91 if (q->sec_delay >= 60*60) /* 1h */
94 catta_elapse_time(&tv, q->sec_delay*1000, 0);
95 catta_time_event_update(q->time_event, &tv);
98 void catta_querier_add(CattaInterface *i, CattaKey *key, struct timeval *ret_ctime) {
105 if ((q = catta_hashmap_lookup(i->queriers_by_key, key))) {
107 /* Someone is already browsing for records of this RR key */
110 /* Return the creation time. This is used for generating the
111 * ALL_FOR_NOW event one second after the querier was
112 * initially created. */
114 *ret_ctime = q->creation_time;
118 /* No one is browsing for this RR key, so we add a new querier */
119 if (!(q = catta_new(CattaQuerier, 1)))
122 q->key = catta_key_ref(key);
126 q->post_id_valid = 0;
127 gettimeofday(&q->creation_time, NULL);
129 /* Do the initial query */
130 if (catta_interface_post_query(i, key, 0, &q->post_id))
131 q->post_id_valid = 1;
133 /* Schedule next queries */
134 q->time_event = catta_time_event_new(i->monitor->server->time_event_queue, catta_elapse_time(&tv, q->sec_delay*1000, 0), querier_elapse_callback, q);
136 CATTA_LLIST_PREPEND(CattaQuerier, queriers, i->queriers, q);
137 catta_hashmap_insert(i->queriers_by_key, q->key, q);
139 /* Return the creation time. This is used for generating the
140 * ALL_FOR_NOW event one second after the querier was initially
143 *ret_ctime = q->creation_time;
146 void catta_querier_remove(CattaInterface *i, CattaKey *key) {
149 /* There was no querier for this RR key, or it wasn't referenced
151 if (!(q = catta_hashmap_lookup(i->queriers_by_key, key)) || q->n_used <= 0)
154 if ((--q->n_used) <= 0) {
156 /* Nobody references us anymore. */
158 if (q->post_id_valid && catta_interface_withraw_query(i, q->post_id)) {
160 /* We succeeded in withdrawing our query from the queue,
161 * so let's drop dead. */
163 catta_querier_free(q);
166 /* If we failed to withdraw our query from the queue, we stay
167 * alive, in case someone else might recycle our querier at a
168 * later point. We are freed at our next expiry, in case
169 * nobody recycled us. */
173 static void remove_querier_callback(CattaInterfaceMonitor *m, CattaInterface *i, void* userdata) {
179 catta_querier_remove(i, (CattaKey*) userdata);
182 void catta_querier_remove_for_all(CattaServer *s, CattaIfIndex idx, CattaProtocol protocol, CattaKey *key) {
186 catta_interface_monitor_walk(s->monitor, idx, protocol, remove_querier_callback, key);
191 struct timeval *ret_ctime;
194 static void add_querier_callback(CattaInterfaceMonitor *m, CattaInterface *i, void* userdata) {
195 struct cbdata *cbdata = userdata;
203 catta_querier_add(i, cbdata->key, &tv);
205 if (cbdata->ret_ctime && catta_timeval_compare(&tv, cbdata->ret_ctime) > 0)
206 *cbdata->ret_ctime = tv;
210 void catta_querier_add_for_all(CattaServer *s, CattaIfIndex idx, CattaProtocol protocol, CattaKey *key, struct timeval *ret_ctime) {
211 struct cbdata cbdata;
217 cbdata.ret_ctime = ret_ctime;
220 ret_ctime->tv_sec = ret_ctime->tv_usec = 0;
222 catta_interface_monitor_walk(s->monitor, idx, protocol, add_querier_callback, &cbdata);
225 int catta_querier_shall_refresh_cache(CattaInterface *i, CattaKey *key) {
231 /* Called by the cache maintainer */
233 if (!(q = catta_hashmap_lookup(i->queriers_by_key, key)))
234 /* This key is currently not subscribed at all, so no cache
235 * refresh is needed */
238 if (q->n_used <= 0) {
240 /* If this is an entry nobody references right now, don't
241 * consider it "existing". */
243 /* Remove this querier since it is referenced by nobody
244 * and the cached data will soon be out of date */
245 catta_querier_free(q);
247 /* Tell the cache that no refresh is needed */
253 /* We can defer our query a little, since the cache will now
254 * issue a refresh query anyway. */
255 catta_elapse_time(&tv, q->sec_delay*1000, 0);
256 catta_time_event_update(q->time_event, &tv);
258 /* Tell the cache that a refresh should be issued */
263 void catta_querier_free_all(CattaInterface *i) {
267 catta_querier_free(i->queriers);