Mux/Handle.pm

filter: Statementstmt 97.3% 180 / 185 Branchbran 91.1% 62 / 68 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 96.9% 32 / 33 Podpod 100% 3 / 3 totaltotal 96.0% 288 / 300 CC 38 SCAR 17.9
CC
38
Cov
96%
READLINE
13.0
READ
12.1
_read_more_data
6.0
CRAP 6.0
« Mux/Packet.pm ↑ Summary Mux.pm »
1 package IO::Mux::Handle ;
2 3 @ISA = qw(IO::Handle) ;
3  
4 3 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use IO::Handle ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 3 our $VERSION = '0.08' ;
9  
10  
11 12 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 12 my $class = shift ;
13 12 my $mux = shift ;
14  
15 12 my $this = $class->SUPER::new() ;
16 12 tie(*{$this}, 'IO::Mux::Tie::Handle', $mux) ;
17  
18 12 return $this ;
19 }
20  
21  
22 13 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
Pod documented
23 13 my $this = shift ;
24 13 my $id = shift ;
25  
26 13 return open($this, $id) ;
27 }
28  
29  
30 2 ▶sub get_error {
Subroutine get_error: called CC=1 SCAR=0.0
Pod documented
31 2 my $this = shift ;
32  
33 2 return $this->_get_tie()->_get_error() ;
34 }
35  
36  
37 39 ▶sub _get_tie {
Subroutine _get_tie: called CC=1 SCAR=0.0
38 39 my $this = shift ;
39  
40 39 return tied(*{$this}) ;
41 }
42  
43  
44  
45 #################################################
46 package IO::Mux::Tie::Handle ;
47 3 @IO::Mux::Tie::Handle::ISA = qw(Tie::Handle) ;
48  
49 3 ▶use Tie::Handle ;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use IO::Mux::Packet ;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use Errno ;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53  
54 12 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
55 12 my $class = shift ;
56 12 my $mux = shift ;
57  
58 12 return $class->new($mux) ;
59 }
60  
61  
62 12 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
63 12 my $class = shift ;
64 12 my $mux = shift ;
65  
66 12 my $this = {} ;
67 12 $this->{mux} = $mux ;
68 12 $this->{id} = undef ;
69 12 $this->{closed} = 1 ;
70 12 $this->{'eof'} = 0 ;
71 12 $this->{error} = undef ;
72  
73 12 bless($this, $class) ;
74 }
75  
76  
77 19 ▶sub OPEN {
Subroutine OPEN: called CC=2 SCAR=6.9
78 19 my $this = shift ;
79 19 my $id = shift ;
80  
81 19 $this->CLOSE() ;
82  
83 19 $id =~ s/\t/ /g ; # no \t's allowed in the id
84 19 ▶ if ($this->_get_mux()->_buffer_exists($id)){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->_get_mux->_buffer_exists($id)) 1 18 19
85 1 $this->_set_error("Id '$id' is already in use by another handle") ;
86 1 return undef ;
87 }
88  
89 18 $this->{id} = $id ;
90 # Allocate the buffer
91 18 $this->_get_mux()->_get_buffer($id) ;
92 18 $this->{closed} = 0 ;
93 18 $this->{'eof'} = 0 ;
94 18 $this->{error} = undef ;
95  
96 18 return 1 ;
97 }
98  
99  
100 277 ▶sub _get_mux {
Subroutine _get_mux: called CC=1 SCAR=0.0
101 277 my $this = shift ;
102  
103 277 return $this->{mux} ;
104 }
105  
106  
107 215 ▶sub _get_id {
Subroutine _get_id: called CC=1 SCAR=0.0
108 215 my $this = shift ;
109  
110 215 return $this->{id} ;
111 }
112  
113  
114 51 ▶sub _get_eof {
Subroutine _get_eof: called CC=1 SCAR=0.0
115 51 my $this = shift ;
116  
117 51 return $this->{'eof'} ;
118 }
119  
120  
121 7 ▶sub _set_eof {
Subroutine _set_eof: called CC=1 SCAR=0.0
122 7 my $this = shift ;
123  
124 7 $this->{'eof'} = 1 ;
125 }
126  
127  
128 2 ▶sub _get_error {
Subroutine _get_error: called CC=1 SCAR=0.0
129 2 my $this = shift ;
130  
131 2 return $this->{error} ;
132 }
133  
134  
135 9 ▶sub _set_error {
Subroutine _set_error: called CC=2 SCAR=7.2
136 9 my $this = shift ;
137 9 my $msg = shift ;
138  
139 9 $this->{error} = $msg ;
140 9 ▶ if (exists($!{EIO})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $!{'EIO'}) { } 9 0 9
141 9 $! = Errno::EIO() ;
142 }
143 else {
144 0 $! = 99999 ;
145 }
146 }
147  
148  
149 130 ▶sub _get_buffer {
Subroutine _get_buffer: called CC=1 SCAR=0.0
150 130 my $this = shift ;
151  
152 130 return $this->_get_mux()->_get_buffer($this->_get_id()) ;
153 }
154  
155  
156 18 ▶sub _kill_buffer {
Subroutine _kill_buffer: called CC=1 SCAR=0.0
157 18 my $this = shift ;
158  
159 18 return $this->_get_mux()->_kill_buffer($this->_get_id()) ;
160 }
161  
162  
163  
164 28 ▶sub WRITE {
Subroutine WRITE: called CC=3 SCAR=11.0
165 28 my $this = shift ;
166 28 my ($buf, $len, $offset) = @_ ;
167  
168 28 ▶ if ($this->{closed}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"closed"}) 3 25 28
169 3 $this->_set_error("WRITE on closed filehandle") ;
170 3 return undef ;
171 }
172  
173 25 ▶ my $p = new IO::Mux::Packet($this->_get_id(), substr($buf, $offset || 0, $len)) ;
Condition100%2/2
$offset || 0
l!l
124
MC/DC100%1/1
$offset || 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
174 25 my $rc = $this->_get_mux()->_write($p) ;
175  
176 25 return $rc ;
177 }
178  
179  
180 15 ▶sub READ {
Subroutine READ: called CC=12 SCAR=24.9
181 15 my $this = shift ;
182 15 my ($buf, $len, $offset) = @_ ;
183  
184 15 ▶ if ($this->{closed}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"closed"}) 1 14 15
185 1 $this->_set_error("READ on closed filehandle") ;
186 1 return undef ;
187 }
188 14 ▶ return 0 if $this->_get_eof() ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->_get_eof 1 13 14
189  
190 # Load the buffer until there is enough data or EOF.
191 #while ($this->_get_buffer()->get_length() < $len){
192  
193 # We must block if the buffer is empty, otherwise we just check
194 # if there is something pending.
195 13 my $probe = 1 ;
196 13 ▶ if (! $this->_get_buffer()->get_length()){
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_get_buffer->get_length) 7 6 13
197 7 my $rc = $this->_read_more_data(1) ;
198 7 ▶ if (! defined($rc)){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rc) { } 0 7 7
elsif (not $rc) { } 3 4 7
199 0 return undef ; # error already set by read_more_data
200 }
201 elsif (! $rc){
202 # EOF
203 3 $probe = 0 ;
204 }
205 }
206  
207 13 ▶ if ($probe){
Branch: 1 branch
BranchTrueFalseTotal
if ($probe) 10 3 13
208 10 my $rc = 1 ;
209 10 ▶ while ($rc > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) 12 10 22
210 12 $rc = $this->_read_more_data(0) ;
211 }
212  
213 10 ▶ if (! defined($rc)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 10 10
214 0 return undef ; # error already set by read_more_data
215 }
216 }
217  
218 # Shorten the length if we hit EOF...
219 13 ▶ if ($this->_get_buffer()->get_length() < $len){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->_get_buffer->get_length < $len) 4 9 13
220 4 $len = $this->_get_buffer()->get_length() ;
221 }
222  
223 13 ▶ if ($len > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 10 3 13
224 # Extract $len bytes from the beginning of the buffer and
225 10 my $data = $this->_get_buffer()->shift_data($len) ;
226 10 ▶ substr($buf, $offset || 0, $len) = $data ;
Condition100%2/2
$offset || 0
l!l
19
MC/DC100%1/1
$offset || 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
227 10 $_[0] = $buf ;
228 }
229  
230 13 return $len ;
231 }
232  
233  
234 17 ▶sub READLINE {
Subroutine READLINE: called CC=13 SCAR=25.6
235 17 my $this = shift ;
236  
237 17 ▶ if ($this->{closed}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"closed"}) 1 16 17
238 1 $this->_set_error("READLINE on closed filehandle") ;
239 1 return undef ;
240 }
241 16 ▶ return (wantarray ? () : undef) if $this->_get_eof() ;
Branch: 2 branches
BranchTrueFalseTotal
if $this->_get_eof 3 13 16
wantarray ? : 1 2 3
242  
243 13 my @ret = () ;
244 13 while (1){
245 15 my $idx = -1 ;
246 15 my $buf = undef ;
247 15 ▶ while ((! length($/))||(($idx = index($this->_get_buffer()->get_data(), $/)) == -1)){
Branch: 1 branch
BranchTrueFalseTotal
if (not length $/ or ($idx = index($this->_get_buffer->get_data, $/)) == -1) 16 11 27
Condition100%3/3
not length $/ or ($idx = index($this->_get_buffer->get_data, $/)) == -1
l!l&&r!l&&!r
41211
MC/DC100%2/2
not length $/ or ($idx = index($this->_get_buffer->get_data, $/)) == -1
not length $/ ($idx = index($this->_get_buffer->get_data, $/)) == -1
Truth table100%3/3
not(A) or B
A= not length $/B= ($idx = index($this->_get_buffer->get_data, $/)) == -1
ABresult
1X1
011
000
248 16 my $rc = $this->_read_more_data(1) ;
249 16 ▶ if (! defined($rc)){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rc) { } 1 15 16
elsif (not $rc) { } 3 12 15
250 # Return what we got or return undef/() ?
251 1 last ; # error already set by read_more_data
252 }
253 elsif (! $rc){
254 # EOF
255 3 last ;
256 }
257 }
258  
259 15 ▶ if ($idx != -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($idx != -1) { } 11 4 15
260 11 $buf = $this->_get_buffer()->shift_data($idx + length($/)) ;
261 }
262 else {
263 # Empty the buffer
264 4 my $len = $this->_get_buffer()->get_length() ;
265 4 ▶ if ($len){
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 1 3 4
266 1 $buf = $this->_get_buffer()->shift_data($len) ;
267 }
268 }
269  
270 15 ▶ if (defined($buf)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buf) { } 12 3 15
271 12 push @ret, $buf ;
272 12 ▶ last unless wantarray ;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 10 2 12
273 }
274 else {
275 3 last ;
276 }
277 }
278  
279 13 ▶ return (wantarray ? @ret : $ret[0]) ;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 12 13
280 }
281  
282  
283 35 ▶sub _read_more_data {
Subroutine _read_more_data: called CC=6 SCAR=17.9
284 35 my $this = shift ;
285 35 my $blocking = shift ;
286  
287 35 ▶ if ($this->_get_buffer()->is_closed()){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->_get_buffer->is_closed) 1 34 35
288 # The handle is closed.
289 1 $this->_set_eof() ;
290 1 return 0 ;
291 }
292  
293 34 my $rc = undef ;
294 34 eval {
295 34 $rc = $this->_get_mux()->_read($this->_get_id(), $blocking) ;
296 } ;
297 34 ▶ if ($@){
Branch: 4 branches
BranchTrueFalseTotal
if ($@) { } 1 33 34
elsif (not defined $rc) { } 0 33 33
elsif ($rc == -1) { } 10 23 33
elsif (not $rc) { } 5 18 23
298 1 $this->_set_error($@) ;
299 1 return undef ;
300 }
301 elsif (! defined($rc)){
302 0 return $rc ;
303 }
304 elsif ($rc == -1){
305 # No data available in non-blocking mode.
306 10 return -1 ;
307 }
308 elsif (! $rc){
309 # We have reached EOF.
310 5 $this->_set_eof() ;
311 }
312
313 23 return $rc ;
314 }
315  
316  
317 22 ▶sub EOF {
Subroutine EOF: called CC=2 SCAR=6.9
318 22 my $this = shift ;
319  
320 22 ▶ return 1 if $this->{closed} ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"closed"} 1 21 22
321 21 return $this->_get_eof() ;
322 }
323  
324  
325 43 ▶sub CLOSE {
Subroutine CLOSE: called CC=2 SCAR=7.0
326 43 my $this = shift ;
327  
328 43 my $ret = 0 ;
329 43 ▶ if (! $this->{closed}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"closed"}) 18 25 43
330 18 my $p = new IO::Mux::Packet($this->{id}, 0) ;
331 18 $p->make_eof() ;
332 # Here the real filehandle is possibly closed, so we must silence
333 # the warning. We may also get a SIGPIPE, which we will solve
334 # by closing the real handle.
335 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
336 0 ▶ warn $_[0] unless ($_[0] =~ /closed filehandle/i) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /closed filehandle/i 0 0 0
337 18 } ;
338 1 ▶ local $SIG{PIPE} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
339 1 close($this->_get_mux()->_get_handle()) ;
340 18 } ;
341  
342 18 $ret = $this->_get_mux()->_write($p) ;
343 18 $this->{closed} = 1 ;
344 18 $this->_kill_buffer() ;
345 18 return 1 ;
346 }
347  
348 25 return $ret ;
349 }
350  
351  
352 2 ▶sub SEEK {
Subroutine SEEK: called CC=1 SCAR=0.0
353 2 my $this = shift ;
354 2 my $pos = shift ;
355 2 my $whence = shift ;
356  
357 2 return 0 ;
358 }
359  
360  
361 2 ▶sub BINMODE {
Subroutine BINMODE: called CC=2 SCAR=6.9
362 2 my $this = shift ;
363  
364 2 ▶ if ($this->{closed}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"closed"}) 1 1 2
365 1 $this->_set_error("BINMODE on closed filehandle") ;
366 1 return undef ;
367 }
368  
369 1 return 1 ;
370 }
371  
372  
373 2 ▶sub FILENO {
Subroutine FILENO: called CC=1 SCAR=0.0
374 2 my $this = shift ;
375  
376 2 return $this->_get_id() ;
377 }
378  
379  
380 2 ▶sub TELL {
Subroutine TELL: called CC=2 SCAR=6.9
381 2 my $this = shift ;
382  
383 2 ▶ if ($this->{closed}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"closed"}) 1 1 2
384 1 $this->_set_error("TELL on closed filehandle") ;
385 1 return -1 ;
386 }
387  
388 1 return 0 ;
389 }
390  
391  
392 12 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
393 12 my $this = shift ;
394  
395 12 $this->CLOSE() ;
396 }
397  
398  
399  
400 3 1 ;
401 __END__
« Mux/Packet.pm ↑ Summary Mux.pm »