ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/libio/misc/event.c
Revision: 69
Committed: Tue Oct 4 16:09:51 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 6045 byte(s)
Log Message:
- splitted ircd/libio, all headers connected with libio sources have been
  moved for internal use only. To use libio interface, include "libio.h"
  (which is already done in "stdinc.h")


File Contents

# User Rev Content
1 adx 61 /*
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: event.c 33 2005-10-02 20:50:00Z knight $
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 adx 65 const char *last_event_ran = NULL;
55     struct ev_entry event_table[MAX_EVENTS];
56    
57 adx 61 static time_t event_time_min = -1;
58     static int eventFind(EVH *func, void *arg);
59    
60     /*
61     * void eventAdd(const char *name, EVH *func, void *arg, time_t when)
62     *
63     * Input: Name of event, function to call, arguments to pass, and frequency
64     * of the event.
65     * Output: None
66     * Side Effects: Adds the event to the event list.
67     */
68     void
69     eventAdd(const char *name, EVH *func, void *arg, time_t when)
70     {
71     int i;
72    
73     /* find first inactive index, or use next index */
74     for (i = 0; i < MAX_EVENTS; i++)
75     {
76     if (event_table[i].active == 0)
77     {
78     event_table[i].func = func;
79     event_table[i].name = name;
80     event_table[i].arg = arg;
81     event_table[i].when = CurrentTime + when;
82     event_table[i].frequency = when;
83     event_table[i].active = 1;
84    
85     if ((event_table[i].when < event_time_min) || (event_time_min == -1))
86     event_time_min = event_table[i].when;
87    
88     return;
89     }
90     }
91     /* XXX if reach here, its an error */
92     ilog(L_ERROR, "Event table is full! (%d)", i);
93     }
94    
95     /*
96     * void eventDelete(EVH *func, void *arg)
97     *
98     * Input: Function handler, argument that was passed.
99     * Output: None
100     * Side Effects: Removes the event from the event list
101     */
102     void
103     eventDelete(EVH *func, void *arg)
104     {
105     int i = eventFind(func, arg);
106    
107     if (i == -1)
108     return;
109    
110     event_table[i].name = NULL;
111     event_table[i].func = NULL;
112     event_table[i].arg = NULL;
113     event_table[i].active = 0;
114     }
115    
116     /*
117     * void eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_isa)
118     *
119     * Input: Name of event, function to call, arguments to pass, and frequency
120     * of the event.
121     * Output: None
122     * Side Effects: Adds the event to the event list within +- 1/3 of the
123     * specified frequency.
124     */
125     void
126     eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_ish)
127     {
128     if (delta_ish >= 3.0)
129     {
130     const time_t two_third = (2 * delta_ish) / 3;
131     delta_ish = two_third + ((rand() % 1000) * two_third) / 1000;
132     /*
133     * XXX I hate the above magic, I don't even know if its right.
134     * Grr. -- adrian
135     */
136     }
137    
138     eventAdd(name, func, arg, delta_ish);
139     }
140    
141     /*
142     * void eventRun(void)
143     *
144     * Input: None
145     * Output: None
146     * Side Effects: Runs pending events in the event list
147     */
148     void
149     eventRun(void)
150     {
151     int i;
152    
153     for (i = 0; i < MAX_EVENTS; i++)
154     {
155     if (event_table[i].active && (event_table[i].when <= CurrentTime))
156     {
157     last_event_ran = event_table[i].name;
158     event_table[i].func(event_table[i].arg);
159     event_table[i].when = CurrentTime + event_table[i].frequency;
160     event_time_min = -1;
161     }
162     }
163     }
164    
165     /*
166     * time_t eventNextTime(void)
167     *
168     * Input: None
169     * Output: Specifies the next time eventRun() should be run
170     * Side Effects: None
171     */
172     time_t
173     eventNextTime(void)
174     {
175     int i;
176    
177     if (event_time_min == -1)
178     {
179     for (i = 0; i < MAX_EVENTS; i++)
180     {
181     if (event_table[i].active && ((event_table[i].when < event_time_min) || (event_time_min == -1)))
182     event_time_min = event_table[i].when;
183     }
184     }
185    
186 adx 65 return event_time_min;
187 adx 61 }
188    
189     /*
190     * void eventInit(void)
191     *
192     * Input: None
193     * Output: None
194     * Side Effects: Initializes the event system.
195     */
196     void
197     eventInit(void)
198     {
199     last_event_ran = NULL;
200     memset(event_table, 0, sizeof(event_table));
201     }
202    
203     /*
204     * int eventFind(EVH *func, void *arg)
205     *
206     * Input: Event function and the argument passed to it
207     * Output: Index to the slow in the event_table
208     * Side Effects: None
209     */
210     static int
211     eventFind(EVH *func, void *arg)
212     {
213     int i;
214    
215     for (i = 0; i < MAX_EVENTS; i++)
216     {
217     if ((event_table[i].func == func) &&
218     (event_table[i].arg == arg) &&
219     event_table[i].active)
220 adx 65 return i;
221 adx 61 }
222    
223 adx 65 return -1;
224 adx 61 }
225    
226     /*
227     * void set_back_events(time_t by)
228     * Input: Time to set back events by.
229     * Output: None.
230     * Side-effects: Sets back all events by "by" seconds.
231     */
232     void
233     set_back_events(time_t by)
234     {
235     int i;
236    
237     for (i = 0; i < MAX_EVENTS; i++)
238     {
239     if (event_table[i].when > by)
240     event_table[i].when -= by;
241     else
242     event_table[i].when = 0;
243     }
244     }

Properties

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