ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/event.C
Revision: 1.4
Committed: Tue Aug 28 17:08:12 2007 UTC (16 years, 9 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.3: +12 -18 lines
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# User Rev Content
1 pippijn 1.1 /*
2     * event.C: Event subsystem.
3 pippijn 1.2 * Rights to this code are documented in doc/pod/license.pod.
4 pippijn 1.1 *
5 pippijn 1.4 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org)
6 pippijn 1.1 */
7    
8 pippijn 1.4 static char const rcsid[] = "$Id: event.C,v 1.3 2007-07-21 13:23:21 pippijn Exp $";
9 pippijn 1.1
10     #include "atheme.h"
11     #include "internal.h"
12    
13 pippijn 1.4 char const *last_event_ran = NULL;
14 pippijn 1.1 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 pippijn 1.4 event_add (char const * const name, EVH * func, void *arg, time_t when)
20 pippijn 1.1 {
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 pippijn 1.4 system_state.event++;
41 pippijn 1.1
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 pippijn 1.4 return UINT_MAX;
50 pippijn 1.1 }
51    
52     /* adds an event to the table to be ran only once */
53     unsigned int
54 pippijn 1.4 event_add_once (char const * const name, EVH * func, void *arg, time_t when)
55 pippijn 1.1 {
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 pippijn 1.4 system_state.event++;
76 pippijn 1.1
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 pippijn 1.4 return UINT_MAX;
85 pippijn 1.1 }
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 pippijn 1.4 system_state.event--;
104 pippijn 1.1 }
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 pippijn 1.4 system_state.event--;
132 pippijn 1.1 }
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 pippijn 1.4 return UINT_MAX;
176 pippijn 1.1 }