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"
29 #define AVAHI_PROBE_HISTORY_MSEC 150
30 #define AVAHI_PROBE_DEFER_MSEC 50
32 typedef struct AvahiProbeJob AvahiProbeJob;
34 struct AvahiProbeJob {
35 AvahiProbeScheduler *scheduler;
36 AvahiTimeEvent *time_event;
38 gboolean chosen; /* Use for packet assembling */
44 AVAHI_LLIST_FIELDS(AvahiProbeJob, jobs);
47 struct AvahiProbeScheduler {
48 AvahiInterface *interface;
49 AvahiTimeEventQueue *time_event_queue;
51 AVAHI_LLIST_HEAD(AvahiProbeJob, jobs);
52 AVAHI_LLIST_HEAD(AvahiProbeJob, history);
55 static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gboolean done) {
61 pj = g_new(AvahiProbeJob, 1);
63 pj->record = avahi_record_ref(record);
64 pj->time_event = NULL;
67 if ((pj->done = done))
68 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
70 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
75 static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
79 avahi_time_event_queue_remove(s->time_event_queue, pj->time_event);
82 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
84 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
86 avahi_record_unref(pj->record);
90 static void elapse_callback(AvahiTimeEvent *e, gpointer data);
92 static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, guint msec, guint jitter) {
98 avahi_elapse_time(&tv, msec, jitter);
101 avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &tv);
103 pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, pj);
106 static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
112 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
113 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
117 job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
118 g_get_current_time(&pj->delivery);
121 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
122 AvahiProbeScheduler *s;
126 s = g_new(AvahiProbeScheduler, 1);
128 s->time_event_queue = i->monitor->server->time_event_queue;
130 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
131 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
136 void avahi_probe_scheduler_free(AvahiProbeScheduler *s) {
139 avahi_probe_scheduler_clear(s);
143 void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) {
147 job_free(s, s->jobs);
149 job_free(s, s->history);
152 static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
161 g_assert(!pj->chosen);
163 /* Estimate the size for this record */
165 avahi_key_get_estimate_size(pj->record->key) +
166 avahi_record_get_estimate_size(pj->record);
169 if (size > avahi_dns_packet_space(p))
172 /* Create the probe query */
173 k = avahi_key_new(pj->record->key->name, pj->record->key->class, AVAHI_DNS_TYPE_ANY);
174 b = !!avahi_dns_packet_append_key(p, k, FALSE);
177 /* Mark this job for addition to the packet */
180 /* Scan for more jobs whith matching key pattern */
181 for (pj = s->jobs; pj; pj = pj->jobs_next) {
185 /* Does the record match the probe? */
186 if (k->class != pj->record->key->class || !avahi_domain_equal(k->name, pj->record->key->name))
189 /* This job wouldn't fit in */
190 if (avahi_record_get_estimate_size(pj->record) > avahi_dns_packet_space(p))
193 /* Mark this job for addition to the packet */
202 static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
203 AvahiProbeJob *pj = data, *next;
204 AvahiProbeScheduler *s;
212 /* Lets remove it from the history */
217 p = avahi_dns_packet_new_query(s->interface->hardware->mtu);
220 /* Add the import probe */
221 if (!packet_add_probe_query(s, p, pj)) {
226 avahi_dns_packet_free(p);
228 /* The probe didn't fit in the package, so let's allocate a larger one */
231 avahi_key_get_estimate_size(pj->record->key) +
232 avahi_record_get_estimate_size(pj->record) +
233 AVAHI_DNS_PACKET_HEADER_SIZE;
235 if (size > AVAHI_DNS_PACKET_MAX_SIZE)
236 size = AVAHI_DNS_PACKET_MAX_SIZE;
238 p = avahi_dns_packet_new_query(size);
240 k = avahi_key_new(pj->record->key->name, pj->record->key->class, AVAHI_DNS_TYPE_ANY);
241 b = avahi_dns_packet_append_key(p, k, FALSE) && avahi_dns_packet_append_record(p, pj->record, FALSE, 0);
245 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, 1);
246 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
247 avahi_interface_send_packet(s->interface, p);
249 g_warning("Probe record too large, cannot send");
251 avahi_dns_packet_free(p);
252 job_mark_done(s, pj);
257 /* Try to fill up packet with more probes, if available */
258 for (pj = s->jobs; pj; pj = pj->jobs_next) {
263 if (!packet_add_probe_query(s, p, pj))
269 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, n);
273 /* Now add the chosen records to the authorative section */
274 for (pj = s->jobs; pj; pj = next) {
276 next = pj->jobs_next;
281 if (!avahi_dns_packet_append_record(p, pj->record, FALSE, 0)) {
282 g_warning("Bad probe size estimate!");
284 /* Unmark all following jobs */
285 for (; pj; pj = pj->jobs_next)
291 job_mark_done(s, pj);
296 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, n);
299 avahi_interface_send_packet(s->interface, p);
300 avahi_dns_packet_free(p);
303 static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
309 for (pj = s->jobs; pj; pj = pj->jobs_next) {
312 if (avahi_record_equal_no_ttl(pj->record, record))
319 static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
325 for (pj = s->history; pj; pj = pj->jobs_next) {
328 if (avahi_record_equal_no_ttl(pj->record, record)) {
329 /* Check whether this entry is outdated */
331 if (avahi_age(&pj->delivery) > AVAHI_PROBE_HISTORY_MSEC*1000) {
332 /* it is outdated, so let's remove it */
344 gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) {
350 g_assert(!avahi_key_is_pattern(record->key));
352 if ((pj = find_history_job(s, record)))
355 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
357 if ((pj = find_scheduled_job(s, record))) {
359 if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
360 /* If the new entry should be scheduled earlier, update the old entry */
362 avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &pj->delivery);
367 /* Create a new job and schedule it */
368 pj = job_new(s, record, FALSE);
370 pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &pj->delivery, elapse_callback, pj);
373 /* g_message("Accepted new probe job."); */