1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(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 |
* |
8 |
* Code borrowed from the squid web cache by Adrian Chadd. |
9 |
* Original header: |
10 |
* |
11 |
* DEBUG: section 41 Event Processing |
12 |
* AUTHOR: Henrik Nordstrom |
13 |
* |
14 |
* SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
15 |
* ---------------------------------------------------------- |
16 |
* |
17 |
* Squid is the result of efforts by numerous individuals from the |
18 |
* Internet community. Development is led by Duane Wessels of the |
19 |
* National Laboratory for Applied Network Research and funded by the |
20 |
* National Science Foundation. Squid is Copyrighted (C) 1998 by |
21 |
* the Regents of the University of California. Please see the |
22 |
* COPYRIGHT file for full details. Squid incorporates software |
23 |
* developed and/or copyrighted by other sources. Please see the |
24 |
* CREDITS file for full details. |
25 |
* |
26 |
* This program is free software; you can redistribute it and/or modify |
27 |
* it under the terms of the GNU General Public License as published by |
28 |
* the Free Software Foundation; either version 2 of the License, or |
29 |
* (at your option) any later version. |
30 |
* |
31 |
* This program is distributed in the hope that it will be useful, |
32 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
33 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
34 |
* GNU General Public License for more details. |
35 |
* |
36 |
* You should have received a copy of the GNU General Public License |
37 |
* along with this program; if not, write to the Free Software |
38 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
39 |
* USA |
40 |
* |
41 |
* $Id$ |
42 |
*/ |
43 |
|
44 |
/* |
45 |
* How it's used: |
46 |
* |
47 |
* Should be pretty self-explanatory. Events are added to the static |
48 |
* array event_table with a frequency time telling eventRun how often |
49 |
* to execute it. |
50 |
*/ |
51 |
|
52 |
#include "stdinc.h" |
53 |
#include "list.h" |
54 |
#include "ircd.h" |
55 |
#include "event.h" |
56 |
#include "client.h" |
57 |
#include "send.h" |
58 |
#include "memory.h" |
59 |
#include "log.h" |
60 |
#include "numeric.h" |
61 |
#include "rng_mt.h" |
62 |
|
63 |
|
64 |
static const char *last_event_ran = NULL; |
65 |
static struct ev_entry event_table[MAX_EVENTS]; |
66 |
static time_t event_time_min = -1; |
67 |
static int eventFind(EVH *func, void *arg); |
68 |
|
69 |
/* |
70 |
* void eventAdd(const char *name, EVH *func, void *arg, time_t when) |
71 |
* |
72 |
* Input: Name of event, function to call, arguments to pass, and frequency |
73 |
* of the event. |
74 |
* Output: None |
75 |
* Side Effects: Adds the event to the event list. |
76 |
*/ |
77 |
void |
78 |
eventAdd(const char *name, EVH *func, void *arg, time_t when) |
79 |
{ |
80 |
int i; |
81 |
|
82 |
/* find first inactive index, or use next index */ |
83 |
for (i = 0; i < MAX_EVENTS; i++) |
84 |
{ |
85 |
if (event_table[i].active == 0) |
86 |
{ |
87 |
event_table[i].func = func; |
88 |
event_table[i].name = name; |
89 |
event_table[i].arg = arg; |
90 |
event_table[i].when = CurrentTime + when; |
91 |
event_table[i].frequency = when; |
92 |
event_table[i].active = 1; |
93 |
|
94 |
if ((event_table[i].when < event_time_min) || (event_time_min == -1)) |
95 |
event_time_min = event_table[i].when; |
96 |
|
97 |
return; |
98 |
} |
99 |
} |
100 |
/* XXX if reach here, its an error */ |
101 |
ilog(LOG_TYPE_IRCD, "Event table is full! (%d)", i); |
102 |
} |
103 |
|
104 |
/* |
105 |
* void eventDelete(EVH *func, void *arg) |
106 |
* |
107 |
* Input: Function handler, argument that was passed. |
108 |
* Output: None |
109 |
* Side Effects: Removes the event from the event list |
110 |
*/ |
111 |
void |
112 |
eventDelete(EVH *func, void *arg) |
113 |
{ |
114 |
int i = eventFind(func, arg); |
115 |
|
116 |
if (i == -1) |
117 |
return; |
118 |
|
119 |
event_table[i].name = NULL; |
120 |
event_table[i].func = NULL; |
121 |
event_table[i].arg = NULL; |
122 |
event_table[i].active = 0; |
123 |
} |
124 |
|
125 |
/* |
126 |
* void eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_isa) |
127 |
* |
128 |
* Input: Name of event, function to call, arguments to pass, and frequency |
129 |
* of the event. |
130 |
* Output: None |
131 |
* Side Effects: Adds the event to the event list within +- 1/3 of the |
132 |
* specified frequency. |
133 |
*/ |
134 |
void |
135 |
eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_ish) |
136 |
{ |
137 |
if (delta_ish >= 3) |
138 |
{ |
139 |
const time_t two_third = (2 * delta_ish) / 3; |
140 |
delta_ish = two_third + ((genrand_int32() % 1000) * two_third) / 1000; |
141 |
/* |
142 |
* XXX I hate the above magic, I don't even know if its right. |
143 |
* Grr. -- adrian |
144 |
*/ |
145 |
} |
146 |
|
147 |
eventAdd(name, func, arg, delta_ish); |
148 |
} |
149 |
|
150 |
/* |
151 |
* void eventRun(void) |
152 |
* |
153 |
* Input: None |
154 |
* Output: None |
155 |
* Side Effects: Runs pending events in the event list |
156 |
*/ |
157 |
void |
158 |
eventRun(void) |
159 |
{ |
160 |
int i; |
161 |
|
162 |
for (i = 0; i < MAX_EVENTS; i++) |
163 |
{ |
164 |
if (event_table[i].active && (event_table[i].when <= CurrentTime)) |
165 |
{ |
166 |
last_event_ran = event_table[i].name; |
167 |
event_table[i].func(event_table[i].arg); |
168 |
event_table[i].when = CurrentTime + event_table[i].frequency; |
169 |
event_time_min = -1; |
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
/* |
175 |
* time_t eventNextTime(void) |
176 |
* |
177 |
* Input: None |
178 |
* Output: Specifies the next time eventRun() should be run |
179 |
* Side Effects: None |
180 |
*/ |
181 |
time_t |
182 |
eventNextTime(void) |
183 |
{ |
184 |
int i; |
185 |
|
186 |
if (event_time_min == -1) |
187 |
{ |
188 |
for (i = 0; i < MAX_EVENTS; i++) |
189 |
{ |
190 |
if (event_table[i].active && ((event_table[i].when < event_time_min) || (event_time_min == -1))) |
191 |
event_time_min = event_table[i].when; |
192 |
} |
193 |
} |
194 |
|
195 |
return(event_time_min); |
196 |
} |
197 |
|
198 |
/* |
199 |
* void eventInit(void) |
200 |
* |
201 |
* Input: None |
202 |
* Output: None |
203 |
* Side Effects: Initializes the event system. |
204 |
*/ |
205 |
void |
206 |
eventInit(void) |
207 |
{ |
208 |
last_event_ran = NULL; |
209 |
memset(event_table, 0, sizeof(event_table)); |
210 |
} |
211 |
|
212 |
/* |
213 |
* int eventFind(EVH *func, void *arg) |
214 |
* |
215 |
* Input: Event function and the argument passed to it |
216 |
* Output: Index to the slow in the event_table |
217 |
* Side Effects: None |
218 |
*/ |
219 |
static int |
220 |
eventFind(EVH *func, void *arg) |
221 |
{ |
222 |
int i; |
223 |
|
224 |
for (i = 0; i < MAX_EVENTS; i++) |
225 |
{ |
226 |
if ((event_table[i].func == func) && |
227 |
(event_table[i].arg == arg) && |
228 |
event_table[i].active) |
229 |
return(i); |
230 |
} |
231 |
|
232 |
return(-1); |
233 |
} |
234 |
|
235 |
/* |
236 |
* void show_events(struct Client *source_p) |
237 |
* |
238 |
* Input: Client requesting the event |
239 |
* Output: List of events |
240 |
* Side Effects: None |
241 |
*/ |
242 |
void |
243 |
show_events(struct Client *source_p) |
244 |
{ |
245 |
int i; |
246 |
|
247 |
if (last_event_ran) |
248 |
{ |
249 |
sendto_one(source_p, ":%s %d %s :Last event to run: %s", |
250 |
me.name, RPL_STATSDEBUG, source_p->name, last_event_ran); |
251 |
sendto_one(source_p, ":%s %d %s : ", |
252 |
me.name, RPL_STATSDEBUG, source_p->name); |
253 |
} |
254 |
|
255 |
sendto_one(source_p, |
256 |
":%s %d %s : Operation Next Execution", |
257 |
me.name, RPL_STATSDEBUG, source_p->name); |
258 |
sendto_one(source_p, |
259 |
":%s %d %s : -------------------------------------------", |
260 |
me.name, RPL_STATSDEBUG, source_p->name); |
261 |
|
262 |
for (i = 0; i < MAX_EVENTS; i++) |
263 |
{ |
264 |
if (event_table[i].active) |
265 |
{ |
266 |
sendto_one(source_p, ":%s %d %s : %-28s %-4d seconds", |
267 |
me.name, RPL_STATSDEBUG, source_p->name, |
268 |
event_table[i].name, |
269 |
(int)(event_table[i].when - CurrentTime)); |
270 |
} |
271 |
} |
272 |
} |
273 |
|
274 |
/* |
275 |
* void set_back_events(time_t by) |
276 |
* Input: Time to set back events by. |
277 |
* Output: None. |
278 |
* Side-effects: Sets back all events by "by" seconds. |
279 |
*/ |
280 |
void |
281 |
set_back_events(time_t by) |
282 |
{ |
283 |
int i; |
284 |
|
285 |
event_time_min = -1; |
286 |
|
287 |
for (i = 0; i < MAX_EVENTS; i++) |
288 |
{ |
289 |
if (event_table[i].when > by) |
290 |
event_table[i].when -= by; |
291 |
else |
292 |
event_table[i].when = 0; |
293 |
} |
294 |
} |
295 |
|