2 * ircd-ratbox: A slightly useful ircd.
3 * event.c: Event functions.
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
9 * Code borrowed from the squid web cache by Adrian Chadd.
12 * DEBUG: section 41 Event Processing
13 * AUTHOR: Henrik Nordstrom
15 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
16 * ----------------------------------------------------------
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.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
42 * $Id: event.c 26 2006-09-20 18:02:06Z spb $
48 * Should be pretty self-explanatory. Events are added to the static
49 * array event_table with a frequency time telling eventRun how often
64 static const char *last_event_ran
= NULL
;
65 struct ev_entry event_table
[MAX_EVENTS
];
66 static time_t event_time_min
= -1;
69 * void eventAdd(const char *name, EVH *func, void *arg, time_t when)
71 * Input: Name of event, function to call, arguments to pass, and frequency
74 * Side Effects: Adds the event to the event list.
77 eventAdd(const char *name
, EVH
* func
, void *arg
, time_t when
)
81 /* find first inactive index */
82 for (i
= 0; i
< MAX_EVENTS
; i
++)
84 if(event_table
[i
].active
== 0)
86 event_table
[i
].func
= func
;
87 event_table
[i
].name
= name
;
88 event_table
[i
].arg
= arg
;
89 event_table
[i
].when
= CurrentTime
+ when
;
90 event_table
[i
].frequency
= when
;
91 event_table
[i
].active
= 1;
93 if((event_table
[i
].when
< event_time_min
) || (event_time_min
== -1))
94 event_time_min
= event_table
[i
].when
;
100 /* erk! couldnt add to event table */
101 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Unable to add event [%s] to event table", name
);
106 eventAddOnce(const char *name
, EVH
*func
, void *arg
, time_t when
)
110 /* find first inactive index */
111 for (i
= 0; i
< MAX_EVENTS
; i
++)
113 if(event_table
[i
].active
== 0)
115 event_table
[i
].func
= func
;
116 event_table
[i
].name
= name
;
117 event_table
[i
].arg
= arg
;
118 event_table
[i
].when
= CurrentTime
+ when
;
119 event_table
[i
].frequency
= 0;
120 event_table
[i
].active
= 1;
122 if ((event_table
[i
].when
< event_time_min
) || (event_time_min
== -1))
123 event_time_min
= event_table
[i
].when
;
129 /* erk! couldnt add to event table */
130 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
131 "Unable to add event [%s] to event table", name
);
135 * void eventDelete(EVH *func, void *arg)
137 * Input: Function handler, argument that was passed.
139 * Side Effects: Removes the event from the event list
142 eventDelete(EVH
* func
, void *arg
)
146 i
= eventFind(func
, arg
);
151 event_table
[i
].name
= NULL
;
152 event_table
[i
].func
= NULL
;
153 event_table
[i
].arg
= NULL
;
154 event_table
[i
].active
= 0;
158 * void eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_isa)
160 * Input: Name of event, function to call, arguments to pass, and frequency
163 * Side Effects: Adds the event to the event list within +- 1/3 of the
164 * specified frequency.
167 eventAddIsh(const char *name
, EVH
* func
, void *arg
, time_t delta_ish
)
171 const time_t two_third
= (2 * delta_ish
) / 3;
172 delta_ish
= two_third
+ ((rand() % 1000) * two_third
) / 1000;
174 * XXX I hate the above magic, I don't even know if its right.
178 eventAdd(name
, func
, arg
, delta_ish
);
182 * void eventRun(void)
186 * Side Effects: Runs pending events in the event list
193 for (i
= 0; i
< MAX_EVENTS
; i
++)
195 if(event_table
[i
].active
&& (event_table
[i
].when
<= CurrentTime
))
197 last_event_ran
= event_table
[i
].name
;
198 event_table
[i
].func(event_table
[i
].arg
);
201 /* event is scheduled more than once */
202 if(event_table
[i
].frequency
)
203 event_table
[i
].when
= CurrentTime
+ event_table
[i
].frequency
;
206 event_table
[i
].name
= NULL
;
207 event_table
[i
].func
= NULL
;
208 event_table
[i
].arg
= NULL
;
209 event_table
[i
].active
= 0;
217 * time_t eventNextTime(void)
220 * Output: Specifies the next time eventRun() should be run
228 if(event_time_min
== -1)
230 for (i
= 0; i
< MAX_EVENTS
; i
++)
232 if(event_table
[i
].active
&&
233 ((event_table
[i
].when
< event_time_min
) || (event_time_min
== -1)))
234 event_time_min
= event_table
[i
].when
;
238 return event_time_min
;
242 * void eventInit(void)
246 * Side Effects: Initializes the event system.
251 last_event_ran
= NULL
;
252 memset((void *) event_table
, 0, sizeof(event_table
));
256 * int eventFind(EVH *func, void *arg)
258 * Input: Event function and the argument passed to it
259 * Output: Index to the slow in the event_table
263 eventFind(EVH
* func
, void *arg
)
267 for (i
= 0; i
< MAX_EVENTS
; i
++)
269 if((event_table
[i
].func
== func
) &&
270 (event_table
[i
].arg
== arg
) && event_table
[i
].active
)
278 * void show_events(struct Client *source_p)
280 * Input: Client requesting the event
281 * Output: List of events
285 show_events(struct Client
*source_p
)
290 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
291 "E :Last event to run: %s",
294 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
295 "E :Operation Next Execution");
297 for (i
= 0; i
< MAX_EVENTS
; i
++)
299 if(event_table
[i
].active
)
301 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
302 "E :%-28s %-4d seconds",
304 (int)(event_table
[i
].when
- CurrentTime
));
310 * void set_back_events(time_t by)
311 * Input: Time to set back events by.
313 * Side-effects: Sets back all events by "by" seconds.
316 set_back_events(time_t by
)
320 for (i
= 0; i
< MAX_EVENTS
; i
++)
322 if(event_table
[i
].when
> by
)
323 event_table
[i
].when
-= by
;
325 event_table
[i
].when
= 0;
330 eventUpdate(const char *name
, time_t freq
)
334 for(i
= 0; i
< MAX_EVENTS
; i
++)
336 if(event_table
[i
].active
&&
337 !irccmp(event_table
[i
].name
, name
))
339 event_table
[i
].frequency
= freq
;
341 /* update when its scheduled to run if its higher
342 * than the new frequency
344 if((CurrentTime
+ freq
) < event_table
[i
].when
)
345 event_table
[i
].when
= CurrentTime
+ freq
;