| 1 |
/*
|
| 2 |
* Copyright (C) 1999,2000 Ross Combs (rocombs@cs.nmsu.edu)
|
| 3 |
* Copyright (C) 2004 Dizzy (dizzy@roedu.net)
|
| 4 |
*
|
| 5 |
* This program is free software; you can redistribute it and/or
|
| 6 |
* modify it under the terms of the GNU General Public License
|
| 7 |
* as published by the Free Software Foundation; either version 2
|
| 8 |
* of the License, or (at your option) any later version.
|
| 9 |
*
|
| 10 |
* This program is distributed in the hope that it will be useful,
|
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
* GNU General Public License for more details.
|
| 14 |
*
|
| 15 |
* You should have received a copy of the GNU General Public License
|
| 16 |
* along with this program; if not, write to the Free Software
|
| 17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 18 |
*/
|
| 19 |
#define TIMER_INTERNAL_ACCESS
|
| 20 |
#include "common/setup_before.h"
|
| 21 |
#ifdef STDC_HEADERS
|
| 22 |
# include <stdlib.h>
|
| 23 |
#else
|
| 24 |
# ifdef HAVE_MALLOC_H
|
| 25 |
# include <malloc.h>
|
| 26 |
# endif
|
| 27 |
#endif
|
| 28 |
#include "common/elist.h"
|
| 29 |
#include "connection.h"
|
| 30 |
#include "common/eventlog.h"
|
| 31 |
#include "common/xalloc.h"
|
| 32 |
#include "timer.h"
|
| 33 |
#include "common/setup_after.h"
|
| 34 |
|
| 35 |
|
| 36 |
static t_elist timerlist_head;
|
| 37 |
|
| 38 |
|
| 39 |
extern int timerlist_add_timer(t_connection * owner, time_t when, t_timer_cb cb, t_timer_data data)
|
| 40 |
{
|
| 41 |
t_timer * timer, *ctimer;
|
| 42 |
t_elist * curr;
|
| 43 |
|
| 44 |
if (!owner)
|
| 45 |
{
|
| 46 |
eventlog(eventlog_level_error,__FUNCTION__,"got NULL owner");
|
| 47 |
return -1;
|
| 48 |
}
|
| 49 |
|
| 50 |
timer = xmalloc(sizeof(t_timer));
|
| 51 |
timer->owner = owner;
|
| 52 |
timer->when = when;
|
| 53 |
timer->cb = cb;
|
| 54 |
timer->data = data;
|
| 55 |
|
| 56 |
/* try to preserve a when based order */
|
| 57 |
elist_for_each(curr,&timerlist_head)
|
| 58 |
{
|
| 59 |
ctimer = elist_entry(curr,t_timer,timers);
|
| 60 |
if (ctimer->when > when) break;
|
| 61 |
}
|
| 62 |
elist_add_tail(curr, &timer->timers);
|
| 63 |
|
| 64 |
/* add it to the t_conn timers list */
|
| 65 |
elist_add_tail(conn_get_timer(owner), &timer->owners);
|
| 66 |
|
| 67 |
return 0;
|
| 68 |
}
|
| 69 |
|
| 70 |
|
| 71 |
extern int timerlist_del_all_timers(t_connection * owner)
|
| 72 |
{
|
| 73 |
t_elist * curr, *save;
|
| 74 |
t_timer * timer;
|
| 75 |
|
| 76 |
if (!owner)
|
| 77 |
{
|
| 78 |
eventlog(eventlog_level_error,__FUNCTION__,"got NULL owner");
|
| 79 |
return -1;
|
| 80 |
}
|
| 81 |
|
| 82 |
elist_for_each_safe(curr,conn_get_timer(owner),save)
|
| 83 |
{
|
| 84 |
timer = elist_entry(curr, t_timer, owners);
|
| 85 |
if (timer->cb)
|
| 86 |
timer->cb(timer->owner,(time_t)0,timer->data);
|
| 87 |
elist_del(&timer->owners);
|
| 88 |
elist_del(&timer->timers);
|
| 89 |
xfree((void*)timer);
|
| 90 |
}
|
| 91 |
|
| 92 |
return 0;
|
| 93 |
}
|
| 94 |
|
| 95 |
|
| 96 |
extern int timerlist_check_timers(time_t when)
|
| 97 |
{
|
| 98 |
t_elist * curr, *save;
|
| 99 |
t_timer * timer;
|
| 100 |
|
| 101 |
elist_for_each_safe(curr,&timerlist_head,save)
|
| 102 |
{
|
| 103 |
timer = elist_entry(curr,t_timer,timers);
|
| 104 |
if (timer->owner && timer->when<when)
|
| 105 |
{
|
| 106 |
if (timer->cb)
|
| 107 |
timer->cb(timer->owner,timer->when,timer->data);
|
| 108 |
elist_del(&timer->owners);
|
| 109 |
elist_del(&timer->timers);
|
| 110 |
xfree((void*)timer);
|
| 111 |
} else break; /* beeing sorted there is no need to go beyond this point */
|
| 112 |
}
|
| 113 |
|
| 114 |
return 0;
|
| 115 |
}
|
| 116 |
|
| 117 |
extern int timerlist_create(void)
|
| 118 |
{
|
| 119 |
elist_init(&timerlist_head);
|
| 120 |
return 0;
|
| 121 |
}
|
| 122 |
|
| 123 |
|
| 124 |
extern int timerlist_destroy(void)
|
| 125 |
{
|
| 126 |
t_elist * curr, *save;
|
| 127 |
t_timer * timer;
|
| 128 |
|
| 129 |
elist_for_each_safe(curr,&timerlist_head,save)
|
| 130 |
{
|
| 131 |
timer = elist_entry(curr,t_timer,timers);
|
| 132 |
elist_del(&timer->owners);
|
| 133 |
elist_del(&timer->timers);
|
| 134 |
xfree((void*)timer);
|
| 135 |
}
|
| 136 |
elist_init(&timerlist_head);
|
| 137 |
|
| 138 |
return 0;
|
| 139 |
}
|