| 1 | package IPC::Manager::Client::SharedMem; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 89 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 89 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 89 | our $VERSION = '0.000028'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | # Set to 1 by Makefile.PL at install time when the host's SysV IPC is | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # unreliable. When true, _viable() throws an exception explaining how | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # to recover instead of returning a successful viability check. The | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # module otherwise loads normally so callers can still introspect it. | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 89 | our $BROKEN_SYSV_IPC = 0; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 89 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 89 | ▶ | use IPC::Manager::Message; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 89 | ▶ | use IPC::Manager::Serializer::JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 89 | ▶ | use IPC::Manager::Util qw/pid_is_running/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 89 | ▶ | use parent 'IPC::Manager::Client'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 89 | ▶ | use Object::HashBase qw{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | +_shm_id | ||||||||||||||||||||||||||||||||||||||||||
| 22 | +_sem_id | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 89 | }; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 89 | my $SHM_INITIAL_SIZE = 65536; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 89 | my $SHM_GROW_FACTOR = 2; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 89 | my ($_IPC_PRIVATE, $_IPC_CREAT, $_IPC_RMID, $_S_IRUSR, $_S_IWUSR); | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 346 | ▶ | sub _load_constants { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_constants: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 346 | ▶ | return if $_IPC_PRIVATE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 346 | require IPC::SysV; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 346 | $_IPC_PRIVATE = IPC::SysV::IPC_PRIVATE(); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 346 | $_IPC_CREAT = IPC::SysV::IPC_CREAT(); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 346 | $_IPC_RMID = IPC::SysV::IPC_RMID(); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 346 | $_S_IRUSR = IPC::SysV::S_IRUSR(); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 346 | $_S_IWUSR = IPC::SysV::S_IWUSR(); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 89 | ▶ | sub _viable { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_viable: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 89 | ▶ | die <<'BROKEN' if $BROKEN_SYSV_IPC; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | IPC::Manager::Client::SharedMem was disabled when IPC::Manager was | ||||||||||||||||||||||||||||||||||||||||||
| 43 | installed because the host's SysV IPC implementation was unreliable. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | To use the SharedMem protocol, fix the SysV IPC support on this | ||||||||||||||||||||||||||||||||||||||||||
| 45 | system (kernel module / capabilities / IPC::SysV version) and then | ||||||||||||||||||||||||||||||||||||||||||
| 46 | re-install IPC::Manager. Other IPC::Manager protocols (UnixSocket, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | AtomicPipe, JSONFile, MessageFiles, the DBI-backed protocols, etc.) | ||||||||||||||||||||||||||||||||||||||||||
| 48 | are unaffected and continue to work. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | BROKEN | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 89 | require IPC::SysV; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 89 | IPC::SysV->VERSION('2.09'); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 89 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | # Route format: "shmid:semid" | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 246 | ▶ | sub _parse_route { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_route: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 246 | my ($route) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 246 | my ($shm_id, $sem_id) = split /:/, $route, 2; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 246 | return ($shm_id + 0, $sem_id + 0); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 100 | ▶ | sub _build_route { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_route: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 100 | my ($shm_id, $sem_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 100 | return "$shm_id:$sem_id"; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 100 | ▶ | sub spawn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 100 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 100 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 100 | _load_constants(); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 100 | ▶ | my $size = delete $params{shm_size} // $SHM_INITIAL_SIZE; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $params{"shm_size"} // $SHM_INITIAL_SIZE
MC/DC0%0/2
delete $params{"shm_size"} // $SHM_INITIAL_SIZE
delete $params{"shm_size"}
$SHM_INITIAL_SIZE
Truth table33%1/3
A // B
A= delete $params{"shm_size"}B= $SHM_INITIAL_SIZE
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 100 | my $flags = $_S_IRUSR | $_S_IWUSR | $_IPC_CREAT; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 100 | my $sem_id = semget($_IPC_PRIVATE, 1, $flags); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 100 | ▶ | defined $sem_id or croak "Cannot create semaphore: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | # Initialize semaphore to 1 (unlocked) | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 100 | ▶ | semop($sem_id, pack("s!3", 0, 1, 0)) or croak "Cannot init semaphore: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 100 | my $shm_id = shmget($_IPC_PRIVATE, $size, $flags); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 100 | ▶ | defined $shm_id or croak "Cannot create shared memory: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 100 | my $state = {clients => {}, messages => {}, stats => {}}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 100 | my $data = IPC::Manager::Serializer::JSON->serialize($state); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 100 | _shm_write($shm_id, $data, $size); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 100 | return _build_route($shm_id, $sem_id); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 72 | ▶ | sub unspawn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unspawn: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 72 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 72 | my ($route) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 72 | ▶ | return unless $route; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 72 | _load_constants(); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 72 | my ($shm_id, $sem_id) = _parse_route($route); | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # Follow any forwarding pointers to find and remove grown segments | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 72 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 72 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 72 | my ($data) = _shm_read($shm_id); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 72 | ▶ | if ($data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 72 | my $state = IPC::Manager::Serializer::JSON->deserialize($data); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 72 | ▶ | if ($state->{_shm_id} && $state->{_shm_id} != $shm_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$state->{"_shm_id"} and $state->{"_shm_id"} != $shm_id
MC/DC0%0/2
$state->{"_shm_id"} and $state->{"_shm_id"} != $shm_id
$state->{"_shm_id"}
$state->{"_shm_id"} != $shm_id
Truth table33%1/3
A and B
A= $state->{"_shm_id"}B= $state->{"_shm_id"} != $shm_id
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | shmctl($state->{_shm_id}, $_IPC_RMID, 0); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 72 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } or warn $@; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # Remove original shared memory segment | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 72 | ▶ | eval { shmctl($shm_id, $_IPC_RMID, 0); 1 } or warn $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # Remove semaphore | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 72 | ▶ | eval { semctl($sem_id, 0, $_IPC_RMID, 0); 1 } or warn $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # --- Raw shared memory I/O --- | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # Segment layout: | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # bytes 0-3: segment capacity (network-order u32) | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # bytes 4-7: data length (network-order u32) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # bytes 8+: JSON data | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 89 | my $HEADER_SIZE = 8; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 942 | ▶ | sub _shm_write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_shm_write: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 942 | my ($shm_id, $data, $capacity) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 942 | my $needed = $HEADER_SIZE + length($data); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 942 | ▶ | croak "Data too large for shared memory segment ($needed > $capacity)" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | if $needed > $capacity; | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 942 | my $header = pack("NN", $capacity, length($data)); | |||||||||||||||||||||||||||||||||||||||||
| 136 | 942 | ▶ | shmwrite($shm_id, $header . $data, 0, $needed) or croak "shmwrite failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 1733 | ▶ | sub _shm_read { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_shm_read: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 1733 | my ($shm_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 1733 | my $header = ''; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 1733 | ▶ | shmread($shm_id, $header, 0, $HEADER_SIZE) or croak "shmread (header) failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 1733 | my ($capacity, $len) = unpack("NN", $header); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 1733 | ▶ | return (undef, 0) unless $len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 1733 | my $data = ''; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1733 | ▶ | shmread($shm_id, $data, $HEADER_SIZE, $len) or croak "shmread (data) failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | # shmread pads with nulls; trim to actual length | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 1733 | $data = substr($data, 0, $len); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 1733 | return ($data, $capacity); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | # --- Locking via semaphore --- | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 2532 | ▶ | sub _get_ids { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_ids: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 2532 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 2532 | ▶ | unless ($self->{+_SHM_ID}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 174 | my ($shm_id, $sem_id) = _parse_route($self->{route}); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 174 | $self->{+_SHM_ID} = $shm_id; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 174 | $self->{+_SEM_ID} = $sem_id; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 2532 | return ($self->{+_SHM_ID}, $self->{+_SEM_ID}); | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 1661 | ▶ | sub _sem_lock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sem_lock: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 1661 | my ($sem_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 168 | # decrement semaphore (wait until > 0, then decrement) | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 1661 | ▶ | semop($sem_id, pack("s!3", 0, -1, 0)) or croak "Cannot acquire semaphore lock: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1661 | ▶ | sub _sem_unlock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sem_unlock: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 1661 | my ($sem_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 174 | # increment semaphore | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 1661 | ▶ | semop($sem_id, pack("s!3", 0, 1, 0)) or croak "Cannot release semaphore lock: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 790 | ▶ | sub _lock_read { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lock_read: called CC=5 SCAR=17.6
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 790 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 790 | my ($shm_id, $sem_id) = $self->_get_ids; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 790 | _sem_lock($sem_id); | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 790 | my ($state, $err); | |||||||||||||||||||||||||||||||||||||||||
| 184 | 790 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 185 | 790 | my ($data) = _shm_read($shm_id); | |||||||||||||||||||||||||||||||||||||||||
| 186 | 790 | $state = IPC::Manager::Serializer::JSON->deserialize($data); | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | # Follow forwarding pointer if segment was grown by another process | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 790 | ▶ | if ($state->{_shm_id} && $state->{_shm_id} != $shm_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$state->{"_shm_id"} and $state->{"_shm_id"} != $shm_id
MC/DC0%0/2
$state->{"_shm_id"} and $state->{"_shm_id"} != $shm_id
$state->{"_shm_id"}
$state->{"_shm_id"} != $shm_id
Truth table33%1/3
A and B
A= $state->{"_shm_id"}B= $state->{"_shm_id"} != $shm_id
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | $self->{+_SHM_ID} = $state->{_shm_id}; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ($data) = _shm_read($self->{+_SHM_ID}); | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | $state = IPC::Manager::Serializer::JSON->deserialize($data); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 790 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 195 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 790 | ▶ | $err = $@ unless $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 790 | _sem_unlock($sem_id); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 790 | ▶ | die $err if $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 790 | return $state; | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 871 | ▶ | sub _lock_write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lock_write: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 871 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 871 | my ($shm_id, $sem_id) = $self->_get_ids; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 871 | _sem_lock($sem_id); | |||||||||||||||||||||||||||||||||||||||||
| 207 | 871 | my ($data) = _shm_read($shm_id); | |||||||||||||||||||||||||||||||||||||||||
| 208 | 871 | my $state = IPC::Manager::Serializer::JSON->deserialize($data); | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # If the segment was grown by another process, follow the pointer | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 871 | ▶ | if ($state->{_shm_id} && $state->{_shm_id} != $shm_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$state->{"_shm_id"} and $state->{"_shm_id"} != $shm_id
MC/DC0%0/2
$state->{"_shm_id"} and $state->{"_shm_id"} != $shm_id
$state->{"_shm_id"}
$state->{"_shm_id"} != $shm_id
Truth table33%1/3
A and B
A= $state->{"_shm_id"}B= $state->{"_shm_id"} != $shm_id
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | $self->{+_SHM_ID} = $state->{_shm_id}; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ($data) = _shm_read($self->{+_SHM_ID}); | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $state = IPC::Manager::Serializer::JSON->deserialize($data); | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 871 | return $state; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 842 | ▶ | sub _commit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_commit: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 842 | my ($self, $state) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 842 | my ($shm_id, $sem_id) = $self->_get_ids; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | # Remove internal tracking keys before serializing | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 842 | delete $state->{_shm_id}; | |||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 842 | my $data = IPC::Manager::Serializer::JSON->serialize($state); | |||||||||||||||||||||||||||||||||||||||||
| 228 | 842 | my $needed = $HEADER_SIZE + length($data); | |||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # Read current capacity from the segment header | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 842 | my $cap_buf = ''; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 842 | ▶ | shmread($shm_id, $cap_buf, 0, 4) or croak "shmread (capacity) failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 842 | my $capacity = unpack("N", $cap_buf); | |||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 842 | ▶ | if ($needed > $capacity) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | $self->_grow_segment($state, $data, $capacity); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 842 | _shm_write($shm_id, $data, $capacity); | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 842 | _sem_unlock($sem_id); | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 29 | ▶ | sub _unlock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unlock: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 29 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 29 | my (undef, $sem_id) = $self->_get_ids; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 29 | ▶ | eval { _sem_unlock($sem_id); 1 } or warn $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub _grow_segment { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_grow_segment: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my ($self, $state, $data, $old_capacity) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | _load_constants(); | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | my $needed = $HEADER_SIZE + length($data); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $new_capacity = $old_capacity; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | $new_capacity *= $SHM_GROW_FACTOR while $new_capacity < $needed; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | my $flags = $_S_IRUSR | $_S_IWUSR | $_IPC_CREAT; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | my $new_shm_id = shmget($_IPC_PRIVATE, $new_capacity, $flags); | |||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | defined $new_shm_id or croak "Cannot create new shared memory segment: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | _shm_write($new_shm_id, $data, $new_capacity); | |||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | # Write a forwarding pointer to the old segment so other processes | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # can find the new one | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my $old_shm_id = $self->{+_SHM_ID}; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | my $fwd = IPC::Manager::Serializer::JSON->serialize({_shm_id => $new_shm_id}); | |||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | eval { _shm_write($old_shm_id, $fwd, $old_capacity); 1 } or warn $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | $self->{+_SHM_ID} = $new_shm_id; | |||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # Update route | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $self->{route} = _build_route($new_shm_id, $self->{+_SEM_ID}); | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | # --- Client lifecycle --- | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 174 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=11 SCAR=25.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 174 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 174 | $self->SUPER::init(); | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 174 | _load_constants(); | |||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 174 | my $id = $self->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 174 | my $state = $self->_lock_write; | |||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 174 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 291 | 174 | ▶ | if ($self->{reconnect}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 3 | ▶ | unless ($state->{clients}{$id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | $self->{disconnected} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | croak "Client '$id' does not exist"; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 3 | my $data = $state->{clients}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 3 | ▶ | if ($data->{pid} && $data->{pid} != $$ && $self->pid_is_running($data->{pid})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$data->{"pid"} and $data->{"pid"} != $$ and $self->pid_is_running($data->{"pid"})
$data->{"pid"} and $data->{"pid"} != $$
MC/DC0%0/3
$data->{"pid"} and $data->{"pid"} != $$ and $self->pid_is_running($data->{"pid"})
$data->{"pid"}
$data->{"pid"} != $$
$self->pid_is_running($data->{"pid"})
Truth table25%1/4
A and B and C
A= $data->{"pid"}B= $data->{"pid"} != $$C= $self->pid_is_running($data->{"pid"})
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $self->{disconnected} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | croak "Connection already running in pid $data->{pid}"; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 171 | ▶ | if (my $existing = $state->{clients}{$id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 4 | my $epid = $existing->{pid}; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 4 | ▶ | if ($epid && pid_is_running($epid) == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$epid and pid_is_running($epid) == 0
MC/DC50%1/2
$epid and pid_is_running($epid) == 0
$epid
pid_is_running($epid) == 0
Truth table66%2/3
A and B
A= $epidB= pid_is_running($epid) == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | delete $state->{clients}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 2 | delete $state->{messages}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | delete $state->{stats}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 2 | $self->{disconnected} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 2 | croak "Client '$id' already exists"; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 169 | $state->{clients}{$id} = {pid => $$}; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 169 | ▶ | $state->{messages}{$id} //= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$state->{"messages"}{$id} //= []
MC/DC0%0/1
$state->{"messages"}{$id} //= []
$state->{"messages"}{$id}
Truth table50%1/2
A
A= $state->{"messages"}{$id}
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 172 | $state->{clients}{$id}{pid} = $$; | |||||||||||||||||||||||||||||||||||||||||
| 320 | 172 | delete $state->{clients}{$id}{suspend_expires_at}; | |||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 172 | $self->_commit($state); | |||||||||||||||||||||||||||||||||||||||||
| 323 | 172 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 324 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 174 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 2 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | # --- Messages --- | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 144 | ▶ | sub pending_messages { 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pending_messages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | 146 | ▶ | sub ready_messages { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ready_messages: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 146 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 339 | 146 | my $state; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 146 | ▶ | unless (eval { $state = $self->_lock_read; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 146 | ▶ | my $msgs = $state->{messages}{$self->{id}} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$state->{"messages"}{$self->{"id"}} // []
MC/DC0%0/1
$state->{"messages"}{$self->{"id"}} // []
$state->{"messages"}{$self->{"id"}}
Truth table50%1/2
A
A= $state->{"messages"}{$self->{"id"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | 146 | ▶ | return @$msgs ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 233 | ▶ | sub get_messages { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_messages: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 233 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 233 | $self->pid_check; | |||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 233 | my $state = $self->_lock_write; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 233 | ▶ | my $raw = delete $state->{messages}{$self->{id}} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $state->{"messages"}{$self->{"id"}} // []
MC/DC0%0/1
delete $state->{"messages"}{$self->{"id"}} // []
delete $state->{"messages"}{$self->{"id"}}
Truth table50%1/2
A
A= delete $state->{"messages"}{$self->{"id"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 233 | $state->{messages}{$self->{id}} = []; | |||||||||||||||||||||||||||||||||||||||||
| 355 | 233 | $self->_commit($state); | |||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | 233 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 233 | for my $h (@$raw) { | |||||||||||||||||||||||||||||||||||||||||
| 359 | 134 | my $msg = IPC::Manager::Message->new(%$h); | |||||||||||||||||||||||||||||||||||||||||
| 360 | 134 | $self->{stats}{read}{$msg->{from}}++; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 134 | push @out, $msg; | |||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 233 | return $self->sort_messages(@out); | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 138 | ▶ | sub send_message { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_message: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 138 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 138 | my $msg = $self->build_message(@_); | |||||||||||||||||||||||||||||||||||||||||
| 370 | 138 | $self->pid_check; | |||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 138 | ▶ | my $peer_id = $msg->to or croak "Message has no peer"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | 138 | my $state = $self->_lock_write; | |||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | 138 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 377 | croak "Client '$peer_id' does not exist" | ||||||||||||||||||||||||||||||||||||||||||
| 378 | 138 | ▶ | unless $state->{clients}{$peer_id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 135 | push @{$state->{messages}{$peer_id}}, $msg->TO_JSON; | |||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 135 | $self->_commit($state); | |||||||||||||||||||||||||||||||||||||||||
| 383 | 135 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 384 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 138 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | 3 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 3 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 3 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 135 | $self->{stats}{sent}{$peer_id}++; | |||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | # --- Peer queries --- | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 301 | ▶ | sub peers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peers: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 301 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 301 | my $state = $self->_lock_read; | |||||||||||||||||||||||||||||||||||||||||
| 400 | 301 | my $my_id = $self->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 401 | 301 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 402 | 301 | for my $peer_id (keys %{$state->{clients}}) { | |||||||||||||||||||||||||||||||||||||||||
| 403 | 490 | ▶ | next if $peer_id eq $my_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 249 | my $data = $state->{clients}{$peer_id}; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 249 | ▶ | my $pid = $data ? $data->{pid} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | 249 | ▶ | next if $pid && pid_is_running($pid) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pid and pid_is_running($pid) == 0
MC/DC50%1/2
$pid and pid_is_running($pid) == 0
$pid
pid_is_running($pid) == 0
Truth table66%2/3
A and B
A= $pidB= pid_is_running($pid) == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 248 | push @out, $peer_id; | |||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 301 | return sort @out; | |||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 25 | ▶ | sub peer_left { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peer_left: called CC=9 SCAR=22.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 25 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | 25 | my $state; | |||||||||||||||||||||||||||||||||||||||||
| 416 | 25 | ▶ | unless (eval { $state = $self->_lock_write; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | 25 | my $removed = 0; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 25 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 423 | 25 | my $my_id = $self->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 424 | 25 | for my $peer_id (keys %{$state->{clients}}) { | |||||||||||||||||||||||||||||||||||||||||
| 425 | 32 | ▶ | next if $peer_id eq $my_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 7 | my $data = $state->{clients}{$peer_id}; | |||||||||||||||||||||||||||||||||||||||||
| 427 | 7 | ▶ | my $pid = $data ? $data->{pid} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 7 | ▶ | next unless $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 7 | ▶ | next unless pid_is_running($pid) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | 1 | delete $state->{clients}{$peer_id}; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 1 | delete $state->{messages}{$peer_id}; | |||||||||||||||||||||||||||||||||||||||||
| 433 | 1 | delete $state->{stats}{$peer_id}; | |||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | $removed++; | |||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 25 | ▶ | if ($removed) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | 1 | $self->_commit($state); | |||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||
| 440 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 24 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 25 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 444 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 25 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | warn $err; | |||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 25 | return $removed; | |||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 61 | ▶ | sub peer_exists { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peer_exists: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | 61 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 458 | 61 | my ($peer_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 61 | ▶ | croak "'peer_id' is required" unless $peer_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 61 | my $state = $self->_lock_read; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 61 | ▶ | return $state->{clients}{$peer_id} ? 1 : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 210 | ▶ | sub peer_pid { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peer_pid: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 210 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 466 | 210 | my ($peer_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 467 | 210 | my $state = $self->_lock_read; | |||||||||||||||||||||||||||||||||||||||||
| 468 | 210 | my $data = $state->{clients}{$peer_id}; | |||||||||||||||||||||||||||||||||||||||||
| 469 | 210 | ▶ | return undef unless $data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | 179 | return $data->{pid}; | |||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | # --- Stats --- | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 152 | ▶ | sub write_stats { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_stats: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 152 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 477 | 152 | my $state; | |||||||||||||||||||||||||||||||||||||||||
| 478 | 152 | ▶ | unless (eval { $state = $self->_lock_write; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | 152 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 484 | 152 | $state->{stats}{$self->{id}} = $self->{stats}; | |||||||||||||||||||||||||||||||||||||||||
| 485 | 152 | $self->_commit($state); | |||||||||||||||||||||||||||||||||||||||||
| 486 | 152 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 487 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 152 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | 8 | ▶ | sub read_stats { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_stats: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 497 | 8 | my $state = $self->_lock_read; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 8 | ▶ | return $state->{stats}{$self->{id}} // {read => {}, sent => {}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$state->{"stats"}{$self->{"id"}} // {"read", {}, "sent", {}}
MC/DC0%0/1
$state->{"stats"}{$self->{"id"}} // {"read", {}, "sent", {}}
$state->{"stats"}{$self->{"id"}}
Truth table50%1/2
A
A= $state->{"stats"}{$self->{"id"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | 2 | ▶ | sub all_stats { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_stats: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 503 | 2 | my $state = $self->_lock_read; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 2 | my %out; | |||||||||||||||||||||||||||||||||||||||||
| 505 | 2 | for my $id (keys %{$state->{stats}}) { | |||||||||||||||||||||||||||||||||||||||||
| 506 | 6 | $out{$id} = $state->{stats}{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 2 | return \%out; | |||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | # --- Suspend --- | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | 7 | ▶ | sub pre_suspend_hook { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pre_suspend_hook: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 515 | 7 | my (%params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | 7 | my $expires_at = $params{expires_at}; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 7 | ▶ | return unless defined $expires_at; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | 4 | my $state; | |||||||||||||||||||||||||||||||||||||||||
| 521 | 4 | ▶ | unless (eval { $state = $self->_lock_write; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | 4 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 527 | 4 | my $entry = $state->{clients}{$self->{id}}; | |||||||||||||||||||||||||||||||||||||||||
| 528 | 4 | ▶ | if ($entry) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | 4 | $entry->{suspend_expires_at} = $expires_at + 0; | |||||||||||||||||||||||||||||||||||||||||
| 530 | 4 | $self->_commit($state); | |||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 532 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 536 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | 4 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 541 | 0 | warn $err; | |||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | 61 | ▶ | sub peer_suspend_expires { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peer_suspend_expires: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | 61 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 547 | 61 | my ($peer_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 548 | 61 | ▶ | return undef unless defined $peer_id && length $peer_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $peer_id and length $peer_id
MC/DC0%0/2
defined $peer_id and length $peer_id
defined $peer_id
length $peer_id
Truth table33%1/3
A and B
A= defined $peer_idB= length $peer_id
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | 61 | my $state = $self->_lock_read; | |||||||||||||||||||||||||||||||||||||||||
| 551 | 61 | ▶ | my $entry = $state->{clients}{$peer_id} or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 45 | return $entry->{suspend_expires_at}; | |||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | # --- Disconnect --- | ||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | 140 | ▶ | sub post_disconnect_hook { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post_disconnect_hook: called CC=3 SCAR=11.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 558 | 140 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 559 | 140 | my $state; | |||||||||||||||||||||||||||||||||||||||||
| 560 | 140 | ▶ | unless (eval { $state = $self->_lock_write; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | 140 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 566 | 140 | delete $state->{clients}{$self->{id}}; | |||||||||||||||||||||||||||||||||||||||||
| 567 | 140 | delete $state->{messages}{$self->{id}}; | |||||||||||||||||||||||||||||||||||||||||
| 568 | 140 | $self->_commit($state); | |||||||||||||||||||||||||||||||||||||||||
| 569 | 140 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 570 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | 140 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | warn $@; | |||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | $self->_unlock; | |||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | 89 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | __END__ | ||||||||||||||||||||||||||||||||||||||||||