blib/lib/IO/Events.pm

filter: Statementstmt 72.6% 430 / 592 Branchbran 43.4% 145 / 334 Conditioncond 38.4% 25 / 65 MC/DCmcdc 15.0% 6 / 40 Subroutinesub 75.2% 67 / 89 Podpod n/a totaltotal 60.0% 673 / 1120 CC 201 SCAR 48.1
CC
201
Cov
61%
yield
548.2
on_error
30.0
new
30.0
CRAP 122.3
↑ Summary
1 #
2 # Copyright (c) 2004 catpipe Systems ApS
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26  
27 # $Id: Events.pm,v 1.32 2007/03/28 08:01:59 dk Exp $
28 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 package IO::Events;
31 2 ▶use vars qw($VERSION $FORK_MODE @loops);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 $VERSION=0.6;
33  
34 # Master loop object
35 package IO::Events::Loop;
36 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use Errno qw(EAGAIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use POSIX qw(sys_wait_h exit);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 sub new
44 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
45 2 my $class = shift;
46 2 my $obj = bless {
47 # options
48 debug => 0,
49 timeout => 50, # seconds
50 waitpid => 1,
51 @_,
52  
53 # private fields
54 read => '',
55 write => '',
56 exc => '',
57 processes => {},
58 filenos => {},
59 ids => {},
60 timers => [],
61 }, $class;
62 2 push @IO::Events::loops, $obj;
63 2 return $obj;
64 }
65  
66 sub yield
67 7 ▶{
Subroutine yield: called CC=60 SCAR=63.1
68 7 my ( $self, %profile) = @_;
69  
70 my ( $ir, $iw, $ie) = (
71 $self->{read},
72 $profile{block_write} ? '' : $self-> {write},
73 $profile{block_exc} ? '' : $self-> {exc}
74 7 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$profile{'block_write'} ? : 0 7 7
$profile{'block_exc'} ? : 0 7 7
75  
76 7 ▶ my $timeout = exists $profile{timeout} ? $profile{timeout} : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
exists $profile{'timeout'} ? : 0 7 7
77 7 ▶ if ( @{$self->{timers}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"timers"};}) 1 6 7
78 1 my $time = time;
79 1 for my $timer (@{$self->{timers}}) {
80 1 ▶ next unless $timer->{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $timer->{"active"} 0 1 1
81 1 my $sleep = $timer->{alert} - $time;
82 1 ▶ $timeout = $sleep if $timeout > $sleep;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > $sleep 1 0 1
83 }
84 1 ▶ $timeout = 0 if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 0 1 1
85 }
86 7 my $n = select( $ir, $iw, $ie, $timeout);
87  
88 7 ▶ if ( @{$self->{timers}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"timers"};}) 1 6 7
89 1 my $time = time;
90 1 for my $timer (@{$self->{timers}}) {
91 1 ▶ next if not $timer->{active} or $time < $timer->{alert};
Branch: 1 branch
BranchTrueFalseTotal
if not $timer->{"active"} or $time < $timer->{"alert"} 0 1 1
Condition33%1/3
not $timer->{"active"} or $time < $timer->{"alert"}
l!l&&r!l&&!r
001
MC/DC0%0/2
not $timer->{"active"} or $time < $timer->{"alert"}
not $timer->{"active"} $time < $timer->{"alert"}
Truth table33%1/3
not(A) or B
A= not $timer->{"active"}B= $time < $timer->{"alert"}
ABresult
1X1
011
000
92 1 $timer-> notify;
93 }
94 }
95
96 7 ▶ unless ( $n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n > 0) 1 6 7
97 1 ▶ if ( $self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 1 1
98 0 print STDERR "IO::Events: empty select";
99 0 ▶ if ( $n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
100 0 print STDERR " error:$!";
101 }
102 0 print STDERR "\n";
103 }
104 1 goto WAITPID;
105 }
106  
107 6 my $i;
108 6 my $lnx = (sort { $a <=> $b } map { length } ( $ir, $iw, $ie))[-1] * 8;
109 6 ▶ for ( $i = 0; $i < $lnx; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $lnx) 80 6 86
110 80 my ( $r, $w, $e) = ( vec( $ir, $i, 1), vec( $iw, $i, 1), vec( $ie, $i, 1));
111 80 ▶ next unless $r || $w || $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $r or $w or $e 71 9 80
Condition83%5/6
$r or $w or $e
l!l&&r!l&&!r
9071
$r or $w
l!l&&r!l&&!r
6371
MC/DC66%2/3
$r or $w or $e
$r $w $e
Truth table75%3/4
A or B or C
A= $rB= $wC= $e
ABCresult
1XX1
01X1
0011
0000
112 9 my $task;
113 9 ▶ if ( exists $self-> {filenos}-> {$i} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'filenos'}{$i} and exists $self->{'ids'}{$self->{'filenos'}{$i}}) { } 9 0 9
Condition33%1/3
exists $self->{"filenos"}{$i} and exists $self->{"ids"}{$self->{"filenos"}{$i}}
!ll&&!rl&&r
009
MC/DC0%0/2
exists $self->{"filenos"}{$i} and exists $self->{"ids"}{$self->{"filenos"}{$i}}
exists $self->{"filenos"}{$i} exists $self->{"ids"}{$self->{"filenos"}{$i}}
Truth table33%1/3
A and B
A= exists $self->{"filenos"}{$i}B= exists $self->{"ids"}{$self->{"filenos"}{$i}}
ABresult
0X0
100
111
114 exists $self->{ids}->{$self-> {filenos}-> {$i}}) {
115 9 $task = $self->{ids}->{$self-> {filenos}-> {$i}};
116 } else {
117 print STDERR "IO::Events: runaway handle $i/$self->{filenos}->{$i}\n"
118 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
119 0 $self-> error( undef, 'select');
120 0 next;
121 }
122 9 ▶ if ( $task-> {callback} and not $task-> {dead} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->{"callback"} and not $task->{"dead"}) 2 7 9
Condition66%2/3
$task->{"callback"} and not $task->{"dead"}
!ll&&!rl&&r
702
MC/DC50%1/2
$task->{"callback"} and not $task->{"dead"}
$task->{"callback"} not $task->{"dead"}
Truth table66%2/3
A and not(B)
A= $task->{"callback"}B= not $task->{"dead"}
ABresult
0X0
100
111
123 2 $task-> {callback}-> ( $task, $r, $w, $e);
124 }
125  
126 9 ▶ if ( $r and not $task-> {dead}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r and not $task->{"dead"}) 6 3 9
Condition66%2/3
$r and not $task->{"dead"}
!ll&&!rl&&r
306
MC/DC50%1/2
$r and not $task->{"dead"}
$r not $task->{"dead"}
Truth table66%2/3
A and not(B)
A= $rB= not $task->{"dead"}
ABresult
0X0
100
111
127 6 my $nbytes;
128 6 ▶ if ( $task-> {read} > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->{"read"} > -1) 3 3 6
129 $nbytes = sysread( $task->{handle}, $task->{read_buffer},
130 3 ▶ $profile{block_read} ? 0 : 65536, length ($task->{read_buffer}));
Branch: 1 branch
BranchTrueFalseTotal
$profile{'block_read'} ? : 0 3 3
131 }
132 6 ▶ if ( $task->{read} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->{'read'} > 0) { } 3 3 6
133 print STDERR "IO::Events: # $i read $nbytes bytes\n"
134 3 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 3 3
135 3 ▶ unless ( defined $nbytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbytes) 0 3 3
136 0 ▶ $self-> error( $task, 'read') unless $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 11 0 0 0
137 0 next;
138 }
139 } else {
140 3 ▶ $nbytes = 1 unless defined $nbytes; # simulate read
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nbytes 3 0 3
141 print STDERR "IO::Events: # $i simulated read $nbytes\n"
142 3 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 3 3
143 }
144 6 ▶ next if $profile{block_read};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"block_read"} 0 6 6
145 6 ▶ if ( $nbytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbytes > 0) 6 0 6
146 6 $task-> notify('on_read');
147 6 next;
148 }
149 0 ▶ $task-> destroy unless $task-> {pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $task->{"pid"} 0 0 0
150 }
151  
152 3 ▶ if ( $w and not $task-> {dead}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w and not $task->{"dead"}) 3 0 3
Condition33%1/3
$w and not $task->{"dead"}
!ll&&!rl&&r
003
MC/DC0%0/2
$w and not $task->{"dead"}
$w not $task->{"dead"}
Truth table33%1/3
A and not(B)
A= $wB= not $task->{"dead"}
ABresult
0X0
100
111
153 3 ▶ unless ( length $task->{write_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $task->{"write_buffer"}) 3 0 3
154 3 vec( $self->{write}, $task-> {fileno}, 1) = 0;
155 3 $task-> notify('on_write');
156 3 next;
157 }
158 0 my $nbytes = syswrite( $task->{handle}, $task->{write_buffer});
159 print STDERR "IO::Events: # $i wrote $nbytes bytes\n"
160 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
161 0 ▶ unless ( defined $nbytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbytes) 0 0 0
162 0 ▶ $self-> error( $task, 'write') unless $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 11 0 0 0
163 0 next;
164 }
165 0 ▶ if ( $nbytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbytes > 0) 0 0 0
166 0 substr( $task->{write_buffer}, 0, $nbytes) = '';
167 0 ▶ unless ( length $task->{write_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $task->{"write_buffer"}) 0 0 0
168 0 vec( $self->{write}, $task-> {fileno}, 1) = 0;
169 0 $task-> notify('on_write');
170 }
171 0 next;
172 }
173 }
174
175 0 ▶ if ( $e and not $task-> {dead}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e and not $task->{"dead"}) 0 0 0
Condition0%0/3
$e and not $task->{"dead"}
!ll&&!rl&&r
000
MC/DC0%0/2
$e and not $task->{"dead"}
$e not $task->{"dead"}
Truth table0%0/3
A and not(B)
A= $eB= not $task->{"dead"}
ABresult
0X0
100
111
176 0 ▶ print STDERR "IO::Events: exception $i\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
177 0 $task-> notify('on_exception');
178 }
179 }
180  
181 # close processes
182 WAITPID:
183 7 ▶ if ( $self-> {waitpid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"waitpid"}) 7 0 7
184 7 ▶ while (($_ = waitpid(-1,WNOHANG)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($_ = waitpid(-1, 1)) > 0) 0 7 7
185 0 ▶ next unless $self->{processes}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"processes"}{$_} 0 0 0
186 0 my @tasks = map { $self-> {ids}-> {$_}} @{$self->{processes}-> {$_}};
187 # read leftovers
188 0 for my $task ( @tasks) {
189 0 ▶ if ( $task-> can_read && $task-> {read} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->can_read and $task->{"read"} > 0) 0 0 0
Condition0%0/3
$task->can_read and $task->{"read"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$task->can_read and $task->{"read"} > 0
$task->can_read $task->{"read"} > 0
Truth table0%0/3
A and B
A= $task->can_readB= $task->{"read"} > 0
ABresult
0X0
100
111
190 0 my $notify;
191 0 while ( 1) {
192 my $nbytes = sysread( $task->{handle},
193 $task->{read_buffer}, 65536,
194 0 length ($task->{read_buffer}));
195 0 ▶ unless ( defined $nbytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbytes) 0 0 0
196 0 ▶ $self-> error( $task, 'read')
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 11 0 0 0
197 unless $! == EAGAIN;
198 0 last;
199 }
200 0 $notify += $nbytes;
201 0 ▶ last unless $nbytes;
Branch: 1 branch
BranchTrueFalseTotal
unless $nbytes 0 0 0
202 }
203 0 ▶ $task-> notify('on_read') if $notify;
Branch: 1 branch
BranchTrueFalseTotal
if $notify 0 0 0
204 }
205 # XXX if $task-> can_exception ... read URG bytes?
206 0 $task->{exitcode} = $?;
207 0 $task->{finished} = 1;
208 }
209 0 for my $task ( @tasks) {
210 0 $task-> destroy;
211 }
212 }
213 }
214  
215 7 return $n;
216 }
217  
218 sub handles
219 0 ▶{
Subroutine handles: not called CC=1 SCAR=6.9
220 0 return scalar(keys %{$_[0]->{ids}});
221 }
222  
223 sub flush
224 0 ▶{
Subroutine flush: not called CC=1 SCAR=6.9
225 0 shift-> yield( block_read => 1, block_exc => 1, timeout => 0);
226 }
227  
228 sub error
229 0 ▶{
Subroutine error: not called CC=2 SCAR=17.9
230 0 my ( $self, $task, $condition) = @_;
231 0 ▶ $task-> notify('on_error', $condition, $!) if $task;
Branch: 1 branch
BranchTrueFalseTotal
if $task 0 0 0
232 }
233  
234 sub on_fork
235 0 ▶{
Subroutine on_fork: not called CC=1 SCAR=6.9
236 0 $IO::Events::FORK_MODE = 1;
237 0 shift-> DESTROY;
238 0 $IO::Events::FORK_MODE = undef;
239 }
240  
241 sub DESTROY
242 2 ▶{
Subroutine DESTROY: called CC=7 SCAR=20.2
243 2 my $self = $_[0];
244 2 ▶ return if $self->{dead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dead"} 0 2 2
245 2 for ( values %{$self->{ids}}) {
246 10 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 10 10
247 10 ▶ $_->{dead} = 1 if $IO::Events::FORK_MODE;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Events::FORK_MODE 0 10 10
248 10 $_-> destroy;
249 }
250 2 for ( @{$self->{timers}}) {
251 1 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 1 1
252 1 $_->{dead} = 1;
253 }
254 0 @IO::Events::loops = grep { $self != $_ } @IO::Event::IPC::loops;
255 2 $self-> {dead} = 1;
256 }
257  
258 END
259 2 ▶{
Subroutine END: called CC=3 SCAR=11.1
260 2 for ( @IO::Events::loops) {
261 2 eval { $_->DESTROY };
262 2 ▶ warn "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
263 }
264 2 @IO::Events::loops = ();
265 }
266  
267 # Single task
268 package IO::Events::Handle;
269 2 ▶use vars qw(@ISA %events);
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 2 ▶use Errno qw(EAGAIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
272  
273 2 ▶use constant SINGLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 2 ▶use constant MULTIPLE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
275  
276 2 %events = (
277 on_read => SINGLE,
278 on_write => SINGLE,
279 on_exception => SINGLE,
280 on_close => MULTIPLE,
281 on_create => MULTIPLE,
282 on_error => MULTIPLE,
283 );
284  
285 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
286  
287 sub new
288 10 ▶{
Subroutine new: called CC=14 SCAR=27.1
289 10 my $class = shift;
290 10 my $self = bless {
291 auto_close => 1,
292 finished => 0,
293 exitcode => 0,
294 read_buffer => '',
295 write_buffer => '',
296 write => 0,
297 read => 0,
298 exception => 0,
299 pid => undef,
300 @_,
301 }, $class;
302 10 ▶ $self->{handle} = IO::Handle-> new() unless defined $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"handle"} 0 10 10
303 10 for ( qw(owner)) {
304 10 ▶ die "No `$_' field" unless defined $self-> {$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$_} 0 10 10
305 }
306 10 ▶ $self-> {id} = "$self" unless defined $self-> {id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"id"} 9 1 10
307 10 my $owner = $self->{owner};
308 10 ▶ die "Id `$self->{id}` already present" if exists $owner->{ids}->{$self->{id}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $owner->{"ids"}{$self->{"id"}} 0 10 10
309 10 my $fno = fileno( $self->{handle});
310 10 ▶ die "Cannot read fileno() from handle" unless defined $fno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fno 0 10 10
311 10 $self-> {fileno} = $fno;
312 10 ▶ unless ( $self-> {nonblock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"nonblock"}) 10 0 10
313 10 my $fl;
314 10 $fl = fcntl( $self->{handle}, F_GETFL, 0);
315 10 ▶ die "$!" unless defined $fl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fl 0 10 10
316 10 ▶ fcntl( $self->{handle}, F_SETFL, $fl|O_NONBLOCK) or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $self->{"handle"}, 4, $fl | 2048 or die "$!" 10 0 10
317 }
318 10 ▶ if ($self-> {write}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"write"}) 1 9 10
319 1 vec( $owner-> {write}, $fno, 1) = 1;
320 #print "write\n";
321 }
322 10 ▶ if ($self-> {read}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"read"}) 7 3 10
323 7 vec( $owner-> {read}, $fno, 1) = 1;
324 #print "read\n";
325 }
326 10 ▶ if ($self-> {exception}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exception"}) 0 10 10
327 0 vec( $owner-> {exc}, $fno, 1) = 1;
328 }
329 10 $owner-> {filenos}-> {$fno} = $self-> {id};
330 10 ▶ push @{$owner-> {processes}-> {$self->{pid}}}, $self-> {id} if defined $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"pid"} 2 8 10
331 10 $owner-> {ids}-> {$self->{id}} = $self;
332 10 $self-> notify('on_create');
333 10 return $self;
334 }
335  
336 sub can_read
337 0 ▶{
Subroutine can_read: not called CC=2 SCAR=17.9
338 0 ▶ return vec( $_[0]->{owner}->{read}, $_[0]-> {fileno}, 1) unless $#_;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 0 0
339 0 vec( $_[0]->{owner}->{read}, $_[0]-> {fileno}, 1) = $_[1];
340 0 $_[0]-> {read} = $_[1];
341 }
342  
343 sub can_write
344 0 ▶{
Subroutine can_write: not called CC=2 SCAR=17.9
345 0 ▶ return vec( $_[0]->{owner}->{write}, $_[0]-> {fileno}, 1) unless $#_;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 0 0
346 0 vec( $_[0]->{owner}->{write}, $_[0]-> {fileno}, 1) = $_[1];
347 0 $_[0]-> {write} = $_[1];
348 }
349  
350 sub can_exception
351 0 ▶{
Subroutine can_exception: not called CC=2 SCAR=17.9
352 0 ▶ return vec( $_[0]->{owner}->{exc}, $_[0]-> {fileno}, 1) unless $#_;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 0 0
353 0 vec( $_[0]->{owner}->{exc}, $_[0]-> {fileno}, 1) = $_[1];
354 0 $_[0]-> {exception} = $_[1];
355 }
356  
357 sub DESTROY
358 19 ▶{
Subroutine DESTROY: called CC=8 SCAR=21.0
359 19 my $self = $_[0];
360 19 ▶ return if $self->{dead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dead"} 9 10 19
361 10 $self->{dead} = 1;
362 10 $self-> flush;
363 10 $self-> notify('on_close');
364 $self-> {handle}-> close
365 10 ▶ if defined $self->{handle} && $self->{auto_close};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"handle"} and $self->{"auto_close"} 10 0 10
Condition33%1/3
defined $self->{"handle"} and $self->{"auto_close"}
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $self->{"handle"} and $self->{"auto_close"}
defined $self->{"handle"} $self->{"auto_close"}
Truth table33%1/3
A and B
A= defined $self->{"handle"}B= $self->{"auto_close"}
ABresult
0X0
100
111
366 10 ▶ if ( defined $self->{owner}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"owner"}) 10 0 10
367 10 ▶ if ( defined $self->{fileno}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fileno"}) 10 0 10
368 10 vec( $self-> {owner}-> {exc}, $self->{fileno}, 1) = 0;
369 10 vec( $self-> {owner}-> {write}, $self->{fileno}, 1) = 0;
370 10 vec( $self-> {owner}-> {read}, $self->{fileno}, 1) = 0;
371 10 delete $self-> {owner}-> {filenos}-> {$self->{fileno}};
372 }
373 10 ▶ if (defined $self->{pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pid"}) 2 8 10
374 2 my $p = $self-> {owner}-> {processes}-> {$self->{pid}};
375 2 @$p = grep { $_ ne $self->{id}} @$p;
376 2 ▶ delete $self-> {owner}-> {processes}-> {$self->{pid}} unless @$p;
Branch: 1 branch
BranchTrueFalseTotal
unless @$p 1 1 2
377 }
378 10 delete $self-> {owner}-> {ids}-> {$self->{id}};
379 }
380 10 delete $self->{fileno};
381 10 delete $self->{id};
382 }
383  
384 sub readline
385 7 ▶{
Subroutine readline: called CC=2 SCAR=6.9
386 7 ▶ return $1 if $_[0]-> {read_buffer} =~ s/^([^\n]*\n)//;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"read_buffer"} =~ s/^([^\n]*\n)// 4 3 7
387 3 return undef;
388 }
389  
390 sub read
391 0 ▶{
Subroutine read: not called CC=1 SCAR=6.9
392 0 my $c = $_[0]-> {read_buffer};
393 0 substr( $_[0]-> {read_buffer}, 0) = '';
394 0 return $c;
395 }
396  
397 sub write
398 3 ▶{
Subroutine write: called CC=8 SCAR=30.1
399 3 my ( $self, $data) = @_;
400 3 $self-> {write_buffer} .= $data;
401 3 ▶ vec( $self->{owner}->{write}, $self-> {fileno}, 1) = 1 if $self->{owner} and defined $self->{fileno};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"owner"} and defined $self->{"fileno"} 3 0 3
Condition33%1/3
$self->{"owner"} and defined $self->{"fileno"}
!ll&&!rl&&r
003
MC/DC0%0/2
$self->{"owner"} and defined $self->{"fileno"}
$self->{"owner"} defined $self->{"fileno"}
Truth table33%1/3
A and B
A= $self->{"owner"}B= defined $self->{"fileno"}
ABresult
0X0
100
111
402  
403 3 my $nbytes = syswrite( $self->{handle}, $self->{write_buffer});
404 3 ▶ unless ( defined $nbytes) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $nbytes) { } 3 0 3
if (do { $nbytes > 0 }) 3 0 3
405 0 ▶ $self-> {owner}-> error( $self, 'write') if $self->{owner} && $! != EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"owner"} and $! != 11 0 0 0
Condition0%0/3
$self->{"owner"} and $! != 11
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"owner"} and $! != 11
$self->{"owner"} $! != 11
Truth table0%0/3
A and B
A= $self->{"owner"}B= $! != 11
ABresult
0X0
100
111
406 0 ▶ $nbytes = 0 if $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 0 0 0
407 0 } elsif ( $nbytes > 0) {
408 3 substr( $self->{write_buffer}, 0, $nbytes) = '';
409 }
410 3 $nbytes;
411 }
412  
413 sub flush
414 10 ▶{
Subroutine flush: called CC=4 SCAR=17.9
415 10 my ( $self, $discard) = @_;
416 10 ▶ if ( $discard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discard) { } 0 10 10
417 0 $self-> {write_buffer} = '';
418 } else {
419 10 ▶ while ( length $self-> {write_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"write_buffer"}) 0 10 10
420 0 ▶ return undef unless defined $self-> write('');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->write("") 0 0 0
421 }
422 }
423 10 return 1;
424 }
425  
426 10 ▶sub destroy { shift-> DESTROY }
Subroutine destroy: called CC=1 SCAR=0.0
427  
428 sub notify
429 31 ▶{
Subroutine notify: called CC=6 SCAR=18.0
430 31 my ( $self, $event, @params) = @_;
431 31 ▶ die( "Unexistent event `$event'") unless $events{$event};
Branch: 1 branch
BranchTrueFalseTotal
unless $IO::Events::Handle::events{$event} 0 31 31
432
433 31 $self-> {event_flag} = 0;
434 31 ▶ if ( exists $self->{$event}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$event}) 8 23 31
435 8 $self->{$event}->($self,@params);
436 8 ▶ return if $events{$event} == SINGLE || $self->{event_flag};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Events::Handle::events{$event} == 1 or $self->{"event_flag"} 7 1 8
Condition66%2/3
$IO::Events::Handle::events{$event} == 1 or $self->{"event_flag"}
l!l&&r!l&&!r
701
MC/DC50%1/2
$IO::Events::Handle::events{$event} == 1 or $self->{"event_flag"}
$IO::Events::Handle::events{$event} == 1 $self->{"event_flag"}
Truth table66%2/3
A or B
A= $IO::Events::Handle::events{$event} == 1B= $self->{"event_flag"}
ABresult
1X1
011
000
437 }
438 24 ▶ $self-> $event(@params) if $self-> can($event);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($event) 1 23 24
439 }
440  
441 sub on_error
442 0 ▶{
Subroutine on_error: not called CC=5 SCAR=34.0
443 0 my ( $self, $condition, $errno) = @_;
444 0 ▶ if ( $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 0 0 0
445 0 $condition .= ' '.ref($self);
446 0 ▶ $condition .= ",#$self->{fileno}" if defined $self->{fileno};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"fileno"} 0 0 0
447 0 ▶ $condition .= " pid $self->{pid}" if defined $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"pid"} 0 0 0
448 0 ▶ $condition .= " ($self->{process})" if defined $self->{process};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"process"} 0 0 0
449 }
450 0 warn "Error on $condition: $errno\n";
451 0 $_[0]-> destroy;
452 }
453  
454  
455 # external writer process
456 package IO::Events::Process::Write;
457 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
458 2 @ISA = qw(IO::Events::Handle);
459  
460 sub new
461 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
462 0 my ( $self, %profile) = @_;
463 0 ▶ die "No `process'" unless defined $profile{process};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $profile{"process"} 0 0 0
464 0 my $handle = IO::Handle-> new();
465 0 $handle-> autoflush(1);
466 0 my $pid = open( $handle, "|$profile{process}");
467 0 ▶ die("Cannot fork:$!") unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
468  
469 0 $self = $self-> SUPER::new(
470 write => 1,
471 %profile,
472 handle => $handle,
473 pid => $pid,
474 );
475 0 return $self;
476 }
477  
478 # external reader process
479 package IO::Events::Process::Read;
480 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
481 2 @ISA = qw(IO::Events::Handle);
482  
483 sub new
484 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
485 0 my ( $self, %profile) = @_;
486 0 ▶ die "No `process'" unless defined $profile{process};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $profile{"process"} 0 0 0
487 0 my $handle = IO::Handle-> new();
488 0 $handle-> autoflush(1);
489 0 my $pid = open( $handle, "$profile{process}|");
490 0 ▶ die("Cannot fork:$!") unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
491  
492 0 $self = $self-> SUPER::new(
493 read => 1,
494 %profile,
495 handle => $handle,
496 pid => $pid,
497 );
498 0 return $self;
499 }
500  
501 # internal reader process
502 package IO::Events::Fork::Read;
503 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
504 2 @ISA = qw(IO::Events::Handle);
505  
506 sub new
507 0 ▶{
Subroutine new: not called CC=5 SCAR=34.0
508 0 my ( $self, %profile) = @_;
509 0 my $handle = IO::Handle-> new();
510 0 $handle-> autoflush(1);
511 0 my $pid = open( $handle, "-|");
512 0 ▶ die("Cannot fork:$!") unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
513 0 ▶ unless ( $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
514 # $profile{owner}->on_fork();
515 0 $|=1;
516 0 ▶ my $on_fork = $profile{on_fork} || $self->can('on_fork');
Condition0%0/3
$profile{'on_fork'} || $self->can('on_fork')
l!l&&r!l&&!r
000
MC/DC0%0/2
$profile{'on_fork'} || $self->can('on_fork')
$profile{'on_fork'} $self->can('on_fork')
Truth table0%0/3
A || B
A= $profile{'on_fork'}B= $self->can('on_fork')
ABresult
1X1
011
000
517 0 ▶ $on_fork->(\%profile) if $on_fork;
Branch: 1 branch
BranchTrueFalseTotal
if $on_fork 0 0 0
518 0 POSIX::_exit(0);
519 }
520  
521 0 $self = $self-> SUPER::new(
522 read => 1,
523 %profile,
524 handle => $handle,
525 pid => $pid,
526 );
527 0 return $self;
528 }
529  
530 # internal writer process
531 package IO::Events::Fork::Write;
532 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
533 2 @ISA = qw(IO::Events::Handle);
534  
535 sub new
536 0 ▶{
Subroutine new: not called CC=5 SCAR=34.0
537 0 my ( $self, %profile) = @_;
538 0 my $handle = IO::Handle-> new();
539 0 $handle-> autoflush(1);
540 0 my $pid = open( $handle, "|-");
541 0 ▶ die("Cannot fork:$!") unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
542 0 ▶ unless ( $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
543 # $profile{owner}->on_fork();
544 0 ▶ my $on_fork = $profile{on_fork} || $self->can('on_fork');
Condition0%0/3
$profile{'on_fork'} || $self->can('on_fork')
l!l&&r!l&&!r
000
MC/DC0%0/2
$profile{'on_fork'} || $self->can('on_fork')
$profile{'on_fork'} $self->can('on_fork')
Truth table0%0/3
A || B
A= $profile{'on_fork'}B= $self->can('on_fork')
ABresult
1X1
011
000
545 0 ▶ $on_fork->(\%profile) if $on_fork;
Branch: 1 branch
BranchTrueFalseTotal
if $on_fork 0 0 0
546 0 POSIX::_exit(0);
547 }
548  
549 0 $self = $self-> SUPER::new(
550 write => 1,
551 %profile,
552 handle => $handle,
553 pid => $pid,
554 );
555 0 return $self;
556 }
557  
558 package IO::Events::internal::Shadow;
559 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
560 2 @ISA = qw(IO::Events::Handle);
561  
562 sub new
563 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
564 1 my ( $self, %profile) = @_;
565 1 $profile{shadow_task} = $profile{owner}->{ids}->{$profile{id}};
566 1 $profile{id} = "shadow:$profile{id}";
567 1 my $ret = $self-> SUPER::new(%profile);
568 1 return $ret;
569 }
570  
571 sub on_close
572 1 ▶{
Subroutine on_close: called CC=1 SCAR=0.0
573 1 undef $_[0]->{shadow_task}-> {shadow};
574 }
575  
576 sub on_error
577 0 ▶{
Subroutine on_error: not called CC=1 SCAR=6.9
578 0 my ( $self, $condition, $errno) = @_;
579 0 $self-> {shadow_task}-> notify('on_error', $condition, $errno);
580 }
581  
582  
583 # internal bidirectional process
584 package IO::Events::Fork::ReadWrite;
585 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
586 2 @ISA = qw(IO::Events::Handle);
587  
588 sub new
589 2 ▶{
Subroutine new: called CC=5 SCAR=16.3
590 2 my ( $self, %profile) = @_;
591  
592 # reader
593 2 my $handle1 = IO::Handle-> new();
594 2 $handle1-> autoflush(1);
595
596 # writer
597 2 my $handle2 = IO::Handle-> new();
598 2 $handle2-> autoflush(1);
599  
600 # fork & pipes
601 2 pipe(READER, $handle2);
602 2 pipe($handle1, WRITER);
603 2 WRITER->autoflush(1);
604
605 2 my $pid = fork();
606 2 ▶ die("Cannot fork:$!") unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 2 2
607
608 2 ▶ unless ( $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 1 1 2
609 1 close $handle1;
610 1 close $handle2;
611 1 open STDOUT, ">&WRITER";
612 1 open STDIN, "<&READER";
613  
614 # $profile{owner}->on_fork();
615 1 $|=1;
616 1 ▶ my $on_fork = $profile{on_fork} || $self->can('on_fork');
Condition33%1/3
$profile{'on_fork'} || $self->can('on_fork')
l!l&&r!l&&!r
100
MC/DC0%0/2
$profile{'on_fork'} || $self->can('on_fork')
$profile{'on_fork'} $self->can('on_fork')
Truth table33%1/3
A || B
A= $profile{'on_fork'}B= $self->can('on_fork')
ABresult
1X1
011
000
617 1 ▶ $on_fork->(\%profile) if $on_fork;
Branch: 1 branch
BranchTrueFalseTotal
if $on_fork 1 0 1
618 0 POSIX::_exit(0);
619 }
620  
621 1 close WRITER;
622 1 close READER;
623  
624 # create objects
625 1 $self = $self-> SUPER::new(
626 read => 1,
627 %profile,
628 handle => $handle1,
629 pid => $pid,
630 );
631  
632 $self-> {shadow} = IO::Events::internal::Shadow-> new(
633 write => 1,
634 %profile,
635 id => $self-> {id},
636 1 handle => $handle2,
637 pid => $pid,
638 on_write => \&shadow_write,
639 on_close => \&shadow_close,
640 );
641  
642 1 return $self;
643 }
644  
645 sub shadow_write
646 1 ▶{
Subroutine shadow_write: called CC=1 SCAR=0.0
647 1 shift-> {shadow_task}-> notify('on_write');
648 }
649  
650 sub shadow_close
651 1 ▶{
Subroutine shadow_close: called CC=1 SCAR=0.0
652 1 my $shadow = shift;
653 1 $shadow-> {shadow_task}->{finished} = $shadow-> {finished};
654 1 $shadow-> {shadow_task}->{exitcode} = $shadow-> {exitcode};
655 1 $shadow-> {shadow_task}-> notify('on_close', 1);
656 }
657  
658 sub shutdown
659 0 ▶{
Subroutine shutdown: not called CC=5 SCAR=34.0
660 0 my ( $self, @cmd) = @_;
661 0 for ( @cmd) {
662 0 ▶ if ( $_ eq 'read') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq 'read') { } 0 0 0
elsif ($_ eq 'write') { } 0 0 0
663 0 $self-> SUPER::DESTROY;
664 } elsif ( $_ eq 'write') {
665 0 ▶ $self-> {shadow}-> DESTROY if $self-> {shadow};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shadow"} 0 0 0
666 }
667 }
668 }
669  
670 sub DESTROY
671 2 ▶{
Subroutine DESTROY: called CC=3 SCAR=11.0
672 2 ▶ return if $_[0]->{dead};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"dead"} 1 1 2
673 1 ▶ $_[0]->{shadow}->DESTROY if $_[0]->{shadow};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"shadow"} 0 1 1
674 1 $_[0]->SUPER::DESTROY;
675 1 $_[0]->{dead} = 1;
676 }
677  
678 1 ▶sub write {
Subroutine write: called CC=2 SCAR=7.1
679 1 my $self = shift;
680 1 ▶ return unless $self->{shadow};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shadow"} 0 1 1
681 1 $self-> {shadow}-> write( @_)
682 }
683  
684 # external bidirectional process
685 package IO::Events::Process::ReadWrite;
686 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
687 2 @ISA = qw(IO::Events::Fork::ReadWrite);
688  
689 sub on_fork
690 0 ▶{
Subroutine on_fork: not called CC=2 SCAR=17.9
691 0 ▶ exec( $_[0]->{process}) or POSIX::_exit(127);
Branch: 1 branch
BranchTrueFalseTotal
exec $_[0]{"process"} or POSIX::_exit(127) 0 0 0
692 }
693  
694 package IO::Events::stdin;
695 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
696 2 @ISA=qw(IO::Events::Handle);
697  
698 sub new
699 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
700 0 my ( $self, %profile) = @_;
701 0 $profile{id} = "stdin";
702 0 $profile{handle} = \*STDIN;
703 0 $profile{read} = 1;
704 0 $profile{auto_close} = 0;
705 0 return $self-> SUPER::new(%profile);
706 }
707  
708 package IO::Events::stdout;
709 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
710 2 @ISA=qw(IO::Events::Handle);
711  
712 sub new
713 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
714 0 my ( $self, %profile) = @_;
715 0 $profile{id} = "stdout";
716 0 $profile{handle} = \*STDOUT;
717 0 $profile{write} = 1;
718 0 $profile{auto_close} = 0;
719 0 return $self-> SUPER::new(%profile);
720 }
721  
722 package IO::Events::stderr;
723 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
724 2 @ISA=qw(IO::Events::Handle);
725  
726 sub new
727 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
728 0 my ( $self, %profile) = @_;
729 0 $profile{id} = "stderr";
730 0 $profile{handle} = \*STDERR;
731 0 $profile{write} = 1;
732 0 $profile{auto_close} = 0;
733 0 return $self-> SUPER::new(%profile);
734 }
735  
736 package IO::Events::Socket;
737 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
738 2 @ISA=qw(IO::Events::Handle);
739  
740 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
741  
742 sub accept
743 2 ▶{
Subroutine accept: called CC=2 SCAR=7.0
744 2 my ( $self, %profile) = @_;
745 2 my $handle = IO::Handle-> new;
746 2 ▶ accept( $handle, $self-> {handle}) or die "accept() error:$!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::accept $handle, $self->{"handle"} or die "accept() error:$!" 2 0 2
747 return IO::Events::Handle-> new(
748 owner => $self-> {owner},
749 2 handle => $handle,
750 %profile,
751 );
752 }
753  
754 sub connect
755 2 ▶{
Subroutine connect: called CC=1 SCAR=0.0
756 2 $_[0]-> {callback} = \&socket_connect_error_checker;
757 }
758  
759 sub socket_connect_error_checker
760 2 ▶{
Subroutine socket_connect_error_checker: called CC=3 SCAR=13.3
761 2 my ( $self, $r, $w, $e) = @_;
762 2 delete $self-> {callback};
763  
764 2 local $! = unpack('i', getsockopt($self-> {handle}, SOL_SOCKET, SO_ERROR));
765 2 ▶ if ( $!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 2 2
766 0 ▶ $self-> {owner}-> error( $self, 'connect') if $self->{owner};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"owner"} 0 0 0
767 }
768 }
769  
770 package IO::Events::Socket::TCP;
771 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
772 2 @ISA=qw(IO::Events::Socket);
773  
774 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
775 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
776 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
777 2 ▶use Errno qw(EWOULDBLOCK EINPROGRESS);
Subroutine BEGIN: called CC=1 SCAR=0.0
778  
779 sub new
780 2 ▶{
Subroutine new: called CC=18 SCAR=32.3
781 2 my ( $self, %profile) = @_;
782  
783 2 ▶ $profile{handle} = IO::Handle-> new unless $profile{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $profile{"handle"} 2 0 2
784 die "Cannot create socket: $!" unless
785 2 ▶ socket( $profile{handle}, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
Branch: 1 branch
BranchTrueFalseTotal
unless socket $profile{"handle"}, 2, 1, getprotobyname "tcp" 0 2 2
786  
787 2 ▶ unless ( $profile{nonblock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($profile{"nonblock"}) 2 0 2
788 2 my $fl;
789 2 $fl = fcntl( $profile{handle}, F_GETFL, 0);
790 2 ▶ die "$!" unless defined $fl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fl 0 2 2
791 2 ▶ fcntl( $profile{handle}, F_SETFL, $fl|O_NONBLOCK) or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $profile{"handle"}, 4, $fl | 2048 or die "$!" 2 0 2
792 }
793  
794 2 ▶ if ( defined $profile{connect}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $profile{'connect'}) { } 1 1 2
elsif (exists $profile{'listen'}) { } 1 0 1
795 1 my $iaddr;
796 die "Cannot resolve host '$profile{connect}'" unless
797 1 ▶ $iaddr = inet_aton( $profile{connect});
Branch: 1 branch
BranchTrueFalseTotal
unless $iaddr = inet_aton($profile{"connect"}) 0 1 1
798 1 my $ok = connect( $profile{handle}, sockaddr_in( $profile{port}, $iaddr));
799 1 ▶ $ok = 1 if !$ok and ( $! == EWOULDBLOCK || $! == EINPROGRESS);
Branch: 1 branch
BranchTrueFalseTotal
if not $ok and $! == 11 || $! == 115 1 0 1
Condition33%2/6
not $ok and $! == 11 || $! == 115
!ll&&!rl&&r
001
$! == 11 or $! == 115
l!l&&r!l&&!r
010
MC/DC0%0/3
not $ok and $! == 11 || $! == 115
not $ok $! == 11 $! == 115
Truth table25%1/4
not(A) and B or C
A= not $okB= $! == 11C= $! == 115
ABCresult
0XX0
1000
11X1
1011
800 1 ▶ die "Connect error: $!" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
801 } elsif ( exists $profile{listen}) {
802 1 ▶ setsockopt( $profile{handle}, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) or
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $profile{"handle"}, 1, 2, "\1\0\0\0" or die "Error in setsockopt(SOL_SOCKET,SO_REUSEADDR,1):$!" 1 0 1
803 die "Error in setsockopt(SOL_SOCKET,SO_REUSEADDR,1):$!";
804 1 ▶ my $addr = $profile{addr} || '0.0.0.0';
Condition50%1/2
$profile{'addr'} || '0.0.0.0'
l!l
10
MC/DC0%0/1
$profile{'addr'} || '0.0.0.0'
$profile{'addr'}
Truth table50%1/2
A
A= $profile{'addr'}
Aresult
11
00
805 1 my $inet = inet_aton( $addr);
806 1 ▶ die "Cannot resolve host '$addr'" unless defined $inet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inet 0 1 1
807 1 ▶ bind( $profile{handle}, sockaddr_in( $profile{port}, $inet)) or
Branch: 1 branch
BranchTrueFalseTotal
bind $profile{"handle"}, sockaddr_in($profile{"port"}, $inet) or die qq[Error in bind($profile{"port"}, $addr):$!] 1 0 1
808 die "Error in bind($profile{port}, $addr):$!";
809 1 listen( $profile{handle}, SOMAXCONN);
810 1 $profile{read} = -1;
811 }
812  
813 2 my $this = $self-> SUPER::new(%profile);
814 2 ▶ $this-> SUPER::connect() if $profile{connect};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"connect"} 1 1 2
815 2 return $this;
816 }
817  
818 sub accept
819 1 ▶{
Subroutine accept: called CC=1 SCAR=0.0
820 1 my ( $self, %profile) = @_;
821 1 my $client = $self-> SUPER::accept( %profile);
822 1 my ($port, $ipaddr) = unpack_sockaddr_in( getpeername( $client->{handle}));
823 1 $client-> {remote_addr} = inet_ntoa($ipaddr);
824 1 $client-> {remote_port} = $port;
825 1 return $client;
826 }
827  
828 package IO::Events::Socket::UDP;
829 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
830 2 @ISA=qw(IO::Events::Socket);
831  
832 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
833 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
834 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
835  
836 sub new
837 2 ▶{
Subroutine new: called CC=12 SCAR=28.1
838 2 my ( $self, %profile) = @_;
839  
840 2 ▶ $profile{handle} = IO::Handle-> new unless $profile{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $profile{"handle"} 2 0 2
841 die "Cannot create socket: $!" unless
842 2 ▶ socket( $profile{handle}, PF_INET, SOCK_DGRAM, getprotobyname('udp'));
Branch: 1 branch
BranchTrueFalseTotal
unless socket $profile{"handle"}, 2, 2, getprotobyname "udp" 0 2 2
843  
844 2 ▶ unless ( $profile{nonblock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($profile{"nonblock"}) 2 0 2
845 2 my $fl;
846 2 $fl = fcntl( $profile{handle}, F_GETFL, 0);
847 2 ▶ die "$!" unless defined $fl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fl 0 2 2
848 2 ▶ fcntl( $profile{handle}, F_SETFL, $fl|O_NONBLOCK) or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $profile{"handle"}, 4, $fl | 2048 or die "$!" 2 0 2
849 }
850
851 2 ▶ my $addr = $profile{addr} || '0.0.0.0';
Condition50%1/2
$profile{'addr'} || '0.0.0.0'
l!l
02
MC/DC0%0/1
$profile{'addr'} || '0.0.0.0'
$profile{'addr'}
Truth table50%1/2
A
A= $profile{'addr'}
Aresult
11
00
852 2 my $inet = inet_aton( $addr);
853 2 ▶ die "Cannot resolve host '$addr'" unless defined $inet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inet 0 2 2
854
855 2 ▶ if ( $profile{broadcast}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile{"broadcast"}) 0 2 2
856 0 ▶ setsockopt( $profile{handle}, SOL_SOCKET, SO_BROADCAST, pack("l", 1)) or
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $profile{"handle"}, 1, 6, "\1\0\0\0" or die "Error in setsockopt(SOL_SOCKET,SO_BROADCAST,1):$!" 0 0 0
857 die "Error in setsockopt(SOL_SOCKET,SO_BROADCAST,1):$!";
858 }
859 2 $profile{read} = -2;
860
861 2 ▶ bind( $profile{handle}, sockaddr_in( $profile{port} || 0, $inet)) or
Branch: 1 branch
BranchTrueFalseTotal
bind $profile{"handle"}, sockaddr_in($profile{"port"} || 0, $inet) or die qq[Error in bind($profile{"port"}, $addr):$!] 2 0 2
Condition100%2/2
$profile{'port'} || 0
l!l
11
MC/DC100%1/1
$profile{'port'} || 0
$profile{'port'}
Truth table100%2/2
A
A= $profile{'port'}
Aresult
11
00
862 die "Error in bind($profile{port}, $addr):$!";
863  
864 2 return $self-> SUPER::new(%profile);
865 }
866  
867 sub recv
868 1 ▶{
Subroutine recv: called CC=8 SCAR=23.0
869 1 my ( $self, %profile) = @_;
870  
871 1 ▶ $profile{maxlen} = 32768 unless defined $profile{maxlen};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $profile{"maxlen"} 1 0 1
872  
873 1 my $flags = MSG_DONTWAIT;
874 1 ▶ $flags |= MSG_OOB if $profile{oob};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"oob"} 0 1 1
875 1 ▶ $flags |= MSG_PEEK if $profile{peek};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"peek"} 0 1 1
876 1 ▶ $flags |= MSG_WAITALL if $profile{waitall};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"waitall"} 0 1 1
877 1 ▶ $flags &= ~MSG_DONTWAIT if defined($profile{nonblock}) and $profile{nonblock} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $profile{"nonblock"} and $profile{"nonblock"} == 0 0 1 1
Condition33%1/3
defined $profile{"nonblock"} and $profile{"nonblock"} == 0
!ll&&!rl&&r
100
MC/DC0%0/2
defined $profile{"nonblock"} and $profile{"nonblock"} == 0
defined $profile{"nonblock"} $profile{"nonblock"} == 0
Truth table33%1/3
A and B
A= defined $profile{"nonblock"}B= $profile{"nonblock"} == 0
ABresult
0X0
100
111
878
879 1 my ( $port, $host);
880 1 my $data = '';
881 1 $host = recv( $self-> {handle}, $data, $profile{maxlen}, $flags);
882 1 ▶ unless ( defined $host) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host) 0 1 1
883 0 $self-> error( 'recv');
884 0 return undef;
885 }
886 1 ( $port, $host) = sockaddr_in( $host);
887 1 $self-> {remote_port} = $port;
888 1 $self-> {remote_host} = gethostbyaddr( $host, AF_INET);
889  
890 1 return $data;
891 }
892  
893 sub send
894 1 ▶{
Subroutine send: called CC=7 SCAR=21.5
895 1 my ( $self, $addr, $port, $data, %profile) = @_;
896  
897 1 my $flags = 0;
898 1 ▶ $flags |= MSG_OOB if $profile{oob};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"oob"} 0 1 1
899 1 ▶ $flags |= MSG_DONTROUTE if $profile{dontroute};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"dontroute"} 0 1 1
900 1 ▶ $flags |= MSG_EOR if $profile{eor};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"eor"} 0 1 1
901 1 ▶ $flags |= MSG_EOF if $profile{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"eof"} 0 1 1
902
903 1 ▶ my $inet = inet_aton($addr) || die "unknown host '$addr'\n";
Condition50%1/2
inet_aton($addr) || die("unknown host '${addr}'\n")
l!l
10
MC/DC0%0/1
inet_aton($addr) || die("unknown host '${addr}'\n")
inet_aton($addr)
Truth table50%1/2
A
A= inet_aton($addr)
Aresult
11
00
904 1 $inet = sockaddr_in( $port, $inet);
905  
906 1 my $ret = send( $self-> {handle}, $data, $flags, $inet);
907 1 ▶ unless ( defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret) 0 1 1
908 0 $self-> error( 'recv');
909 0 return undef;
910 }
911 1 return $ret;
912 }
913  
914 package IO::Events::Socket::UNIX;
915 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
916 2 @ISA=qw(IO::Events::Socket);
917  
918 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
919 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
920  
921 sub new
922 2 ▶{
Subroutine new: called CC=11 SCAR=25.1
923 2 my ( $self, %profile) = @_;
924  
925 2 ▶ $profile{handle} = IO::Handle-> new unless $profile{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $profile{"handle"} 2 0 2
926 die "Cannot create socket: $!" unless
927 2 ▶ socket( $profile{handle}, PF_UNIX, SOCK_STREAM, 0);
Branch: 1 branch
BranchTrueFalseTotal
unless socket $profile{"handle"}, 1, 1, 0 0 2 2
928  
929 2 ▶ unless ( $profile{nonblock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($profile{"nonblock"}) 2 0 2
930 2 my $fl;
931 2 $fl = fcntl( $profile{handle}, F_GETFL, 0);
932 2 ▶ die "$!" unless defined $fl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fl 0 2 2
933 2 ▶ fcntl( $profile{handle}, F_SETFL, $fl|O_NONBLOCK) or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $profile{"handle"}, 4, $fl | 2048 or die "$!" 2 0 2
934 }
935  
936 2 ▶ if ( defined $profile{connect}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $profile{'connect'}) { } 1 1 2
elsif (exists $profile{'listen'}) { } 1 0 1
937 1 ▶ connect( $profile{handle}, pack_sockaddr_un($profile{connect})) or
Branch: 1 branch
BranchTrueFalseTotal
connect $profile{"handle"}, pack_sockaddr_un($profile{"connect"}) or die qq[connect($profile{"connect"}) error: $!] 1 0 1
938 die "connect($profile{connect}) error: $!";
939 } elsif ( exists $profile{listen}) {
940 1 ▶ bind( $profile{handle}, pack_sockaddr_un($profile{listen})) or
Branch: 1 branch
BranchTrueFalseTotal
bind $profile{"handle"}, pack_sockaddr_un($profile{"listen"}) or die qq[Error in bind($profile{"listen"}):$!] 1 0 1
941 die "Error in bind($profile{listen}):$!";
942 1 listen( $profile{handle}, SOMAXCONN);
943 1 $profile{read} = -1;
944 }
945  
946 2 my $this = $self-> SUPER::new(%profile);
947 2 ▶ $this-> SUPER::connect() if $profile{connect};
Branch: 1 branch
BranchTrueFalseTotal
if $profile{"connect"} 1 1 2
948 2 return $this;
949 }
950  
951 package IO::Events::Timer;
952 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
953  
954 sub new
955 1 ▶{
Subroutine new: called CC=4 SCAR=14.0
956 1 my $class = shift;
957  
958 1 my $self = bless {
959 timeout => 10000,
960 repetitive => 0,
961 active => 0,
962 @_,
963 }, $class;
964
965 1 for ( qw(owner)) {
966 1 ▶ die "No `$_' field" unless defined $self-> {$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$_} 0 1 1
967 }
968  
969 1 push @{$self-> {owner}-> {timers}}, $self;
970  
971 1 ▶ $self-> start if $self-> {active};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active"} 1 0 1
972  
973 1 return $self;
974 }
975  
976 sub DESTROY
977 0 ▶{
Subroutine DESTROY: not called CC=2 SCAR=17.9
978 0 my $self = $_[0];
979 0 ▶ return if $self-> {dead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dead"} 0 0 0
980 0 @{$self-> {owner}-> {timers}} = grep { $_ != $self } @{$self-> {owner}-> {timers}};
981 }
982  
983 sub start
984 1 ▶{
Subroutine start: called CC=1 SCAR=0.0
985 1 my $self = $_[0];
986 1 $self-> {alert} = time + $self-> {timeout};
987 1 $self-> {active} = 1;
988 }
989  
990 0 ▶sub stop { $_[0]-> {active} = 0 }
Subroutine stop: not called CC=1 SCAR=6.9
991  
992 sub active
993 0 ▶{
Subroutine active: not called CC=3 SCAR=24.8
994 0 my ( $self, $active) = @_;
995 0 ▶ return if $active == $self-> {active}; # to avoid restarts
Branch: 1 branch
BranchTrueFalseTotal
if $active == $self->{"active"} 0 0 0
996 0 ▶ $active ? $self-> start : $self-> stop;
Branch: 1 branch
BranchTrueFalseTotal
$active ? : 0 0 0
997 }
998  
999 sub notify
1000 1 ▶{
Subroutine notify: called CC=6 SCAR=21.2
1001 1 my $self = $_[0];
1002  
1003 1 ▶ if ( $self-> {repetitive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'repetitive'}) { } 0 1 1
1004 0 my $time = time;
1005 # eat up late events
1006 0 ▶ $self-> {alert} += $self-> {timeout} while $self-> {alert} < $time;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"alert"} < $time and do { $self->{"alert"} += $self->{"timeout"} } 0 0 0
1007 } else {
1008 1 $self-> {active} = 0;
1009 }
1010
1011 1 $self-> {event_flag} = 0;
1012 1 ▶ if ( defined $self->{on_tick}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_tick"}) 1 0 1
1013 1 $self->{on_tick}->($self);
1014 1 ▶ return if $self->{event_flag};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"event_flag"} 0 1 1
1015 }
1016 1 ▶ $self-> on_tick() if $self-> can('on_tick');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("on_tick") 0 1 1
1017 }
1018  
1019  
1020 2 1;
1021  
1022 __DATA__
↑ Summary