2002-02-11 10:05:58 +00:00
|
|
|
/*
|
|
|
|
event.c -- event queue
|
2009-03-05 13:12:36 +00:00
|
|
|
Copyright (C) 2002-2009 Guus Sliepen <guus@tinc-vpn.org>,
|
2006-04-26 13:52:58 +00:00
|
|
|
2002-2005 Ivo Timmermans
|
2002-02-11 10:05:58 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2009-09-24 22:01:00 +00:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2002-02-11 10:05:58 +00:00
|
|
|
*/
|
|
|
|
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "system.h"
|
2002-02-11 10:05:58 +00:00
|
|
|
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "avl_tree.h"
|
2002-02-11 10:05:58 +00:00
|
|
|
#include "event.h"
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "xalloc.h"
|
2002-02-11 10:05:58 +00:00
|
|
|
|
|
|
|
avl_tree_t *event_tree;
|
2002-03-01 14:09:31 +00:00
|
|
|
extern time_t now;
|
2002-02-11 10:05:58 +00:00
|
|
|
|
|
|
|
int id;
|
|
|
|
|
2003-07-24 12:08:16 +00:00
|
|
|
static int event_compare(const event_t *a, const event_t *b)
|
2002-02-11 10:05:58 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
if(a->time > b->time)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(a->time < b->time)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return a->id - b->id;
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_events(void)
|
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
2009-01-19 21:50:05 +00:00
|
|
|
event_tree = avl_alloc_tree((avl_compare_t) event_compare, (avl_action_t) free_event);
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit_events(void)
|
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
avl_delete_tree(event_tree);
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|
|
|
|
|
2009-01-19 21:50:05 +00:00
|
|
|
void expire_events(void)
|
2007-02-14 09:20:20 +00:00
|
|
|
{
|
2009-01-19 21:50:05 +00:00
|
|
|
avl_node_t *node;
|
2007-02-14 09:20:20 +00:00
|
|
|
event_t *event;
|
2009-01-19 21:50:05 +00:00
|
|
|
time_t diff;
|
2007-02-14 09:20:20 +00:00
|
|
|
|
|
|
|
/*
|
2009-01-19 21:50:05 +00:00
|
|
|
* Make all events appear expired by substracting the difference between
|
|
|
|
* the expiration time of the last event and the current time.
|
2007-02-14 09:20:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
cp();
|
|
|
|
|
2009-01-19 21:50:05 +00:00
|
|
|
if(!event_tree->tail)
|
|
|
|
return;
|
|
|
|
|
|
|
|
event = event_tree->tail->data;
|
|
|
|
if(event->time < now)
|
|
|
|
return;
|
|
|
|
|
|
|
|
diff = 1 + event->time - now;
|
|
|
|
|
|
|
|
for(node = event_tree->head; node; node = node->next) {
|
|
|
|
event = node->data;
|
|
|
|
event->time -= diff;
|
2007-02-14 09:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-11 10:05:58 +00:00
|
|
|
event_t *new_event(void)
|
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
2003-08-28 21:05:11 +00:00
|
|
|
return xmalloc_and_zero(sizeof(event_t));
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 22:33:31 +00:00
|
|
|
void free_event(event_t *event)
|
2002-02-11 10:05:58 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
free(event);
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 22:33:31 +00:00
|
|
|
void event_add(event_t *event)
|
2002-02-11 10:05:58 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
event->id = ++id;
|
|
|
|
avl_insert(event_tree, event);
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 22:33:31 +00:00
|
|
|
void event_del(event_t *event)
|
2002-02-11 10:05:58 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
avl_delete(event_tree, event);
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event_t *get_expired_event(void)
|
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
event_t *event;
|
|
|
|
|
|
|
|
cp();
|
|
|
|
|
|
|
|
if(event_tree->head) {
|
2003-08-28 21:05:11 +00:00
|
|
|
event = event_tree->head->data;
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
if(event->time < now) {
|
2009-01-20 12:12:41 +00:00
|
|
|
avl_node_t *node = event_tree->head;
|
|
|
|
avl_unlink_node(event_tree, node);
|
|
|
|
free(node);
|
2002-09-09 21:25:28 +00:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2002-02-11 10:05:58 +00:00
|
|
|
}
|