4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include "probe-sched.h"
30 #define AVAHI_PROBE_HISTORY_MSEC 150
31 #define AVAHI_PROBE_DEFER_MSEC 50
33 typedef struct AvahiProbeJob AvahiProbeJob;
35 struct AvahiProbeJob {
36 AvahiProbeScheduler *scheduler;
37 AvahiTimeEvent *time_event;
39 gboolean chosen; /* Use for packet assembling */
45 AVAHI_LLIST_FIELDS(AvahiProbeJob, jobs);
48 struct AvahiProbeScheduler {
49 AvahiInterface *interface;
50 AvahiTimeEventQueue *time_event_queue;
52 AVAHI_LLIST_HEAD(AvahiProbeJob, jobs);
53 AVAHI_LLIST_HEAD(AvahiProbeJob, history);
56 static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gboolean done) {
62 pj = g_new(AvahiProbeJob, 1);
64 pj->record = avahi_record_ref(record);
65 pj->time_event = NULL;
68 if ((pj->done = done))
69 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
71 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
76 static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
80 avahi_time_event_queue_remove(s->time_event_queue, pj->time_event);
83 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
85 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
87 avahi_record_unref(pj->record);
91 static void elapse_callback(AvahiTimeEvent *e, gpointer data);
93 static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, guint msec, guint jitter) {
99 avahi_elapse_time(&tv, msec, jitter);
102 avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &tv);
104 pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, pj);
107 static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
113 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
114 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
118 job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
119 g_get_current_time(&pj->delivery);
122 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
123 AvahiProbeScheduler *s;
127 s = g_new(AvahiProbeScheduler, 1);
129 s->time_event_queue = i->monitor->server->time_event_queue;
131 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
132 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
137 void avahi_probe_scheduler_free(AvahiProbeScheduler *s) {
140 avahi_probe_scheduler_clear(s);
144 void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) {
148 job_free(s, s->jobs);
150 job_free(s, s->history);
153 static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
162 g_assert(!pj->chosen);
164 /* Estimate the size for this record */
166 avahi_key_get_estimate_size(pj->record->key) +
167 avahi_record_get_estimate_size(pj->record);
170 if (size > avahi_dns_packet_space(p))
173 /* Create the probe query */
174 k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
175 b = !!avahi_dns_packet_append_key(p, k, FALSE);
178 /* Mark this job for addition to the packet */
181 /* Scan for more jobs whith matching key pattern */
182 for (pj = s->jobs; pj; pj = pj->jobs_next) {
186 /* Does the record match the probe? */
187 if (k->clazz != pj->record->key->clazz || !avahi_domain_equal(k->name, pj->record->key->name))
190 /* This job wouldn't fit in */
191 if (avahi_record_get_estimate_size(pj->record) > avahi_dns_packet_space(p))
194 /* Mark this job for addition to the packet */
203 static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
204 AvahiProbeJob *pj = data, *next;
205 AvahiProbeScheduler *s;
213 /* Lets remove it from the history */
218 p = avahi_dns_packet_new_query(s->interface->hardware->mtu);
221 /* Add the import probe */
222 if (!packet_add_probe_query(s, p, pj)) {
227 avahi_dns_packet_free(p);
229 /* The probe didn't fit in the package, so let's allocate a larger one */
232 avahi_key_get_estimate_size(pj->record->key) +
233 avahi_record_get_estimate_size(pj->record) +
234 AVAHI_DNS_PACKET_HEADER_SIZE;
236 if (size > AVAHI_DNS_PACKET_MAX_SIZE)
237 size = AVAHI_DNS_PACKET_MAX_SIZE;
239 p = avahi_dns_packet_new_query(size);
241 k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
242 b = avahi_dns_packet_append_key(p, k, FALSE) && avahi_dns_packet_append_record(p, pj->record, FALSE, 0);
246 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, 1);
247 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
248 avahi_interface_send_packet(s->interface, p);
250 avahi_log_warn("Probe record too large, cannot send");
252 avahi_dns_packet_free(p);
253 job_mark_done(s, pj);
258 /* Try to fill up packet with more probes, if available */
259 for (pj = s->jobs; pj; pj = pj->jobs_next) {
264 if (!packet_add_probe_query(s, p, pj))
270 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, n);
274 /* Now add the chosen records to the authorative section */
275 for (pj = s->jobs; pj; pj = next) {
277 next = pj->jobs_next;
282 if (!avahi_dns_packet_append_record(p, pj->record, FALSE, 0)) {
283 avahi_log_warn("Bad probe size estimate!");
285 /* Unmark all following jobs */
286 for (; pj; pj = pj->jobs_next)
292 job_mark_done(s, pj);
297 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, n);
300 avahi_interface_send_packet(s->interface, p);
301 avahi_dns_packet_free(p);
304 static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
310 for (pj = s->jobs; pj; pj = pj->jobs_next) {
313 if (avahi_record_equal_no_ttl(pj->record, record))
320 static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
326 for (pj = s->history; pj; pj = pj->jobs_next) {
329 if (avahi_record_equal_no_ttl(pj->record, record)) {
330 /* Check whether this entry is outdated */
332 if (avahi_age(&pj->delivery) > AVAHI_PROBE_HISTORY_MSEC*1000) {
333 /* it is outdated, so let's remove it */
345 gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) {
351 g_assert(!avahi_key_is_pattern(record->key));
353 if ((pj = find_history_job(s, record)))
356 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
358 if ((pj = find_scheduled_job(s, record))) {
360 if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
361 /* If the new entry should be scheduled earlier, update the old entry */
363 avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &pj->delivery);
368 /* Create a new job and schedule it */
369 pj = job_new(s, record, FALSE);
371 pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &pj->delivery, elapse_callback, pj);
374 /* avahi_log_debug("Accepted new probe job."); */