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>
30 #include <catta/log.h>
33 #define CATTA_ANNOUNCEMENT_JITTER_MSEC 250
34 #define CATTA_PROBE_JITTER_MSEC 250
35 #define CATTA_PROBE_INTERVAL_MSEC 250
37 static void remove_announcer(CattaServer *s, CattaAnnouncer *a) {
42 catta_time_event_free(a->time_event);
44 CATTA_LLIST_REMOVE(CattaAnnouncer, by_interface, a->iface->announcers, a);
45 CATTA_LLIST_REMOVE(CattaAnnouncer, by_entry, a->entry->announcers, a);
50 static void elapse_announce(CattaTimeEvent *e, void *userdata);
52 static void set_timeout(CattaAnnouncer *a, const struct timeval *tv) {
57 catta_time_event_free(a->time_event);
63 catta_time_event_update(a->time_event, tv);
65 a->time_event = catta_time_event_new(a->server->time_event_queue, tv, elapse_announce, a);
69 static void next_state(CattaAnnouncer *a);
71 void catta_s_entry_group_check_probed(CattaSEntryGroup *g, int immediately) {
76 /* Check whether all group members have been probed */
78 if (g->state != CATTA_ENTRY_GROUP_REGISTERING || g->n_probing > 0)
81 catta_s_entry_group_change_state(g, CATTA_ENTRY_GROUP_ESTABLISHED);
86 for (e = g->entries; e; e = e->by_group_next) {
89 for (a = e->announcers; a; a = a->by_entry_next) {
91 if (a->state != CATTA_WAITING)
94 a->state = CATTA_ANNOUNCING;
104 catta_elapse_time(&tv, 0, CATTA_ANNOUNCEMENT_JITTER_MSEC);
111 static void next_state(CattaAnnouncer *a) {
114 if (a->state == CATTA_WAITING) {
116 assert(a->entry->group);
118 catta_s_entry_group_check_probed(a->entry->group, 1);
120 } else if (a->state == CATTA_PROBING) {
122 if (a->n_iteration >= 4) {
125 if (a->entry->group) {
126 assert(a->entry->group->n_probing);
127 a->entry->group->n_probing--;
130 if (a->entry->group && a->entry->group->state == CATTA_ENTRY_GROUP_REGISTERING)
131 a->state = CATTA_WAITING;
133 a->state = CATTA_ANNOUNCING;
137 set_timeout(a, NULL);
142 catta_interface_post_probe(a->iface, a->entry->record, 0);
144 catta_elapse_time(&tv, CATTA_PROBE_INTERVAL_MSEC, 0);
150 } else if (a->state == CATTA_ANNOUNCING) {
152 if (a->entry->flags & CATTA_PUBLISH_UNIQUE)
153 /* Send the whole rrset at once */
154 catta_server_prepare_matching_responses(a->server, a->iface, a->entry->record->key, 0);
156 catta_server_prepare_response(a->server, a->iface, a->entry, 0, 0);
158 catta_server_generate_response(a->server, a->iface, NULL, NULL, 0, 0, 0);
160 if (++a->n_iteration >= 4) {
161 /* Announcing done */
163 a->state = CATTA_ESTABLISHED;
165 set_timeout(a, NULL);
168 catta_elapse_time(&tv, a->sec_delay*1000, CATTA_ANNOUNCEMENT_JITTER_MSEC);
170 if (a->n_iteration < 10)
178 static void elapse_announce(CattaTimeEvent *e, void *userdata) {
181 next_state(userdata);
184 static CattaAnnouncer *get_announcer(CattaServer *s, CattaEntry *e, CattaInterface *i) {
191 for (a = e->announcers; a; a = a->by_entry_next)
198 static void go_to_initial_state(CattaAnnouncer *a) {
205 if ((e->flags & CATTA_PUBLISH_UNIQUE) && !(e->flags & CATTA_PUBLISH_NO_PROBE))
206 a->state = CATTA_PROBING;
207 else if (!(e->flags & CATTA_PUBLISH_NO_ANNOUNCE)) {
209 if (!e->group || e->group->state == CATTA_ENTRY_GROUP_ESTABLISHED)
210 a->state = CATTA_ANNOUNCING;
212 a->state = CATTA_WAITING;
215 a->state = CATTA_ESTABLISHED;
220 if (a->state == CATTA_PROBING && e->group)
221 e->group->n_probing++;
223 if (a->state == CATTA_PROBING)
224 set_timeout(a, catta_elapse_time(&tv, 0, CATTA_PROBE_JITTER_MSEC));
225 else if (a->state == CATTA_ANNOUNCING)
226 set_timeout(a, catta_elapse_time(&tv, 0, CATTA_ANNOUNCEMENT_JITTER_MSEC));
228 set_timeout(a, NULL);
231 static void new_announcer(CattaServer *s, CattaInterface *i, CattaEntry *e) {
239 if (!catta_interface_match(i, e->iface, e->protocol) || !i->announcing || !catta_entry_is_commited(e))
242 /* We don't want duplicate announcers */
243 if (get_announcer(s, e, i))
246 if ((!(a = catta_new(CattaAnnouncer, 1)))) {
247 catta_log_error(__FILE__": Out of memory.");
254 a->time_event = NULL;
256 CATTA_LLIST_PREPEND(CattaAnnouncer, by_interface, i->announcers, a);
257 CATTA_LLIST_PREPEND(CattaAnnouncer, by_entry, e->announcers, a);
259 go_to_initial_state(a);
262 void catta_announce_interface(CattaServer *s, CattaInterface *i) {
271 for (e = s->entries; e; e = e->entries_next)
273 new_announcer(s, i, e);
276 static void announce_walk_callback(CattaInterfaceMonitor *m, CattaInterface *i, void* userdata) {
277 CattaEntry *e = userdata;
284 new_announcer(m->server, i, e);
287 void catta_announce_entry(CattaServer *s, CattaEntry *e) {
292 catta_interface_monitor_walk(s->monitor, e->iface, e->protocol, announce_walk_callback, e);
295 void catta_announce_group(CattaServer *s, CattaSEntryGroup *g) {
301 for (e = g->entries; e; e = e->by_group_next)
303 catta_announce_entry(s, e);
306 int catta_entry_is_registered(CattaServer *s, CattaEntry *e, CattaInterface *i) {
314 if (!(a = get_announcer(s, e, i)))
318 a->state == CATTA_ANNOUNCING ||
319 a->state == CATTA_ESTABLISHED ||
320 (a->state == CATTA_WAITING && !(e->flags & CATTA_PUBLISH_UNIQUE));
323 int catta_entry_is_probing(CattaServer *s, CattaEntry *e, CattaInterface *i) {
331 if (!(a = get_announcer(s, e, i)))
335 a->state == CATTA_PROBING ||
336 (a->state == CATTA_WAITING && (e->flags & CATTA_PUBLISH_UNIQUE));
339 void catta_entry_return_to_initial_state(CattaServer *s, CattaEntry *e, CattaInterface *i) {
346 if (!(a = get_announcer(s, e, i)))
349 if (a->state == CATTA_PROBING && a->entry->group)
350 a->entry->group->n_probing--;
352 go_to_initial_state(a);
355 static CattaRecord *make_goodbye_record(CattaRecord *r) {
360 if (!(g = catta_record_copy(r)))
361 return NULL; /* OOM */
369 static int is_duplicate_entry(CattaServer *s, CattaEntry *e) {
375 for (i = catta_hashmap_lookup(s->entries_by_key, e->record->key); i; i = i->by_key_next) {
377 if ((i == e) || (i->dead))
380 if (!catta_record_equal_no_ttl(i->record, e->record))
389 static void send_goodbye_callback(CattaInterfaceMonitor *m, CattaInterface *i, void* userdata) {
390 CattaEntry *e = userdata;
398 if (!catta_interface_match(i, e->iface, e->protocol))
401 if (e->flags & CATTA_PUBLISH_NO_ANNOUNCE)
404 if (!catta_entry_is_registered(m->server, e, i))
407 if (is_duplicate_entry(m->server, e))
410 if (!(g = make_goodbye_record(e->record)))
413 catta_interface_post_response(i, g, e->flags & CATTA_PUBLISH_UNIQUE, NULL, 1);
414 catta_record_unref(g);
417 static void reannounce(CattaAnnouncer *a) {
424 /* If the group this entry belongs to is not even commited, there's nothing to reannounce */
425 if (e->group && (e->group->state == CATTA_ENTRY_GROUP_UNCOMMITED || e->group->state == CATTA_ENTRY_GROUP_COLLISION))
428 /* Because we might change state we decrease the probing counter first */
429 if (a->state == CATTA_PROBING && a->entry->group)
430 a->entry->group->n_probing--;
432 if (a->state == CATTA_PROBING ||
433 (a->state == CATTA_WAITING && (e->flags & CATTA_PUBLISH_UNIQUE) && !(e->flags & CATTA_PUBLISH_NO_PROBE)))
435 /* We were probing or waiting after probe, so we restart probing from the beginning here */
437 a->state = CATTA_PROBING;
438 else if (a->state == CATTA_WAITING)
440 /* We were waiting, but were not probing before, so we continue waiting */
441 a->state = CATTA_WAITING;
443 else if (e->flags & CATTA_PUBLISH_NO_ANNOUNCE)
445 /* No announcer needed */
446 a->state = CATTA_ESTABLISHED;
450 /* Ok, let's restart announcing */
451 a->state = CATTA_ANNOUNCING;
454 /* Now let's increase the probing counter again */
455 if (a->state == CATTA_PROBING && e->group)
456 e->group->n_probing++;
461 if (a->state == CATTA_PROBING)
462 set_timeout(a, catta_elapse_time(&tv, 0, CATTA_PROBE_JITTER_MSEC));
463 else if (a->state == CATTA_ANNOUNCING)
464 set_timeout(a, catta_elapse_time(&tv, 0, CATTA_ANNOUNCEMENT_JITTER_MSEC));
466 set_timeout(a, NULL);
470 static void reannounce_walk_callback(CattaInterfaceMonitor *m, CattaInterface *i, void* userdata) {
471 CattaEntry *e = userdata;
479 if (!(a = get_announcer(m->server, e, i)))
485 void catta_reannounce_entry(CattaServer *s, CattaEntry *e) {
491 catta_interface_monitor_walk(s->monitor, e->iface, e->protocol, reannounce_walk_callback, e);
494 void catta_goodbye_interface(CattaServer *s, CattaInterface *i, int send_goodbye, int remove) {
502 for (e = s->entries; e; e = e->entries_next)
504 send_goodbye_callback(s->monitor, i, e);
508 while (i->announcers)
509 remove_announcer(s, i->announcers);
512 void catta_goodbye_entry(CattaServer *s, CattaEntry *e, int send_goodbye, int remove) {
518 catta_interface_monitor_walk(s->monitor, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, send_goodbye_callback, e);
521 while (e->announcers)
522 remove_announcer(s, e->announcers);