Mux/Select.pm

filter: Statementstmt 100% 102 / 102 Branchbran 90.0% 27 / 30 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 15 / 15 Podpod 28.5% 2 / 7 totaltotal 93.9% 154 / 164 CC 24 SCAR 22.6
CC
24
Cov
97%
can_read
18.0
remove
4.0
add
3.0
CRAP 9.6
↑ Summary Mux/Packet.pm »
1 package IO::Mux::Select ;
2  
3 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::Select ;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::Mux ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Mux::Handle ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Mux::Packet ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 1 our $VERSION = '0.08' ;
12  
13  
14 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 1 my $class = shift ;
16  
17 1 my $this = {} ;
18 1 $this->{'select'} = new IO::Select() ;
19 1 $this->{mux_handles} = {} ;
20 1 bless($this, $class) ;
21  
22 1 $this->add(@_) ;
23  
24 1 return $this ;
25 }
26  
27  
28 50 ▶sub _get_select {
Subroutine _get_select: called CC=1 SCAR=0.0
29 50 my $this = shift ;
30  
31 50 return $this->{'select'} ;
32 }
33  
34  
35 42 ▶sub _get_mux_handles {
Subroutine _get_mux_handles: called CC=1 SCAR=0.0
36 42 my $this = shift ;
37  
38 42 return $this->{mux_handles} ;
39 }
40  
41  
42 4 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod undocumented
43 4 my $this = shift ;
44  
45 4 foreach my $h (@_){
46 3 ▶ if ($h->isa('IO::Mux::Handle')){
Branch: 1 branch
BranchTrueFalseTotal
if ($h->isa('IO::Mux::Handle')) { } 2 1 3
47 2 $this->_get_mux_handles()->{$h->_get_tie()->_get_id()} = $h ;
48 }
49 else {
50 1 $this->_get_select()->add($h) ;
51 }
52 }
53 }
54  
55  
56 3 ▶sub remove {
Subroutine remove: called CC=4 SCAR=13.9
Pod undocumented
57 3 my $this = shift ;
58  
59 3 foreach my $h (@_){
60 3 ▶ if ($h->isa('IO::Mux::Handle')){
Branch: 2 branches
BranchTrueFalseTotal
if ($h->isa('IO::Mux::Handle')) { } 1 2 3
elsif ($this->_get_select->exists($h)) { } 1 1 2
61 1 delete $this->_get_mux_handles()->{$h->_get_tie()->_get_id()} ;
62 }
63 elsif ($this->_get_select()->exists($h)){
64 1 $this->_get_select()->remove($h) ;
65 }
66 }
67 }
68  
69  
70 5 ▶sub exists {
Subroutine exists: called CC=2 SCAR=6.9
Pod undocumented
71 5 my $this = shift ;
72 5 my $h = shift ;
73  
74 5 ▶ if ($h->isa('IO::Mux::Handle')){
Branch: 1 branch
BranchTrueFalseTotal
if ($h->isa('IO::Mux::Handle')) { } 3 2 5
75 3 return $this->_get_mux_handles()->{$h->_get_tie()->_get_id()} ;
76 }
77 else {
78 2 return $this->_get_select()->exists($h) ;
79 }
80 }
81  
82  
83 3 ▶sub handles {
Subroutine handles: called CC=1 SCAR=0.0
Pod undocumented
84 3 my $this = shift ;
85  
86 3 my @ret = () ;
87 3 push @ret, values %{$this->_get_mux_handles()} ;
88 3 push @ret, $this->_get_select()->handles() ;
89  
90 3 return @ret ;
91 }
92  
93  
94 3 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod undocumented
95 3 my $this = shift ;
96  
97 3 return scalar($this->handles()) ;
98 }
99  
100  
101 15 ▶sub can_read {
Subroutine can_read: called CC=18 SCAR=28.9
Pod documented
102 15 my $this = shift ;
103 15 my $timeout = shift ;
104  
105 # First, we will check to see if the IO::Mux::Handles have data in their buffers.
106 15 my @ready = () ;
107 15 foreach my $h (values %{$this->_get_mux_handles()}){
108 17 ▶ if ((eof($h))||($h->_get_tie()->_get_buffer()->get_length() > 0)){
Branch: 1 branch
BranchTrueFalseTotal
if (eof $h or $h->_get_tie->_get_buffer->get_length > 0) 2 15 17
Condition100%3/3
eof $h or $h->_get_tie->_get_buffer->get_length > 0
l!l&&r!l&&!r
1115
MC/DC100%2/2
eof $h or $h->_get_tie->_get_buffer->get_length > 0
eof $h $h->_get_tie->_get_buffer->get_length > 0
Truth table100%3/3
A or B
A= eof $hB= $h->_get_tie->_get_buffer->get_length > 0
ABresult
1X1
011
000
109 2 push @ready, $h ;
110 }
111 }
112  
113 15 ▶ if (scalar(@ready)){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ready) 2 13 15
114 # Maybe some real handles are immediately ready
115 2 push @ready, $this->_get_select()->can_read(0) ;
116 2 return @ready ;
117 }
118  
119 # So it seems we may have to wait after all. We now need to build a list
120 # of all the REAL handles underneath all the IO::Mux::Handles.
121 13 my %mux_objects = () ;
122 13 foreach my $h (values %{$this->_get_mux_handles()}){
123 14 my $mux = $h->_get_tie()->_get_mux() ;
124 14 my $rh = $mux->_get_handle() ;
125 14 ▶ if (! exists($mux_objects{$rh})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $mux_objects{$rh}) 12 2 14
126 12 $mux_objects{$rh} = {mux => $mux, mux_handles => {}} ;
127 }
128 14 $mux_objects{$rh}->{mux_handles}->{$h} = $h ;
129 }
130  
131 13 my @real_handles = map {$_->{mux}->_get_handle()} values(%mux_objects) ;
132 13 $this->_get_select()->add(@real_handles) ;
133 13 @ready = $this->_get_select()->can_read($timeout) ;
134 13 $this->_get_select()->remove(@real_handles) ;
135  
136 13 ▶ if (scalar(@ready)){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ready) 9 4 13
137 9 my @tmp = @ready ;
138 9 my %ready = () ;
139 9 @ready = () ;
140 9 foreach my $h (@tmp){
141 10 my $mux_data = $mux_objects{$h} ;
142 10 ▶ if ($mux_data){
Branch: 1 branch
BranchTrueFalseTotal
if ($mux_data) { } 6 4 10
143 6 my $mux = $mux_data->{mux} ;
144 # We have data ready on the REAL handle. Let's consume the packet
145 # and add the corresponding IO::Mux::Handle in the new ready list.
146 6 ▶ while ((my $p = $mux->_read_packet(0)) != -1){
Branch: 1 branch
BranchTrueFalseTotal
if ((my $p = $mux->_read_packet(0)) != -1) 7 4 11
147 7 ▶ if ((! defined($p))||(! $p)){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $p && $p) { } 2 5 7
Condition66%2/3
defined $p && $p
!ll&&!rl&&r
025
MC/DC50%1/2
defined $p && $p
defined $p $p
Truth table66%2/3
A && B
A= defined $pB= $p
ABresult
0X0
100
111
148 # ERROR or EOF on the real handle. Return all mux_handles
149 # as they all now are at EOF or have an error state.
150 2 foreach my $mh (values %{$mux_data->{mux_handles}}){
151 2 ▶ if (! $ready{$mh}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready{$mh}) 2 0 2
152 2 push @ready, $mh ;
153 2 $ready{$mh} = 1 ;
154 }
155 }
156 2 last ;
157 }
158 else {
159 5 my $mh = $this->_get_mux_handles()->{$p->get_id()} ;
160 5 ▶ next unless defined($mh) ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mh 0 5 5
161 5 ▶ if (! $ready{$mh}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready{$mh}) 5 0 5
162 5 push @ready, $mh ;
163 5 ▶ if ($p->is_eof()){
Branch: 1 branch
BranchTrueFalseTotal
if ($p->is_eof) 1 4 5
164 1 $mh->_get_tie()->_set_eof() ;
165 }
166 5 $ready{$mh} = 1 ;
167 }
168 }
169 }
170 }
171 else {
172 # REAL handle, we simply push it.
173 4 push @ready, $h ;
174 }
175 }
176 }
177  
178 13 return @ready ;
179 }
180  
181  
182 1 1 ;
183 __END__
↑ Summary Mux/Packet.pm »