#include "probe-sched.h"
#include "util.h"
+#include "log.h"
-#define AVAHI_PROBE_DEFER_MSEC 70
+#define AVAHI_PROBE_HISTORY_MSEC 150
+#define AVAHI_PROBE_DEFER_MSEC 50
typedef struct AvahiProbeJob AvahiProbeJob;
AvahiTimeEvent *time_event;
gboolean chosen; /* Use for packet assembling */
+ gboolean done;
GTimeVal delivery;
AvahiRecord *record;
AvahiTimeEventQueue *time_event_queue;
AVAHI_LLIST_HEAD(AvahiProbeJob, jobs);
+ AVAHI_LLIST_HEAD(AvahiProbeJob, history);
};
-static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record) {
+static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gboolean done) {
AvahiProbeJob *pj;
g_assert(s);
pj->record = avahi_record_ref(record);
pj->time_event = NULL;
pj->chosen = FALSE;
-
- AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
+
+ if ((pj->done = done))
+ AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
+ else
+ AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
return pj;
}
if (pj->time_event)
avahi_time_event_queue_remove(s->time_event_queue, pj->time_event);
- AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
+ if (pj->done)
+ AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
+ else
+ AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
avahi_record_unref(pj->record);
g_free(pj);
}
+static void elapse_callback(AvahiTimeEvent *e, gpointer data);
+
+static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, guint msec, guint jitter) {
+ GTimeVal tv;
+
+ g_assert(s);
+ g_assert(pj);
+
+ avahi_elapse_time(&tv, msec, jitter);
+
+ if (pj->time_event)
+ avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &tv);
+ else
+ pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, pj);
+}
+
+static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
+ g_assert(s);
+ g_assert(pj);
+
+ g_assert(!pj->done);
+
+ AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
+ AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
+
+ pj->done = TRUE;
+
+ job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
+ g_get_current_time(&pj->delivery);
+}
AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
AvahiProbeScheduler *s;
s->time_event_queue = i->monitor->server->time_event_queue;
AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
+ AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
return s;
}
while (s->jobs)
job_free(s, s->jobs);
+ while (s->history)
+ job_free(s, s->history);
}
static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
return FALSE;
/* Create the probe query */
- k = avahi_key_new(pj->record->key->name, pj->record->key->class, AVAHI_DNS_TYPE_ANY);
+ k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
b = !!avahi_dns_packet_append_key(p, k, FALSE);
g_assert(b);
continue;
/* Does the record match the probe? */
- if (k->class != pj->record->key->class || !avahi_domain_equal(k->name, pj->record->key->name))
+ if (k->clazz != pj->record->key->clazz || !avahi_domain_equal(k->name, pj->record->key->name))
continue;
/* This job wouldn't fit in */
g_assert(pj);
s = pj->scheduler;
+ if (pj->done) {
+ /* Lets remove it from the history */
+ job_free(s, pj);
+ return;
+ }
+
p = avahi_dns_packet_new_query(s->interface->hardware->mtu);
n = 1;
p = avahi_dns_packet_new_query(size);
- k = avahi_key_new(pj->record->key->name, pj->record->key->class, AVAHI_DNS_TYPE_ANY);
+ k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
b = avahi_dns_packet_append_key(p, k, FALSE) && avahi_dns_packet_append_record(p, pj->record, FALSE, 0);
avahi_key_unref(k);
avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
avahi_interface_send_packet(s->interface, p);
} else
- g_warning("Probe record too large, cannot send");
+ avahi_log_warn("Probe record too large, cannot send");
avahi_dns_packet_free(p);
- job_free(s, pj);
+ job_mark_done(s, pj);
return;
}
continue;
if (!avahi_dns_packet_append_record(p, pj->record, FALSE, 0)) {
- g_warning("Bad probe size estimate!");
+ avahi_log_warn("Bad probe size estimate!");
/* Unmark all following jobs */
for (; pj; pj = pj->jobs_next)
break;
}
- job_free(s, pj);
+ job_mark_done(s, pj);
n ++;
}
avahi_dns_packet_free(p);
}
+static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
+ AvahiProbeJob *pj;
+
+ g_assert(s);
+ g_assert(record);
+
+ for (pj = s->jobs; pj; pj = pj->jobs_next) {
+ g_assert(!pj->done);
+
+ if (avahi_record_equal_no_ttl(pj->record, record))
+ return pj;
+ }
+
+ return NULL;
+}
+
+static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
+ AvahiProbeJob *pj;
+
+ g_assert(s);
+ g_assert(record);
+
+ for (pj = s->history; pj; pj = pj->jobs_next) {
+ g_assert(pj->done);
+
+ if (avahi_record_equal_no_ttl(pj->record, record)) {
+ /* Check whether this entry is outdated */
+
+ if (avahi_age(&pj->delivery) > AVAHI_PROBE_HISTORY_MSEC*1000) {
+ /* it is outdated, so let's remove it */
+ job_free(s, pj);
+ return NULL;
+ }
+
+ return pj;
+ }
+ }
+
+ return NULL;
+}
+
gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) {
AvahiProbeJob *pj;
GTimeVal tv;
g_assert(s);
g_assert(record);
g_assert(!avahi_key_is_pattern(record->key));
+
+ if ((pj = find_history_job(s, record)))
+ return FALSE;
avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
- /* Create a new job and schedule it */
- pj = job_new(s, record);
- pj->delivery = tv;
- pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &pj->delivery, elapse_callback, pj);
+ if ((pj = find_scheduled_job(s, record))) {
+
+ if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
+ /* If the new entry should be scheduled earlier, update the old entry */
+ pj->delivery = tv;
+ avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &pj->delivery);
+ }
-/* g_message("Accepted new probe job."); */
+ return TRUE;
+ } else {
+ /* Create a new job and schedule it */
+ pj = job_new(s, record, FALSE);
+ pj->delivery = tv;
+ pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &pj->delivery, elapse_callback, pj);
- return TRUE;
+
+/* avahi_log_debug("Accepted new probe job."); */
+
+ return TRUE;
+ }
}