blib/lib/IO/BindHandles.pm

filter: Statementstmt 87.3% 90 / 103 Branchbran 58.8% 20 / 34 Conditioncond 43.8% 25 / 57 MC/DCmcdc 12.9% 4 / 31 Subroutinesub 90.0% 9 / 10 Podpod 100% 5 / 5 totaltotal 63.7% 153 / 240 CC 42 SCAR 33.3
CC
42
Cov
70%
rwcycle
47.1
loop
20.0
bound
8.4
CRAP 27.9
↑ Summary
1 package IO::BindHandles;
2 # ABSTRACT: Bind a set of handles for buffered tunneling
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶sub new {
Subroutine new: called CC=7 SCAR=19.9
Pod documented
10 3 my $class = shift;
11 3 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
12 3 my %options = @_;
13  
14 3 my $timeout = 0.5;
15 3 ▶ $timeout = $options{timeout} if exists $options{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"timeout"} 0 3 3
16 3 ▶ $timeout += 0 if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 3 0 3
17  
18 3 my $handles = $options{handles};
19 3 ▶ die "handles must be an ARRAY ref" if ref $handles ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $handles ne "ARRAY" 0 3 3
20 3 ▶ warn "odd number of handles in BindHandles" if scalar(@$handles) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$handles & 1 0 3 3
21  
22 3 my @all_handles;
23 3 ▶ for (my $i = 0; $i < scalar @$handles; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$handles) 4 3 7
24 4 my $read = $handles->[$i];
25 4 my $write = $handles->[$i+1];
26 4 $read->autoflush(1);
27 4 $write->autoflush(1);
28 4 my $buffer = '';
29 4 push @all_handles, [ $read, $write, $buffer ];
30 }
31  
32 3 my $self = {
33 timeout => $timeout,
34 handles => \@all_handles,
35 };
36  
37 3 bless $self, $class;
38 3 return $self;
39 }
40  
41  
42 0 ▶sub loop {
Subroutine loop: not called CC=4 SCAR=30.0
Pod documented
43 0 my $self = shift;
44 0 ▶ $self->timeout(0.1) if defined $self->timeout && $self->timeout == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->timeout and $self->timeout == 0 0 0 0
Condition0%0/3
defined $self->timeout and $self->timeout == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->timeout and $self->timeout == 0
defined $self->timeout $self->timeout == 0
Truth table0%0/3
A and B
A= defined $self->timeoutB= $self->timeout == 0
ABresult
0X0
100
111
45 #warn "In loop...";
46 0 ▶ while ($self->bound()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bound) 0 0 0
47 #warn "rwcycle...";
48 0 $self->rwcycle();
49 }
50 #warn "Out loop...";
51 }
52  
53  
54 9 ▶sub bound {
Subroutine bound: called CC=7 SCAR=21.3
Pod documented
55 9 my $self = shift;
56 #warn "in bound...";
57 my @list =
58 grep {
59 (
60 14 ▶ $_->[0]->opened && $_->[1]->opened &&
Condition61%11/18
$_->[0]->opened && $_->[1]->opened && !$_->[0]->error && !$_->[1]->error || $_->[2] && $_->[1]->opened && !$_->[1]->error
l!l&&r!l&&!r
1103
$_->[0]->opened and $_->[1]->opened and not $_->[0]->error and not $_->[1]->error
!ll&&!rl&&r
3011
$_->[0]->opened and $_->[1]->opened and not $_->[0]->error
!ll&&!rl&&r
3011
$_->[0]->opened and $_->[1]->opened
!ll&&!rl&&r
2111
$_->[2] and $_->[1]->opened and not $_->[1]->error
!ll&&!rl&&r
300
$_->[2] and $_->[1]->opened
!ll&&!rl&&r
300
MC/DC28%2/7
$_->[0]->opened && $_->[1]->opened && !$_->[0]->error && !$_->[1]->error || $_->[2] && $_->[1]->opened && !$_->[1]->error
$_->[0]->opened $_->[1]->opened not $_->[0]->error not $_->[1]->error $_->[2] $_->[1]->opened not $_->[1]->error
Truth table17%3/17
A and B and not(C) and not(D) || E and F and not(G)
A= $_->[0]->openedB= $_->[1]->openedC= not $_->[0]->errorD= not $_->[1]->errorE= $_->[2]F= $_->[1]->openedG= not $_->[1]->error
ABCDEFGresult
1111XXX1
0XXX1111
10XX1111
110X1111
11101111
0XXX0XX0
0XXX10X0
0XXX1100
10XX0XX0
10XX10X0
10XX1100
110X0XX0
110X10X0
110X1100
11100XX0
111010X0
11101100
61 !$_->[0]->error && !$_->[1]->error
62 )
63 ||
64 ( $_->[2] && $_->[1]->opened && !$_->[1]->error )
65 9 } @{$self->{handles}};
66 #warn "out bound...";
67 return
68 9 scalar @list;
69 }
70  
71  
72 7 ▶sub rwcycle {
Subroutine rwcycle: called CC=26 SCAR=38.5
Pod documented
73 7 my $self = shift;
74 5 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 #warn "in rwcycle...";
76  
77 # we listen on all handles all the time for reading...
78 11 ▶ my @read_handles = grep { $_->opened && !$_->error }
Condition33%1/3
$_->opened && !$_->error
!ll&&!rl&&r
0011
MC/DC0%0/2
$_->opened && !$_->error
$_->opened !$_->error
Truth table33%1/3
A && not(B)
A= $_->openedB= !$_->error
ABresult
0X0
100
111
79 11 map { $_->[0] }
80 11 ▶ grep { $_->[1]->opened && !$_->[1]->error }
Condition33%1/3
$_->[1]->opened && !$_->[1]->error
!ll&&!rl&&r
0011
MC/DC0%0/2
$_->[1]->opened && !$_->[1]->error
$_->[1]->opened !$_->[1]->error
Truth table33%1/3
A && not(B)
A= $_->[1]->openedB= !$_->[1]->error
ABresult
0X0
100
111
81 7 @{$self->{handles}};
82 #warn "Selecting read on ".join ', ', @read_handles;
83 7 my $read_select = IO::Select->new(@read_handles);
84  
85  
86 # but we only listen for writing on those that we have
87 # something to write to.
88 3 ▶ my @write_handles = grep { $_->opened && !$_->error }
Condition33%1/3
$_->opened && !$_->error
!ll&&!rl&&r
003
MC/DC0%0/2
$_->opened && !$_->error
$_->opened !$_->error
Truth table33%1/3
A && not(B)
A= $_->openedB= !$_->error
ABresult
0X0
100
111
89 3 map { $_->[1] }
90 7 grep { $_->[2] } @{$self->{handles}};
91 #warn "Selecting write on ".join ', ', @write_handles;
92 7 my $write_select = IO::Select->new(@write_handles);
93  
94  
95 # we check for exception in all handles;
96 7 my @except_handles = (@read_handles, @write_handles);
97 #warn "Selecting exception on ".join ', ', @except_handles;
98 7 my $except_select = IO::Select->new(@except_handles);
99  
100  
101 # now let's see if there's something to be read or written
102 #warn "Select...";
103 7 my ($r_r, $r_w, $e) = IO::Select::select($read_select, $write_select, $except_select, $self->timeout);
104 #warn "done...";
105  
106 7 ▶ if ($r_r && scalar @$r_r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r_r and scalar @$r_r) 5 2 7
Condition50%1/2
$r_r and scalar @$r_r
!ll
07
MC/DC0%0/1
$r_r and scalar @$r_r
$r_r
Truth table50%1/2
A
A= $r_r
Aresult
00
11
107 5 foreach my $h (@$r_r) {
108 5 my ($h_desc) = grep { $_->[0] eq $h } @{$self->{handles}};
109 5 my $handle = $h_desc->[0];
110 5 my $temp_buf;
111 #warn "Reading from $handle.";
112 5 my $num_read = $handle->sysread($temp_buf, 1024);
113 5 ▶ if (defined $num_read && $num_read > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num_read and $num_read > 0) { } 3 2 5
Condition66%2/3
defined $num_read and $num_read > 0
!ll&&!rl&&r
023
MC/DC50%1/2
defined $num_read and $num_read > 0
defined $num_read $num_read > 0
Truth table66%2/3
A and B
A= defined $num_readB= $num_read > 0
ABresult
0X0
100
111
114 #warn "Read $num_read";
115 3 $h_desc->[2] .= substr($temp_buf, 0, $num_read);
116 } else {
117 #warn "Error...";
118 2 $handle->close;
119 2 $h_desc->[3] = 1;
120 2 ▶ $h_desc->[1]->close unless $h_desc->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $h_desc->[2] 1 1 2
121 }
122 }
123 }
124  
125 7 ▶ if ($r_w && scalar @$r_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r_w and scalar @$r_w) 3 4 7
Condition50%1/2
$r_w and scalar @$r_w
!ll
07
MC/DC0%0/1
$r_w and scalar @$r_w
$r_w
Truth table50%1/2
A
A= $r_w
Aresult
00
11
126 3 foreach my $h (@$r_w) {
127 3 my ($h_desc) = grep { $_->[1] eq $h } @{$self->{handles}};
128 3 my $handle = $h_desc->[1];
129 #warn "Writing on $handle.";
130 3 my $num_write = $handle->syswrite($h_desc->[2], length($h_desc->[2]));
131 3 ▶ if (defined $num_write && $num_write >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num_write and $num_write >= 0) { } 3 0 3
Condition33%1/3
defined $num_write and $num_write >= 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $num_write and $num_write >= 0
defined $num_write $num_write >= 0
Truth table33%1/3
A and B
A= defined $num_writeB= $num_write >= 0
ABresult
0X0
100
111
132 #warn "Wrote $num_write.";
133 3 substr($h_desc->[2],0,$num_write,'');
134 } else {
135 #warn "Error.";
136 0 $handle->close;
137 0 $h_desc->[0]->close;
138 }
139 }
140 }
141 7 ▶ if ($e && scalar @$e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e and scalar @$e) 0 7 7
Condition50%1/2
$e and scalar @$e
!ll
07
MC/DC0%0/1
$e and scalar @$e
$e
Truth table50%1/2
A
A= $e
Aresult
00
11
142 0 foreach my $h (@$r_w) {
143 #warn "Exception in $h";
144 0 ▶ my ($h_desc) = grep { $_->[1] eq $h || $_->[0] eq $h } @{$self->{handles}};
Condition0%0/3
$_->[1] eq $h || $_->[0] eq $h
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->[1] eq $h || $_->[0] eq $h
$_->[1] eq $h $_->[0] eq $h
Truth table0%0/3
A || B
A= $_->[1] eq $hB= $_->[0] eq $h
ABresult
1X1
011
000
145 # we close the writing handles unless the
146 # exception was in the reading handle and there
147 # is still buffered content to be sent.
148 0 ▶ unless ($h eq $h_desc->[0] && $h_desc->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h eq $h_desc->[0] and $h_desc->[2]) 0 0 0
Condition0%0/3
$h eq $h_desc->[0] and $h_desc->[2]
!ll&&!rl&&r
000
MC/DC0%0/2
$h eq $h_desc->[0] and $h_desc->[2]
$h eq $h_desc->[0] $h_desc->[2]
Truth table0%0/3
A and B
A= $h eq $h_desc->[0]B= $h_desc->[2]
ABresult
0X0
100
111
149 0 $h_desc->[1]->close;
150 }
151 # in any case we won't read anymore
152 0 $h_desc->[0]->close;
153 }
154 }
155  
156 7 foreach my $h (@{$self->{handles}}) {
157 11 ▶ if (exists $h->[3] && $h->[3] && !$h->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h->[3] and $h->[3] and not $h->[2]) 2 9 11
Condition66%4/6
exists $h->[3] and $h->[3] and not $h->[2]
!ll&&!rl&&r
902
exists $h->[3] and $h->[3]
!ll&&!rl&&r
902
MC/DC33%1/3
exists $h->[3] and $h->[3] and not $h->[2]
exists $h->[3] $h->[3] not $h->[2]
Truth table50%2/4
A and B and not(C)
A= exists $h->[3]B= $h->[3]C= not $h->[2]
ABCresult
0XX0
10X0
1100
1111
158 $h->[1]->close()
159 2 ▶ unless grep { $_->[0] eq $h->[1] } @{$self->{handles}}
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->[0] eq $h->[1];} @{$$self{"handles"};} 2 0 2
160 ;
161 }
162 }
163  
164 }
165  
166  
167 7 ▶sub timeout {
Subroutine timeout: called CC=2 SCAR=7.0
Pod documented
168 7 my $self = shift;
169 7 my $ret = $self->{timeout};
170 7 ▶ $self->{timeout} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 7 7
171 7 return $ret;
172 }
173  
174  
175 5 1;
176  
177 __END__
↑ Summary