IOMux.pm

filter: Statementstmt 79.3% 69 / 87 Branchbran 36.9% 17 / 46 Conditioncond 20.0% 3 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 80.0% 16 / 20 Podpod 90.9% 10 / 11 totaltotal 61.3% 116 / 189 CC 30 SCAR 27.7
CC
30
Cov
60%
changeTimeout
72.0
_checkTimeouts
11.8
loop
8.6
CRAP 16.0
« IOMux/IPC.pm ↑ Summary IOMux/File/Write.pm »
1 # Copyrights 2011-2020 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution IOMux. Meta-POD processed with OODoc
6 # into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package IOMux;
10 8 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 $VERSION = '1.01';
12  
13 8 ▶use Log::Report 'iomux';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use POSIX 'errno_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 $SIG{PIPE} = 'IGNORE'; # pipes are handled in mux
22  
23 use constant
24 8 ▶ { LONG_TIMEOUT => 60 # no-one has set a timeout
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 };
26  
27  
28 sub new(@)
29 7 ▶{ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
30  
31 7 ▶ error __x"initiate an extension of {pkg}", pkg => __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ 0 7 7
32 if $class eq __PACKAGE__;
33  
34 7 (bless {}, $class)->init(\%args);
35 }
36  
37 sub init($)
38 7 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
39 7 $self->{IM_handlers} = {};
40 7 $self->{IM_timeouts} = {};
41 7 $self;
42 }
43  
44 #-------------
45  
46 #-------------
47  
48 # add() is the main user interface to mux, because from then the
49 # user works with connection objects. Therefore, offer some extra
50 # features here.
51  
52 sub add($)
53 12 ▶{ my ($self, $handler) = @_;
Subroutine add: called CC=3 SCAR=13.3
Pod documented
54  
55 12 ▶ UNIVERSAL::isa($handler, 'IOMux::Handler')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($handler, "IOMux::Handler") or &error(&__x("attempt to add non handler {pkg}", "pkg", ref $handler || $handler)) 12 0 12
Condition0%0/3
ref $handler || $handler
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $handler || $handler
ref $handler $handler
Truth table0%0/3
A || B
A= ref $handlerB= $handler
ABresult
1X1
011
000
56 or error __x"attempt to add non handler {pkg}"
57 , pkg => (ref $handler || $handler);
58  
59 12 $handler->muxInit($self);
60 12 $handler;
61 }
62  
63  
64 sub open(@)
65 4 ▶{ my $self = shift;
Subroutine open: called CC=3 SCAR=11.3
Pod documented
66 4 ▶ IOMux::Open->can('new')
Branch: 1 branch
BranchTrueFalseTotal
"IOMux::Open"->can("new") or &error(&__x("IOMux::Open not loaded")) 4 0 4
67 or error __x"IOMux::Open not loaded";
68 4 my $conn = IOMux::Open->new(@_);
69 4 ▶ $self->add($conn) if $conn;
Branch: 1 branch
BranchTrueFalseTotal
if $conn 4 0 4
70 4 $conn;
71 }
72  
73  
74 sub loop(;$)
75 4 ▶{ my($self, $heartbeat) = @_;
Subroutine loop: called CC=8 SCAR=21.5
Pod documented
76 4 $self->{IM_endloop} = 0;
77  
78 4 my $handlers = $self->{IM_handlers};
79 4 ▶ keys %$handlers
Branch: 1 branch
BranchTrueFalseTotal
keys %$handlers or &error(&__x("there are no handlers for the mux loop")) 4 0 4
80 or error __x"there are no handlers for the mux loop";
81  
82 LOOP:
83 4 ▶ while(!$self->{IM_endloop} && keys %$handlers)
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"IM_endloop"} and keys %$handlers) 12 4 16
Condition66%2/3
not $self->{"IM_endloop"} and keys %$handlers
!ll&&!rl&&r
0412
MC/DC50%1/2
not $self->{"IM_endloop"} and keys %$handlers
not $self->{"IM_endloop"} keys %$handlers
Truth table66%2/3
not(A) and B
A= not $self->{"IM_endloop"}B= keys %$handlers
ABresult
0X0
100
111
84 {
85 # while(my($fileno, $conn) = each %$handlers)
86 # { $conn->read
87 # if $conn->usesSSL && $conn->pending;
88 # }
89  
90 12 my $timeout = $self->{IM_next_timeout};
91 12 ▶ my $wait = defined $timeout ? $timeout-time : LONG_TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 0 12 12
92  
93 # For negative values, still give select a chance, to avoid
94 # starvation when timeout handling starts consuming all
95 # processor time.
96 12 ▶ $wait = 0.001 if $wait < 0.001;
Branch: 1 branch
BranchTrueFalseTotal
if $wait < "0.001" 0 12 12
97  
98 12 ▶ $self->one_go($wait, $heartbeat)
Branch: 1 branch
BranchTrueFalseTotal
$self->one_go($wait, $heartbeat) or last LOOP 12 0 12
99 or last LOOP;
100  
101 12 $self->_checkTimeouts($timeout);
102 }
103  
104 $_->close
105 4 for values %$handlers;
106 }
107  
108  
109 0 ▶sub endLoop($) { $_[0]->{IM_endloop} = $_[1] }
Subroutine endLoop: not called CC=1 SCAR=6.9
Pod documented
110  
111 #-------------
112  
113 0 ▶sub handlers() {values %{shift->{IM_handlers}}}
Subroutine handlers: not called CC=1 SCAR=6.9
Pod documented
114 36 ▶sub _handlers() {shift->{IM_handlers}}
Subroutine _handlers: called CC=1 SCAR=0.0
115  
116  
117 sub handler($;$)
118 45 ▶{ my $hs = shift->{IM_handlers};
Subroutine handler: called CC=3 SCAR=11.0
Pod documented
119 45 my $fileno = shift;
120 45 ▶ @_ or return $hs->{$fileno};
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $hs->{$fileno} 13 32 45
121 13 ▶ (defined $_[0]) ? ($hs->{$fileno} = shift) : (delete $hs->{$fileno});
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 13 0 13
122 }
123  
124  
125 sub remove($)
126 13 ▶{ my ($self, $fileno) = @_;
Subroutine remove: called CC=4 SCAR=15.9
Pod documented
127  
128 13 ▶ my $obj = delete $self->{IM_handlers}{$fileno}
Branch: 1 branch
BranchTrueFalseTotal
my $obj = delete $self->{"IM_handlers"}{$fileno} or return $self 13 0 13
129 or return $self;
130  
131 13 $self->fdset($fileno, 0, 1, 1, 1);
132 13 $obj->muxRemove;
133  
134 13 ▶ if(my $timeout = delete $self->{IM_timeouts}{$fileno})
Branch: 1 branch
BranchTrueFalseTotal
if (my $timeout = delete $self->{"IM_timeouts"}{$fileno}) 0 13 13
135 { delete $self->{IM_next_timeout}
136 0 ▶ if $self->{IM_next_timeout}==$timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"IM_next_timeout"} == $timeout 0 0 0
137 }
138  
139 13 $self;
140 }
141  
142  
143 0 ▶sub fdset($$$$$) {panic}
Subroutine fdset: not called CC=1 SCAR=6.9
Pod documented
144  
145  
146 sub changeTimeout($$$)
147 0 ▶{ my ($self, $fileno, $old, $when) = @_;
Subroutine changeTimeout: not called CC=8 SCAR=42.8
Pod documented
148 0 ▶ return if $old==$when;
Branch: 1 branch
BranchTrueFalseTotal
if $old == $when 0 0 0
149  
150 0 my $next = $self->{IM_next_timeout};
151 0 ▶ if($old)
Branch: 1 branch
BranchTrueFalseTotal
if ($old) 0 0 0
152 { # next timeout will be recalculated max once per loop
153 0 delete $self->{IM_timeouts}{$fileno};
154 0 ▶ $self->{IM_next_timeout} = $next = undef if $next && $next==$old;
Branch: 1 branch
BranchTrueFalseTotal
if $next and $next == $old 0 0 0
Condition0%0/3
$next and $next == $old
!ll&&!rl&&r
000
MC/DC0%0/2
$next and $next == $old
$next $next == $old
Truth table0%0/3
A and B
A= $nextB= $next == $old
ABresult
0X0
100
111
155 }
156  
157 0 ▶ if($when)
Branch: 1 branch
BranchTrueFalseTotal
if ($when) 0 0 0
158 0 ▶ { $self->{IM_next_timeout} = $when if !$next || $next > $when;
Branch: 1 branch
BranchTrueFalseTotal
if not $next or $next > $when 0 0 0
Condition0%0/3
not $next or $next > $when
l!l&&r!l&&!r
000
MC/DC0%0/2
not $next or $next > $when
not $next $next > $when
Truth table0%0/3
not(A) or B
A= not $nextB= $next > $when
ABresult
1X1
011
000
159 0 $self->{IM_timeouts}{$fileno} = $when;
160 }
161 }
162  
163 # handle all timeouts which have expired either during the select
164 # or during the processing of flags.
165 sub _checkTimeouts($)
166 12 ▶{ my ($self, $next) = @_;
Subroutine _checkTimeouts: called CC=6 SCAR=24.7
167  
168 12 my $now = time;
169 12 ▶ if($next && $now < $next)
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $now < $next) 0 12 12
Condition33%1/3
$next and $now < $next
!ll&&!rl&&r
1200
MC/DC0%0/2
$next and $now < $next
$next $now < $next
Truth table33%1/3
A and B
A= $nextB= $now < $next
ABresult
0X0
100
111
170 { # Even when next is cancelled, none can have expired.
171 # However, a new timeout may have arrived which may expire immediately.
172 0 ▶ return $next if $self->{IM_next_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"IM_next_timeout"} 0 0 0
173 }
174  
175 12 my $timo = $self->{IM_timeouts};
176 12 my $hnd = $self->{IM_handlers};
177 12 ▶ while(my ($fileno, $when) = each %$timo)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$timo) 0 12 12
178 0 ▶ { $when <= $now or next;
Branch: 1 branch
BranchTrueFalseTotal
$when <= $now or next 0 0 0
179 0 $hnd->{$fileno}->muxTimeout($self);
180 0 delete $timo->{$fileno};
181 }
182  
183 12 $self->{IM_next_timeout} = min values %$timo;
184 }
185  
186 8 1;
187  
188 __END__
« IOMux/IPC.pm ↑ Summary IOMux/File/Write.pm »