ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/event.C
Revision: 1.6
Committed: Sat Sep 22 14:27:30 2007 UTC (16 years, 7 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +3 -1 lines
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

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