Server/Util.pm

filter: Statementstmt 54.7% 80 / 146 Branchbran 21.8% 14 / 64 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 72.4% 21 / 29 Podpod 30.7% 4 / 13 totaltotal 44.2% 120 / 271 CC 38 SCAR 37.4
CC
38
Cov
43%
maybe_start_server
95.1
start_server
80.0
do_start_server
43.8
CRAP 42.1
↑ Summary Server.pm »
1 package RPC::ExtDirect::Server::Util;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶no warnings 'uninitialized'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use RPC::ExtDirect::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 our @EXPORT = qw/
17 maybe_start_server
18 start_server
19 stop_server
20 /;
21  
22 ### PRIVATE PACKAGE SUBROUTINES ###
23 #
24 # Internal use only.
25 #
26  
27 {
28 5 my ($server_pid, $server_host, $server_port, $dont_stop);
29
30 5 ▶ sub get_server_pid { $server_pid };
Subroutine get_server_pid: called CC=1 SCAR=0.0
Pod undocumented
31 8 ▶ sub set_server_pid { $server_pid = shift; };
Subroutine set_server_pid: called CC=1 SCAR=0.0
Pod undocumented
32
33 3 ▶ sub get_server_host { $server_host };
Subroutine get_server_host: called CC=1 SCAR=0.0
Pod undocumented
34 3 ▶ sub set_server_host { $server_host = shift };
Subroutine set_server_host: called CC=1 SCAR=0.0
Pod undocumented
35
36 3 ▶ sub get_server_port { $server_port };
Subroutine get_server_port: called CC=1 SCAR=0.0
Pod undocumented
37 8 ▶ sub set_server_port { $server_port = shift; };
Subroutine set_server_port: called CC=1 SCAR=0.0
Pod undocumented
38
39 5 ▶ sub get_no_shutdown { $dont_stop };
Subroutine get_no_shutdown: called CC=1 SCAR=0.0
Pod undocumented
40 0 ▶ sub no_shutdown { $dont_stop = shift; };
Subroutine no_shutdown: not called CC=1 SCAR=6.9
Pod documented
41 }
42  
43 ### EXPORTED PUBLIC PACKAGE SUBROUTINE ###
44 #
45 # See if a host and port were given in the @ARGV, and start a new
46 # server instance if not.
47 #
48  
49 5 ▶sub maybe_start_server {
Subroutine maybe_start_server: called CC=11 SCAR=45.5
Pod documented
50 3 ▶ if ( @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 0 3 3
51 0 my %opt;
52
53 0 getopts('h:p:fes:t:l:', \%opt);
54
55 0 ▶ if ( $opt{p} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"p"}) 0 0 0
56
57 # If a port is given but not the host name,
58 # we assume localhost
59 0 ▶ my $host = $opt{h} || '127.0.0.1';
Condition0%0/2
$opt{'h'} || '127.0.0.1'
l!l
00
MC/DC0%0/1
$opt{'h'} || '127.0.0.1'
$opt{'h'}
Truth table0%0/2
A
A= $opt{'h'}
Aresult
11
00
60 0 my $port = $opt{p};
61
62 0 ▶ return wantarray ? ($host, $port) : "$host:$port";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
63 }
64
65 # Not quoting $opt{s} makes my text editor lose its mind ;)
66 0 ▶ push @_, static_dir => $opt{'s'} if $opt{'s'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"s"} 0 0 0
67 0 ▶ push @_, foreground => 1 if $opt{f};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"f"} 0 0 0
68 0 ▶ push @_, enbugger => 1 if $opt{e};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"e"} 0 0 0
69 0 ▶ push @_, enbugger_timer => $opt{t} if defined $opt{t};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"t"} 0 0 0
70 0 ▶ push @_, host => $opt{h} if defined $opt{h};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"h"} 0 0 0
71 0 ▶ push @_, port => $opt{l} if defined $opt{l};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"l"} 0 0 0
72 }
73
74 3 return start_server( @_ );
75 }
76  
77 ### EXPORTED PUBLIC PACKAGE SUBROUTINE ###
78 #
79 # Start an RPC::ExtDirect::Server instance, wait for it to bind
80 # to a port and return the host and port number.
81 # If an instance has already been started, return its parameters
82 # instead of starting a new one.
83 #
84  
85 3 ▶sub start_server {
Subroutine start_server: called CC=17 SCAR=43.8
Pod documented
86 3 my (%arg) = @_;
87
88 {
89 3 my $host = get_server_host;
90 3 my $port = get_server_port;
91
92 3 ▶ if ( $port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port) 0 3 3
93 0 ▶ return wantarray ? ($host, $port) : "$host:$port";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
94 }
95 }
96
97 # This parameter is used for internal testing
98 3 my $sleep = delete $arg{sleep};
99 3 my $foreground = delete $arg{foreground};
100 3 my $enbugger = delete $arg{enbugger};
101 3 my $enbugger_timer = delete $arg{set_timer};
102 3 ▶ my $timeout = delete $arg{timeout} || 30;
Condition50%1/2
delete $arg{'timeout'} || 30
l!l
03
MC/DC0%0/1
delete $arg{'timeout'} || 30
delete $arg{'timeout'}
Truth table50%1/2
A
A= delete $arg{'timeout'}
Aresult
11
00
103
104 # Debug flag is checked below to avoid printing the banner
105 3 ▶ my $server_debug = $arg{config} ? $arg{config}->debug : $arg{debug};
Branch: 1 branch
BranchTrueFalseTotal
$arg{'config'} ? : 0 3 3
106
107 # We default to verbose exceptions, which is against Ext.Direct spec
108 # but feels somewhat saner and is better for testing
109 3 ▶ $arg{verbose_exceptions} = 1 unless defined $arg{verbose_exceptions};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"verbose_exceptions"} 3 0 3
110
111 3 ▶ if ( $enbugger ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enbugger) 0 3 3
112 0 local $@;
113 0 eval "require Enbugger";
114 }
115
116 # Interactive start means we're not forking but running the server
117 # in the current process. Useful for Enbugging.
118 3 ▶ if ( $foreground ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foreground) 0 3 3
119 0 ▶ if ( $enbugger_timer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enbugger_timer) 0 0 0
120 0 my $old_alarm = $SIG{ALRM};
121
122 0 ▶ $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 alarm 0;
124 0 $SIG{ALRM} = $old_alarm;
125 0 Enbugger->stop;
126 0 };
127
128 0 alarm $enbugger_timer;
129 }
130
131 do_start_server(
132 %arg,
133
134 0 ▶ after_listener => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
135 0 my ($self) = @_;
136
137 0 my $host = $self->host;
138 0 my $port = $self->port;
139
140 0 ▶ print ref($self)." is listening on $host:$port\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $server_debug 0 0 0
141 unless $server_debug;
142 }
143 0 );
144
145 # This should be unreachable, but just in case
146 0 exit 0;
147 }
148  
149 3 my ($pid, $pipe_rd, $pipe_wr);
150 3 ▶ pipe($pipe_rd, $pipe_wr) or die "Can't open pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $pipe_rd, $pipe_wr or die "Can't open pipe: $!" 3 0 3
151  
152 3 ▶ if ( $pid = fork ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid = fork) { } 3 0 3
elsif (defined $pid and $pid == 0) { } 0 0 0
153 3 close $pipe_wr;
154 0 ▶ local $SIG{CHLD} = sub { waitpid $pid, 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
155  
156 # Wait until the kid starts up, but don't block forever either
157 3 my ($host, $port) = eval {
158 0 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
159 3 alarm $timeout;
160
161 3 my ($host, $port) = split /:/, <$pipe_rd>;
162 3 close $pipe_rd;
163
164 3 alarm 0;
165
166 3 ($host, $port + 0); # Easier than chomp
167 };
168
169 3 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 3 3
170 # If timed out, try to clean up the kid anyway
171 0 eval { kill 2, $pid };
172
173 0 ▶ croak $err eq "alarm\n" ? "Timed out waiting for " .
Branch: 1 branch
BranchTrueFalseTotal
$err eq "alarm\n" ? : 0 0 0
174 "the server instance to start " .
175 "after $timeout seconds"
176 : $err
177 ;
178 }
179
180 3 set_server_pid($pid);
181 3 set_server_host($host);
182 3 set_server_port($port);
183  
184 3 ▶ return wantarray ? ($host, $port)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
185 : "$host:$port"
186 ;
187 }
188 ▶ elsif ( defined $pid && $pid == 0 ) {
Condition0%0/3
defined $pid and $pid == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pid and $pid == 0
defined $pid $pid == 0
Truth table0%0/3
A and B
A= defined $pidB= $pid == 0
ABresult
0X0
100
111
189 0 close $pipe_rd;
190  
191 0 srand;
192
193 0 ▶ sleep $sleep if $sleep;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep 0 0 0
194
195 do_start_server(
196 %arg,
197
198 0 ▶ after_listener => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
199 0 my $self = shift;
200
201 0 my $host = inet_ntoa inet_aton $self->host;
202 0 my $port = $self->port;
203  
204 0 print $pipe_wr "$host:$port\n";
205 0 close $pipe_wr;
206
207 my $after_setup_listener
208 0 = $self->{_old_after_setup_listener};
209
210 0 ▶ $after_setup_listener->($self, @_)
Branch: 1 branch
BranchTrueFalseTotal
if $after_setup_listener 0 0 0
211 if $after_setup_listener;
212 }
213 0 );
214  
215 # Should be unreachable, just in case
216 0 exit 0;
217 }
218 else {
219 0 croak "Can't fork: $!";
220 };
221  
222 0 return;
223 }
224  
225 ### EXPORTED PUBLIC PACKAGE SUBROUTINE ###
226 #
227 # Stop previously started server instance
228 #
229  
230 5 ▶sub stop_server {
Subroutine stop_server: called CC=3 SCAR=11.0
Pod documented
231 5 my ($pid) = @_;
232  
233 5 ▶ $pid = get_server_pid unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 5 0 5
234  
235 5 ▶ kill 2, $pid if defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid 3 2 5
236  
237 5 set_server_port(undef);
238 5 set_server_pid(undef);
239 }
240  
241 ############## PRIVATE METHODS BELOW ##############
242  
243 ### PRIVATE PACKAGE SUBROUTINE ###
244 #
245 # Try to start the server, re-rolling port randomizer
246 # if the old port is taken
247 #
248  
249 0 ▶sub do_start_server {
Subroutine do_start_server: not called CC=7 SCAR=37.8
Pod undocumented
250 0 my (%arg) = @_;
251
252 0 my $forced_port = defined $arg{port};
253 0 my $after_listener = delete $arg{after_listener};
254 my $server_class = delete $arg{server_class} ||
255 0 ▶ 'RPC::ExtDirect::Server';
Condition0%0/2
delete $arg{'server_class'} || 'RPC::ExtDirect::Server'
l!l
00
MC/DC0%0/1
delete $arg{'server_class'} || 'RPC::ExtDirect::Server'
delete $arg{'server_class'}
Truth table0%0/2
A
A= delete $arg{'server_class'}
Aresult
11
00
256  
257 0 ▶ if ( !$forced_port ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($forced_port) 0 0 0
258 0 $arg{port} = random_port();
259 }
260  
261 0 my $server = $server_class->new(%arg);
262
263 # TODO This is a dirty hack - find a better way of
264 # injecting after_setup_listener. Maybe send a patch
265 # to HTTP::Server::Simple maintainer to make this easier?
266 0 ▶ if ( $after_listener ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after_listener) 0 0 0
267 $server->{_old_after_setup_listener}
268 0 = $server_class->can('after_setup_listener');
269  
270 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
271 0 *{$server_class.'::after_setup_listener'} = $after_listener;
272 }
273  
274 # If the port is taken, reroll the random generator and try again
275 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$@ and do { do { eval { do { $server->run } }; croak("$@\n") if $forced_port and $@; $server->port(random_port()) } } 0 0 0
276 0 eval { $server->run() };
277  
278 # If the port was forced by the caller, punt
279 0 ▶ croak "$@\n" if $forced_port && $@;
Branch: 1 branch
BranchTrueFalseTotal
if $forced_port and $@ 0 0 0
Condition0%0/3
$forced_port and $@
!ll&&!rl&&r
000
MC/DC0%0/2
$forced_port and $@
$forced_port $@
Truth table0%0/3
A and B
A= $forced_portB= $@
ABresult
0X0
100
111
280  
281 0 $server->port( random_port() );
282 }
283 while ( $@ );
284
285 0 return 1; # This should be unreachable
286 }
287  
288 ### PRIVATE PACKAGE SUBROUTINE ###
289 #
290 # Generate a random port for the server to listen on
291 #
292  
293 0 ▶sub random_port { 30000 + int rand 10000 };
Subroutine random_port: not called CC=1 SCAR=6.9
Pod undocumented
294  
295 # Ensure that the server is stopped cleanly at exit
296 5 ▶END { stop_server unless get_no_shutdown }
Subroutine END: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless get_no_shutdown() 5 0 5
297  
298 5 1;
↑ Summary Server.pm »