-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathqueue.php
More file actions
executable file
·306 lines (246 loc) · 9.95 KB
/
queue.php
File metadata and controls
executable file
·306 lines (246 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/*
+---------------------------------------------------------------------------
| PHP-IRC v2.2.1 Service Release
| ========================================================
| by Manick
| (c) 2001-2005 by http://www.phpbots.org/
| Contact: manick@manekian.com
| irc: #manekian@irc.rizon.net
| ========================================
+---------------------------------------------------------------------------
| > queue module
| > Module written by Manick
| > Module Version Number: 2.2.0
+---------------------------------------------------------------------------
| > This program is free software; you can redistribute it and/or
| > modify it under the terms of the GNU General Public License
| > as published by the Free Software Foundation; either version 2
| > of the License, or (at your option) any later version.
| >
| > This program is distributed in the hope that it will be useful,
| > but WITHOUT ANY WARRANTY; without even the implied warranty of
| > MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| > GNU General Public License for more details.
| >
| > You should have received a copy of the GNU General Public License
| > along with this program; if not, write to the Free Software
| > Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+---------------------------------------------------------------------------
| Changes
| =======-------
| > If you wish to suggest or submit an update/change to the source
| > code, email me at manick@manekian.com with the change, and I
| > will look to adding it in as soon as I can.
+---------------------------------------------------------------------------
*/
/* This module is my response to a big problem. PHP-IRC, on idling, would use
* 2.0% or more of the CPU on a 500mhz machine. This annoyed me, and I decided
* to do something about it. So, I changed the way that PHP-IRC works, from a
* "round-robin" approach to an "interrupt" type approach. Whenever something
* happens, say, new data is read from a socket, a process Queue is added with
* a pointer to the function that reads input for that socket; or for
* another example, if a file transfer is in effect, and new data is read from
* that socket, but we also have a dcc chat going, we will only handle data for
* the file transfer, instead of wasting CPU cycles on the dcc chat, like is
* currently done in <=2.1.1. I've learned a bit more about timers and socket
* timeouts since then. Also, we have to handle timers in a different way now.
* There will still be a "timers" class, for re-occuring processes, but the
* next timer is inserted into this process queue, and the callback for the timer
* will be a pointer to the timer class which handles the timer. Then, the timer
* class will do the appropriate stuff for the timer, and then add the next timer
* into the process Queue. --Manick
*
* P.S.; I never knew this would change so much code... I figured adding in select()
* timeouts would be a piece of cake... until I realized that my whole framework
* was incompatible with the idea. What a pain in the ass -_-.
*/
/* Module Written 11/30/04 by Manick */
class processQueue
{
private $numQueued;
private $queuedItems;
private $currProc;
function __construct()
{
$this->queuedItemsArray = array();
$this->queuedItems = NULL;
$this->currProc = NULL;
$this->numQueued = 0;
}
public function getNumQueued()
{
return $this->numQueued;
}
public static function getMicroTime()
{
return microtime(true);
}
/* Only allow removal of entire irc class (as in we shut down the bot.. otherwise, we don't
want to deal with shutting down specific queues during the queueing process. Have the callbacks
handle that themselves. We only have to worry when the callbacks won't exist anymore, as when an
irc bot is shut down, and the ircclass is discarded
*/
public function removeOwner($class)
{
$next = NULL;
for ($queue = $this->queuedItems; $queue != NULL;) {
$next = $queue->next;
if ($queue->owner === $class) {
$this->removeQueue($queue);
}
$queue = $next;
}
}
/* Remove reference to queued item, let PHP5 do the rest */
private function removeQueue($item)
{
if ($item->prev == NULL) {
$this->queuedItems = $item->next;
if ($item->next != NULL) {
$item->next->prev = NULL;
}
} else {
$item->prev->next = $item->next;
if ($item->next != NULL) {
$item->next->prev = $item->prev;
}
}
$item->removed = true;
unset($item->args);
unset($item->owner);
unset($item->callBack_class);
unset($item->next);
unset($item->prev);
unset($item);
$this->numQueued--;
}
/* Add an item to the process queue */
public function addQueue($owner, $class, $function, $args, $nextRunTime)
{
// echo "Queue Added: $function with $nextRunTime\n";
if ($function == "" || $function == NULL) {
return false;
}
if (!is_object($class)) {
$class = null;
}
$nextRunTime = floatval($nextRunTime);
$queue = new queueItem;
$queue->args = $args;
$queue->owner = $owner;
$queue->removed = false;
$queue->callBack_class = $class;
$queue->callBack_function = $function;
$queue->nextRunTime = self::getMicroTime() + $nextRunTime;
//Now insert as sorted into queue
$prev = NULL;
for ($item = $this->queuedItems; $item != NULL; $item = $item->next) {
if ($queue->nextRunTime < $item->nextRunTime) {
break;
}
$prev = $item;
}
if ($item == NULL) {
if ($prev == NULL) {
$queue->next = NULL;
$queue->prev = NULL;
$this->queuedItems = $queue;
} else {
$queue->next = NULL;
$queue->prev = $prev;
$prev->next = $queue;
}
} else {
if ($item->prev == NULL) {
$queue->next = $this->queuedItems;
$queue->prev = NULL;
$item->prev = $queue;
$this->queuedItems = $queue;
} else {
$queue->next = $item;
$queue->prev = $item->prev;
$item->prev = $queue;
$queue->prev->next = $queue;
}
}
//Okay, we're inserted, return true;
$this->numQueued++;
return true;
}
public function displayQueue()
{
//Used for debug
echo "Current Time: " . self::getMicroTime() . "\n";
echo "\n\n";
for ($i = $this->queuedItems; $i != NULL; $i = $i->next) {
echo $i->callBack_function . "-" . $i->nextRunTime . "\n";
echo "---" . "Prev: " . $i->prev . " Next: " . $i->next . " Me: " . $i . "\n";
}
echo "\n\n";
}
/* Handle the process queue, return the time until the next item */
public function handle()
{
// Handle all items with $queue->nextRunTime < getMicroTime(), then return with time until next item must
// be run
// Populate a runQueue with all current items that need to be run. We need to do this because some of these
// callback functions might add another process to the queue, and if the runtime is < 0, we would run that item
// instead of timing out before we do. If we have something like a file transfer, this could be a bad thing.
$runQueue = array();
$time = self::getMicroTime();
for ($item = $this->queuedItems; $item != NULL; $item = $item->next) {
if ($item->nextRunTime <= $time) {
$runQueue[] = $item;
} else {
break;
}
}
//Okay, now run each item.
foreach ($runQueue AS $index => $item) {
if (!is_object($item) || $item->removed === true) {
if (is_object($item)) {
unset($item);
}
continue;
}
self::handleQueueItem($item);
}
unset($runQueue);
//Return time until next item needs to be run, or true if there are no queued items
//Hmm, true returned here, means we'll just sleep for like an hour or something until data
//is recieved from the sockets, because we have no active timers
if ($this->queuedItems == null) {
return true;
}
//Get new time
$time = self::getMicroTime();
$timeTillNext = $this->queuedItems->nextRunTime - $time;
if ($timeTillNext < 0) {
$timeTillNext = 0;
}
//When zero is returned, we'll always sleep at least 50000 usec in the socket class anyway
return $timeTillNext;
}
/* Specific function to deal with queued items */
private function handleQueueItem($item)
{
$this->currTimer = $item;
$class = $item->callBack_class;
$func = $item->callBack_function;
//Call the callback function! Now the callback function will check all possible triggers,
//such as socket input, etc, and add new queued items if it needs more processing/other processing
if ($class == null) {
$status = $func($item->args);
} else {
$status = $class->$func($item->args);
}
//If true is returned from the function, then keep the bitch in the queue. This is useful when a
//function has not completed processing (i.e., irc->connection waiting on socket class to return
//the fact that its connected.
if ($item->removed !== true && $status !== true) {
self::removeQueue($item);
}
}
}
?>