ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/event.C
Revision: 1.1
Committed: Thu Jul 19 08:24:57 2007 UTC (16 years, 10 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Log Message:
initial import. the most important changes since Atheme are:
- fixed many memory leaks
- fixed many bugs
- converted to C++ and use more STL containers
- added a (not very enhanced yet) perl module
- greatly improved XML-RPC speed
- added a JSON-RPC module with code from json-cpp
- added a valgrind memcheck module to operserv
- added a more object oriented base64 implementation
- added a specialised unit test framework
- improved stability
- use gettimeofday() if available
- reworked adding/removing commands
- MemoServ IGNORE DEL can now remove indices

File Contents

# Content
1 /*
2 * event.C: Event subsystem.
3 * Rights to this code are documented in doc/LICENSE.
4 *
5 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org)
6 */
7
8 static char const rcsid[] = "$Id";
9
10 #include "atheme.h"
11 #include "internal.h"
12
13 const char *last_event_ran = NULL;
14 struct ev_entry event_table[MAX_EVENTS];
15 static time_t event_time_min = -1;
16
17 /* add an event to the table to be continually ran */
18 unsigned int
19 event_add (const char *name, EVH * func, void *arg, time_t when)
20 {
21 unsigned int i;
22
23 /* find the first inactive index */
24 for (i = 0; i < MAX_EVENTS; i++)
25 {
26 if (!event_table[i].active)
27 {
28 event_table[i].func = func;
29 event_table[i].name = name;
30 event_table[i].arg = arg;
31 event_table[i].when = NOW + when;
32 event_table[i].frequency = when;
33 event_table[i].active = true;
34
35 if ((event_table[i].when < event_time_min) || (event_time_min == -1))
36 event_time_min = event_table[i].when;
37
38 slog (LG_DEBUG, "event_add(): \"%s\"", name);
39
40 claro_state.event++;
41
42 return i;
43 }
44 }
45
46 /* failed to add it... */
47 slog (LG_DEBUG, "event_add(): failed to add \"%s\" to event table", name);
48
49 return -1;
50 }
51
52 /* adds an event to the table to be ran only once */
53 unsigned int
54 event_add_once (const char *name, EVH * func, void *arg, time_t when)
55 {
56 unsigned int i;
57
58 /* find the first inactive index */
59 for (i = 0; i < MAX_EVENTS; i++)
60 {
61 if (!event_table[i].active)
62 {
63 event_table[i].func = func;
64 event_table[i].name = name;
65 event_table[i].arg = arg;
66 event_table[i].when = NOW + when;
67 event_table[i].frequency = 0;
68 event_table[i].active = true;
69
70 if ((event_table[i].when < event_time_min) || (event_time_min == -1))
71 event_time_min = event_table[i].when;
72
73 slog (LG_DEBUG, "event_add_once(): \"%s\"", name);
74
75 claro_state.event++;
76
77 return i;
78 }
79 }
80
81 /* failed to add it... */
82 slog (LG_DEBUG, "event_add(): failed to add \"%s\" to event table", name);
83
84 return -1;
85 }
86
87 /* delete an event from the table */
88 void
89 event_delete (EVH * func, void *arg)
90 {
91 int i = event_find (func, arg);
92
93 if (i == -1)
94 return;
95
96 slog (LG_DEBUG, "event_delete(): removing \"%s\"", event_table[i].name);
97
98 event_table[i].name = NULL;
99 event_table[i].func = NULL;
100 event_table[i].arg = NULL;
101 event_table[i].active = false;
102
103 claro_state.event--;
104 }
105
106 /* checks all pending events */
107 void
108 event_run (void)
109 {
110 unsigned int i;
111
112 for (i = 0; i < MAX_EVENTS; i++)
113 {
114 if (event_table[i].active && (event_table[i].when <= NOW))
115 {
116 /* now we call it */
117 last_event_ran = event_table[i].name;
118 event_table[i].func (event_table[i].arg);
119 event_time_min = -1;
120
121 /* event is scheduled more than once */
122 if (event_table[i].frequency)
123 event_table[i].when = NOW + event_table[i].frequency;
124 else
125 {
126 event_table[i].name = NULL;
127 event_table[i].func = NULL;
128 event_table[i].arg = NULL;
129 event_table[i].active = false;
130
131 claro_state.event--;
132 }
133 }
134 }
135 }
136
137 /* returns the time the next event_run() should happen */
138 time_t
139 event_next_time (void)
140 {
141 unsigned int i;
142
143 if (event_time_min == -1)
144 {
145 for (i = 0; i < MAX_EVENTS; i++)
146 {
147 if (event_table[i].active && ((event_table[i].when < event_time_min) || event_time_min == -1))
148 event_time_min = event_table[i].when;
149 }
150 }
151
152 return event_time_min;
153 }
154
155 /* initializes event system */
156 void
157 event_init (void)
158 {
159 last_event_ran = NULL;
160 memset ((void *) event_table, 0, sizeof (event_table));
161 }
162
163 /* finds an event in the table */
164 unsigned int
165 event_find (EVH * func, void *arg)
166 {
167 unsigned int i;
168
169 for (i = 0; i < MAX_EVENTS; i++)
170 {
171 if ((event_table[i].func == func) && (event_table[i].arg == arg))
172 return i;
173 }
174
175 return -1;
176 }
177
178 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
179 * vim:ts=8
180 * vim:sw=8
181 * vim:noexpandtab
182 */