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
29 #include <avahi-common/timeval.h>
30 #include <avahi-common/malloc.h>
32 #include "timeeventq.h"
35 struct AvahiTimeEvent {
36 AvahiTimeEventQueue *queue;
37 AvahiPrioQueueNode *node;
38 struct timeval expiry;
39 struct timeval last_run;
40 AvahiTimeEventCallback callback;
44 struct AvahiTimeEventQueue {
45 const AvahiPoll *poll_api;
46 AvahiPrioQueue *prioq;
47 AvahiTimeout *timeout;
50 static int compare(const void* _a, const void* _b) {
51 const AvahiTimeEvent *a = _a, *b = _b;
54 if ((ret = avahi_timeval_compare(&a->expiry, &b->expiry)) != 0)
57 /* If both exevents are scheduled for the same time, put the entry
58 * that has been run earlier the last time first. */
59 return avahi_timeval_compare(&a->last_run, &b->last_run);
62 static void update_timeout(AvahiTimeEventQueue *q) {
66 if ((e = avahi_time_event_queue_root(q)))
67 q->poll_api->timeout_update(q->timeout, &e->expiry);
69 q->poll_api->timeout_update(q->timeout, NULL);
72 static void expiration_event(AvahiTimeout *timeout, void *userdata) {
73 AvahiTimeEventQueue *q = userdata;
76 if ((e = avahi_time_event_queue_root(q))) {
79 gettimeofday(&now, NULL);
81 /* Check if expired */
82 if (avahi_timeval_compare(&now, &e->expiry) >= 0) {
84 /* Make sure to move the entry away from the front */
86 avahi_prio_queue_shuffle(q->prioq, e->node);
90 e->callback(e, e->userdata);
97 avahi_log_debug(__FILE__": Strange, expiration_event() called, but nothing really happened.");
101 static void fix_expiry_time(AvahiTimeEvent *e) {
105 return; /*** DO WE REALLY NEED THIS? ***/
107 gettimeofday(&now, NULL);
109 if (avahi_timeval_compare(&now, &e->expiry) > 0)
113 AvahiTimeEventQueue* avahi_time_event_queue_new(const AvahiPoll *poll_api) {
114 AvahiTimeEventQueue *q;
116 if (!(q = avahi_new(AvahiTimeEventQueue, 1))) {
117 avahi_log_error(__FILE__": Out of memory");
121 q->poll_api = poll_api;
123 if (!(q->prioq = avahi_prio_queue_new(compare)))
126 if (!(q->timeout = poll_api->timeout_new(poll_api, NULL, expiration_event, q)))
137 avahi_prio_queue_free(q->prioq);
143 void avahi_time_event_queue_free(AvahiTimeEventQueue *q) {
148 while ((e = avahi_time_event_queue_root(q)))
149 avahi_time_event_free(e);
150 avahi_prio_queue_free(q->prioq);
152 q->poll_api->timeout_free(q->timeout);
157 AvahiTimeEvent* avahi_time_event_new(
158 AvahiTimeEventQueue *q,
159 const struct timeval *timeval,
160 AvahiTimeEventCallback callback,
169 if (!(e = avahi_new(AvahiTimeEvent, 1))) {
170 avahi_log_error(__FILE__": Out of memory");
171 return NULL; /* OOM */
175 e->callback = callback;
176 e->userdata = userdata;
179 e->expiry = *timeval;
181 e->expiry.tv_sec = 0;
182 e->expiry.tv_usec = 0;
187 e->last_run.tv_sec = 0;
188 e->last_run.tv_usec = 0;
190 if (!(e->node = avahi_prio_queue_put(q->prioq, e))) {
199 void avahi_time_event_free(AvahiTimeEvent *e) {
200 AvahiTimeEventQueue *q;
205 avahi_prio_queue_remove(q->prioq, e->node);
211 void avahi_time_event_update(AvahiTimeEvent *e, const struct timeval *timeval) {
215 e->expiry = *timeval;
217 avahi_prio_queue_shuffle(e->queue->prioq, e->node);
219 update_timeout(e->queue);
222 AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q) {
225 return q->prioq->root ? q->prioq->root->data : NULL;
228 AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e) {
231 return e->node->next->data;