4 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
5 #define AVAHI_PROBE_JITTER_MSEC 250
6 #define AVAHI_PROBE_INTERVAL_MSEC 250
8 static void remove_announcement(AvahiServer *s, AvahiAnnouncement *a) {
13 avahi_time_event_queue_remove(s->time_event_queue, a->time_event);
15 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_interface, a->interface->announcements, a);
16 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_entry, a->entry->announcements, a);
21 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
23 static void set_timeout(AvahiAnnouncement *a, const GTimeVal *tv) {
28 avahi_time_event_queue_remove(a->server->time_event_queue, a->time_event);
34 avahi_time_event_queue_update(a->server->time_event_queue, a->time_event, tv);
36 a->time_event = avahi_time_event_queue_add(a->server->time_event_queue, tv, elapse_announce, a);
40 static void next_state(AvahiAnnouncement *a);
42 void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately) {
47 /* Check whether all group members have been probed */
49 if (g->state != AVAHI_ENTRY_GROUP_REGISTERING || g->n_probing > 0)
52 avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_ESTABLISHED);
57 for (e = g->entries; e; e = e->entries_next) {
60 for (a = e->announcements; a; a = a->by_entry_next) {
62 if (a->state != AVAHI_WAITING)
65 a->state = AVAHI_ANNOUNCING;
75 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
82 static void next_state(AvahiAnnouncement *a) {
85 /* g_message("%i -- %u", a->state, a->n_iteration); */
87 if (a->state == AVAHI_WAITING) {
89 g_assert(a->entry->group);
91 avahi_entry_group_check_probed(a->entry->group, TRUE);
93 } else if (a->state == AVAHI_PROBING) {
95 if (a->n_iteration >= 4) {
100 g_message("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record));
103 if (a->entry->group) {
104 g_assert(a->entry->group->n_probing);
105 a->entry->group->n_probing--;
108 if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
109 a->state = AVAHI_WAITING;
111 a->state = AVAHI_ANNOUNCING;
115 set_timeout(a, NULL);
120 avahi_interface_post_probe(a->interface, a->entry->record, FALSE);
122 avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
128 } else if (a->state == AVAHI_ANNOUNCING) {
130 avahi_interface_post_response(a->interface, NULL, a->entry->record, a->entry->flags & AVAHI_ENTRY_UNIQUE, FALSE);
132 if (++a->n_iteration >= 4) {
134 /* Announcing done */
136 g_message("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record));
139 a->state = AVAHI_ESTABLISHED;
141 set_timeout(a, NULL);
144 avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
146 if (a->n_iteration < 10)
154 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
157 next_state(userdata);
160 AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
161 AvahiAnnouncement *a;
167 for (a = e->announcements; a; a = a->by_entry_next)
168 if (a->interface == i)
174 static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
175 AvahiAnnouncement *a;
184 /* g_message("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */
187 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_commited(e))
190 /* We don't want duplicate announcements */
191 if (avahi_get_announcement(s, e, i))
194 a = g_new(AvahiAnnouncement, 1);
199 if ((e->flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_NOPROBE))
200 a->state = AVAHI_PROBING;
201 else if (!(e->flags & AVAHI_ENTRY_NOANNOUNCE)) {
203 if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
204 a->state = AVAHI_ANNOUNCING;
206 a->state = AVAHI_WAITING;
209 a->state = AVAHI_ESTABLISHED;
212 g_message("New announcement on interface %s.%i for entry [%s] state=%i", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record), a->state);
217 a->time_event = NULL;
219 if (a->state == AVAHI_PROBING)
221 e->group->n_probing++;
223 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_interface, i->announcements, a);
224 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_entry, e->announcements, a);
226 if (a->state == AVAHI_PROBING) {
227 avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC);
229 } else if (a->state == AVAHI_ANNOUNCING) {
230 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
235 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
244 for (e = s->entries; e; e = e->entries_next)
246 new_announcement(s, i, e);
249 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
250 AvahiEntry *e = userdata;
257 new_announcement(m->server, i, e);
260 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
265 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
268 void avahi_announce_group(AvahiServer *s, AvahiEntryGroup *g) {
274 for (e = g->entries; e; e = e->by_group_next)
276 avahi_announce_entry(s, e);
279 gboolean avahi_entry_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
280 AvahiAnnouncement *a;
287 if (!(a = avahi_get_announcement(s, e, i)))
290 return a->state == AVAHI_ANNOUNCING || a->state == AVAHI_ESTABLISHED;
293 gboolean avahi_entry_registering(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
294 AvahiAnnouncement *a;
301 if (!(a = avahi_get_announcement(s, e, i)))
304 return a->state == AVAHI_PROBING || a->state == AVAHI_WAITING;
307 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
313 /* g_message("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */
316 g = avahi_record_copy(r);
317 g_assert(g->ref == 1);
323 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
324 AvahiEntry *e = userdata;
332 if (!avahi_interface_match(i, e->interface, e->protocol))
335 if (e->flags & AVAHI_ENTRY_NOANNOUNCE)
338 if (!avahi_entry_registered(m->server, e, i))
341 g = make_goodbye_record(e->record);
342 avahi_interface_post_response(i, NULL, g, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
343 avahi_record_unref(g);
346 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, gboolean goodbye) {
350 /* g_message("goodbye interface: %s.%u", i->hardware->name, i->protocol); */
352 if (goodbye && avahi_interface_relevant(i)) {
355 for (e = s->entries; e; e = e->entries_next)
357 send_goodbye_callback(s->monitor, i, e);
360 while (i->announcements)
361 remove_announcement(s, i->announcements);
363 /* g_message("goodbye interface done: %s.%u", i->hardware->name, i->protocol); */
367 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, gboolean goodbye) {
371 /* g_message("goodbye entry: %p", e); */
373 if (goodbye && !e->dead)
374 avahi_interface_monitor_walk(s->monitor, 0, AF_UNSPEC, send_goodbye_callback, e);
376 while (e->announcements)
377 remove_announcement(s, e->announcements);
379 /* g_message("goodbye entry done: %p", e); */
383 void avahi_goodbye_all(AvahiServer *s, gboolean goodbye) {
388 /* g_message("goodbye all"); */
390 for (e = s->entries; e; e = e->entries_next)
392 avahi_goodbye_entry(s, e, goodbye);
394 /* g_message("goodbye all done"); */