Threaded/IOSelector.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 128 CC 3 SCAR 11.3
CC
3
Cov
0%
select
12.0
new
2.0
addRead
2.0
CRAP 3.1
1 =pod
2  
3 =begin classdoc
4  
5  
6 Provides a configurable I/O selector that permits
7 distinguishing between read, write, and exception
8 selectors. Used within the HTTP::Daemon::Threaded::Socket object
9 to simplify the process of managing events on I/O handles.
10 <p>
11 Copyright&copy 2006-2008, Dean Arnold, Presicient Corp., USA<br>
12 All rights reserved.
13 <p>
14 Licensed under the Academic Free License version 3.0, as specified at
15 <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>.
16  
17 @author D. Arnold
18 @since 2005-12-01
19 @self $_[0]
20  
21  
22  
23 =end classdoc
24  
25 =cut
26 package HTTP::Daemon::Threaded::IOSelector;
27  
28 use IO::Select;
29 use Time::HiRes qw(time);
30  
31 use strict;
32 use warnings;
33  
34 our $VERSION = '0.91';
35  
36 use constant HTTPD_SELECT_RD => 1;
37 use constant HTTPD_SELECT_WR => 2;
38 use constant HTTPD_SELECT_EX => 4;
39  
40 =pod
41  
42 =begin classdoc
43  
44 Constructor. Creates separate read, write, and exception
45 IO::Select objects.
46  
47 @return HTTP::Daemon::Threaded::IOSelector object
48  
49  
50 =end classdoc
51  
52 =cut
53 sub new {
54 my ($class, $interval) = @_;
55 #
56 # create 3 selectors: read, write, and exception,
57 # and accepts a static timeout to use for all
58 # select() ops
59 #
60 my $readsel = IO::Select->new();
61 my $writesel = IO::Select->new();
62 my $exceptsel = IO::Select->new();
63 return bless [ $readsel, $writesel, $exceptsel, $interval ], $class;
64 }
65  
66 =pod
67  
68 =begin classdoc
69  
70 Add a HTTP::Daemon::Threaded::Socket object to the read selector.
71  
72 @param $fd a HTTP::Daemon::Threaded::Socket
73 @return HTTP::Daemon::Threaded::IOSelector object
74  
75  
76 =end classdoc
77  
78 =cut
79 sub addRead {
80 $_[0]->[0]->add($_[1]);
81 return $_[0];
82 }
83  
84 =pod
85  
86 =begin classdoc
87  
88 Add a HTTP::Daemon::Threaded::Socket object to the write selector.
89  
90 @param $fd a HTTP::Daemon::Threaded::Socket
91 @return HTTP::Daemon::Threaded::IOSelector object
92  
93  
94 =end classdoc
95  
96 =cut
97 sub addWrite {
98 $_[0]->[1]->add($_[1]);
99 return $_[0];
100 }
101  
102 =pod
103  
104 =begin classdoc
105  
106 Add a HTTP::Daemon::Threaded::Socket object to the exception selector.
107  
108 @param $fd a HTTP::Daemon::Threaded::Socket
109 @return HTTP::Daemon::Threaded::IOSelector object
110  
111  
112 =end classdoc
113  
114 =cut
115 sub addExcept {
116 $_[0]->[2]->add($_[1]);
117 return $_[0];
118 }
119  
120 =pod
121  
122 =begin classdoc
123  
124 Add a HTTP::Daemon::Threaded::Socket object to the read and exception selector.
125  
126 @param $fd a HTTP::Daemon::Threaded::Socket
127 @return HTTP::Daemon::Threaded::IOSelector object
128  
129  
130 =end classdoc
131  
132 =cut
133 sub addNoWrite {
134 $_[0]->[0]->add($_[1]);
135 $_[0]->[2]->add($_[1]);
136 return $_[0];
137 }
138  
139 =pod
140  
141 =begin classdoc
142  
143 Add a HTTP::Daemon::Threaded::Socket object to the all selectors.
144  
145 @param $fd a HTTP::Daemon::Threaded::Socket
146 @return HTTP::Daemon::Threaded::IOSelector object
147  
148  
149 =end classdoc
150  
151 =cut
152 sub addAll {
153 $_[0]->[0]->add($_[1]);
154 $_[0]->[1]->add($_[1]);
155 $_[0]->[2]->add($_[1]);
156 return $_[0];
157 }
158  
159 =pod
160  
161 =begin classdoc
162  
163 Remove a HTTP::Daemon::Threaded::Socket object from the read selector.
164  
165 @param $fd a HTTP::Daemon::Threaded::Socket
166 @return HTTP::Daemon::Threaded::IOSelector object
167  
168  
169 =end classdoc
170  
171 =cut
172 sub removeRead {
173 $_[0]->[0]->remove($_[1]);
174 return $_[0];
175 }
176  
177 =pod
178  
179 =begin classdoc
180  
181 Remove a HTTP::Daemon::Threaded::Socket object from the write selector.
182  
183 @param $fd a HTTP::Daemon::Threaded::Socket
184 @return HTTP::Daemon::Threaded::IOSelector object
185  
186  
187 =end classdoc
188  
189 =cut
190 sub removeWrite {
191 $_[0]->[1]->remove($_[1]);
192 return $_[0];
193 }
194  
195 =pod
196  
197 =begin classdoc
198  
199 Remove a HTTP::Daemon::Threaded::Socket object from the exception selector.
200  
201 @param $fd a HTTP::Daemon::Threaded::Socket
202 @return HTTP::Daemon::Threaded::IOSelector object
203  
204  
205 =end classdoc
206  
207 =cut
208 sub removeExcept {
209 $_[0]->[2]->remove($_[1]);
210 return $_[0];
211 }
212  
213 =pod
214  
215 =begin classdoc
216  
217 Remove a HTTP::Daemon::Threaded::Socket object from the read and exception selectors.
218  
219 @param $fd a HTTP::Daemon::Threaded::Socket
220 @return HTTP::Daemon::Threaded::IOSelector object
221  
222  
223 =end classdoc
224  
225 =cut
226 sub removeNoWrite {
227 $_[0]->[0]->remove($_[1]);
228 $_[0]->[2]->remove($_[1]);
229 return $_[0];
230 }
231  
232 =pod
233  
234 =begin classdoc
235  
236 Remove a HTTP::Daemon::Threaded::Socket object from all the selectors.
237  
238 @param $fd a HTTP::Daemon::Threaded::Socket
239 @return HTTP::Daemon::Threaded::IOSelector object
240  
241  
242 =end classdoc
243  
244 =cut
245 sub removeAll {
246  
247 # my @frame = caller(1);
248  
249 # print STDERR 'IOSelector removing for ',
250 # join('', $frame[3], ':', $frame[2]), "\n";
251  
252 $_[0]->[0]->remove($_[1]);
253 $_[0]->[1]->remove($_[1]);
254 $_[0]->[2]->remove($_[1]);
255 return $_[0];
256 }
257  
258 =pod
259  
260 =begin classdoc
261  
262 Return the read selector.
263  
264 @return IO::Select object
265  
266  
267 =end classdoc
268  
269 =cut
270 sub getRead { return $_[0]->[0]; }
271  
272 =pod
273  
274 =begin classdoc
275  
276 Return the write selector.
277  
278 @return IO::Select object
279  
280  
281 =end classdoc
282  
283 =cut
284 sub getWrite { return $_[0]->[1]; }
285  
286 =pod
287  
288 =begin classdoc
289  
290 Return the exception selector.
291  
292 @return IO::Select object
293  
294  
295 =end classdoc
296  
297 =cut
298 sub getExcept { return $_[0]->[2]; }
299  
300 =pod
301  
302 =begin classdoc
303  
304 Return all selectors.
305  
306 @returnlist read, write, exception IO::Select objects
307  
308  
309 =end classdoc
310  
311 =cut
312 sub getAll { return ( @{$_[0]} ); }
313  
314 =pod
315  
316 =begin classdoc
317  
318 Set the select() timeout
319  
320 @param $timeout number of seconds to select()
321 @return HTTP::Daemon::Threaded::IOSelector object
322  
323  
324 =end classdoc
325  
326 =cut
327 sub setTimeout { $_[0]->[3] = $_[1]; return $_[0]; }
328  
329 =pod
330  
331 =begin classdoc
332  
333 Return the current select() timeout value
334  
335 @return timeout value
336  
337  
338 =end classdoc
339  
340 =cut
341 sub getTimeout { return $_[0]->[3] = $_[1]; }
342  
343 =pod
344  
345 =begin classdoc
346  
347 Wait up to the configured timeout for an event on any of
348 the HTTP::Daemon::Threaded::Socket objects installed in any of the read, write,
349 or exception IO::Select objects. When events are detected,
350 the handleSocketEvent() method on the HTTP::Daemon::Threaded::Socket object
351 is called, with a bit mask indicating which of the events was
352 detected.
353  
354 @return elapsed time in the function, in seconds
355  
356  
357 =end classdoc
358  
359 =cut
360 sub select {
361 my $obj = shift;
362  
363 my $start = time();
364 $! = undef;
365 my ($read, $write, $except) = IO::Select->select(
366 $obj->[0],
367 $obj->[1],
368 $obj->[2],
369 $obj->[3] );
370  
371 if ($! ne '') {
372 print STDERR "select() failure: $!\n";
373 print STDERR join("\n",
374 $obj->[0]->as_string(),
375 $obj->[1]->as_string(),
376 $obj->[2]->as_string()), "\n";
377 }
378 #
379 # returns undef if no events
380 #
381 return time() - $start
382 unless $read;
383  
384 # print STDERR "IO::Select failed after ", time() - $start, " secs:\n",
385 # join("\n", $obj->[0]->as_string(), $obj->[1]->as_string(), $obj->[2]->as_string()),
386 # "\n";
387 #
388 # consolidate selected objects, with a flag indicating which
389 # events they have
390 #
391 my %ready = ();
392 my %ready_flags = ();
393  
394 $ready_flags{$_} = HTTPD_SELECT_RD,
395 $ready{$_} = $_
396 foreach (@$read);
397  
398 $ready_flags{$_} |= HTTPD_SELECT_WR,
399 $ready{$_} = $_
400 foreach (@$write);
401  
402 $ready_flags{$_} |= HTTPD_SELECT_EX,
403 $ready{$_} = $_
404 foreach (@$except);
405  
406 $ready{$_}->handleSocketEvent($ready_flags{$_}) foreach (keys %ready);
407 #
408 # returns the time spent here..
409 #
410 return time() - $start;
411 }