| 1 | package IPC::Simple::Group; | ||||||||||
| 2 | # ABSTRACT: work with several processes as a group | ||||||||||
| 3 | 4 | $IPC::Simple::Group::VERSION = '0.08'; | |||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use IPC::Simple::Channel qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | sub new { | ||||||||||
| 12 | 1 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | my $self = bless{ | |||||||||
| 15 | members => {}, | ||||||||||
| 16 | messages => IPC::Simple::Channel->new, | ||||||||||
| 17 | }, $class; | ||||||||||
| 18 | |||||||||||
| 19 | 1 | $self->add(@_); | |||||||||
| 20 | |||||||||||
| 21 | 1 | return $self; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | sub add { | ||||||||||
| 25 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
add: called CC=7 SCAR=20.4
Pod
undocumented
| |||||||||||
| 26 | |||||||||||
| 27 | 1 | for (@_) { | |||||||||
| 28 | 2 | ▶ | croak 'processes must be named to be grouped' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | unless $_->name; | ||||||||||
| 30 | |||||||||||
| 31 | croak 'processes with a recv_cb may not be grouped' | ||||||||||
| 32 | 2 | ▶ | if $_->{cb}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | |||||||||||
| 34 | croak 'processes with a term_cb may not be grouped' | ||||||||||
| 35 | 2 | ▶ | if $_->{term_cb}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 1 | for (@_) { | |||||||||
| 39 | 2 | $self->{members}{ $_->{name} } = $_; | |||||||||
| 40 | 2 | ▶ | $_->{recv_cb} = sub{ $self->{messages}->put( $_[0] ) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | ▶ | $_->{term_cb} = sub{ $self->drop( $_[0] ) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | |||||||||||
| 43 | # If the process has already been launched, move existing messages into the | ||||||||||
| 44 | # group queue. | ||||||||||
| 45 | 2 | ▶ | unless ($_->is_ready) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | $self->{messages}->put( $_->{messages}->clear ); | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub drop { | ||||||||||
| 52 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
drop: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 53 | |||||||||||
| 54 | delete $self->{members}{ $_->{name} } | ||||||||||
| 55 | 2 | for @_; | |||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | unless (%{ $self->{members} }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 1 | $self->{messages}->shutdown; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | sub members { | ||||||||||
| 63 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
members: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 64 | 3 | return values %{ $self->{members} }; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | sub launch { | ||||||||||
| 68 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
launch: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 69 | |||||||||||
| 70 | 1 | for ($self->members) { | |||||||||
| 71 | 2 | ▶ | $_->launch if $_->is_ready; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | sub terminate { | ||||||||||
| 76 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
terminate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 77 | 1 | $_->terminate(@_) for $self->members; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub signal { | ||||||||||
| 81 | 0 | ▶ | my ($self, $signal) = @_; | ||||||||
|
Subroutine
signal: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 82 | 0 | $self->signal($signal) for $self->members; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | sub join { | ||||||||||
| 86 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
join: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 87 | 1 | $_->join for $self->members; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | sub recv { | ||||||||||
| 91 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
recv: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 92 | 5 | $self->{messages}->recv; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 4 | 1; | |||||||||
| 96 | |||||||||||
| 97 | __END__ | ||||||||||