| 1 | package PEF::Front::WebSocket::QueueClient; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use AnyEvent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use AnyEvent::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use AnyEvent::Handle; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use CBOR::XS; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Scalar::Util qw'weaken refaddr'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 8 | ▶ | sub publish { | ||||||||||
|
Subroutine
publish: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 12 | 8 | my ($self, $queue, $id_message, $message) = @_; | |||||||||||
| 13 | $self->{handle}->push_write( | ||||||||||||
| 14 | 8 | cbor => { | |||||||||||
| 15 | command => 'publish', | ||||||||||||
| 16 | queue => $queue, | ||||||||||||
| 17 | id_message => $id_message, | ||||||||||||
| 18 | message => $message, | ||||||||||||
| 19 | } | ||||||||||||
| 20 | ); | ||||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | 24 | ▶ | sub subscribe { | ||||||||||
|
Subroutine
subscribe: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 24 | 24 | my ($self, $queue, $client, $last_id) = @_; | |||||||||||
| 25 | 24 | my $id_client = refaddr $client; | |||||||||||
| 26 | 24 | ▶ | return if exists $self->{queues}{$queue}{$id_client}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 24 | $self->{queues}{$queue}{$id_client} = undef; | |||||||||||
| 28 | 24 | $self->{clients}{$id_client} = $client; | |||||||||||
| 29 | $self->{handle}->push_write( | ||||||||||||
| 30 | 24 | cbor => { | |||||||||||
| 31 | command => 'subscribe', | ||||||||||||
| 32 | queue => $queue, | ||||||||||||
| 33 | id_client => $id_client, | ||||||||||||
| 34 | last_id => $last_id, | ||||||||||||
| 35 | } | ||||||||||||
| 36 | ); | ||||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 4 | ▶ | sub unsubscribe { | ||||||||||
|
Subroutine
unsubscribe: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 40 | 4 | my ($self, $queue, $client) = @_; | |||||||||||
| 41 | 4 | my $id_client = refaddr $client; | |||||||||||
| 42 | 4 | ▶ | return if not exists $self->{queues}{$queue}{$id_client}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 4 | $self->{queues}{$queue}{$id_client} = undef; | |||||||||||
| 44 | $self->{handle}->push_write( | ||||||||||||
| 45 | 4 | cbor => { | |||||||||||
| 46 | command => 'unsubscribe', | ||||||||||||
| 47 | queue => $queue, | ||||||||||||
| 48 | id_client => $id_client, | ||||||||||||
| 49 | } | ||||||||||||
| 50 | ); | ||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 2 | ▶ | sub unregister_client { | ||||||||||
|
Subroutine
unregister_client: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 54 | 2 | my ($self, $client) = @_; | |||||||||||
| 55 | 2 | my $id_client = refaddr $client; | |||||||||||
| 56 | 2 | ▶ | return if not $self->{clients}{$id_client}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 2 | delete $self->{clients}{$id_client}; | |||||||||||
| 58 | 2 | for my $queue (keys %{$self->{queues}}) { | |||||||||||
| 59 | 6 | delete $self->{queues}{$queue}{$id_client}; | |||||||||||
| 60 | } | ||||||||||||
| 61 | $self->{handle}->push_write( | ||||||||||||
| 62 | 2 | cbor => { | |||||||||||
| 63 | command => 'unregister', | ||||||||||||
| 64 | id_client => $id_client, | ||||||||||||
| 65 | } | ||||||||||||
| 66 | ); | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | sub on_disconnect { | ||||||||||
|
Subroutine
on_disconnect: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 70 | 0 | my ($self, $handle, $fatal, $msg) = @_; | |||||||||||
| 71 | 0 | for my $cid (keys %{$self->{clients}}) { | |||||||||||
| 72 | 0 | my $client = $self->{clients}{$cid}; | |||||||||||
| 73 | 0 | $client->on_queue_error('disconnect'); | |||||||||||
| 74 | } | ||||||||||||
| 75 | 0 | delete $self->{handle}; | |||||||||||
| 76 | 0 | delete $self->{tcp_guard}; | |||||||||||
| 77 | 0 | delete $self->{clients}; | |||||||||||
| 78 | 0 | delete $self->{queues}; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 24 | ▶ | sub on_queue { | ||||||||||
|
Subroutine
on_queue: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 82 | 24 | my ($self, $handle, $cmd) = @_; | |||||||||||
| 83 | $handle->push_read( | ||||||||||||
| 84 | 18 | ▶ | cbor => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 85 | 18 | $self->on_queue(@_); | |||||||||||
| 86 | } | ||||||||||||
| 87 | 24 | ); | |||||||||||
| 88 | 24 | my ($queue, $id_message, $message, $cidref) = @$cmd; | |||||||||||
| 89 | 24 | for my $cid (@$cidref) { | |||||||||||
| 90 | 30 | my $client = $self->{clients}{$cid}; | |||||||||||
| 91 | 30 | ▶ | if (exists $self->{queues}{$queue}{$cid}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 30 | $client->on_queue($queue, $id_message, $message); | |||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | 6 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||
| 98 | 6 | my ($class, %args) = @_; | |||||||||||
| 99 | 6 | ▶ | my $tcp_address = delete $args{address} || '127.0.0.1'; | ||||||||||
|
Condition50%1/2
delete $args{'address'} || '127.0.0.1'
MC/DC0%0/1
delete $args{'address'} || '127.0.0.1'
delete $args{'address'}
Truth table50%1/2
A
A= delete $args{'address'}
| |||||||||||||
| 100 | 6 | ▶ | my $tcp_port = delete $args{port} || 54321; | ||||||||||
|
Condition50%1/2
delete $args{'port'} || 54321
MC/DC0%0/1
delete $args{'port'} || 54321
delete $args{'port'}
Truth table50%1/2
A
A= delete $args{'port'}
| |||||||||||||
| 101 | 6 | my $self; | |||||||||||
| 102 | 6 | my $cv = AnyEvent->condvar(); | |||||||||||
| 103 | $self = { | ||||||||||||
| 104 | address => $tcp_address, | ||||||||||||
| 105 | port => $tcp_port, | ||||||||||||
| 106 | tcp_guard => tcp_connect( | ||||||||||||
| 107 | $tcp_address, | ||||||||||||
| 108 | $tcp_port, | ||||||||||||
| 109 | 6 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
| |||||||||||||
| 110 | 6 | my ($fh) = @_; | |||||||||||
| 111 | $self->{handle} = AnyEvent::Handle->new( | ||||||||||||
| 112 | fh => $fh, | ||||||||||||
| 113 | 0 | ▶ | on_eof => sub {$self->on_disconnect(@_)}, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 114 | 0 | ▶ | on_error => sub {$self->on_disconnect(@_)}, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 115 | 6 | ); | |||||||||||
| 116 | 6 | ▶ | $self->{handle}->push_read(cbor => sub {$self->on_queue(@_)}); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 117 | 6 | $cv->send; | |||||||||||
| 118 | } | ||||||||||||
| 119 | 6 | ), | |||||||||||
| 120 | clients => {}, | ||||||||||||
| 121 | queues => {} | ||||||||||||
| 122 | }; | ||||||||||||
| 123 | 6 | bless $self, $class; | |||||||||||
| 124 | 6 | $cv->recv; | |||||||||||
| 125 | 6 | $self; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | 2 | 1; | |||||||||||