Generic/Child.pm

filter: Statementstmt 90.8% 139 / 153 Branchbran 66.6% 56 / 84 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 15 totaltotal 77.4% 223 / 288 CC 51 SCAR 25.0
CC
51
Cov
81%
request
30.7
loop
7.5
OOB_setup
7.4
CRAP 12.1
↑ Summary Generic.pm »
1 package POE::Component::Generic::Child;
2 # $Id: Child.pm 759 2011-05-18 16:55:01Z fil $
3  
4 # This is the object that does all the work in the child process
5  
6 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 ##################################################
11 # Called from Generic::process_requests
12 sub new
13 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 4 my( $package, %params ) = @_;
15 4 my $self = bless { %params }, $package;
16  
17 4 $self->{filter} = POE::Filter::Reference->new();
18  
19 # there's room for other callbacks
20 4 $self->{callback_defs} = {};
21 # sub-objects
22 4 $self->{objects} = {};
23 4 $params{ID} =~ s/[^A-Za-z]+/A/g;
24 4 $self->{object_id} = "$params{ID}OBJ000000";
25 4 $self->init_handles;
26  
27 4 return $self;
28 }
29  
30 ##################################################
31 # Setup handles we want to play with
32 sub init_handles
33 4 ▶{
Subroutine init_handles: called CC=3 SCAR=11.1
Pod undocumented
34 4 my( $self ) = @_;
35  
36 4 my $myout = $self->{myout} = gensym;
37  
38 # Redirect STDOUT to STDERR, so that badly behaved user code doesn't
39 # mess up our communication with the parent
40 4 ▶ open $myout, ">&STDOUT" or die "Can't dup STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $myout, ">&STDOUT" or die "Can't dup STDOUT: $!\n" 4 0 4
41 4 ▶ open STDOUT, ">&STDERR" or die "Can't dup STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&STDERR" or die "Can't dup STDERR: $!\n" 4 0 4
42  
43 # binmode so that Storable refs can make it across
44 4 binmode(STDIN);
45 4 binmode(STDOUT);
46 4 binmode($myout);
47 # No buffers for us
48 4 STDOUT->autoflush(1);
49 4 $myout->autoflush(1);
50  
51 }
52
53  
54 ##################################################
55 # Main loop.
56 # When this method exits, the child will exit.
57 sub loop
58 4 ▶{
Subroutine loop: called CC=7 SCAR=20.1
Pod undocumented
59 4 my( $self ) = @_;
60
61 4 $self->status( 'startup' );
62  
63  
64 READ:
65 4 ▶ while ( my $requests = $self->get_requests ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $requests = $self->get_requests) 27 4 31
66 27 $self->status( 'request' );
67 27 ▶ unless ($self->{obj}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"obj"}) 4 23 27
68 4 my $req = shift @{$requests};
69 4 ▶ unless( ref( $req ) eq 'HASH' and $req->{req} eq 'setup' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $req eq "HASH" and $req->{"req"} eq "setup") 0 4 4
Condition33%1/3
ref $req eq "HASH" and $req->{"req"} eq "setup"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $req eq "HASH" and $req->{"req"} eq "setup"
ref $req eq "HASH" $req->{"req"} eq "setup"
Truth table33%1/3
A and B
A= ref $req eq "HASH"B= $req->{"req"} eq "setup"
ABresult
0X0
100
111
70 0 die "First request must be req=>setup";
71 }
72 # use Data::Denter;
73 # warn "setup=", Denter $req;
74 4 $self->OOB_req( $req );
75 }
76
77 27 foreach my $req (@{$requests}) {
78 # use Data::Denter;
79 # warn "req=", Denter $req;
80 27 ▶ if( $req->{req} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{'req'}) { } 0 27 27
81 0 $self->OOB_req( $req );
82 }
83 else {
84 27 $self->request( $req );
85 }
86 }
87 27 $self->status( 'read' );
88 }
89 4 $self->status( 'exit' );
90 }
91  
92 ##################################################
93 # Get the next block of requests
94 # Return :
95 # undef() - shutdown child
96 # arrayref of request hashes
97 sub get_requests
98 31 ▶{
Subroutine get_requests: called CC=2 SCAR=6.9
Pod undocumented
99 31 my( $self ) = @_;
100  
101 31 my $raw;
102 31 ▶ return unless sysread ( STDIN, $raw, $self->{size} );
Branch: 1 branch
BranchTrueFalseTotal
unless sysread STDIN, $raw, $self->{"size"} 4 27 31
103 27 return $self->{filter}->get([$raw]);
104 }
105  
106  
107 ##################################################
108 # Update our status
109 sub status
110 101 ▶{
Subroutine status: called CC=2 SCAR=7.0
Pod undocumented
111 101 my $self = shift;
112 101 ▶ $self->{debug} and warn join ' ', @_, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn join(" ", @_, "\n") 0 101 101
113 101 $0 = join ' ', $self->{proc}, $self->{name}, @_;
114 101 return;
115 }
116  
117 ##################################################
118 # Send a response to the parent
119 sub reply
120 35 ▶{
Subroutine reply: called CC=2 SCAR=7.0
Pod undocumented
121 35 my( $self, $resp ) = @_;
122 35 $self->status( 'reply' );
123 # use Data::Denter;
124 # warn "reply=", Denter $resp;
125 35 my $replies = $self->{filter}->put( [ $resp ] );
126
127 35 my $rv = $self->{myout}->print( join '', @$replies );
128 35 ▶ die "STDOUT: $!" unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 35 35
129 }
130  
131  
132  
133  
134 ##################################################
135 # Handle a regular request from the parent
136 sub request
137 27 ▶{
Subroutine request: called CC=22 SCAR=34.2
Pod undocumented
138 27 my( $self, $req ) = @_;
139
140 27 my $method = $req->{method};
141 27 ▶ $self->{debug} and warn "method=$method";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn "method=$method" 0 27 27
142  
143 27 ▶ if( $req->{callbacks} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"callbacks"}) 3 24 27
144 3 $self->callback_demarshall( $req, $req->{callbacks} );
145 }
146 27 ▶ if( $req->{postbacks} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"postbacks"}) 5 22 27
147 5 $self->postback_demarshall( $req, $req->{postbacks} );
148 }
149  
150 # The object we want to work on
151 27 my $obj = $self->{obj};
152 27 ▶ if( $req->{obj} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"obj"}) 5 22 27
153 5 $obj = $self->{objects}{ $req->{obj} };
154 5 ▶ unless( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 1 4 5
155 1 $req->{error} = "Unknown object $req->{obj}";
156 1 ▶ $self->{debug} and warn $req->{error};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn $req->{"error"} 0 1 1
157 1 $self->reply( $req );
158 1 return;
159 }
160  
161 4 ▶ if( $method eq 'DESTROY' ) { # special case
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "DESTROY") 1 3 4
162 1 ▶ $self->{debug} and warn "DESTROY for object $req->{obj}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn qq[DESTROY for object $req->{"obj"}] 0 1 1
163  
164 1 delete $self->{objects}{ $req->{obj} };
165  
166 # Generic::Object requires DESTROY getting this far.
167 # However, if the object can't really handle it, skip out now
168 1 ▶ return unless $obj->can( $method );
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->can($method) 1 0 1
169  
170 0 delete $req->{wantarray}; # never
171 0 delete $req->{event}; # ever
172 }
173 }
174  
175  
176 # keeping {args} in req messes up callbacks
177 25 my $args = delete $req->{args};
178  
179 25 eval {
180 25 ▶ $self->{debug} and do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and do { if ($req->{"factory"}) { warn "Calling factory $method on $obj"; } else { warn "Calling $method on $obj"; } } 0 25 25
181 0 ▶ if( $req->{factory} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{'factory'}) { } 0 0 0
182 0 warn "Calling factory $method on $obj";
183 }
184 else {
185 0 warn "Calling $method on $obj";
186 }
187 };
188 25 ▶ if( $req->{wantarray} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($req->{'wantarray'}) { } 3 22 25
elsif (defined $req->{'wantarray'} or $req->{'factory'}) { } 16 6 22
elsif ($method eq 'DESTROY' and not $obj->can($method)) { } 1 5 6
189 3 $req->{result} = [ $obj->$method( @$args ) ];
190 }
191 ▶ elsif( defined $req->{wantarray} or $req->{factory} ) {
Condition66%2/3
defined $req->{"wantarray"} or $req->{"factory"}
l!l&&r!l&&!r
1606
MC/DC50%1/2
defined $req->{"wantarray"} or $req->{"factory"}
defined $req->{"wantarray"} $req->{"factory"}
Truth table66%2/3
A or B
A= defined $req->{"wantarray"}B= $req->{"factory"}
ABresult
1X1
011
000
192 16 $req->{result} = [ scalar $obj->$method( @$args ) ];
193 }
194 ▶ elsif( $method eq 'DESTROY' and not $obj->can( $method ) ) {
Condition66%2/3
$method eq "DESTROY" and not $obj->can($method)
!ll&&!rl&&r
501
MC/DC50%1/2
$method eq "DESTROY" and not $obj->can($method)
$method eq "DESTROY" not $obj->can($method)
Truth table66%2/3
A and not(B)
A= $method eq "DESTROY"B= not $obj->can($method)
ABresult
0X0
100
111
195 # DESTROY is dispacted from Generic::DESTROY. $obj might not
196 # implement it. If it doesn't, we don't want the error produced
197 # by blindly calling it.
198 }
199 else {
200 5 $obj->$method( @$args );
201 }
202 };
203  
204 25 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 25 25
205 0 ▶ $self->{debug} and warn $@;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn $@ 0 0 0
206 0 $req->{error} = $@;
207 0 delete $req->{result};
208 }
209  
210 #############
211 25 ▶ if( $req->{factory} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"factory"}) 2 23 25
212 2 $self->factory_response( $req );
213 }
214  
215 #############
216 25 ▶ if( defined $req->{event} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $req->{'event'}) { } 19 6 25
elsif ($req->{'error'}) { } 0 6 6
217 19 $self->reply( $req );
218 }
219 elsif( $req->{error} ) {
220 0 warn $req->{error};
221 }
222  
223 }
224  
225 ##################################################
226 # Convert callbacks into coderefs
227 sub callback_demarshall
228 3 ▶{
Subroutine callback_demarshall: called CC=3 SCAR=11.4
Pod undocumented
229 3 my( $self, $req, $cdef ) = @_;
230
231 3 foreach my $cb ( @$cdef ) {
232 4 ▶ unless( $req->{args}[ $cb->{pos} ] eq $cb->{CBid} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->{"args"}[$cb->{"pos"}] eq $cb->{"CBid"}) 0 4 4
233 0 die "Argument at position $cb->{pos} isn't $cb->{CBid}";
234 }
235
236 6 ▶ $req->{args}[ $cb->{pos} ] = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
237 $self->reply( {
238 response => 'callback',
239 RID => $req->{RID},
240 pos => $cb->{pos},
241 6 result => [ @_ ]
242 } );
243 4 };
244 }
245 }
246  
247 ##################################################
248 # Convert postbacks into a coderef
249 sub postback_demarshall
250 5 ▶{
Subroutine postback_demarshall: called CC=3 SCAR=11.2
Pod undocumented
251 5 my( $self, $req, $pdef ) = @_;
252
253 5 foreach my $pb ( @$pdef ) {
254 5 ▶ unless( $req->{args}[ $pb->{pos} ] eq $pb->{PBid} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->{"args"}[$pb->{"pos"}] eq $pb->{"PBid"}) 0 5 5
255 0 die "Argument at position $pb->{pos} isn't $pb->{PBid}";
256 }
257
258 5 my $PBid = $pb->{PBid};
259 5 my $session = $pb->{session};
260 5 my $event = $pb->{event};
261  
262 5 ▶ $req->{args}[ $pb->{pos} ] = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
263 5 $self->reply( {
264 response => 'postback',
265 PBid => $PBid,
266 session => $session,
267 event => $event,
268 result => [ @_ ]
269 } );
270 5 };
271 }
272 }
273  
274 ##################################################
275 # Modify the response from a factory method
276 sub factory_response
277 2 ▶{
Subroutine factory_response: called CC=3 SCAR=11.2
Pod undocumented
278 2 my( $self, $req ) = @_;
279  
280 2 my $OBJid = $self->{object_id}++;
281  
282 2 $self->{objects}{ $OBJid } = $req->{result}[0];
283 2 my $package = ref $self->{objects}{ $OBJid };
284  
285 2 ▶ Carp::confess "Didn't return an object for $OBJid" unless $req->{result}[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"result"}[0] 0 2 2
286  
287 $self->{debug} and
288 2 ▶ warn "factory_response package=$package $OBJid=$self->{objects}{ $OBJid }";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn qq[factory_response package=$package $OBJid=$self->{"objects"}{$OBJid}] 0 2 2
289  
290 $req->{result}[0] = {
291 package => $package,
292 debug => $self->{debug},
293 2 methods => [ POE::Component::Generic->__package_methods( $package )
294 ],
295 OBJid => $OBJid
296 };
297 }
298  
299 ##################################################
300 # Out-of-band request
301 sub OOB_req
302 4 ▶{
Subroutine OOB_req: called CC=2 SCAR=7.2
Pod undocumented
303 4 my( $self, $req ) = @_;
304  
305 4 $self->status( 'OOB' );
306  
307 4 my $method = $req->{req};
308 4 ▶ if( $method eq 'setup' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'setup') { } 4 0 4
309 4 $self->OOB_setup( $req );
310 }
311 else {
312 0 warn "Unknown OOB request $method";
313 }
314 }
315  
316 ##################################################
317 # First request from parent
318 # Create the object, configure the child process
319 sub OOB_setup
320 4 ▶{
Subroutine OOB_setup: called CC=7 SCAR=20.0
Pod undocumented
321 4 my( $self, $req ) = @_;
322
323 4 foreach my $f ( qw( name size debug verbose ) ) {
324 16 ▶ next unless exists $req->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $req->{$f} 4 12 16
325 12 $self->{$f} = $req->{$f};
326 12 ▶ $self->{debug} and warn "Setting $f=$self->{$f}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn "Setting $f=$self->{$f}" 0 12 12
327 }
328
329 4 ▶ $self->{debug} and warn "build object $req->{package}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn qq[build object $req->{"package"}] 0 4 4
330 4 $self->{obj} = object_build( $req->{package}, $req->{args} );
331  
332 4 ▶ $self->{debug} and warn "Child PID is $$\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn "Child PID is ${$}\n" 0 4 4
333  
334 $self->{debug} and
335 4 ▶ warn "object=$self->{obj}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and warn qq[object=$self->{"obj"}] 0 4 4
336 4 $self->reply( { PID=>$$, response=>'new' } );
337 }
338  
339  
340  
341  
342  
343 ##################################################
344 # Create an object of a given class
345 sub object_build
346 5 ▶{
Subroutine object_build: called CC=2 SCAR=7.0
Pod undocumented
347 5 my( $package, $args ) = @_;
348 5 my $ctor = package_load( $package );
349 5 ▶ die "Can't find constructor for package $package" unless $ctor;
Branch: 1 branch
BranchTrueFalseTotal
unless $ctor 0 5 5
350 5 return $package->can($ctor)->( $package, @$args );
351 }
352  
353  
354 ##################################################
355 # Load the user package. Also used by PoCo::Generic
356 sub package_load
357 23 ▶{
Subroutine package_load: called CC=3 SCAR=11.0
Pod undocumented
358 23 my( $package ) = @_;
359 23 my $ctor = find_ctor( $package );
360 23 ▶ return $ctor if $ctor; # package already loaded
Branch: 1 branch
BranchTrueFalseTotal
if $ctor 9 14 23
361 14 ▶ eval "use $package";
Subroutine BEGIN: called CC=1 SCAR=0.0
362 14 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
363 14 return find_ctor( $package );
364 }
365  
366 ##################################################
367 # Find an object constructor.
368 sub find_ctor
369 37 ▶{
Subroutine find_ctor: called CC=3 SCAR=11.0
Pod undocumented
370 37 my( $package ) = @_;
371 37 foreach my $ctor ( qw( new spawn create ) ) {
372 65 ▶ return $ctor if $package->can( $ctor );
Branch: 1 branch
BranchTrueFalseTotal
if $package->can($ctor) 23 42 65
373 }
374 14 return;
375 }
376  
377  
378 21 1;
379  
380 __END__
↑ Summary Generic.pm »