eg/forking

filter: Statementstmt 84.6% 88 / 104 Branchbran 70.0% 21 / 30 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.9% 17 / 21 Podpod n/a totaltotal 79.3% 127 / 160 CC 16 SCAR 13.7
CC
16
Cov
80%
__ANON__
8.0
__ANON__
6.0
__ANON__
6.0
CRAP 3.9
« eg/preforking ↑ Summary blib/lib/POE/Component/Daemon/Scoreboard.pm »
1 #!/usr/bin/perl -w
2 # $Id: forking 688 2010-12-15 14:05:10Z fil $
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # use Religion::Package qw(1 1);
7 4 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use POE::Component::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use POE::Driver::SysRW;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use POSIX qw(EADDRINUSE);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Socket qw(inet_ntoa sockaddr_in);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub DEBUG () { 0 }
18  
19 4 my $port=shift;
20 4 ▶die "Usage: $0 port" unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 0 4 4
21  
22 4 my $logfile = "$FindBin::Dir/log_forking";
23  
24  
25  
26 #########################
27 POE::Session->create(
28 inline_states=>{
29  
30 4 ▶ _start=>sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
31 4 my($kernel, $heap)=@_[KERNEL, HEAP];
32 4 $heap->{wheel}=POE::Wheel::SocketFactory->new(
33 BindPort => $port,
34 Reuse => 'on', # Lets the port be reused
35 BindAddress => '127.0.0.1',
36 SuccessEvent => 'accept',
37 FailureEvent => 'error'
38 );
39 4 ▶ if( $port == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port == 0) { } 4 0 4
40 4 $port = ( sockaddr_in($heap->{wheel}->getsockname()) )[0];
41 4 print "PORT=$port\n";
42 }
43 else {
44 0 warn "$$: Listening on port $port";
45 }
46 4 $heap->{rid}=0;
47  
48 4 $kernel->sig('daemon_child' => 'daemon_child' );
49 4 $kernel->sig('daemon_shutdown' => 'daemon_shutdown' );
50 4 $kernel->sig('daemon_pause' => 'daemon_pause' );
51 4 $kernel->sig('daemon_resume' => 'daemon_resume' );
52  
53 4 $kernel->sig( USR1 => 'USR1' );
54 },
55  
56 3 ▶ error=>sub {
Subroutine __ANON__: called CC=5 SCAR=17.4
57 3 my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0, ARG1, ARG2];
58  
59 3 ▶ if(0==$errnum and $operation eq 'read') { # EOF
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $errnum and $operation eq 'read') { } 3 0 3
Condition33%1/3
0 == $errnum and $operation eq "read"
!ll&&!rl&&r
003
MC/DC0%0/2
0 == $errnum and $operation eq "read"
0 == $errnum $operation eq "read"
Truth table33%1/3
A and B
A= 0 == $errnumB= $operation eq "read"
ABresult
0X0
100
111
60 3 ▶ if($heap->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'pending'}) { } 1 2 3
61 1 $heap->{done}=1;
62 1 return;
63 } else {
64 2 Daemon->shutdown();
65 }
66 }
67 else {
68 0 warn "$$: $operation:$errnum: $errstr";
69 }
70 2 ▶ if($errnum==EADDRINUSE) { # EADDRINUSE
Branch: 1 branch
BranchTrueFalseTotal
if ($errnum == 98) 0 2 2
71 0 Daemon->shutdown(); # THIS IS IMPORTANT
72 }
73 2 delete $heap->{wheel};
74 2 delete $heap->{wheel_client};
75 # Daemon->shutdown;
76 },
77  
78 ###############
79 # daemon_shutdown signal, caused by going from req -> done
80 0 ▶ daemon_shutdown => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my ($heap, $kernel) = @_[HEAP, KERNEL];
82 0 delete $heap->{wheel};
83 0 delete $heap->{wheel_client};
84 },
85  
86 ###############
87 # socketfactory got a connection handle it here
88 10 ▶ accept=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 10 my ($heap, $handle, $peer, $port, $id)=@_[HEAP, ARG0..ARG3];
90  
91 10 $peer=inet_ntoa($peer);
92 10 DEBUG and warn "Connection id=$id from $peer:$port";
93  
94 10 my $info={handle=>$handle, peer=>$peer, port=>$port, id=>$id};
95 10 $heap->{parent} = $$;
96  
97 10 Daemon->update_status('req', $info);
98 },
99  
100 ###############
101 # PoCo::Daemon thinks there are too many proceses, and that we should
102 # prevent more from happening
103 0 ▶ daemon_pause => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
104 0 my( $heap, $kernel ) = @_[ HEAP, KERNEL ];
105 0 DEBUG and warn "PAUSE";
106 0 ▶ if( $heap->{wheel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"wheel"}) 0 0 0
107 0 $heap->{wheel}->pause_accept;
108 }
109 },
110 # PoCo::Daemon no longer thinks there are too many proceses.
111 0 ▶ daemon_resume => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
112 0 my( $heap, $kernel ) = @_[ HEAP, KERNEL ];
113 0 DEBUG and warn "RESUME";
114 0 ▶ if( $heap->{wheel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"wheel"}) 0 0 0
115 0 $heap->{wheel}->resume_accept;
116 }
117 },
118  
119 ###############
120 # We are now the child process. That is, we went from wait -> req
121 # and so PoCo::Daemon forked a process to handle the req
122 4 ▶ daemon_child=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 4 my($heap, $info)=@_[HEAP, ARG1];
124  
125 # $info is the hash we built in 'accept'
126  
127 4 delete $heap->{wheel};
128  
129 $heap->{wheel_client} = POE::Wheel::ReadWrite->new(
130 Handle=>$info->{handle},
131 4 Driver=> new POE::Driver::SysRW, # using sysread and syswrite
132 Filter=> POE::Filter::Line->new(), # use a line filter for negociati
133 InputEvent => 'input',
134 FlushedEvent => 'flushed',
135 ErrorEvent => 'error'
136 );
137 4 Daemon->update_status('long');
138 },
139  
140 ###############
141 # ReadWrite's InputEvent.
142 13 ▶ input => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
143 13 my($heap, $line)=@_[HEAP, ARG0];
144 13 DEBUG and warn "Received $line";
145  
146 13 $line = uc $line;
147  
148 13 ▶ if($line eq 'PID') {
Branch: 7 branches
BranchTrueFalseTotal
if ($line eq 'PID') { } 4 9 13
elsif ($line eq 'PARENT') { } 1 8 9
elsif ($line eq 'PING') { } 2 6 8
elsif ($line eq 'LOGFILE') { } 1 5 6
elsif ($line eq 'KERNEL') { } 2 3 5
elsif ($line eq 'DONE') { } 2 1 3
elsif ($line eq 'STATUS') { } 0 1 1
149 4 $heap->{wheel_client}->put($$);
150 }
151 elsif($line eq 'PARENT') {
152 1 $heap->{wheel_client}->put( $heap->{parent} );
153 }
154 elsif($line eq 'PING') {
155 2 $heap->{wheel_client}->put('PONG');
156 }
157 elsif($line eq 'LOGFILE') {
158 1 $heap->{wheel_client}->put( $logfile );
159 }
160 elsif($line eq 'KERNEL') {
161 2 $heap->{wheel_client}->put( $poe_kernel->ID );
162 }
163 elsif($line eq 'DONE') {
164 2 $heap->{wheel_client}->put('OK');
165 2 $heap->{done}=1;
166 }
167 elsif($line eq 'STATUS') {
168 0 $heap->{wheel_client}->put(Daemon->status);
169 }
170 else {
171 1 $heap->{wheel_client}->put('???');
172 }
173 13 $heap->{pending}=1;
174 },
175  
176 ###############
177 # ReadWrite's FlushedEvent.
178 13 ▶ flushed=>sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
179 13 my($heap)=$_[HEAP];
180 # DEBUG and warn "Flushed";
181 13 $heap->{pending}=0;
182 13 ▶ return unless $heap->{done};
Branch: 1 branch
BranchTrueFalseTotal
unless $heap->{"done"} 11 2 13
183  
184 2 delete $heap->{wheel_client};
185 2 $poe_kernel->post(Daemon=>'update_status', 'done');
186 },
187  
188 ###############
189 0 ▶ USR1=>sub { Daemon->peek( 1 ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
190  
191 4 });
192  
193 #########################
194 4 POE::Component::Daemon->spawn(
195 verbose=>1,
196 alias=>'Daemon',
197 logfile=>$logfile,
198 detach=>1,
199 max_children=>3,
200 );
201  
202 #########################
203 4 $poe_kernel->run();
204  
205 4 DEBUG and warn "$$: Exiting";
206 1;
207  
« eg/preforking ↑ Summary blib/lib/POE/Component/Daemon/Scoreboard.pm »