eg/preforking

filter: Statementstmt 91.9% 103 / 112 Branchbran 69.2% 18 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 20 / 20 Podpod n/a totaltotal 87.1% 142 / 163 CC 14 SCAR 14.5
CC
14
Cov
87%
__ANON__
9.2
__ANON__
8.3
__ANON__
2.0
CRAP 4.3
« blib/lib/POE/Component/Daemon/ScoreboardSemaphore.pm ↑ Summary eg/forking »
1 #!/usr/bin/perl -w
2 # $Id: preforking 911 2012-07-29 13:52:13Z monaco $
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # use Religion::Package qw(1 1);
6 7 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use POE::Component::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use POE::Driver::SysRW;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use POSIX qw(EADDRINUSE);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Socket qw(inet_ntoa sockaddr_in);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub DEBUG () { 0 }
17  
18 7 my $port=shift;
19 7 ▶die "Usage: $0 port" unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 0 7 7
20  
21 7 my $logfile = "$FindBin::Dir/log_preforking";
22  
23 # $DB::fork_TTY='/dev/pts/4';
24  
25 #########################
26 POE::Session->create(
27 inline_states=>{
28 7 ▶ _start=>sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
29 7 my($kernel, $heap)=@_[KERNEL, HEAP];
30 7 $heap->{wheel}=POE::Wheel::SocketFactory->new(
31 BindPort => $port,
32 Reuse => 'on', # Lets the port be reused
33 BindAddress => '127.0.0.1',
34 SuccessEvent => 'accept',
35 FailureEvent => 'error',
36 );
37 7 ▶ if( $port == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port == 0) { } 7 0 7
38 7 $port = ( sockaddr_in($heap->{wheel}->getsockname()) )[0];
39 7 print "PORT=$port\n";
40 }
41 else {
42 0 warn "$$: Listening on port $port";
43 }
44 7 $heap->{wheel}->pause_accept(); # is resumed in started
45  
46 7 $kernel->sig('daemon_child'=>'daemon_child');
47 7 $kernel->sig('daemon_parent'=>'daemon_parent');
48 7 $kernel->sig('daemon_accept'=>'daemon_accept');
49  
50 7 $kernel->sig('daemon_shutdown'=>'daemon_shutdown');
51  
52 7 $heap->{parent} = 0;
53 7 $heap->{rid}=0;
54 },
55  
56 2 ▶ error=>sub {
Subroutine __ANON__: called CC=4 SCAR=21.2
57 2 my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0, ARG1, ARG2];
58 2 ▶ if( 0==$errnum and $operation eq 'read' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $errnum and $operation eq "read") 2 0 2
Condition33%1/3
0 == $errnum and $operation eq "read"
!ll&&!rl&&r
002
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
59 # EOF
60 2 $heap->{done}=1;
61 2 return;
62 }
63 0 warn "$$: $operation:$errnum: $errstr";
64 0 ▶ if($errnum==EADDRINUSE) { # EADDRINUSE
Branch: 1 branch
BranchTrueFalseTotal
if ($errnum == 98) 0 0 0
65 0 Daemon->shutdown(); # THIS IS IMPORTANT
66 }
67 0 delete $heap->{wheel};
68 0 delete $heap->{wheel_client};
69 0 Daemon->shutdown();
70 },
71  
72 ###############
73 ## Called when we switch to a child process
74 6 ▶ daemon_child=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 6 my($kernel, $heap)=@_[KERNEL, HEAP];
76 6 DEBUG and
77 warn "Started (parent=$heap->{parent})";
78 6 $kernel->post(Daemon=>'update_status', 'wait');
79 6 return;
80 },
81  
82 ###############
83 ## Called when a child process set status to 'wait'
84 6 ▶ daemon_accept=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 6 my($kernel, $heap)=@_[KERNEL, HEAP];
86 6 DEBUG and
87 warn "Accepting (parent=$heap->{parent})";
88 6 $heap->{wheel}->resume_accept(); # was paused in _start and accept
89 6 return;
90 },
91  
92 ###############
93 ## PoCo::Daemon's daemon_parent. We are a parent process, after the
94 ## initial children are forked off.
95 6 ▶ daemon_parent=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 6 my($kernel, $heap)=@_[KERNEL, HEAP];
97 6 DEBUG and warn "PARENT";
98 6 $heap->{parent} = $$;
99 },
100  
101  
102 ###############
103 # SocketFactory got a connection handle it here
104 4 ▶ accept=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
105 4 my ($heap, $handle, $peer, $port, $id)=@_[HEAP, ARG0..ARG3];
106  
107 4 $peer=inet_ntoa($peer);
108 # DEBUG and
109 4 warn "Connection id=$id from $peer:$port";
110
111 4 $heap->{wheel}->pause_accept(); # is resumed in started
112 4 my $info={handle=>$handle, peer=>$peer, port=>$port, id=>$id};
113 4 Daemon->update_status('req');
114  
115 4 $heap->{done} = 0;
116  
117 $heap->{wheel_client} = POE::Wheel::ReadWrite->new(
118 Handle=>$info->{handle},
119 4 Driver=> new POE::Driver::SysRW, # using sysread and syswrite
120 Filter=> POE::Filter::Line->new(), # use a line filter for negociati
121 InputEvent => 'input',
122 FlushedEvent => 'flushed',
123 ErrorEvent => 'error'
124 );
125 },
126  
127 ###############
128 ## ReadWrite's InputEvent
129 11 ▶ input => sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
130 11 my($heap, $line)=@_[HEAP, ARG0];
131 # DEBUG and
132 11 warn "Received $line";
133  
134 11 $line = uc $line;
135  
136 11 ▶ if($line eq 'PID') {
Branch: 7 branches
BranchTrueFalseTotal
if ($line eq 'PID') { } 5 6 11
elsif ($line eq 'PING') { } 0 6 6
elsif ($line eq 'PARENT') { } 1 5 6
elsif ($line eq 'PEEK') { } 1 4 5
elsif ($line eq 'STATUS') { } 1 3 4
elsif ($line eq 'DONE') { } 2 1 3
elsif ($line eq 'LOGFILE') { } 1 0 1
137 5 $heap->{wheel_client}->put($$);
138 }
139 elsif($line eq 'PING') {
140 0 $heap->{wheel_client}->put('PONG');
141 }
142 elsif($line eq 'PARENT') {
143 1 $heap->{wheel_client}->put( $heap->{parent} );
144 }
145 elsif($line eq 'PEEK') {
146 1 my $peek = eval { Daemon->peek( 1 ) };
147 1 ▶ $peek .= $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
148 1 $heap->{wheel_client}->put( split "\n", $peek );
149 1 $heap->{wheel_client}->put( "DONE" );
150 }
151 elsif($line eq 'STATUS') {
152 1 $heap->{wheel_client}->put( split "\n", Daemon->status );
153 1 $heap->{wheel_client}->put( "DONE" );
154 }
155 elsif($line eq 'DONE') {
156 2 $heap->{wheel_client}->put('OK');
157 2 $heap->{done} = 1;
158 }
159 elsif($line eq 'LOGFILE') {
160 1 $heap->{wheel_client}->put( $logfile );
161 }
162 else {
163 0 $heap->{wheel_client}->put('???');
164 }
165 11 $heap->{pending}=1;
166 },
167  
168 ###############
169 ## ReadWrite's FlushedEvent
170 11 ▶ flushed=>sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
171 11 my($heap)=$_[HEAP];
172 11 $heap->{pending}=0;
173  
174 11 ▶ return unless $heap->{done};
Branch: 1 branch
BranchTrueFalseTotal
unless $heap->{"done"} 9 2 11
175  
176 2 DEBUG and warn "DONE";
177 2 delete $heap->{wheel_client};
178 2 $poe_kernel->post(Daemon=>'update_status', 'done');
179 },
180  
181 ################
182 ## daemon_shutdown signal. Sent when we get at TERM or INT, or when
183 ## we handle enough requests to be recycled.
184 7 ▶ daemon_shutdown => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 7 my($heap)=$_[HEAP];
186  
187 7 DEBUG and warn "$$: daemon_shutdown\n";
188  
189 7 delete $heap->{wheel};
190 7 delete $heap->{wheel_client};
191 7 return;
192 }
193  
194 7 });
195  
196 #########################
197 7 POE::Component::Daemon->spawn(
198 alias => 'Daemon',
199 logfile => $logfile,
200 detach => 1,
201 verbose => 1,
202 start_children => 1,
203 requests => 1,
204 min_spare => 2,
205 max_children => 10,
206 );
207  
208  
209  
210  
211  
212  
213 #########################
214 7 $poe_kernel->run();
215  
216 7 warn "$$: Exiting";
217 1;
218  
« blib/lib/POE/Component/Daemon/ScoreboardSemaphore.pm ↑ Summary eg/forking »