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