| 1 | package IPC::QWorker::Worker; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 12 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | # ABSTRACT: worker process for IPC::QWorker | ||||||||||||||||||||
| 8 | 12 | our $VERSION = '0.08'; # VERSION | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 12 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 12 | ▶ | use IO::Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 12 | ▶ | use Storable qw(fd_retrieve store_fd); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 12 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 65 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 16 | 65 | my $this = shift; | |||||||||||||||||||
| 17 | 65 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 18 | 65 | my $self = { | |||||||||||||||||||
| 19 | 'pid' => 0, | ||||||||||||||||||||
| 20 | 'pipe' => undef, | ||||||||||||||||||||
| 21 | 'calls' => {@_}, | ||||||||||||||||||||
| 22 | 'ctx' => {}, | ||||||||||||||||||||
| 23 | 'ready' => 0, | ||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||
| 25 | 65 | bless( $self, $class ); | |||||||||||||||||||
| 26 | 65 | $self->_fork_worker(); | |||||||||||||||||||
| 27 | 55 | return ($self); | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 65 | ▶ | sub _fork_worker { | ||||||||||||||||||
|
Subroutine
_fork_worker: called CC=5 SCAR=16.5
| |||||||||||||||||||||
| 31 | 65 | my $self = shift; | |||||||||||||||||||
| 32 | 65 | my $pid; | |||||||||||||||||||
| 33 | my $parent_pipe; | ||||||||||||||||||||
| 34 | 65 | my $child_pipe; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 65 | ▶ | socketpair( $parent_pipe, $child_pipe, AF_UNIX, SOCK_STREAM, PF_UNIX ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | or croak("cant create socketpair: $!"); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 65 | $parent_pipe->autoflush(1); | |||||||||||||||||||
| 40 | 65 | $child_pipe->autoflush(1); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 65 | ▶ | if ( $pid = fork ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 55 | close($parent_pipe); | |||||||||||||||||||
| 44 | 55 | $self->{'pid'} = $pid; | |||||||||||||||||||
| 45 | 55 | $self->{'pipe'} = $child_pipe; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||
| 48 | 10 | ▶ | if ( !defined($pid) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | croak("cannot fork child process"); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | 10 | close($child_pipe); | |||||||||||||||||||
| 52 | 10 | $self->{'pipe'} = $parent_pipe; | |||||||||||||||||||
| 53 | 10 | ▶ | if ( defined( $self->{'calls'}->{'_init'} ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 10 | $self->{'calls'}->{'_init'}->( $self->{'ctx'} ); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | 10 | ▶ | $SIG{'HUP'} = sub { $self->_shutdown() }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 57 | 10 | $self->_child_loop(); | |||||||||||||||||||
| 58 | 0 | close($parent_pipe); | |||||||||||||||||||
| 59 | 0 | exit; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 10 | ▶ | sub _child_loop { | ||||||||||||||||||
|
Subroutine
_child_loop: called CC=4 SCAR=14.8
| |||||||||||||||||||||
| 64 | 10 | my $self = shift; | |||||||||||||||||||
| 65 | 10 | my $qentry; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 10 | $self->send_ready(); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 10 | ▶ | while ( $qentry = ${ fd_retrieve( $self->{'pipe'} ) } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | if ($IPC::QWorker::DEBUG) { print STDERR Dumper($qentry); } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 120 | ▶ | if ( defined $self->{'calls'}->{ $qentry->{'cmd'} } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | $self->{'calls'}->{ $qentry->{'cmd'} } | ||||||||||||||||||||
| 74 | 120 | ->( $self->{'ctx'}, $qentry->{'params'} ); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||
| 77 | 0 | croak( $$ . ": no such call defined in this worker" ); | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 120 | $self->send_ready(); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 130 | ▶ | sub send_ready { | ||||||||||||||||||
|
Subroutine
send_ready: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 85 | 130 | my $self = shift(); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 130 | print ${ $self->{'pipe'} }($$." READY\n"); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 120 | ▶ | sub send_entry { | ||||||||||||||||||
|
Subroutine
send_entry: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 91 | 120 | my $self = shift; | |||||||||||||||||||
| 92 | 120 | my $qentry = shift; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 120 | $self->{'ready'} = 0; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 120 | ▶ | if ($IPC::QWorker::DEBUG) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | print STDERR $$ . ": sending entry...\n"; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 120 | store_fd( \$qentry, $self->{'pipe'} ); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 10 | ▶ | sub exit_child { | ||||||||||||||||||
|
Subroutine
exit_child: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 103 | 10 | my $self = shift(); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 10 | ▶ | if ( $self->{'pid'} == 0 ) { # do nothing when called from within child | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 0 | return (); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 10 | kill( 'HUP', $self->{'pid'} ); | |||||||||||||||||||
| 110 | 10 | waitpid( $self->{'pid'}, 0 ); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 10 | ▶ | sub _shutdown { | ||||||||||||||||||
|
Subroutine
_shutdown: called CC=3 SCAR=11.8
| |||||||||||||||||||||
| 114 | 10 | my $self = shift(); | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 10 | ▶ | if ( defined( $self->{'calls'}->{'_destroy'} ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 10 | $self->{'calls'}->{'_destroy'}->( $self->{'ctx'} ); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | 0 | ▶ | if ($IPC::QWorker::DEBUG) { print STDERR $$ . ": exiting...\n"; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 10 | exit(0); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 12 | 1; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | # vim:ts=2:expandtab:syntax=perl: | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||