]> jfr.im git - solanum.git/blame - libratbox/src/event.c
Update 3.5 NEWS.
[solanum.git] / libratbox / src / event.c
CommitLineData
db137867
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * event.c: Event functions.
4 *
5 * Copyright (C) 1998-2000 Regents of the University of California
6 * Copyright (C) 2001-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * Code borrowed from the squid web cache by Adrian Chadd.
10 * Original header:
11 *
12 * DEBUG: section 41 Event Processing
13 * AUTHOR: Henrik Nordstrom
14 *
15 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
16 * ----------------------------------------------------------
17 *
18 * Squid is the result of efforts by numerous individuals from the
19 * Internet community. Development is led by Duane Wessels of the
20 * National Laboratory for Applied Network Research and funded by the
21 * National Science Foundation. Squid is Copyrighted (C) 1998 by
22 * the Regents of the University of California. Please see the
23 * COPYRIGHT file for full details. Squid incorporates software
24 * developed and/or copyrighted by other sources. Please see the
25 * CREDITS file for full details.
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
40 * USA
41 *
030272f3 42 * $Id: event.c 26272 2008-12-10 05:55:10Z androsyn $
db137867
AC
43 */
44
45#include <libratbox_config.h>
46#include <ratbox_lib.h>
47#include <commio-int.h>
48#include <event-int.h>
49
3202e249
VY
50#define EV_NAME_LEN 33
51static char last_event_ran[EV_NAME_LEN];
db137867
AC
52static rb_dlink_list event_list;
53
54static time_t event_time_min = -1;
db137867
AC
55
56/*
55abcbb2 57 * struct ev_entry *
db137867
AC
58 * rb_event_find(EVH *func, void *arg)
59 *
60 * Input: Event function and the argument passed to it
61 * Output: Index to the slow in the event_table
62 * Side Effects: None
63 */
64static struct ev_entry *
65rb_event_find(EVH * func, void *arg)
66{
67 rb_dlink_node *ptr;
68 struct ev_entry *ev;
69 RB_DLINK_FOREACH(ptr, event_list.head)
70 {
71 ev = ptr->data;
72 if((ev->func == func) && (ev->arg == arg))
73 return ev;
74 }
75
76 return NULL;
77}
78
79/*
55abcbb2 80 * struct ev_entry *
db137867
AC
81 * rb_event_add(const char *name, EVH *func, void *arg, time_t when)
82 *
83 * Input: Name of event, function to call, arguments to pass, and frequency
84 * of the event.
85 * Output: None
86 * Side Effects: Adds the event to the event list.
87 */
88struct ev_entry *
89rb_event_add(const char *name, EVH * func, void *arg, time_t when)
90{
91 struct ev_entry *ev;
92 ev = rb_malloc(sizeof(struct ev_entry));
93 ev->func = func;
3202e249 94 ev->name = rb_strndup(name, EV_NAME_LEN);
db137867
AC
95 ev->arg = arg;
96 ev->when = rb_current_time() + when;
030272f3 97 ev->next = when;
db137867
AC
98 ev->frequency = when;
99
100 if((ev->when < event_time_min) || (event_time_min == -1))
101 {
102 event_time_min = ev->when;
103 }
104 rb_dlinkAdd(ev, &ev->node, &event_list);
105 rb_io_sched_event(ev, when);
106 return ev;
107}
3202e249 108
db137867
AC
109struct ev_entry *
110rb_event_addonce(const char *name, EVH * func, void *arg, time_t when)
111{
112 struct ev_entry *ev;
113 ev = rb_malloc(sizeof(struct ev_entry));
114 ev->func = func;
3202e249 115 ev->name = rb_strndup(name, EV_NAME_LEN);
db137867
AC
116 ev->arg = arg;
117 ev->when = rb_current_time() + when;
030272f3 118 ev->next = when;
db137867
AC
119 ev->frequency = 0;
120
121 if((ev->when < event_time_min) || (event_time_min == -1))
122 event_time_min = ev->when;
123
124 rb_dlinkAdd(ev, &ev->node, &event_list);
125 rb_io_sched_event(ev, when);
126 return ev;
127}
128
129/*
130 * void rb_event_delete(struct ev_entry *ev)
131 *
132 * Input: pointer to ev_entry for the event
133 * Output: None
134 * Side Effects: Removes the event from the event list
135 */
136void
137rb_event_delete(struct ev_entry *ev)
138{
139 if(ev == NULL)
140 return;
141
142 rb_dlinkDelete(&ev->node, &event_list);
143 rb_io_unsched_event(ev);
3202e249 144 rb_free(ev->name);
db137867
AC
145 rb_free(ev);
146}
147
148/*
149 * void rb_event_find_delete(EVH *func, void *arg)
150 *
151 * Input: pointer to func and data
152 * Output: None
153 * Side Effects: Removes the event from the event list
154 */
155void
3202e249 156rb_event_find_delete(EVH * func, void *arg)
db137867
AC
157{
158 rb_event_delete(rb_event_find(func, arg));
159}
160
55abcbb2 161/*
db137867
AC
162 * struct ev_entry *
163 * rb_event_addish(const char *name, EVH *func, void *arg, time_t delta_isa)
164 *
165 * Input: Name of event, function to call, arguments to pass, and frequency
166 * of the event.
167 * Output: None
168 * Side Effects: Adds the event to the event list within +- 1/3 of the
169 * specified frequency.
170 */
171struct ev_entry *
172rb_event_addish(const char *name, EVH * func, void *arg, time_t delta_ish)
173{
174 if(delta_ish >= 3.0)
175 {
176 const time_t two_third = (2 * delta_ish) / 3;
177 delta_ish = two_third + ((rand() % 1000) * two_third) / 1000;
178 /*
179 * XXX I hate the above magic, I don't even know if its right.
180 * Grr. -- adrian
181 */
182 }
183 return rb_event_add(name, func, arg, delta_ish);
184}
185
186
187void
188rb_run_event(struct ev_entry *ev)
189{
3202e249 190 rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran));
db137867
AC
191 ev->func(ev->arg);
192 if(!ev->frequency)
193 {
1d393245 194 rb_event_delete(ev);
db137867
AC
195 return;
196 }
197 ev->when = rb_current_time() + ev->frequency;
198 if((ev->when < event_time_min) || (event_time_min == -1))
199 event_time_min = ev->when;
200}
201
202/*
203 * void rb_event_run(void)
204 *
205 * Input: None
206 * Output: None
207 * Side Effects: Runs pending events in the event list
208 */
209void
210rb_event_run(void)
211{
212 rb_dlink_node *ptr, *next;
213 struct ev_entry *ev;
3202e249 214
db137867
AC
215 if(rb_io_supports_event())
216 return;
217
218 event_time_min = -1;
219 RB_DLINK_FOREACH_SAFE(ptr, next, event_list.head)
220 {
221 ev = ptr->data;
222 if(ev->when <= rb_current_time())
223 {
3202e249 224 rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran));
db137867
AC
225 ev->func(ev->arg);
226
227 /* event is scheduled more than once */
3202e249 228 if(ev->frequency)
db137867
AC
229 {
230 ev->when = rb_current_time() + ev->frequency;
231 if((ev->when < event_time_min) || (event_time_min == -1))
232 event_time_min = ev->when;
233 }
234 else
235 {
236 rb_dlinkDelete(&ev->node, &event_list);
237 rb_free(ev);
238 }
3202e249
VY
239 }
240 else
241 {
db137867
AC
242 if((ev->when < event_time_min) || (event_time_min == -1))
243 event_time_min = ev->when;
244 }
245 }
246}
247
248void
249rb_event_io_register_all(void)
250{
251 rb_dlink_node *ptr;
252 struct ev_entry *ev;
030272f3 253
db137867
AC
254 if(!rb_io_supports_event())
255 return;
3202e249 256
db137867 257 RB_DLINK_FOREACH(ptr, event_list.head)
3202e249 258 {
db137867 259 ev = ptr->data;
030272f3 260 rb_io_sched_event(ev, ev->next);
db137867
AC
261 }
262}
3202e249 263
db137867
AC
264/*
265 * void rb_event_init(void)
266 *
267 * Input: None
268 * Output: None
55abcbb2 269 * Side Effects: Initializes the event system.
db137867
AC
270 */
271void
272rb_event_init(void)
273{
3202e249 274 rb_strlcpy(last_event_ran, "NONE", sizeof(last_event_ran));
db137867
AC
275}
276
277void
278rb_dump_events(void (*func) (char *, void *), void *ptr)
279{
280 int len;
281 char buf[512];
282 rb_dlink_node *dptr;
283 struct ev_entry *ev;
284 len = sizeof(buf);
3202e249
VY
285
286 rb_snprintf(buf, len, "Last event to run: %s", last_event_ran);
287 func(buf, ptr);
288
db137867
AC
289 rb_strlcpy(buf, "Operation Next Execution", len);
290 func(buf, ptr);
291
292 RB_DLINK_FOREACH(dptr, event_list.head)
293 {
294 ev = dptr->data;
295 rb_snprintf(buf, len, "%-28s %-4ld seconds", ev->name,
3202e249 296 ev->when - (long)rb_current_time());
db137867
AC
297 func(buf, ptr);
298 }
299}
300
55abcbb2 301/*
db137867
AC
302 * void rb_set_back_events(time_t by)
303 * Input: Time to set back events by.
304 * Output: None.
305 * Side-effects: Sets back all events by "by" seconds.
306 */
307void
308rb_set_back_events(time_t by)
309{
310 rb_dlink_node *ptr;
311 struct ev_entry *ev;
312 RB_DLINK_FOREACH(ptr, event_list.head)
313 {
314 ev = ptr->data;
315 if(ev->when > by)
316 ev->when -= by;
317 else
318 ev->when = 0;
319 }
320}
321
322void
323rb_event_update(struct ev_entry *ev, time_t freq)
324{
325 if(ev == NULL)
326 return;
327
328 ev->frequency = freq;
329
330 /* update when its scheduled to run if its higher
331 * than the new frequency
332 */
333 if((rb_current_time() + freq) < ev->when)
334 ev->when = rb_current_time() + freq;
335 return;
336}
337
338time_t
339rb_event_next(void)
340{
341 return event_time_min;
342}