1 |
adx |
30 |
/* |
2 |
michael |
4130 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 2000-2014 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
michael |
4130 |
* 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 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
michael |
4130 |
* USA |
20 |
adx |
30 |
*/ |
21 |
|
|
|
22 |
michael |
4130 |
/*! \file event.c |
23 |
|
|
* \brief Timer based event execution |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
michael |
1011 |
#include "list.h" |
29 |
adx |
30 |
#include "ircd.h" |
30 |
|
|
#include "event.h" |
31 |
|
|
#include "client.h" |
32 |
|
|
#include "send.h" |
33 |
|
|
#include "numeric.h" |
34 |
michael |
2680 |
#include "rng_mt.h" |
35 |
adx |
30 |
|
36 |
michael |
4094 |
static dlink_list events; |
37 |
michael |
2680 |
|
38 |
michael |
4094 |
void |
39 |
|
|
event_add(struct event *ev, void *data) |
40 |
|
|
{ |
41 |
|
|
dlink_node *node; |
42 |
adx |
30 |
|
43 |
michael |
4094 |
event_delete(ev); |
44 |
michael |
3599 |
|
45 |
michael |
4094 |
ev->data = data; |
46 |
|
|
ev->next = CurrentTime + ev->when; |
47 |
|
|
ev->enabled = 1; |
48 |
michael |
3599 |
|
49 |
michael |
4094 |
DLINK_FOREACH(node, events.head) |
50 |
|
|
{ |
51 |
|
|
struct event *e = node->data; |
52 |
michael |
3599 |
|
53 |
michael |
4094 |
if (e->next > ev->next) |
54 |
adx |
30 |
{ |
55 |
michael |
4094 |
dlinkAddBefore(node, ev, &ev->node, &events); |
56 |
adx |
30 |
return; |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
|
60 |
michael |
4094 |
dlinkAddTail(ev, &ev->node, &events); |
61 |
adx |
30 |
} |
62 |
|
|
|
63 |
|
|
void |
64 |
michael |
4094 |
event_addish(struct event *ev, void *data) |
65 |
adx |
30 |
{ |
66 |
michael |
4094 |
if (ev->when >= 3) |
67 |
adx |
30 |
{ |
68 |
michael |
4094 |
const time_t two_third = (2 * ev->when) / 3; |
69 |
michael |
4126 |
|
70 |
michael |
4094 |
ev->when = two_third + ((genrand_int32() % 1000) * two_third) / 1000; |
71 |
adx |
30 |
} |
72 |
|
|
|
73 |
michael |
4094 |
event_add(ev, data); |
74 |
adx |
30 |
} |
75 |
|
|
|
76 |
|
|
void |
77 |
michael |
4094 |
event_delete(struct event *ev) |
78 |
adx |
30 |
{ |
79 |
michael |
4094 |
if (!ev->enabled) |
80 |
|
|
return; |
81 |
|
|
|
82 |
|
|
dlinkDelete(&ev->node, &events); |
83 |
|
|
ev->enabled = 0; |
84 |
adx |
30 |
} |
85 |
|
|
|
86 |
michael |
4094 |
void |
87 |
|
|
event_run(void) |
88 |
adx |
30 |
{ |
89 |
michael |
4094 |
static time_t last = 0; |
90 |
michael |
4118 |
unsigned int len = 0; |
91 |
adx |
30 |
|
92 |
michael |
4094 |
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 |
michael |
4099 |
if (e->next > CurrentTime) |
102 |
michael |
4094 |
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 |
adx |
30 |
} |
112 |
|
|
|
113 |
|
|
/* |
114 |
michael |
4094 |
* 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 |
adx |
30 |
*/ |
119 |
|
|
void |
120 |
michael |
4094 |
set_back_events(time_t by) |
121 |
adx |
30 |
{ |
122 |
michael |
4094 |
dlink_node *node; |
123 |
|
|
|
124 |
|
|
DLINK_FOREACH(node, events.head) |
125 |
|
|
{ |
126 |
|
|
struct event *ev = node->data; |
127 |
|
|
ev->next -= by; |
128 |
|
|
} |
129 |
adx |
30 |
} |
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 |
michael |
4094 |
const dlink_node *node; |
142 |
adx |
30 |
|
143 |
michael |
3589 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
144 |
michael |
4432 |
"E :Operation Next Execution"); |
145 |
michael |
3589 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
146 |
michael |
4432 |
"E :---------------------------------------------"); |
147 |
adx |
30 |
|
148 |
michael |
4094 |
DLINK_FOREACH(node, events.head) |
149 |
|
|
{ |
150 |
|
|
const struct event *ev = node->data; |
151 |
adx |
30 |
|
152 |
michael |
4094 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
153 |
michael |
4432 |
"E :%-30s %-4d seconds", |
154 |
michael |
4094 |
ev->name, |
155 |
|
|
(int)(ev->next - CurrentTime)); |
156 |
adx |
30 |
} |
157 |
|
|
} |
158 |
michael |
4094 |
|