1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2000-2014 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file event.c |
23 |
* \brief Timer based event execution |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "ircd.h" |
30 |
#include "event.h" |
31 |
#include "client.h" |
32 |
#include "send.h" |
33 |
#include "numeric.h" |
34 |
#include "rng_mt.h" |
35 |
|
36 |
static dlink_list events; |
37 |
|
38 |
void |
39 |
event_add(struct event *ev, void *data) |
40 |
{ |
41 |
dlink_node *node; |
42 |
|
43 |
event_delete(ev); |
44 |
|
45 |
ev->data = data; |
46 |
ev->next = CurrentTime + ev->when; |
47 |
ev->enabled = 1; |
48 |
|
49 |
DLINK_FOREACH(node, events.head) |
50 |
{ |
51 |
struct event *e = node->data; |
52 |
|
53 |
if (e->next > ev->next) |
54 |
{ |
55 |
dlinkAddBefore(node, ev, &ev->node, &events); |
56 |
return; |
57 |
} |
58 |
} |
59 |
|
60 |
dlinkAddTail(ev, &ev->node, &events); |
61 |
} |
62 |
|
63 |
void |
64 |
event_addish(struct event *ev, void *data) |
65 |
{ |
66 |
if (ev->when >= 3) |
67 |
{ |
68 |
const time_t two_third = (2 * ev->when) / 3; |
69 |
|
70 |
ev->when = two_third + ((genrand_int32() % 1000) * two_third) / 1000; |
71 |
} |
72 |
|
73 |
event_add(ev, data); |
74 |
} |
75 |
|
76 |
void |
77 |
event_delete(struct event *ev) |
78 |
{ |
79 |
if (!ev->enabled) |
80 |
return; |
81 |
|
82 |
dlinkDelete(&ev->node, &events); |
83 |
ev->enabled = 0; |
84 |
} |
85 |
|
86 |
void |
87 |
event_run(void) |
88 |
{ |
89 |
static time_t last = 0; |
90 |
unsigned int len = 0; |
91 |
|
92 |
if (last == CurrentTime) |
93 |
return; |
94 |
last = CurrentTime; |
95 |
|
96 |
len = dlink_list_length(&events); |
97 |
while (len-- && dlink_list_length(&events)) |
98 |
{ |
99 |
struct event *e = events.head->data; |
100 |
|
101 |
if (e->next > CurrentTime) |
102 |
break; |
103 |
|
104 |
event_delete(e); |
105 |
|
106 |
e->handler(e->data); |
107 |
|
108 |
if (!e->oneshot) |
109 |
event_add(e, e->data); |
110 |
} |
111 |
} |
112 |
|
113 |
/* |
114 |
* void set_back_events(time_t by) |
115 |
* Input: Time to set back events by. |
116 |
* Output: None. |
117 |
* Side-effects: Sets back all events by "by" seconds. |
118 |
*/ |
119 |
void |
120 |
set_back_events(time_t by) |
121 |
{ |
122 |
dlink_node *node; |
123 |
|
124 |
DLINK_FOREACH(node, events.head) |
125 |
{ |
126 |
struct event *ev = node->data; |
127 |
ev->next -= by; |
128 |
} |
129 |
} |
130 |
|
131 |
/* |
132 |
* void show_events(struct Client *source_p) |
133 |
* |
134 |
* Input: Client requesting the event |
135 |
* Output: List of events |
136 |
* Side Effects: None |
137 |
*/ |
138 |
void |
139 |
show_events(struct Client *source_p) |
140 |
{ |
141 |
const dlink_node *node; |
142 |
|
143 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
144 |
"E :Operation Next Execution"); |
145 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
146 |
"E :-------------------------------------------"); |
147 |
|
148 |
DLINK_FOREACH(node, events.head) |
149 |
{ |
150 |
const struct event *ev = node->data; |
151 |
|
152 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
153 |
"E :%-28s %-4d seconds", |
154 |
ev->name, |
155 |
(int)(ev->next - CurrentTime)); |
156 |
} |
157 |
} |
158 |
|