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 |
|
54 |
#include "ircd.h" |
55 |
#include "event.h" |
56 |
#include "client.h" |
57 |
#include "send.h" |
58 |
#include "memory.h" |
59 |
#include "s_log.h" |
60 |
#include "numeric.h" |
61 |
|
62 |
static const char *last_event_ran = NULL; |
63 |
static struct ev_entry event_table[MAX_EVENTS]; |
64 |
static time_t event_time_min = -1; |
65 |
static int eventFind(EVH *func, void *arg); |
66 |
|
67 |
/* |
68 |
* void eventAdd(const char *name, EVH *func, void *arg, time_t when) |
69 |
* |
70 |
* Input: Name of event, function to call, arguments to pass, and frequency |
71 |
* of the event. |
72 |
* Output: None |
73 |
* Side Effects: Adds the event to the event list. |
74 |
*/ |
75 |
void |
76 |
eventAdd(const char *name, EVH *func, void *arg, time_t when) |
77 |
{ |
78 |
int i; |
79 |
|
80 |
/* find first inactive index, or use next index */ |
81 |
for (i = 0; i < MAX_EVENTS; i++) |
82 |
{ |
83 |
if (event_table[i].active == 0) |
84 |
{ |
85 |
event_table[i].func = func; |
86 |
event_table[i].name = name; |
87 |
event_table[i].arg = arg; |
88 |
event_table[i].when = CurrentTime + when; |
89 |
event_table[i].frequency = when; |
90 |
event_table[i].active = 1; |
91 |
|
92 |
if ((event_table[i].when < event_time_min) || (event_time_min == -1)) |
93 |
event_time_min = event_table[i].when; |
94 |
|
95 |
return; |
96 |
} |
97 |
} |
98 |
/* XXX if reach here, its an error */ |
99 |
ilog(L_ERROR, "Event table is full! (%d)", i); |
100 |
} |
101 |
|
102 |
/* |
103 |
* void eventDelete(EVH *func, void *arg) |
104 |
* |
105 |
* Input: Function handler, argument that was passed. |
106 |
* Output: None |
107 |
* Side Effects: Removes the event from the event list |
108 |
*/ |
109 |
void |
110 |
eventDelete(EVH *func, void *arg) |
111 |
{ |
112 |
int i = eventFind(func, arg); |
113 |
|
114 |
if (i == -1) |
115 |
return; |
116 |
|
117 |
event_table[i].name = NULL; |
118 |
event_table[i].func = NULL; |
119 |
event_table[i].arg = NULL; |
120 |
event_table[i].active = 0; |
121 |
} |
122 |
|
123 |
/* |
124 |
* void eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_isa) |
125 |
* |
126 |
* Input: Name of event, function to call, arguments to pass, and frequency |
127 |
* of the event. |
128 |
* Output: None |
129 |
* Side Effects: Adds the event to the event list within +- 1/3 of the |
130 |
* specified frequency. |
131 |
*/ |
132 |
void |
133 |
eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_ish) |
134 |
{ |
135 |
if (delta_ish >= 3.0) |
136 |
{ |
137 |
const time_t two_third = (2 * delta_ish) / 3; |
138 |
delta_ish = two_third + ((rand() % 1000) * two_third) / 1000; |
139 |
/* |
140 |
* XXX I hate the above magic, I don't even know if its right. |
141 |
* Grr. -- adrian |
142 |
*/ |
143 |
} |
144 |
|
145 |
eventAdd(name, func, arg, delta_ish); |
146 |
} |
147 |
|
148 |
/* |
149 |
* void eventRun(void) |
150 |
* |
151 |
* Input: None |
152 |
* Output: None |
153 |
* Side Effects: Runs pending events in the event list |
154 |
*/ |
155 |
void |
156 |
eventRun(void) |
157 |
{ |
158 |
int i; |
159 |
|
160 |
for (i = 0; i < MAX_EVENTS; i++) |
161 |
{ |
162 |
if (event_table[i].active && (event_table[i].when <= CurrentTime)) |
163 |
{ |
164 |
last_event_ran = event_table[i].name; |
165 |
event_table[i].func(event_table[i].arg); |
166 |
event_table[i].when = CurrentTime + event_table[i].frequency; |
167 |
event_time_min = -1; |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
/* |
173 |
* time_t eventNextTime(void) |
174 |
* |
175 |
* Input: None |
176 |
* Output: Specifies the next time eventRun() should be run |
177 |
* Side Effects: None |
178 |
*/ |
179 |
time_t |
180 |
eventNextTime(void) |
181 |
{ |
182 |
int i; |
183 |
|
184 |
if (event_time_min == -1) |
185 |
{ |
186 |
for (i = 0; i < MAX_EVENTS; i++) |
187 |
{ |
188 |
if (event_table[i].active && ((event_table[i].when < event_time_min) || (event_time_min == -1))) |
189 |
event_time_min = event_table[i].when; |
190 |
} |
191 |
} |
192 |
|
193 |
return(event_time_min); |
194 |
} |
195 |
|
196 |
/* |
197 |
* void eventInit(void) |
198 |
* |
199 |
* Input: None |
200 |
* Output: None |
201 |
* Side Effects: Initializes the event system. |
202 |
*/ |
203 |
void |
204 |
eventInit(void) |
205 |
{ |
206 |
last_event_ran = NULL; |
207 |
memset(event_table, 0, sizeof(event_table)); |
208 |
} |
209 |
|
210 |
/* |
211 |
* int eventFind(EVH *func, void *arg) |
212 |
* |
213 |
* Input: Event function and the argument passed to it |
214 |
* Output: Index to the slow in the event_table |
215 |
* Side Effects: None |
216 |
*/ |
217 |
static int |
218 |
eventFind(EVH *func, void *arg) |
219 |
{ |
220 |
int i; |
221 |
|
222 |
for (i = 0; i < MAX_EVENTS; i++) |
223 |
{ |
224 |
if ((event_table[i].func == func) && |
225 |
(event_table[i].arg == arg) && |
226 |
event_table[i].active) |
227 |
return(i); |
228 |
} |
229 |
|
230 |
return(-1); |
231 |
} |
232 |
|
233 |
/* |
234 |
* void show_events(struct Client *source_p) |
235 |
* |
236 |
* Input: Client requesting the event |
237 |
* Output: List of events |
238 |
* Side Effects: None |
239 |
*/ |
240 |
void |
241 |
show_events(struct Client *source_p) |
242 |
{ |
243 |
int i; |
244 |
|
245 |
if (last_event_ran) |
246 |
{ |
247 |
sendto_one(source_p, ":%s %d %s :Last event to run: %s", |
248 |
me.name, RPL_STATSDEBUG, source_p->name, last_event_ran); |
249 |
sendto_one(source_p, ":%s %d %s : ", |
250 |
me.name, RPL_STATSDEBUG, source_p->name); |
251 |
} |
252 |
|
253 |
sendto_one(source_p, |
254 |
":%s %d %s : Operation Next Execution", |
255 |
me.name, RPL_STATSDEBUG, source_p->name); |
256 |
sendto_one(source_p, |
257 |
":%s %d %s : -------------------------------------------", |
258 |
me.name, RPL_STATSDEBUG, source_p->name); |
259 |
|
260 |
for (i = 0; i < MAX_EVENTS; i++) |
261 |
if (event_table[i].active) |
262 |
{ |
263 |
sendto_one(source_p, ":%s %d %s : %-28s %-4d seconds", |
264 |
me.name, RPL_STATSDEBUG, source_p->name, |
265 |
event_table[i].name, |
266 |
(int)(event_table[i].when - CurrentTime)); |
267 |
} |
268 |
|
269 |
sendto_one(source_p, ":%s %d %s : ", |
270 |
me.name, RPL_STATSDEBUG, source_p->name); |
271 |
} |
272 |
|
273 |
/* |
274 |
* void set_back_events(time_t by) |
275 |
* Input: Time to set back events by. |
276 |
* Output: None. |
277 |
* Side-effects: Sets back all events by "by" seconds. |
278 |
*/ |
279 |
void |
280 |
set_back_events(time_t by) |
281 |
{ |
282 |
int i; |
283 |
|
284 |
for (i = 0; i < MAX_EVENTS; i++) |
285 |
{ |
286 |
if (event_table[i].when > by) |
287 |
event_table[i].when -= by; |
288 |
else |
289 |
event_table[i].when = 0; |
290 |
} |
291 |
} |
292 |
|