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