MPS.pm

filter: Statementstmt 70.1% 230 / 328 Branchbran 37.7% 65 / 172 Conditioncond 22.0% 13 / 59 MC/DCmcdc 5.5% 2 / 36 Subroutinesub 81.8% 18 / 22 Podpod 0.0% 0 / 11 totaltotal 52.2% 328 / 628 CC 114 SCAR 56.3
CC
114
Cov
55%
ipc_loop
529.0
_pack_unpack
132.0
listener
30.0
CRAP 278.8
↑ Summary MPS/Event.pm »
1 package IPC::MPS;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our @ISA = qw(Exporter);
8 2 our @EXPORT = qw(spawn receive msg snd quit wt snd_wt listener open_node vpid2pid);
9  
10 2 our $VERSION = '0.21';
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Socket qw(SOCK_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Storable qw(freeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 2 my $DEBUG = 0;
21 2 ▶$DEBUG and require Data::Dumper;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and require Data::Dumper 0 2 2
22  
23 2 my $sel = IO::Select->new();
24  
25 2 my @spawn = ();
26 2 my %msg = ();
27 2 my %fh2vpid = ();
28 2 my %vpid2fh = ();
29 2 my %fh2fh = ();
30 2 my $self_vpid = 0;
31 2 my $self_parent_fh;
32 2 my $self_parent_vpid = 0;
33 2 my $self_parent_closed = 0;
34 2 my %listener = ();
35 2 my %node = ();
36 2 my %snd = ();
37 2 my $ipc_loop = 0;
38 2 my $quit = 0;
39  
40 2 my %vpid2pid = ();
41 1 ▶sub vpid2pid { my ($vpid) = @_; $vpid2pid{$vpid} }
Subroutine vpid2pid: called CC=1 SCAR=0.0
Pod undocumented
42  
43 2 my @rcv = ();
44 2 my %r_bufs = ();
45 2 my %w_bufs = ();
46  
47 2 my %pack = ();
48 2 my %unpack = ();
49  
50 2 my %closed = ();
51  
52 2 my $need_reset = 0;
53  
54 2 my $blksize = 1024 * 16;
55  
56  
57 2 ▶END {
Subroutine END: called CC=4 SCAR=19.1
58 2 ▶ $ipc_loop or @spawn and carp "Probably have forgotten to call receive.";
Branch: 1 branch
BranchTrueFalseTotal
$ipc_loop or @spawn and carp("Probably have forgotten to call receive.") 2 0 2
Condition20%1/5
$ipc_loop || @spawn && carp('Probably have forgotten to call receive.')
l!l
20
@spawn and carp("Probably have forgotten to call receive.")
!ll&&!rl&&r
000
MC/DC0%0/3
$ipc_loop || @spawn && carp('Probably have forgotten to call receive.')
$ipc_loop @spawn carp("Probably have forgotten to call receive.")
Truth table0%0/4
A || B and C
A= $ipc_loopB= @spawnC= carp("Probably have forgotten to call receive.")
ABCresult
1XX1
0111
00X0
0100
59 2 close $_ foreach values %fh2fh;
60 }
61  
62 2 ▶sub spawn(&) {
Subroutine spawn: called CC=2 SCAR=7.0
Pod undocumented
63 2 my ($spawn) = @_;
64 2 ▶ socketpair(my $child, my $parent, AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, PF_UNSPEC) or die "socketpair: $!";
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $child, my $parent, 1, 2049, 0 or die "socketpair: $!" 2 0 2
65 2 my $vpid = refaddr $child;
66 2 push @spawn, [$vpid, $child, $parent, $spawn];
67 2 return $vpid;
68 }
69  
70  
71 3 ▶sub msg($$) {
Subroutine msg: called CC=1 SCAR=0.0
Pod undocumented
72 3 my ($msg, $sub) = @_;
73 3 $msg{$msg} = $sub;
74 }
75  
76  
77 9 ▶sub snd($$;@) {
Subroutine snd: called CC=7 SCAR=20.8
Pod undocumented
78 9 my ($vpid, $msg, @args) = @_;
79 9 ▶ defined $vpid or carp("Argument vpid required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $vpid or carp("Argument vpid required"), return 9 0 9
80 9 ▶ defined $msg or carp("Argument msg required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $msg or carp("Argument msg required"), return 9 0 9
81 9 ▶ $vpid = $self_parent_vpid if $vpid == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $vpid == 0 4 5 9
82 9 ▶ $DEBUG and print "Send message '$msg' from $self_vpid to $vpid vpid in $self_vpid (\$\$=$$) with args: ", join(", ", @args), ".\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Send message '${msg}' from $self_vpid to $vpid vpid in $self_vpid (\$\$=${$}) with args: ", join(", ", @args), ".\n" 0 9 9
83 9 push @{$snd{$vpid}}, [$self_vpid, $vpid, $msg, \@args];
84 9 ▶ $closed{$vpid} = 1 if $msg eq "close" or $msg eq "exit";
Branch: 1 branch
BranchTrueFalseTotal
if $msg eq "close" or $msg eq "exit" 0 9 9
Condition33%1/3
$msg eq "close" or $msg eq "exit"
l!l&&r!l&&!r
009
MC/DC0%0/2
$msg eq "close" or $msg eq "exit"
$msg eq "close" $msg eq "exit"
Truth table33%1/3
A or B
A= $msg eq "close"B= $msg eq "exit"
ABresult
1X1
011
000
85 9 return 1;
86 }
87  
88  
89 0 ▶sub quit() { $quit = 1 }
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
90  
91  
92 1 ▶sub snd_wt($$;@) {
Subroutine snd_wt: called CC=3 SCAR=11.3
Pod undocumented
93 1 my ($vpid, $msg, @args) = @_;
94 1 ▶ defined $vpid or carp("Argument vpid required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $vpid or carp("Argument vpid required"), return 1 0 1
95 1 ▶ defined $msg or carp("Argument msg required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $msg or carp("Argument msg required"), return 1 0 1
96 1 snd($vpid, $msg, @args);
97 1 wt($vpid, $msg);
98 }
99  
100  
101 0 ▶sub listener($$;%) {
Subroutine listener: not called CC=5 SCAR=34.0
Pod undocumented
102 0 my ($host, $port, %args) = @_;
103 0 ▶ defined $host or carp("Argument host required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $host or carp("Argument host required"), return 0 0 0
104 0 ▶ defined $port or carp("Argument port required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $port or carp("Argument port required"), return 0 0 0
105 0 my $sock = IO::Socket::INET->new(Proto => 'tcp', Blocking => 0, LocalHost => $host, LocalPort => $port, Listen => 20, ReuseAddr => 1);
106 0 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) { } 0 0 0
107 0 ▶ _pack_unpack($sock, %args) or return;
Branch: 1 branch
BranchTrueFalseTotal
&_pack_unpack($sock, %args) or return 0 0 0
108 0 $listener{$sock} = $sock;
109 0 $sel->add($sock);
110 0 return $sock;
111 } else {
112 0 carp "Cannot open socket '$host:$port' in $self_vpid: $!";
113 0 return;
114 }
115 }
116  
117  
118 0 ▶sub open_node($$;%) {
Subroutine open_node: not called CC=5 SCAR=34.0
Pod undocumented
119 0 my ($host, $port, %args) = @_;
120 0 ▶ defined $host or carp("Argument host required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $host or carp("Argument host required"), return 0 0 0
121 0 ▶ defined $port or carp("Argument port required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $port or carp("Argument port required"), return 0 0 0
122 0 my $sock = IO::Socket::INET->new(Proto => 'tcp', Blocking => 0);
123 0 my $addr = sockaddr_in($port,inet_aton($host));
124 0 $sock->sockopt(SO_KEEPALIVE, 1);
125 0 my $rv = $sock->connect($addr);
126 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 0 0
127 0 ▶ _pack_unpack($sock, %args) or return;
Branch: 1 branch
BranchTrueFalseTotal
&_pack_unpack($sock, %args) or return 0 0 0
128 0 my $vpid = refaddr $sock;
129 0 $node{$sock} = $vpid;
130 0 $fh2vpid{$sock} = $vpid;
131 0 $vpid2fh{$vpid} = $sock;
132 0 $fh2fh{$sock} = $sock;
133 0 $sel->add($sock);
134 0 return $vpid;
135 } else {
136 0 carp "Cannot connect to socket '$host:$port' in $self_vpid: $!";
137 0 return;
138 }
139 }
140  
141  
142 0 ▶sub _pack_unpack($%) {
Subroutine _pack_unpack: not called CC=11 SCAR=48.8
143 0 my ($fh, %args) = @_;
144 0 ▶ if (my $pack = $args{pack} and my $unpack = $args{unpack}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pack = $args{'pack'} and my $unpack = $args{'unpack'}) { } 0 0 0
elsif ($args{'pack'} or $args{'unpack'}) { } 0 0 0
Condition0%0/3
my $pack = $args{"pack"} and my $unpack = $args{"unpack"}
!ll&&!rl&&r
000
MC/DC0%0/2
my $pack = $args{"pack"} and my $unpack = $args{"unpack"}
my $pack = $args{"pack"} my $unpack = $args{"unpack"}
Truth table0%0/3
A and B
A= my $pack = $args{"pack"}B= my $unpack = $args{"unpack"}
ABresult
0X0
100
111
145 0 my $r = eval {
146 0 my $r = $unpack->($pack->({a => ["b"]}));
147 0 ▶ if (ref $r eq "HASH" and ref $$r{a} eq "ARRAY" and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq 'HASH' and ref $r->{'a'} eq 'ARRAY' and $r->{'a'}[0] and $r->{'a'}[0] eq 'b') { } 0 0 0
Condition0%0/9
ref $r eq "HASH" and ref $r->{"a"} eq "ARRAY" and $r->{"a"}[0] and $r->{"a"}[0] eq "b"
!ll&&!rl&&r
000
ref $r eq "HASH" and ref $r->{"a"} eq "ARRAY" and $r->{"a"}[0]
!ll&&!rl&&r
000
ref $r eq "HASH" and ref $r->{"a"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/4
ref $r eq "HASH" and ref $r->{"a"} eq "ARRAY" and $r->{"a"}[0] and $r->{"a"}[0] eq "b"
ref $r eq "HASH" ref $r->{"a"} eq "ARRAY" $r->{"a"}[0] $r->{"a"}[0] eq "b"
Truth table0%0/5
A and B and C and D
A= ref $r eq "HASH"B= ref $r->{"a"} eq "ARRAY"C= $r->{"a"}[0]D= $r->{"a"}[0] eq "b"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
148 $$r{a}[0] and $$r{a}[0] eq "b")
149 {
150 0 return 1;
151 } else {
152 0 return 0;
153 }
154 };
155 0 ▶ if (not $r or $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $r or $@) 0 0 0
Condition0%0/3
not $r or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
not $r or $@
not $r $@
Truth table0%0/3
not(A) or B
A= not $rB= $@
ABresult
1X1
011
000
156 0 carp "False pack unpack test";
157 0 return;
158 }
159 0 $pack{$fh} = $pack;
160 0 $unpack{$fh} = $unpack;
161 ▶ } elsif ($args{pack} or $args{unpack}) {
Condition0%0/3
$args{"pack"} or $args{"unpack"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"pack"} or $args{"unpack"}
$args{"pack"} $args{"unpack"}
Truth table0%0/3
A or B
A= $args{"pack"}B= $args{"unpack"}
ABresult
1X1
011
000
162 0 carp "pack and unpack is pair options";
163 0 return;
164 }
165 0 return 1;
166 }
167  
168  
169 3 ▶sub receive(&) {
Subroutine receive: called CC=10 SCAR=23.1
Pod undocumented
170 3 my ($receive) = @_;
171  
172 3 ▶ $DEBUG > 1 and print "Call receive in $self_vpid (\$\$=$$)\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and print "Call receive in $self_vpid (\$\$=${$})\n" 0 3 3
173  
174 3 local $SIG{CHLD} = "IGNORE";
175 3 local $SIG{PIPE} = "IGNORE";
176  
177 3 foreach (@spawn) {
178 2 my ($vpid, $child, $parent, $spawn) = @$_;
179  
180 2 my $kid_pid = fork;
181 2 ▶ defined $kid_pid or die "Can't fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $kid_pid or die "Can't fork: $!" 2 0 2
182  
183 2 ▶ unless ($kid_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid_pid) { } 1 1 2
184  
185 1 foreach (@spawn) {
186 1 close $$_[1];
187 1 ▶ close $$_[2] if $$_[2] ne $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[2] ne $parent 0 1 1
188 }
189  
190 1 close $_ foreach values %fh2fh, values %listener;
191 1 $sel = IO::Select->new();
192 1 @spawn = ();
193 1 %listener = ();
194 1 %node = ();
195 1 %msg = ();
196 1 %fh2vpid = ();
197 1 %vpid2fh = ();
198 1 %fh2fh = ();
199 1 %snd = ();
200  
201 1 %vpid2pid = ();
202  
203 1 $ipc_loop = 0;
204  
205 1 @rcv = ();
206 1 %r_bufs = ();
207 1 %w_bufs = ();
208  
209 1 %pack = ();
210 1 %unpack = ();
211  
212 1 %closed = ();
213  
214 1 $need_reset = 0;
215  
216 1 $self_parent_fh = $parent;
217 1 $self_parent_vpid = $self_vpid;
218  
219 1 $self_vpid = $vpid;
220  
221 1 $fh2vpid{$self_parent_fh} = $self_parent_vpid;
222 1 $vpid2fh{$self_parent_vpid} = $self_parent_fh;
223 1 $fh2fh{$self_parent_fh} = $self_parent_fh;
224  
225 1 $sel->add($self_parent_fh);
226  
227 1 $spawn->();
228  
229 0 exit;
230 }
231 else {
232 1 $vpid2pid{$vpid} = $kid_pid;
233 }
234 }
235  
236  
237 2 foreach (@spawn) {
238 1 my ($vpid, $child, $parent, $spawn) = @$_;
239 1 close $parent;
240 1 $fh2vpid{$child} = $vpid;
241 1 $vpid2fh{$vpid} = $child;
242 1 $fh2fh{$child} = $child;
243 1 $sel->add($child);
244 }
245 2 @spawn = ();
246  
247  
248  
249 2 $receive->();
250  
251  
252  
253 2 ▶ unless ($ipc_loop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ipc_loop) 2 0 2
254 2 $ipc_loop = 1;
255 2 ipc_loop();
256 0 $ipc_loop = 0;
257 }
258 }
259  
260  
261 1 ▶sub wt($$) {
Subroutine wt: called CC=9 SCAR=32.5
Pod undocumented
262 1 my ($waited_vpid, $waited_msg) = @_;
263 1 ▶ defined $waited_vpid or carp("Argument vpid required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $waited_vpid or carp("Argument vpid required"), return 1 0 1
264 1 ▶ defined $waited_msg or carp("Argument msg required"), return;
Branch: 1 branch
BranchTrueFalseTotal
defined $waited_msg or carp("Argument msg required"), return 1 0 1
265 1 ▶ $waited_vpid = $self_parent_vpid if $waited_vpid == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $waited_vpid == 0 0 1 1
266 1 foreach my $i (0 .. $#rcv) {
267 0 my ($from, $msg, $args)= @{$rcv[$i]};
268 0 ▶ if ($from eq $waited_vpid and $msg eq $waited_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq $waited_vpid and $msg eq $waited_msg) 0 0 0
Condition0%0/3
$from eq $waited_vpid and $msg eq $waited_msg
!ll&&!rl&&r
000
MC/DC0%0/2
$from eq $waited_vpid and $msg eq $waited_msg
$from eq $waited_vpid $msg eq $waited_msg
Truth table0%0/3
A and B
A= $from eq $waited_vpidB= $msg eq $waited_msg
ABresult
0X0
100
111
269 0 splice @rcv, $i, 1;
270 0 ▶ return wantarray ? @$args : $$args[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
271 }
272 }
273 1 ▶ $DEBUG and print "Start waiting for '$waited_vpid -> $waited_msg' in $self_vpid (\$\$=$$)\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Start waiting for '$waited_vpid -> ${waited_msg}' in $self_vpid (\$\$=${$})\n" 0 1 1
274 1 return ipc_loop($waited_vpid, $waited_msg);
275 }
276  
277  
278 3 ▶sub ipc_loop(;$$) {
Subroutine ipc_loop: called CC=67 SCAR=62.7
Pod undocumented
279 3 my ($waited_vpid, $waited_msg) = @_;
280 3 ▶ $DEBUG and print "Start ipc_loop in $self_vpid (\$\$=$$)\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Start ipc_loop in $self_vpid (\$\$=${$})\n" 0 3 3
281 3 ▶ RESET: while ($sel->count() and not $quit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sel->count and not $quit) 17 0 17
Condition33%1/3
$sel->count and not $quit
!ll&&!rl&&r
0017
MC/DC0%0/2
$sel->count and not $quit
$sel->count not $quit
Truth table33%1/3
A and not(B)
A= $sel->countB= not $quit
ABresult
0X0
100
111
282  
283 17 foreach my $to (keys %snd) {
284 16 ▶ if (@{$snd{$to}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$snd{$to};}) 8 8 16
285 8 my $fh = $vpid2fh{$to};
286 8 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 8 8
287 0 ▶ if (@spawn) {
Branch: 1 branch
BranchTrueFalseTotal
if (@spawn) { } 0 0 0
288 0 carp "Probably have forgotten to call receive.";
289 0 next;
290 } else {
291 0 ▶ if ($self_parent_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_parent_fh) { } 0 0 0
292 0 ▶ unless ($self_parent_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_parent_closed) { } 0 0 0
293 0 $fh = $self_parent_fh;
294 } else {
295 0 next;
296 }
297 } else {
298 0 carp "The addressee $to is unknown or has left in $self_vpid (\$\$=$$)\n";
299 0 next;
300 }
301 }
302 }
303 8 ▶ unless (exists $w_bufs{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $w_bufs{$fh}) 8 0 8
304 8 my $packet;
305 8 ▶ if (my $pack = $pack{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pack = $pack{$fh}) { } 0 8 8
306 0 $packet = $pack->(shift @{$snd{$to}});
307 } else {
308 8 $packet = freeze shift @{$snd{$to}};
309 }
310 8 my $buf = join "", pack("N", length $packet), $packet;
311 8 $w_bufs{$fh} = $buf;
312 0 ▶ $DEBUG and (@{$snd{$to}} or delete $snd{$to});
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and @{$snd{$to};} || delete $snd{$to} 0 8 8
Condition20%1/5
$DEBUG && (@{$snd{$to};} || delete $snd{$to})
!ll
80
@{$snd{$to};} or delete $snd{$to}
l!l&&r!l&&!r
000
MC/DC0%0/3
$DEBUG && (@{$snd{$to};} || delete $snd{$to})
$DEBUG @{$snd{$to};} delete $snd{$to}
Truth table0%0/4
A && B or C
A= $DEBUGB= @{$snd{$to};}C= delete $snd{$to}
ABCresult
0XX0
1000
11X1
1011
313 }
314 }
315 }
316  
317 17 my $w_sel = IO::Select->new(map { $fh2fh{$_} } keys %w_bufs);
318  
319 17 ▶ if ($DEBUG > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG > 1) 0 17 17
320 0 ▶ print "Select count from $self_vpid, sel->count=", $sel->count(), ", w_sel->count=", $w_sel->count(), "\n",
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 ? : 0 0 0
321 $DEBUG > 2 ? Data::Dumper::Dumper({ snd => \%snd, r_bufs => \%r_bufs, w_bufs => \%w_bufs }) : "";
322 }
323  
324 17 my ($can_read, $can_write, $has_exception)= IO::Select->select($sel, $w_sel, $sel);
325 17 ▶ $DEBUG > 1 and print "Select from $self_vpid: ", scalar(@$can_read), " ", ($can_write ? scalar(@$can_write) : ""), " ", scalar(@$has_exception), "\n";
Branch: 2 branches
BranchTrueFalseTotal
$DEBUG > 1 and print "Select from ${self_vpid}: ", scalar @$can_read, " ", $can_write ? scalar @$can_write : "", " ", scalar @$has_exception, "\n" 0 17 17
$can_write ? : 0 0 0
326  
327 17 foreach my $fh (@$can_read) {
328 9 ▶ if ($listener{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listener{$fh}) 0 9 9
329 0 my $sock = $fh->accept;
330 0 $pack{$sock} = $pack{$fh};
331 0 $unpack{$sock} = $unpack{$fh};
332 0 $sock->sockopt(SO_KEEPALIVE, 1);
333 0 my $vpid = refaddr $sock;
334 0 $node{$sock} = $vpid;
335 0 $fh2vpid{$sock} = $vpid;
336 0 $vpid2fh{$vpid} = $sock;
337 0 $fh2fh{$sock} = $sock;
338 0 $sel->add($sock);
339 0 next;
340 }
341 9 my $len = sysread $fh, (my $_buf), $blksize;
342 9 ▶ if ($len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len) { } 8 1 9
elsif (defined $len) { } 1 0 1
343 8 $r_bufs{$fh} .= $_buf;
344 NEXT_MSG: {
345 8 my $buf = $r_bufs{$fh};
346 8 ▶ if (length $buf >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf >= 4) 8 0 8
347 8 my $packet_length = unpack "N", substr $buf, 0, 4, "";
348 8 ▶ if (length $buf >= $packet_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf >= $packet_length) 8 0 8
349 8 my $packet = substr $buf, 0, $packet_length, "";
350 8 ▶ $r_bufs{$fh} = $buf || "";
Condition50%1/2
$buf || ''
l!l
08
MC/DC0%0/1
$buf || ''
$buf
Truth table50%1/2
A
A= $buf
Aresult
11
00
351 8 ▶ $DEBUG and ($r_bufs{$fh} or delete $r_bufs{$fh});
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and $r_bufs{$fh} || delete $r_bufs{$fh} 0 8 8
Condition20%1/5
$DEBUG && ($r_bufs{$fh} || delete $r_bufs{$fh})
!ll
80
$r_bufs{$fh} or delete $r_bufs{$fh}
l!l&&r!l&&!r
000
MC/DC0%0/3
$DEBUG && ($r_bufs{$fh} || delete $r_bufs{$fh})
$DEBUG $r_bufs{$fh} delete $r_bufs{$fh}
Truth table0%0/4
A && B or C
A= $DEBUGB= $r_bufs{$fh}C= delete $r_bufs{$fh}
ABCresult
0XX0
1000
11X1
1011
352  
353 8 my ($from, $to, $msg, $args);
354 8 ▶ if (my $unpack = $unpack{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $unpack = $unpack{$fh}) { } 0 8 8
355 0 ($from, $to, $msg, $args) = @{$unpack->($packet)};
356 } else {
357 8 ($from, $to, $msg, $args) = @{thaw $packet};
358 }
359  
360 8 ▶ if ($node{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node{$fh}) 0 8 8
361 0 $from = $node{$fh};
362 0 $to = $self_vpid;
363 }
364  
365 8 ▶ $DEBUG and print "Got message '$msg' from $from to $to vpid in $self_vpid (\$\$=$$) with args: ", join(", ", @$args), ".\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Got message '${msg}' from $from to $to vpid in $self_vpid (\$\$=${$}) with args: ", join(", ", @$args), ".\n" 0 8 8
366 8 ▶ if ($to == $self_vpid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to == $self_vpid) { } 8 0 8
elsif ($vpid2fh{$to}) { } 0 0 0
367 8 ▶ $DEBUG and print "Run message sub '$msg' from $from to $to vpid in $self_vpid (\$\$=$$) with args: ", join(", ", @$args), ".\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Run message sub '${msg}' from $from to $to vpid in $self_vpid (\$\$=${$}) with args: ", join(", ", @$args), ".\n" 0 8 8
368 8 ▶ if (defined $waited_vpid and defined $waited_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $waited_vpid and defined $waited_msg) { } 1 7 8
Condition66%2/3
defined $waited_vpid and defined $waited_msg
!ll&&!rl&&r
701
MC/DC50%1/2
defined $waited_vpid and defined $waited_msg
defined $waited_vpid defined $waited_msg
Truth table66%2/3
A and B
A= defined $waited_vpidB= defined $waited_msg
ABresult
0X0
100
111
369 1 push @rcv, [$from, $msg, $args];
370 } else {
371 7 ▶ if ($msg{$msg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg{$msg}) { } 7 0 7
372 7 push @rcv, [$from, $msg, $args];
373 } else {
374 0 ▶ $DEBUG and print "Unknown message '$msg'\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Unknown message '${msg}'\n" 0 0 0
375 }
376 }
377 } elsif ($vpid2fh{$to}) {
378 0 ▶ $DEBUG and print "Remittance message '$msg' from $from to $to vpid in $self_vpid (\$\$=$$) with args: ", join(", ", @$args), ".\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Remittance message '${msg}' from $from to $to vpid in $self_vpid (\$\$=${$}) with args: ", join(", ", @$args), ".\n" 0 0 0
379 0 push @{$snd{$to}}, [$from, $to, $msg, $args];
380 } else {
381 0 carp "Got Wandered message '$msg' from $from to $to in $self_vpid (\$\$=$$)\n";
382 }
383  
384 8 ▶ redo NEXT_MSG if $r_bufs{$fh};
Branch: 1 branch
BranchTrueFalseTotal
if $r_bufs{$fh} 0 8 8
385 }
386 }
387 }
388 } elsif (defined $len) {
389 1 $sel->remove($fh);
390 1 $w_sel->remove($fh);
391 1 my $vpid = delete $fh2vpid{$fh};
392 1 delete $vpid2fh{$vpid};
393 1 delete $r_bufs{$fh};
394 1 delete $w_bufs{$fh};
395 1 delete $fh2fh{$fh};
396 1 delete $vpid2pid{$vpid};
397 1 delete $pack{$fh};
398 1 delete $unpack{$fh};
399 1 ▶ if (my $node_vpid = $node{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node_vpid = $node{$fh}) { } 0 1 1
400 0 delete $node{$fh};
401 0 ▶ if ($msg{NODE_CLOSED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg{"NODE_CLOSED"}) 0 0 0
402 0 ▶ $msg{NODE_CLOSED}->($node_vpid, $fh->connected ? 1 : 0) unless $closed{$vpid};
Branch: 2 branches
BranchTrueFalseTotal
unless $closed{$vpid} 0 0 0
$fh->connected ? : 0 0 0
403 }
404 } else {
405 1 ▶ if ($msg{SPAWN_CLOSED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg{"SPAWN_CLOSED"}) 0 1 1
406 0 ▶ $msg{SPAWN_CLOSED}->($vpid) unless $closed{$vpid};
Branch: 1 branch
BranchTrueFalseTotal
unless $closed{$vpid} 0 0 0
407 }
408 }
409 1 delete $closed{$vpid};
410 1 close $fh;
411 1 ▶ if ($self_parent_fh and $self_parent_fh eq $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_parent_fh and $self_parent_fh eq $fh) 1 0 1
Condition33%1/3
$self_parent_fh and $self_parent_fh eq $fh
!ll&&!rl&&r
001
MC/DC0%0/2
$self_parent_fh and $self_parent_fh eq $fh
$self_parent_fh $self_parent_fh eq $fh
Truth table33%1/3
A and B
A= $self_parent_fhB= $self_parent_fh eq $fh
ABresult
0X0
100
111
412 1 $self_parent_closed = 1;
413 1 ▶ unless (defined $waited_vpid and defined $waited_msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $waited_vpid and defined $waited_msg) 1 0 1
Condition33%1/3
defined $waited_vpid and defined $waited_msg
!ll&&!rl&&r
100
MC/DC0%0/2
defined $waited_vpid and defined $waited_msg
defined $waited_vpid defined $waited_msg
Truth table33%1/3
A and B
A= defined $waited_vpidB= defined $waited_msg
ABresult
0X0
100
111
414 1 ▶ unless (@rcv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rcv) 1 0 1
415 1 exit;
416 }
417 }
418 }
419 } else {
420 0 ▶ $DEBUG and die "Can't read '$fh': $!";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and die "Can't read '${fh}': $!" 0 0 0
421 }
422 }
423  
424  
425 16 ▶ if (defined $waited_vpid and defined $waited_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $waited_vpid and defined $waited_msg) { } 2 14 16
Condition66%2/3
defined $waited_vpid and defined $waited_msg
!ll&&!rl&&r
1402
MC/DC50%1/2
defined $waited_vpid and defined $waited_msg
defined $waited_vpid defined $waited_msg
Truth table66%2/3
A and B
A= defined $waited_vpidB= defined $waited_msg
ABresult
0X0
100
111
426 2 foreach my $i (0 .. $#rcv) {
427 1 my ($from, $msg, $args)= @{$rcv[$i]};
428 1 ▶ if ($msg eq $waited_msg and $from eq $waited_vpid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg eq $waited_msg and $from eq $waited_vpid) 1 0 1
Condition33%1/3
$msg eq $waited_msg and $from eq $waited_vpid
!ll&&!rl&&r
001
MC/DC0%0/2
$msg eq $waited_msg and $from eq $waited_vpid
$msg eq $waited_msg $from eq $waited_vpid
Truth table33%1/3
A and B
A= $msg eq $waited_msgB= $from eq $waited_vpid
ABresult
0X0
100
111
429 1 splice @rcv, $i, 1;
430 1 ▶ $DEBUG and print "Stop waiting for '$waited_vpid -> $waited_msg' in $self_vpid (\$\$=$$)\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print "Stop waiting for '$waited_vpid -> ${waited_msg}' in $self_vpid (\$\$=${$})\n" 0 1 1
431 1 $need_reset = 1;
432 1 ▶ return wantarray ? @$args : $$args[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
433 }
434 }
435 1 ▶ unless (exists $vpid2fh{$waited_vpid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $vpid2fh{$waited_vpid}) 0 1 1
436 0 return;
437 }
438 } else {
439 14 ▶ while (my $rcv = shift @rcv) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rcv = shift @rcv) 7 13 20
440 7 my ($from, $msg, $args)= @{$rcv};
441 7 ▶ $msg{$msg}->($from, @$args) unless $closed{$from};
Branch: 1 branch
BranchTrueFalseTotal
unless $closed{$from} 7 0 7
442 }
443 }
444 14 ▶ if ($need_reset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_reset) 0 14 14
445 0 $need_reset = 0;
446 0 next RESET;
447 }
448  
449  
450 14 foreach my $fh (@$can_write) {
451 8 ▶ $fh2fh{$fh} or next;
Branch: 1 branch
BranchTrueFalseTotal
$fh2fh{$fh} or next 8 0 8
452  
453 8 my $buf = $w_bufs{$fh};
454 8 my $len = syswrite $fh, $buf, $blksize;
455 8 ▶ if ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) { } 8 0 8
456 8 substr $buf, 0, $len, "";
457 8 ▶ if (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) { } 0 8 8
458 0 $w_bufs{$fh} = $buf;
459 } else {
460 8 delete $w_bufs{$fh};
461 }
462 } else {
463 0 ▶ $DEBUG and die "Can't write to '$fh': $!";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and die "Can't write to '${fh}': $!" 0 0 0
464 }
465 }
466  
467 }
468 0 $need_reset = 1;
469 0 $quit = 0;
470 0 return;
471 }
472  
473  
474  
475 2 1;
476  
477  
478 __END__
↑ Summary MPS/Event.pm »