ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/event.c
Revision: 2919
Committed: Sat Jan 25 21:37:34 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 7244 byte(s)
Log Message:
- event.c: fixed compile error

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * event.c: Event functions.
4     *
5 michael 2916 * Copyright (c) 1998-2000 Regents of the University of California
6     * Copyright (c) 2000-2014 ircd-hybrid development team
7 adx 30 *
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 knight 31 * $Id$
42 adx 30 */
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 michael 1011 #include "list.h"
54 adx 30 #include "ircd.h"
55     #include "event.h"
56     #include "client.h"
57     #include "send.h"
58     #include "memory.h"
59 michael 1309 #include "log.h"
60 adx 30 #include "numeric.h"
61 michael 2680 #include "rng_mt.h"
62 adx 30
63 michael 2680
64 adx 30 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 michael 2916
82 adx 30 /* 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 michael 2916 event_table[i].frequency = when;
92 adx 30 event_table[i].active = 1;
93    
94     if ((event_table[i].when < event_time_min) || (event_time_min == -1))
95 michael 2916 event_time_min = event_table[i].when;
96 adx 30
97     return;
98     }
99     }
100     /* XXX if reach here, its an error */
101 michael 1247 ilog(LOG_TYPE_IRCD, "Event table is full! (%d)", i);
102 adx 30 }
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 michael 2916 /*
126 adx 30 * 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 michael 2680 if (delta_ish >= 3)
138 adx 30 {
139     const time_t two_third = (2 * delta_ish) / 3;
140 michael 2680 delta_ish = two_third + ((genrand_int32() % 1000) * two_third) / 1000;
141 adx 30 /*
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 michael 2916 *
177 adx 30 * 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 michael 2916 return event_time_min;
196 adx 30 }
197    
198     /*
199     * void eventInit(void)
200     *
201     * Input: None
202     * Output: None
203 michael 2916 * Side Effects: Initializes the event system.
204 adx 30 */
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     if ((event_table[i].func == func) &&
226     (event_table[i].arg == arg) &&
227     event_table[i].active)
228 michael 2916 return i;
229 adx 30
230 michael 2916 return -1;
231 adx 30 }
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 michael 2670 {
262 adx 30 if (event_table[i].active)
263     {
264     sendto_one(source_p, ":%s %d %s : %-28s %-4d seconds",
265     me.name, RPL_STATSDEBUG, source_p->name,
266     event_table[i].name,
267 michael 2916 (int)(event_table[i].when - CurrentTime));
268 adx 30 }
269 michael 2670 }
270 adx 30 }
271    
272     /*
273     * void set_back_events(time_t by)
274     * Input: Time to set back events by.
275     * Output: None.
276     * Side-effects: Sets back all events by "by" seconds.
277     */
278     void
279     set_back_events(time_t by)
280     {
281     int i;
282    
283 michael 1671 event_time_min = -1;
284    
285 adx 30 for (i = 0; i < MAX_EVENTS; i++)
286     {
287     if (event_table[i].when > by)
288     event_table[i].when -= by;
289     else
290     event_table[i].when = 0;
291     }
292     }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision