Resolver/Worker.pm

filter: Statementstmt 87.8% 116 / 132 Branchbran 58.1% 50 / 86 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 76.6% 180 / 235 CC 1 SCAR 0.0
CC
1
Cov
75%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Resolver/Mojo/IOLoop.pm ↑ Summary
1 package Socket::More::Resolver::Worker;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use feature "say";
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶unless(caller){
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 2 0 2
6 2 $0="S::M::R::T";
7 2 my $gai_data_pack="l> l> l> l> l>/a* l>/a*";
8 2 my $gai_pack="($gai_data_pack)*";
9  
10 package main;
11 2 ▶ use feature "say";
Subroutine BEGIN: called CC=1 SCAR=0.0
12 #use POSIX ":sys_wait_h";
13 #use constant::more DEBUG=>0;
14 #use constant::more qw<CMD_GAI=0 CMD_GNI CMD_SPAWN CMD_KILL CMD_REAP>;
15 2 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ *DEBUG=sub {0};
Subroutine __ANON__: called CC=1 SCAR=0.0
17 2 ▶ *CMD_GAI=sub {0};
Subroutine __ANON__: called CC=1 SCAR=0.0
18 2 ▶ *CMD_GNI=sub {1};
Subroutine __ANON__: called CC=1 SCAR=0.0
19 2 ▶ *CMD_SPAWN=sub {2};
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 ▶ *CMD_KILL=sub {3};
Subroutine __ANON__: called CC=1 SCAR=0.0
21 2 ▶ *CMD_REAP=sub {4};
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 ▶ *WNOHANG=sub {1};
Subroutine __ANON__: called CC=1 SCAR=0.0
23 }
24  
25 # process any command line arguments for input and output FDs
26 2 my $run=1;
27 2 my @in_fds;
28 my @out_fds;
29 2 my $use_core=0;#=1;
30 2 ▶ while(@ARGV){
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 4 2 6
31 4 local $_=shift;
32 4 ▶ if(/--in/){
Branch: 1 branch
BranchTrueFalseTotal
if (/--in/) 2 2 4
33 2 @in_fds=split ",", shift;
34 2 next;
35 }
36 2 ▶ if(/--out/){
Branch: 1 branch
BranchTrueFalseTotal
if (/--out/) 2 0 2
37 2 @out_fds=split ",", shift;
38 2 next;
39 }
40 }
41
42 2 ▶ DEBUG and say STDERR "TEMPLATE: ins: @in_fds";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "TEMPLATE: ins: @in_fds" 0 2 2
43 2 ▶ DEBUG and say STDERR "TEMPLATE: outs: @out_fds";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "TEMPLATE: outs: @out_fds" 0 2 2
44  
45 # Pipes back to the API
46 #
47 2 ▶ open my $in, "<&=$in_fds[0]" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<&=$in_fds[0]" or die $! 2 0 2
48 2 ▶ open my $out, ">&=$out_fds[0]" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">&=$out_fds[0]" or die $! 2 0 2
49 #$out->autoflush;
50  
51 #Simply loop over inputs and outputs
52 2 ▶ DEBUG and say STDERR "Worker waiting for line ...";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "Worker waiting for line ..." 0 2 2
53 2 my $counter=0;
54 2 ▶ while(<$in>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 10 2 12
55  
56 10 ▶ DEBUG and say STDERR "Worker got line...";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "Worker got line..." 0 10 10
57 10 $0="S::M::R::W-".$counter++;
58 #parse
59 # Host, port, hints
60 10 chomp;
61  
62 10 my $bin=pack "H*", $_;
63 10 my ($cmd, $req_id)=unpack "l> l>", $bin;
64 10 $bin=substr $bin, 8;
65  
66 10 ▶ DEBUG and say STDERR "WORKER $$ REQUEST, ID: $req_id";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "WORKER ${$} REQUEST, ID: $req_id" 0 10 10
67  
68 10 my $return_out=pack "l> l>", $cmd, $req_id;
69 10 ▶ if($cmd == CMD_SPAWN){
Branch: 5 branches
BranchTrueFalseTotal
if ($cmd == CMD_SPAWN()) { } 2 8 10
elsif ($cmd == CMD_GAI()) { } 2 6 8
elsif ($cmd == CMD_GNI()) { } 2 4 6
elsif ($cmd == CMD_KILL()) { } 0 4 4
elsif ($cmd == CMD_REAP()) { } 4 0 4
70 #Fork from me. Presumably the template
71 2 my $pid=fork;
72 2 ▶ if($pid){
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 1 1 2
73 #Parent
74 # return message back to API with PID of offspring
75 1 ▶ DEBUG and say STDERR "FORKED WORKER... in parent child is $pid";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "FORKED WORKER... in parent child is $pid" 0 1 1
76 1 $return_out.=pack "l>", $pid;
77 }
78 else {
79 #child.
80  
81 1 $0="S::M::R::W";
82 1 ▶ DEBUG and say STDERR "FORKED WORKER... child with fds";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "FORKED WORKER... child with fds" 0 1 1
83 1 my ($in_fd, $out_fd)=unpack "l> l>", $bin;
84 1 close $in;
85 1 close $out;
86
87 1 ▶ DEBUG and say STDERR "infd $in_fd, out_fd $out_fd";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "infd $in_fd, out_fd $out_fd" 0 1 1
88 1 ▶ open $in, "<&=$in_fd" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $in, "<&=$in_fd" or die $! 1 0 1
89 1 ▶ open $out, ">&=$out_fd" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $out, ">&=$out_fd" or die $! 1 0 1
90  
91 1 next; #Do not respond.
92 }
93  
94 }
95 elsif($cmd== CMD_GAI){
96 #Assume a request
97 2 my @e =unpack $gai_pack, $bin;
98 2 my @results;
99 2 my $port=pop @e;
100 2 my $host=pop @e;
101 2 ▶ DEBUG and say STDERR "WORKER $$ PROCESSIG GAI REQUEST, id: $req_id";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "WORKER ${$} PROCESSIG GAI REQUEST, id: $req_id" 0 2 2
102 2 my $rc;
103  
104  
105 2 ▶ if($use_core){
Branch: 1 branch
BranchTrueFalseTotal
if ($use_core) { } 0 2 2
106 0 require Socket;
107 0 my %hints=@e;
108 0 ($rc, @results)=Socket::getaddrinfo($host, $port, \%hints);
109 0 ▶ if($rc){
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) { } 0 0 0
110 0 my $a=[$rc+0, -1, -1, -1, "", ""];
111 0 $return_out.=pack($gai_data_pack, @$a);
112 }
113 else {
114 0 for(@results){
115 0 ▶ my $a=[$rc, $_->{family}, $_->{socktype}, $_->{protocol}, $_->{addr}, $_->{cannonname}//""];
Condition0%0/2
$_->{"cannonname"} // ""
l!l
00
MC/DC0%0/1
$_->{"cannonname"} // ""
$_->{"cannonname"}
Truth table0%0/2
A
A= $_->{"cannonname"}
Aresult
11
00
116 0 $return_out.=pack($gai_data_pack, @$a);
117 }
118 }
119 }
120 else {
121 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 2 require Socket::More::Lookup;
123 2 ▶ DEBUG and say STDERR "host $host, port $port";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "host $host, port $port" 0 2 2
124 2 $rc=Socket::More::Lookup::getaddrinfo($host, $port, \@e, \@results);
125 2 ▶ unless (defined $rc){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 2 2
126 0 $results[0]=[$!, -1, -1, -1, "", ""];
127 }
128  
129 2 for(@results){
130 54 $return_out.=pack($gai_data_pack, @$_);
131 }
132 }
133 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 2 ▶ DEBUG and say STDERR Dumper @results;
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR Dumper(@results) 0 2 2
135 }
136  
137 elsif($cmd==CMD_GNI){
138 2 ▶ DEBUG and say STDERR "WORKER $$ PROCESSIG GNI REQUEST, id: $req_id";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "WORKER ${$} PROCESSIG GNI REQUEST, id: $req_id" 0 2 2
139 2 my @e=unpack "l>/a* l>", $bin;
140 2 ▶ if($use_core){
Branch: 1 branch
BranchTrueFalseTotal
if ($use_core) { } 0 2 2
141 0 require Socket;
142 0 my($rc, $host, $service)=Socket::getnameinfo(@e);
143 0 $return_out.=pack "l> l>/a* l>/a*",$rc, $host, $service;
144 }
145 else {
146 2 require Socket::More::Lookup;
147 2 my $rc=Socket::More::Lookup::getnameinfo($e[0],my $host="", my $service="", $e[1]);
148  
149 2 ▶ DEBUG and say STDERR "worker side rc $rc";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "worker side rc $rc" 0 2 2
150 2 ▶ DEBUG and say STDERR "worker side host $host";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "worker side host $host" 0 2 2
151 2 ▶ DEBUG and say STDERR "worker side service Service $service";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "worker side service Service $service" 0 2 2
152  
153 2 ▶ unless (defined $rc){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc) { } 2 0 2
154 0 $return_out.=pack "l> l>/a* l>/a*",$!, $host, $service;
155  
156 }
157 else {
158  
159 2 $return_out.=pack "l> l>/a* l>/a*",0, $host, $service;
160 }
161 }
162 }
163  
164 elsif($cmd==CMD_KILL){
165 # worker needs to exit
166 #
167 0 $run=undef;
168 }
169 elsif($cmd==CMD_REAP){
170 #
171 4 my @pids=unpack "l>/l>*", $bin;
172 4 ▶ DEBUG and say STDERR "WORKER $$ REAP HANDLER @pids";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "WORKER ${$} REAP HANDLER @pids" 0 4 4
173 4 my @reaped;
174 4 for(@pids){
175 20 my $ret;
176 20 ▶ if($_){
Branch: 1 branch
BranchTrueFalseTotal
if ($_) { } 2 18 20
177 # Only do the syscall if the pid is non zero
178 2 $ret=waitpid $_, WNOHANG;
179 }
180 else {
181 18 $ret=0;
182 }
183 20 push @reaped, $ret;
184 }
185 4 ▶ DEBUG and say STDERR "WORKER Reaped @reaped";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "WORKER Reaped @reaped" 0 4 4
186 4 $return_out.=pack "l>/l>*", @reaped;
187 }
188  
189 else {
190 0 die "Unkown command";
191 }
192  
193 9 ▶ DEBUG and say STDERR "** BEFORE WORKER WRITE $$";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "** BEFORE WORKER WRITE ${$}" 0 9 9
194 9 ▶ syswrite $out, unpack("H*", $return_out)."\n" or say $!;
Branch: 1 branch
BranchTrueFalseTotal
syswrite $out, unpack("H*", $return_out) . "\n" or say $! 9 0 9
195 9 ▶ DEBUG and say STDERR "** AFTER WORKER WRITE $$";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "** AFTER WORKER WRITE ${$}" 0 9 9
196  
197 9 ▶ last unless $run;
Branch: 1 branch
BranchTrueFalseTotal
unless $run 0 9 9
198 }
199  
200 2 ▶ DEBUG and say STDERR "** EXITING WORKER $$";
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and say STDERR "** EXITING WORKER ${$}" 0 2 2
201 }
202  
203 1;
« Resolver/Mojo/IOLoop.pm ↑ Summary