| 1 | package Beekeeper::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 11 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Beekeeper::AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Beekeeper::MQTT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Beekeeper::JSONRPC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Beekeeper::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use Digest::MD5 'md5_base64'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 11 | ▶ | use constant QUEUE_LANES => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 11 | ▶ | use constant REQ_TIMEOUT => 60; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 11 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 11 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 25 | send_notification | ||||||||||||||||||||||||||||
| 26 | call_remote | ||||||||||||||||||||||||||||
| 27 | call_remote_async | ||||||||||||||||||||||||||||
| 28 | fire_remote | ||||||||||||||||||||||||||||
| 29 | wait_async_calls | ||||||||||||||||||||||||||||
| 30 | get_authentication_data | ||||||||||||||||||||||||||||
| 31 | set_authentication_data | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | __do_rpc_request | ||||||||||||||||||||||||||||
| 34 | __create_response_topic | ||||||||||||||||||||||||||||
| 35 | __use_authorization_token | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 11 | our %EXPORT_TAGS = ('worker' => \@EXPORT_OK ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | our $singleton; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub new { | ||||||||||||||||||||||||||||
| 44 | 6 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 6 | my $self = { | |||||||||||||||||||||||||||
| 47 | _CLIENT => undef, | ||||||||||||||||||||||||||||
| 48 | _BUS => undef, | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | $self->{_CLIENT} = { | ||||||||||||||||||||||||||||
| 52 | 6 | forward_to => undef, | |||||||||||||||||||||||||||
| 53 | response_topic => undef, | ||||||||||||||||||||||||||||
| 54 | in_progress => undef, | ||||||||||||||||||||||||||||
| 55 | curr_request => undef, | ||||||||||||||||||||||||||||
| 56 | caller_id => undef, | ||||||||||||||||||||||||||||
| 57 | caller_addr => undef, | ||||||||||||||||||||||||||||
| 58 | auth_data => undef, | ||||||||||||||||||||||||||||
| 59 | auth_salt => undef, | ||||||||||||||||||||||||||||
| 60 | async_cv => undef, | ||||||||||||||||||||||||||||
| 61 | correlation_id => 1, | ||||||||||||||||||||||||||||
| 62 | callbacks => {}, | ||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | unless (exists $args{'username'} && exists $args{'password'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"username"} and exists $args{"password"}
MC/DC0%0/2
exists $args{"username"} and exists $args{"password"}
exists $args{"username"}
exists $args{"password"}
Truth table33%1/3
A and B
A= exists $args{"username"}B= exists $args{"password"}
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Get broker connection parameters from config file | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 6 | my $bus_id = $args{'bus_id'}; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | if (defined $bus_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | # Use parameters for specific bus | ||||||||||||||||||||||||||||
| 73 | 0 | my $config = Beekeeper::Config->get_bus_config( bus_id => $bus_id ); | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | croak "Bus '$bus_id' is not defined into config file bus.config.json" unless $config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | %args = ( %$config, %args ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 6 | my $config = Beekeeper::Config->get_bus_config( bus_id => '*'); | |||||||||||||||||||||||||||
| 79 | 6 | ▶ | if (scalar(keys %$config) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | # Use the only config present | ||||||||||||||||||||||||||||
| 81 | 6 | ($bus_id) = (keys %$config); | |||||||||||||||||||||||||||
| 82 | 6 | %args = ( %{$config->{$bus_id}}, bus_id => $bus_id, %args ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||
| 85 | # Use default parameters (if any) | ||||||||||||||||||||||||||||
| 86 | 0 | my ($default) = grep { $config->{$_}->{default} } keys %$config; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | croak "No default bus defined into config file bus.config.json" unless $default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $bus_id = $config->{$default}->{'bus_id'}; | |||||||||||||||||||||||||||
| 89 | 0 | %args = ( %{$config->{$default}}, bus_id => $bus_id, %args ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 6 | $self->{_CLIENT}->{forward_to} = delete $args{'forward_to'}; | |||||||||||||||||||||||||||
| 95 | 6 | ▶ | $self->{_CLIENT}->{auth_salt} = delete $args{'auth_salt'} || $args{'bus_id'}; | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $args{'auth_salt'} || $args{'bus_id'}
MC/DC0%0/2
delete $args{'auth_salt'} || $args{'bus_id'}
delete $args{'auth_salt'}
$args{'bus_id'}
Truth table33%1/3
A || B
A= delete $args{'auth_salt'}B= $args{'bus_id'}
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Start a fresh new MQTT session on connect | ||||||||||||||||||||||||||||
| 98 | 6 | $args{'clean_start'} = 1; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Make the MQTT session ends when the connection is closed | ||||||||||||||||||||||||||||
| 101 | 6 | $args{'session_expiry_interval'} = 0; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Keep only 1 unacked message (of QoS 1) in flight | ||||||||||||||||||||||||||||
| 104 | 6 | $args{'receive_maximum'} = 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Do not use topic aliases | ||||||||||||||||||||||||||||
| 107 | 6 | $args{'topic_alias_maximum'} = 0; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 6 | $self->{_BUS} = Beekeeper::MQTT->new( %args ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Connect to MQTT broker | ||||||||||||||||||||||||||||
| 113 | 6 | $self->{_BUS}->connect( blocking => 1 ); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 6 | bless $self, $class; | |||||||||||||||||||||||||||
| 116 | 6 | return $self; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub instance { | ||||||||||||||||||||||||||||
| 120 | 31 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
instance: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 31 | ▶ | if ($singleton) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | # Return existing singleton | ||||||||||||||||||||||||||||
| 124 | 25 | return $singleton; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Create a new instance | ||||||||||||||||||||||||||||
| 128 | 6 | my $self = $class->new( @_ ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Keep a global reference to $self | ||||||||||||||||||||||||||||
| 131 | 6 | $singleton = $self; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 6 | return $self; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub send_notification { | ||||||||||||||||||||||||||||
| 138 | 5 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
send_notification: called CC=11 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 5 | ▶ | my $fq_meth = $args{'method'} or croak "Method was not specified"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 5 | ▶ | $fq_meth .= '@' . $args{'address'} if (defined $args{'address'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | ▶ | $fq_meth =~ m/^ ( [\w-]+ (?:\.[\w-]+)* ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | \. ( [\w-]+ ) | ||||||||||||||||||||||||||||
| 146 | (?: \@ ( [\w-]+ ) (\.[\w-]+)* )? $/x or croak "Invalid method '$fq_meth'"; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 5 | my ($service, $method, $remote_bus, $addr) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | my $json = encode_json({ | ||||||||||||||||||||||||||||
| 151 | jsonrpc => '2.0', | ||||||||||||||||||||||||||||
| 152 | method => "$service.$method", | ||||||||||||||||||||||||||||
| 153 | 5 | params => $args{'params'}, | |||||||||||||||||||||||||||
| 154 | }); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 5 | my %send_args; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 5 | my $local_bus = $self->{_BUS}->{bus_role}; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 5 | ▶ | $remote_bus = $self->{_CLIENT}->{forward_to} unless (defined $remote_bus); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | ▶ | if (defined $remote_bus) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | $send_args{'topic'} = "msg/$remote_bus-" . int( rand(QUEUE_LANES) + 1 ); | |||||||||||||||||||||||||||
| 165 | 0 | $send_args{'topic'} =~ tr|.|/|; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | $send_args{'fwd_to'} = "msg/$remote_bus/$service/$method"; | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | $send_args{'fwd_to'} .= "\@$addr" if (defined $addr && $addr =~ s/^\.//); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $addr and $addr =~ s/^\.//
MC/DC0%0/2
defined $addr and $addr =~ s/^\.//
defined $addr
$addr =~ s/^\.//
Truth table0%0/3
A and B
A= defined $addrB= $addr =~ s/^\.//
| |||||||||||||||||||||||||||||
| 169 | 0 | $send_args{'fwd_to'} =~ tr|.|/|; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||
| 172 | 5 | $send_args{'topic'} = "msg/$local_bus/$service/$method"; | |||||||||||||||||||||||||||
| 173 | 5 | $send_args{'topic'} =~ tr|.|/|; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 5 | ▶ | $send_args{'auth'} = $self->{_CLIENT}->{auth_data} if defined $self->{_CLIENT}->{auth_data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 5 | ▶ | $send_args{'clid'} = $self->{_CLIENT}->{caller_id} if defined $self->{_CLIENT}->{caller_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 5 | ▶ | if (exists $args{'buffer_id'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | $send_args{'buffer_id'} = $args{'buffer_id'}; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 5 | $self->{_BUS}->publish( payload => \$json, %send_args ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub accept_notifications { | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
accept_notifications: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | my ($file, $line) = (caller)[1,2]; | |||||||||||||||||||||||||||
| 191 | 0 | my $at = "at $file line $line\n"; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | my $callbacks = $self->{_CLIENT}->{callbacks}; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | foreach my $fq_meth (keys %args) { | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | $fq_meth =~ m/^ ( [\w-]+ (?: \.[\w-]+ )* ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | \. ( [\w-]+ | \* ) $/x or croak "Invalid notification method '$fq_meth'"; | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | my ($service, $method) = ($1, $2); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | my $callback = $args{$fq_meth}; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | unless (ref $callback eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | croak "Invalid callback for '$fq_meth'"; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | croak "Already accepting notifications '$fq_meth'" if exists $callbacks->{"msg.$fq_meth"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | $callbacks->{"msg.$fq_meth"} = $callback; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | #TODO: Allow to accept private notifications without subscribing | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | my $local_bus = $self->{_BUS}->{bus_role}; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | my $topic = "msg/$local_bus/$service/$method"; | |||||||||||||||||||||||||||
| 216 | 0 | $topic =~ tr|.*|/#|; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | $self->{_BUS}->subscribe( | ||||||||||||||||||||||||||||
| 219 | topic => $topic, | ||||||||||||||||||||||||||||
| 220 | on_publish => sub { | ||||||||||||||||||||||||||||
| 221 | 0 | ▶ | my ($payload_ref, $mqtt_properties) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 0 | local $@; | |||||||||||||||||||||||||||
| 224 | 0 | my $request = eval { decode_json($$payload_ref) }; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | unless (ref $request eq 'HASH' && $request->{jsonrpc} eq '2.0') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $request eq "HASH" and $request->{"jsonrpc"} eq "2.0"
MC/DC0%0/2
ref $request eq "HASH" and $request->{"jsonrpc"} eq "2.0"
ref $request eq "HASH"
$request->{"jsonrpc"} eq "2.0"
Truth table0%0/3
A and B
A= ref $request eq "HASH"B= $request->{"jsonrpc"} eq "2.0"
| |||||||||||||||||||||||||||||
| 227 | 0 | warn "Received invalid JSON-RPC 2.0 notification $at"; | |||||||||||||||||||||||||||
| 228 | 0 | return; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | bless $request, 'Beekeeper::JSONRPC::Notification'; | |||||||||||||||||||||||||||
| 232 | 0 | $request->{_mqtt_properties} = $mqtt_properties; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | my $method = $request->{method}; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | unless (defined $method && $method =~ m/^([\.\w-]+)\.([\w-]+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $method and $method =~ /^([\.\w-]+)\.([\w-]+)$/
MC/DC0%0/2
defined $method and $method =~ /^([\.\w-]+)\.([\w-]+)$/
defined $method
$method =~ /^([\.\w-]+)\.([\w-]+)$/
Truth table0%0/3
A and B
A= defined $methodB= $method =~ /^([\.\w-]+)\.([\w-]+)$/
| |||||||||||||||||||||||||||||
| 237 | 0 | warn "Received notification with invalid method '$method' $at"; | |||||||||||||||||||||||||||
| 238 | 0 | return; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | my $cb = $callbacks->{"msg.$1.$2"} || | ||||||||||||||||||||||||||||
| 242 | 0 | ▶ | $callbacks->{"msg.$1.*"}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$$callbacks{"msg.$1.$2"} || $$callbacks{"msg.$1.*"}
MC/DC0%0/2
$$callbacks{"msg.$1.$2"} || $$callbacks{"msg.$1.*"}
$$callbacks{"msg.$1.$2"}
$$callbacks{"msg.$1.*"}
Truth table0%0/3
A || B
A= $$callbacks{"msg.$1.$2"}B= $$callbacks{"msg.$1.*"}
| |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | unless ($cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | warn "No callback found for received notification '$method' $at"; | |||||||||||||||||||||||||||
| 246 | 0 | return; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | $cb->($request->{params}, $request); | |||||||||||||||||||||||||||
| 250 | }, | ||||||||||||||||||||||||||||
| 251 | on_suback => sub { | ||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my ($success, $prop) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 253 | 0 | ▶ | die "Could not subscribe to topic '$topic' $at" unless $success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | 0 | ); | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | sub stop_accepting_notifications { | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my ($self, @methods) = @_; | ||||||||||||||||||||||||||
|
Subroutine
stop_accepting_notifications: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 0 | my ($file, $line) = (caller)[1,2]; | |||||||||||||||||||||||||||
| 264 | 0 | my $at = "at $file line $line\n"; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | croak "No method specified" unless @methods; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | foreach my $fq_meth (@methods) { | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | ▶ | $fq_meth =~ m/^ ( [\w-]+ (?: \.[\w-]+ )* ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | \. ( [\w-]+ | \* ) $/x or croak "Invalid method '$fq_meth'"; | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | my ($service, $method) = ($1, $2); | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | unless (defined $self->{_CLIENT}->{callbacks}->{"msg.$fq_meth"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 0 | carp "Not previously accepting notifications '$fq_meth'"; | |||||||||||||||||||||||||||
| 277 | 0 | next; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 0 | my $local_bus = $self->{_BUS}->{bus_role}; | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | my $topic = "msg/$local_bus/$service/$method"; | |||||||||||||||||||||||||||
| 283 | 0 | $topic =~ tr|.*|/#|; | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | $self->{_BUS}->unsubscribe( | ||||||||||||||||||||||||||||
| 286 | topic => $topic, | ||||||||||||||||||||||||||||
| 287 | on_unsuback => sub { | ||||||||||||||||||||||||||||
| 288 | 0 | ▶ | my ($success, $prop) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | ▶ | die "Could not unsubscribe from topic '$topic' $at" unless $success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | delete $self->{_CLIENT}->{callbacks}->{"msg.$fq_meth"}; | |||||||||||||||||||||||||||
| 293 | }, | ||||||||||||||||||||||||||||
| 294 | 0 | ); | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 11 | our $AE_WAITING; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | sub call_remote { | ||||||||||||||||||||||||||||
| 302 | 52 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
call_remote: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 52 | my $req = $self->__do_rpc_request( @_, req_type => 'SYNCHRONOUS' ); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | # Make AnyEvent allow one level of recursive condvar blocking, as we may | ||||||||||||||||||||||||||||
| 307 | # block both in $worker->__work_forever and in $client->__do_rpc_request | ||||||||||||||||||||||||||||
| 308 | 51 | ▶ | $AE_WAITING && Carp::confess "Recursive blocking call attempted: " . | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | "trying to make a call_remote while another call_remote is still in progress, " . | ||||||||||||||||||||||||||||
| 310 | "but it is not possible to make two blocking calls simultaneously " . | ||||||||||||||||||||||||||||
| 311 | "(tip: one of the two calls must be made with call_remote_async)"; | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 51 | local $AE_WAITING = 1; | |||||||||||||||||||||||||||
| 314 | 51 | local $AnyEvent::CondVar::Base::WAITING = 0; | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # Block until a response is received or request timed out | ||||||||||||||||||||||||||||
| 317 | 51 | $req->{_waiting_response}->recv; | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 51 | my $resp = $req->{_response}; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 51 | ▶ | if (!exists $resp->{result} && $req->{_raise_error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $resp->{"result"} and $req->{"_raise_error"}
MC/DC100%2/2
not exists $resp->{"result"} and $req->{"_raise_error"}
not exists $resp->{"result"}
$req->{"_raise_error"}
Truth table100%3/3
not(A) and B
A= not exists $resp->{"result"}B= $req->{"_raise_error"}
| |||||||||||||||||||||||||||||
| 322 | 5 | my $errmsg = $resp->code . " " . $resp->message; | |||||||||||||||||||||||||||
| 323 | 5 | croak "Call to '$req->{method}' failed: $errmsg"; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 46 | return $resp; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | sub call_remote_async { | ||||||||||||||||||||||||||||
| 330 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
call_remote_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 13 | my $req = $self->__do_rpc_request( @_, req_type => 'ASYNCHRONOUS' ); | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 13 | return $req; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | sub fire_remote { | ||||||||||||||||||||||||||||
| 338 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
fire_remote: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # Send request to a worker, but do not wait for response | ||||||||||||||||||||||||||||
| 341 | 3 | $self->__do_rpc_request( @_, req_type => 'FIRE_FORGET' ); | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 3 | return; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 11 | my $__now = 0; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | sub __do_rpc_request { | ||||||||||||||||||||||||||||
| 349 | 68 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__do_rpc_request: called CC=22 SCAR=33.4
| |||||||||||||||||||||||||||||
| 350 | 68 | my $client = $self->{_CLIENT}; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 68 | ▶ | my $fq_meth = $args{'method'} or croak "Method was not specified"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 68 | ▶ | $fq_meth .= '@' . $args{'address'} if (defined $args{'address'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 68 | ▶ | $fq_meth =~ m/^ ( [\w-]+ (?:\.[\w-]+)* ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | \. ( [\w-]+ ) | ||||||||||||||||||||||||||||
| 358 | (?: \@ ( [\w-]+ ) (\.[\w-]+)* )? $/x or croak "Invalid method '$fq_meth'"; | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 67 | my ($service, $method, $remote_bus, $addr) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 67 | my %send_args; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 67 | my $local_bus = $self->{_BUS}->{bus_role}; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 67 | ▶ | $remote_bus = $client->{forward_to} unless (defined $remote_bus); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # Local bus request sent to: req/{local_bus}/{service_class} | ||||||||||||||||||||||||||||
| 369 | # Remote bus request sent to: req/{remote_bus} | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 67 | ▶ | if (defined $remote_bus) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 0 | $send_args{'topic'} = "req/$remote_bus-" . int( rand(QUEUE_LANES) + 1 ); | |||||||||||||||||||||||||||
| 374 | 0 | $send_args{'topic'} =~ tr|.|/|; | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | $send_args{'fwd_to'} = "req/$remote_bus/$service"; | |||||||||||||||||||||||||||
| 377 | 0 | ▶ | $send_args{'fwd_to'} .= "\@$addr" if (defined $addr && $addr =~ s/^\.//); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $addr and $addr =~ s/^\.//
MC/DC0%0/2
defined $addr and $addr =~ s/^\.//
defined $addr
$addr =~ s/^\.//
Truth table0%0/3
A and B
A= defined $addrB= $addr =~ s/^\.//
| |||||||||||||||||||||||||||||
| 378 | 0 | $send_args{'fwd_to'} =~ tr|.|/|; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | else { | ||||||||||||||||||||||||||||
| 381 | 67 | $send_args{'topic'} = "req/$local_bus/$service"; | |||||||||||||||||||||||||||
| 382 | 67 | $send_args{'topic'} =~ tr|.|/|; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 67 | ▶ | $send_args{'auth'} = $client->{auth_data} if defined $client->{auth_data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 67 | ▶ | $send_args{'clid'} = $client->{caller_id} if defined $client->{caller_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 67 | my $FIRE_FORGET = $args{req_type} eq 'FIRE_FORGET'; | |||||||||||||||||||||||||||
| 389 | 67 | my $SYNCHRONOUS = $args{req_type} eq 'SYNCHRONOUS'; | |||||||||||||||||||||||||||
| 390 | 67 | my $raise_error = $args{'raise_error'}; | |||||||||||||||||||||||||||
| 391 | 67 | my $req_id; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | # JSON-RPC call | ||||||||||||||||||||||||||||
| 394 | my $req = { | ||||||||||||||||||||||||||||
| 395 | jsonrpc => '2.0', | ||||||||||||||||||||||||||||
| 396 | method => "$service.$method", | ||||||||||||||||||||||||||||
| 397 | 67 | params => $args{'params'}, | |||||||||||||||||||||||||||
| 398 | }; | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | # Reuse or create a private topic which will receive responses | ||||||||||||||||||||||||||||
| 401 | $send_args{'response_topic'} = $client->{response_topic} || | ||||||||||||||||||||||||||||
| 402 | 67 | ▶ | $self->__create_response_topic; | ||||||||||||||||||||||||||
|
Condition66%2/3
$client->{'response_topic'} || $self->__create_response_topic
MC/DC0%0/2
$client->{'response_topic'} || $self->__create_response_topic
$client->{'response_topic'}
$self->__create_response_topic
Truth table66%2/3
A || B
A= $client->{'response_topic'}B= $self->__create_response_topic
| |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 67 | ▶ | unless ($FIRE_FORGET) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | # Assign an unique request id (unique only for this client) | ||||||||||||||||||||||||||||
| 406 | 64 | $req_id = $client->{correlation_id}++; | |||||||||||||||||||||||||||
| 407 | 64 | $req->{'id'} = $req_id; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 67 | my $json = encode_json($req); | |||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 67 | ▶ | if (exists $args{'buffer_id'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | 0 | $send_args{'buffer_id'} = $args{'buffer_id'}; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | # Send request | ||||||||||||||||||||||||||||
| 417 | $self->{_BUS}->publish( | ||||||||||||||||||||||||||||
| 418 | 67 | payload => \$json, | |||||||||||||||||||||||||||
| 419 | qos => 1, | ||||||||||||||||||||||||||||
| 420 | %send_args, | ||||||||||||||||||||||||||||
| 421 | ); | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 67 | ▶ | if ($FIRE_FORGET) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 424 | # Nothing else to do | ||||||||||||||||||||||||||||
| 425 | 3 | return; | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | elsif ($SYNCHRONOUS) { | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 51 | ▶ | $req->{_raise_error} = (defined $raise_error) ? $raise_error : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | # Wait until a response is received in the reply queue | ||||||||||||||||||||||||||||
| 432 | 51 | $req->{_waiting_response} = AnyEvent->condvar; | |||||||||||||||||||||||||||
| 433 | 51 | $req->{_waiting_response}->begin; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | else { | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 13 | $req->{_on_success_cb} = $args{'on_success'}; | |||||||||||||||||||||||||||
| 438 | 13 | $req->{_on_error_cb} = $args{'on_error'}; | |||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 13 | ▶ | if ($raise_error && !$req->{_on_error_cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$raise_error and not $req->{"_on_error_cb"}
MC/DC0%0/2
$raise_error and not $req->{"_on_error_cb"}
$raise_error
not $req->{"_on_error_cb"}
Truth table33%1/3
A and not(B)
A= $raise_errorB= not $req->{"_on_error_cb"}
| |||||||||||||||||||||||||||||
| 441 | $req->{_on_error_cb} = sub { | ||||||||||||||||||||||||||||
| 442 | 0 | ▶ | my $errmsg = $_[0]->code . " " . $_[0]->message; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 443 | 0 | croak "Call to '$service.$method' failed: $errmsg"; | |||||||||||||||||||||||||||
| 444 | 0 | }; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | # Use shared cv for all requests | ||||||||||||||||||||||||||||
| 448 | 13 | ▶ | if (!$client->{async_cv} || $client->{async_cv}->ready) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $client->{"async_cv"} or $client->{"async_cv"}->ready
MC/DC100%2/2
not $client->{"async_cv"} or $client->{"async_cv"}->ready
not $client->{"async_cv"}
$client->{"async_cv"}->ready
Truth table100%3/3
not(A) or B
A= not $client->{"async_cv"}B= $client->{"async_cv"}->ready
| |||||||||||||||||||||||||||||
| 449 | 4 | $client->{async_cv} = AnyEvent->condvar; | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 13 | $req->{_waiting_response} = $client->{async_cv}; | |||||||||||||||||||||||||||
| 453 | 13 | $req->{_waiting_response}->begin; | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 64 | $client->{in_progress}->{$req_id} = $req; | |||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | # Ensure that timeout is set properly when the event loop was blocked | ||||||||||||||||||||||||||||
| 459 | 64 | ▶ | if ($__now != time) { $__now = time; AnyEvent->now_update } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | # Request timeout timer | ||||||||||||||||||||||||||||
| 462 | 64 | ▶ | my $timeout = $args{'timeout'} || REQ_TIMEOUT; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'timeout'} || 60
MC/DC100%1/1
$args{'timeout'} || 60
$args{'timeout'}
Truth table100%2/2
A
A= $args{'timeout'}
| |||||||||||||||||||||||||||||
| 463 | $req->{_timeout} = AnyEvent->timer( after => $timeout, cb => sub { | ||||||||||||||||||||||||||||
| 464 | 2 | ▶ | my $req = delete $client->{in_progress}->{$req_id}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 465 | 2 | $req->{_response} = Beekeeper::JSONRPC::Error->request_timeout; | |||||||||||||||||||||||||||
| 466 | 2 | ▶ | $req->{_on_error_cb}->($req->{_response}) if $req->{_on_error_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 467 | 2 | $req->{_waiting_response}->end; | |||||||||||||||||||||||||||
| 468 | 64 | }); | |||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 64 | bless $req, 'Beekeeper::JSONRPC::Request'; | |||||||||||||||||||||||||||
| 471 | 64 | return $req; | |||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | sub __create_response_topic { | ||||||||||||||||||||||||||||
| 475 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__create_response_topic: called CC=1 SCAR=0.7
| |||||||||||||||||||||||||||||
| 476 | 6 | my $client = $self->{_CLIENT}; | |||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 6 | my ($file, $line) = (caller(2))[1,2]; | |||||||||||||||||||||||||||
| 479 | 6 | my $at = "at $file line $line\n"; | |||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | # Subscribe to an exclusive topic for receiving RPC responses | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 6 | my $response_topic = 'priv/' . $self->{_BUS}->{client_id}; | |||||||||||||||||||||||||||
| 484 | 6 | $client->{response_topic} = $response_topic; | |||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | $self->{_BUS}->subscribe( | ||||||||||||||||||||||||||||
| 487 | topic => $response_topic, | ||||||||||||||||||||||||||||
| 488 | maximum_qos => 0, | ||||||||||||||||||||||||||||
| 489 | on_publish => sub { | ||||||||||||||||||||||||||||
| 490 | 64 | ▶ | my ($payload_ref, $mqtt_properties) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=34.0
| |||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 64 | local $@; | |||||||||||||||||||||||||||
| 493 | 64 | my $resp = eval { decode_json($$payload_ref) }; | |||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 64 | ▶ | unless (ref $resp eq 'HASH' && $resp->{jsonrpc} eq '2.0') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $resp eq "HASH" and $resp->{"jsonrpc"} eq "2.0"
MC/DC0%0/2
ref $resp eq "HASH" and $resp->{"jsonrpc"} eq "2.0"
ref $resp eq "HASH"
$resp->{"jsonrpc"} eq "2.0"
Truth table33%1/3
A and B
A= ref $resp eq "HASH"B= $resp->{"jsonrpc"} eq "2.0"
| |||||||||||||||||||||||||||||
| 496 | 0 | warn "Received invalid JSON-RPC 2.0 message $at"; | |||||||||||||||||||||||||||
| 497 | 0 | return; | |||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 64 | ▶ | if (exists $resp->{'id'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | # Response of an RPC request | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 64 | my $req_id = $resp->{'id'}; | |||||||||||||||||||||||||||
| 505 | 64 | my $req = delete $client->{in_progress}->{$req_id}; | |||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | # Ignore unexpected responses | ||||||||||||||||||||||||||||
| 508 | 64 | ▶ | return unless $req; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | # Cancel request timeout | ||||||||||||||||||||||||||||
| 511 | 62 | delete $req->{_timeout}; | |||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | 62 | ▶ | if (exists $resp->{'result'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | # Success response | ||||||||||||||||||||||||||||
| 515 | 57 | $req->{_response} = bless $resp, 'Beekeeper::JSONRPC::Response'; | |||||||||||||||||||||||||||
| 516 | 57 | ▶ | $req->{_on_success_cb}->($resp) if $req->{_on_success_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | else { | ||||||||||||||||||||||||||||
| 519 | # Error response | ||||||||||||||||||||||||||||
| 520 | 5 | $req->{_response} = bless $resp, 'Beekeeper::JSONRPC::Error'; | |||||||||||||||||||||||||||
| 521 | 5 | ▶ | $req->{_on_error_cb}->($resp) if $req->{_on_error_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 62 | $req->{_waiting_response}->end; | |||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||
| 526 | else { | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | # Unicasted notification | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | 0 | bless $resp, 'Beekeeper::JSONRPC::Notification'; | |||||||||||||||||||||||||||
| 531 | 0 | $resp->{_headers} = $mqtt_properties; | |||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 0 | my $method = $resp->{method}; | |||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | 0 | ▶ | unless (defined $method && $method =~ m/^([\.\w-]+)\.([\w-]+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $method and $method =~ /^([\.\w-]+)\.([\w-]+)$/
MC/DC0%0/2
defined $method and $method =~ /^([\.\w-]+)\.([\w-]+)$/
defined $method
$method =~ /^([\.\w-]+)\.([\w-]+)$/
Truth table0%0/3
A and B
A= defined $methodB= $method =~ /^([\.\w-]+)\.([\w-]+)$/
| |||||||||||||||||||||||||||||
| 536 | 0 | warn "Received notification with invalid method '$method' $at"; | |||||||||||||||||||||||||||
| 537 | 0 | return; | |||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | my $cb = $client->{callbacks}->{"msg.$1.$2"} || | ||||||||||||||||||||||||||||
| 541 | 0 | ▶ | $client->{callbacks}->{"msg.$1.*"}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$client->{'callbacks'}{"msg.$1.$2"} || $client->{'callbacks'}{"msg.$1.*"}
MC/DC0%0/2
$client->{'callbacks'}{"msg.$1.$2"} || $client->{'callbacks'}{"msg.$1.*"}
$client->{'callbacks'}{"msg.$1.$2"}
$client->{'callbacks'}{"msg.$1.*"}
Truth table0%0/3
A || B
A= $client->{'callbacks'}{"msg.$1.$2"}B= $client->{'callbacks'}{"msg.$1.*"}
| |||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | 0 | ▶ | unless ($cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | 0 | warn "No callback found for received notification '$method' $at"; | |||||||||||||||||||||||||||
| 545 | 0 | return; | |||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | 0 | $cb->($resp->{params}, $resp); | |||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||
| 550 | }, | ||||||||||||||||||||||||||||
| 551 | on_suback => sub { | ||||||||||||||||||||||||||||
| 552 | 6 | ▶ | my ($success, $prop) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 553 | 6 | ▶ | die "Could not subscribe to response topic '$response_topic' $at" unless $success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | 6 | ); | |||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 6 | return $response_topic; | |||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | sub wait_async_calls { | ||||||||||||||||||||||||||||
| 561 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
wait_async_calls: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | # Wait for all pending async requests | ||||||||||||||||||||||||||||
| 564 | 2 | my $cv = delete $self->{_CLIENT}->{async_cv}; | |||||||||||||||||||||||||||
| 565 | 2 | ▶ | return unless defined $cv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | # Make AnyEvent to allow one level of recursive condvar blocking, as we may | ||||||||||||||||||||||||||||
| 568 | # block both in $worker->__work_forever and here | ||||||||||||||||||||||||||||
| 569 | 2 | ▶ | $AE_WAITING && Carp::confess "Recursive condvar blocking wait attempted"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 570 | 2 | local $AE_WAITING = 1; | |||||||||||||||||||||||||||
| 571 | 2 | local $AnyEvent::CondVar::Base::WAITING = 0; | |||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | 2 | $cv->recv; | |||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | sub get_authentication_data { | ||||||||||||||||||||||||||||
| 578 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_authentication_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | 0 | $self->{_CLIENT}->{auth_data}; | |||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | sub set_authentication_data { | ||||||||||||||||||||||||||||
| 584 | 0 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_authentication_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | 0 | $self->{_CLIENT}->{auth_data} = $data; | |||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | sub __use_authorization_token { | ||||||||||||||||||||||||||||
| 590 | 18 | ▶ | my ($self, $token) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__use_authorization_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | # Using a hashing function makes harder to access the wrong worker pool by mistake, | ||||||||||||||||||||||||||||
| 593 | # but it is not an effective access restriction: anyone with access to the backend | ||||||||||||||||||||||||||||
| 594 | # bus credentials can easily inspect and clone auth data tokens | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | 18 | my $salt = $self->{_CLIENT}->{auth_salt}; | |||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | 18 | my $adata_ref = \$self->{_CLIENT}->{auth_data}; | |||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 18 | my $guard = Beekeeper::Client::Guard->new( $adata_ref ); | |||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | 18 | $$adata_ref = md5_base64($token . $salt); | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 18 | return $guard; | |||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | 11 | 1; | |||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | package | ||||||||||||||||||||||||||||
| 610 | Beekeeper::Client::Guard; # hide from PAUSE | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | sub new { | ||||||||||||||||||||||||||||
| 613 | 18 | ▶ | my ($class, $ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | 18 | bless [$ref, $$ref], $class; | |||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | sub DESTROY { | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | 18 | ▶ | ${$_[0]->[0]} = $_[0]->[1]; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | 11 | 1; | |||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | __END__ | ||||||||||||||||||||||||||||