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