blib/lib/SignalWire/Relay/Client.pm

filter: Statementstmt 44.1% 186 / 421 Branchbran 27.3% 47 / 172 Conditioncond 23.4% 37 / 158 MC/DCmcdc 8.0% 7 / 87 Subroutinesub 59.7% 43 / 72 Podpod 0.0% 0 / 16 totaltotal 34.5% 320 / 926 CC 154 SCAR 48.2
CC
154
Cov
36%
send_message
756.0
dial
156.0
authenticate
110.0
CRAP 123.4
« blib/lib/SignalWire/Skills/Builtin/WebSearch.pm ↑ Summary blib/lib/SignalWire/SWAIG/FunctionResult.pm »
1 package SignalWire::Relay::Client;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Protocol::WebSocket::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use SignalWire::Relay::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 PROTOCOL_VERSION
12 CALL_TERMINAL_STATES
13 DIAL_STATE_ANSWERED DIAL_STATE_FAILED
14 MESSAGE_TERMINAL_STATES
15 3 );
16 3 ▶use SignalWire::Relay::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use SignalWire::Relay::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use SignalWire::Relay::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use SignalWire::Logging;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 my $logger = SignalWire::Logging->get_logger('relay_client');
22  
23 0 ▶has 'project' => ( is => 'ro', default => sub { '' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 0 ▶has 'token' => ( is => 'ro', default => sub { '' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 3 has 'host' => ( is => 'ro', required => 1 );
26 19 ▶has 'contexts' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
27 20 ▶has 'agent' => ( is => 'ro', default => sub { 'signalwire-agents-perl/1.0' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
28 # Optional JWT-based authentication (alternative to project/token).
29 20 ▶has 'jwt_token' => ( is => 'ro', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
30 20 ▶has '_jwt_token' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
31 # Scheme: "wss" (production, TLS — the default) or "ws" (plain, used by
32 # the local audit fixture in audit_relay_handshake.py). Keeping this
33 # explicit lets the same client drive both real RELAY and a 127.0.0.1
34 # fixture without forking the transport.
35 20 ▶has 'scheme' => ( is => 'ro', default => sub { 'wss' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
36 # Path component appended to the host. Defaults to '/api/relay/ws' (the
37 # documented production endpoint per RELAY_IMPLEMENTATION_GUIDE).
38 20 ▶has 'path' => ( is => 'ro', default => sub { '/api/relay/ws' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 # Connection state
41 20 ▶has 'protocol' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
42 20 ▶has 'authorization_state' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 20 ▶has 'connected' => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 20 ▶has 'session_id' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
45  
46 # Aliases for Python parity (same value, different names).
47 0 ▶sub relay_protocol { $_[0]->protocol }
Subroutine relay_protocol: not called CC=1 SCAR=6.9
Pod undocumented
48 0 ▶sub _connected { $_[0]->connected }
Subroutine _connected: not called CC=1 SCAR=6.9
49 0 ▶sub _authorization_state { $_[0]->authorization_state }
Subroutine _authorization_state: not called CC=1 SCAR=6.9
50  
51 # Correlation maps
52 20 ▶has '_pending' => ( is => 'rw', default => sub { {} } ); # rpc_id => { resolve => sub, reject => sub }
Subroutine __ANON__: called CC=1 SCAR=0.0
53 20 ▶has '_calls' => ( is => 'rw', default => sub { {} } ); # call_id => Call
Subroutine __ANON__: called CC=1 SCAR=0.0
54 20 ▶has '_pending_dials' => ( is => 'rw', default => sub { {} } ); # tag => { resolve => sub, reject => sub }
Subroutine __ANON__: called CC=1 SCAR=0.0
55 20 ▶has '_messages' => ( is => 'rw', default => sub { {} } ); # message_id => Message
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 # WebSocket internals
58 20 ▶has '_socket' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
59 20 ▶has '_ws' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
60  
61 # Reconnect state
62 20 ▶has '_reconnect_attempts' => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
63 20 ▶has '_max_backoff' => ( is => 'ro', default => sub { 30 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 # Callbacks
66 20 ▶has '_on_call' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
67 20 ▶has '_on_message' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
68 20 ▶has '_on_event' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
69  
70 # --- UUID generation ---
71 0 ▶sub _generate_uuid {
Subroutine _generate_uuid: not called CC=1 SCAR=6.9
72 0 my @hex = map { sprintf('%02x', int(rand(256))) } 1..16;
73 0 $hex[6] = sprintf('%02x', (hex($hex[6]) & 0x0f) | 0x40);
74 0 $hex[8] = sprintf('%02x', (hex($hex[8]) & 0x3f) | 0x80);
75 0 return join('-',
76 join('', @hex[0..3]),
77 join('', @hex[4..5]),
78 join('', @hex[6..7]),
79 join('', @hex[8..9]),
80 join('', @hex[10..15]),
81 );
82 }
83  
84 # --- Public API: register handlers ---
85  
86 2 ▶sub on_call {
Subroutine on_call: called CC=1 SCAR=0.0
Pod undocumented
87 2 my ($self, $cb) = @_;
88 2 $self->_on_call($cb);
89 2 return $self;
90 }
91  
92 3 ▶sub on_message {
Subroutine on_message: called CC=1 SCAR=0.0
Pod undocumented
93 3 my ($self, $cb) = @_;
94 3 $self->_on_message($cb);
95 3 return $self;
96 }
97  
98 1 ▶sub on_event {
Subroutine on_event: called CC=1 SCAR=0.0
Pod undocumented
99 1 my ($self, $cb) = @_;
100 1 $self->_on_event($cb);
101 1 return $self;
102 }
103  
104 # --- Connection ---
105  
106 # Public connect: opens the WebSocket and runs the signalwire.connect
107 # handshake in one call (matches Python RelayClient.connect()). Returns
108 # the authenticate result hashref on success, dies on failure.
109 0 ▶sub connect {
Subroutine connect: not called CC=6 SCAR=37.4
Pod undocumented
110 0 my ($self) = @_;
111 0 ▶ die "project and token are required (or jwt_token)"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->project and $self->token or $self->jwt_token or $self->_jwt_token 0 0 0
Condition0%0/9
$self->project and $self->token or $self->jwt_token or $self->_jwt_token
l!l&&r!l&&!r
000
$self->project and $self->token or $self->jwt_token
l!l&&r!l&&!r
000
$self->project and $self->token
!ll&&!rl&&r
000
MC/DC0%0/4
$self->project and $self->token or $self->jwt_token or $self->_jwt_token
$self->project $self->token $self->jwt_token $self->_jwt_token
Truth table0%0/7
A and B or C or D
A= $self->projectB= $self->tokenC= $self->jwt_tokenD= $self->_jwt_token
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
112 unless ($self->project && $self->token) || $self->jwt_token || $self->_jwt_token;
113 0 my $ok = $self->connect_ws;
114 0 ▶ die "WebSocket connect failed" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
115 0 return $self->authenticate;
116 }
117  
118 # Public disconnect: tears down the WebSocket transport. Mirrors
119 # Python RelayClient.disconnect().
120 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self) = @_;
122 0 return $self->disconnect_ws;
123 }
124  
125 0 ▶sub connect_ws {
Subroutine connect_ws: not called CC=9 SCAR=45.0
Pod undocumented
126 0 my ($self) = @_;
127  
128 0 ▶ my $scheme = $self->scheme || 'wss';
Condition0%0/2
$self->scheme || 'wss'
l!l
00
MC/DC0%0/1
$self->scheme || 'wss'
$self->scheme
Truth table0%0/2
A
A= $self->scheme
Aresult
11
00
129 0 my $raw_host = $self->host;
130  
131 # `host` may be a bare hostname ("example.com") or a hostname+port
132 # ("127.0.0.1:9000"). Split if needed; otherwise pick the default
133 # port for the scheme.
134 0 my ($host, $port);
135 0 ▶ if ($raw_host =~ /^(.+):(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_host =~ /^(.+):(\d+)$/) { } 0 0 0
136 0 ($host, $port) = ($1, $2);
137 } else {
138 0 $host = $raw_host;
139 0 ▶ $port = ($scheme eq 'ws') ? 80 : 443;
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'ws' ? : 0 0 0
140 }
141  
142 0 my $url = "$scheme://$raw_host" . $self->path;
143 0 $logger->debug("Connecting to $url");
144  
145 0 my $socket;
146 0 ▶ if ($scheme eq 'ws') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'ws') { } 0 0 0
147 # Plain TCP — used by the local audit fixture. No TLS, no
148 # certificate validation. Production relay never runs over
149 # plain ws://.
150 0 $socket = IO::Socket::INET->new(
151 PeerHost => $host,
152 PeerPort => $port,
153 Proto => 'tcp',
154 Timeout => 10,
155 );
156 0 ▶ unless ($socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 0 0 0
157 0 $logger->error("TCP connection failed: $!");
158 0 return 0;
159 }
160 } else {
161 0 $socket = IO::Socket::SSL->new(
162 PeerHost => $host,
163 PeerPort => $port,
164 SSL_verify_mode => SSL_VERIFY_PEER,
165 Timeout => 10,
166 );
167 0 ▶ unless ($socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 0 0 0
168 0 $logger->error("SSL connection failed: $! $IO::Socket::SSL::SSL_ERROR");
169 0 return 0;
170 }
171 }
172  
173 0 my $ws = Protocol::WebSocket::Client->new(url => $url);
174  
175 0 ▶ $ws->on(write => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 my ($ws_client, $buf) = @_;
177 0 syswrite($socket, $buf);
178 0 });
179  
180 0 ▶ $ws->on(connect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 $logger->debug("WebSocket connected");
182 0 });
183  
184 0 ▶ $ws->on(error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 0 my ($ws_client, $error) = @_;
186 0 $logger->error("WebSocket error: $error");
187 0 });
188  
189 0 ▶ $ws->on(read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 my ($ws_client, $message) = @_;
191 0 $self->_handle_message($message);
192 0 });
193  
194 0 $self->_socket($socket);
195 0 $self->_ws($ws);
196  
197 # Initiate the WebSocket handshake
198 0 $ws->connect;
199  
200 # Read the handshake response
201 0 my $buf = '';
202 0 ▶ while (my $bytes = sysread($socket, $buf, 4096, length($buf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bytes = sysread($socket, $buf, 4096, length $buf)) 0 0 0
203 0 $ws->read($buf);
204 0 $buf = '';
205 0 ▶ last if $ws->{hs}->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $ws->{"hs"}->is_done 0 0 0
206 }
207  
208 0 $self->connected(1);
209 0 $self->_reconnect_attempts(0);
210 0 return 1;
211 }
212  
213 0 ▶sub authenticate {
Subroutine authenticate: not called CC=10 SCAR=47.0
Pod undocumented
214 0 my ($self) = @_;
215  
216 # Build authentication block: either project/token or jwt_token.
217 0 my %auth;
218 0 ▶ my $jwt = $self->jwt_token || $self->_jwt_token;
Condition0%0/3
$self->jwt_token || $self->_jwt_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->jwt_token || $self->_jwt_token
$self->jwt_token $self->_jwt_token
Truth table0%0/3
A || B
A= $self->jwt_tokenB= $self->_jwt_token
ABresult
1X1
011
000
219 0 ▶ if ($jwt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jwt) { } 0 0 0
220 0 $auth{jwt_token} = $jwt;
221 } else {
222 0 $auth{project} = $self->project;
223 0 $auth{token} = $self->token;
224 }
225  
226 0 my %params = (
227 version => PROTOCOL_VERSION,
228 agent => $self->agent,
229 event_acks => JSON::true,
230 authentication => \%auth,
231 );
232 # Mirror project/token at the top level when not using JWT (Rust agent
233 # convention; production tolerates both shapes).
234 0 ▶ unless ($jwt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jwt) 0 0 0
235 0 $params{project} = $self->project;
236 0 $params{token} = $self->token;
237 }
238  
239 # Add contexts if any
240 0 ▶ if (@{$self->contexts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->contexts;}) 0 0 0
241 0 $params{contexts} = $self->contexts;
242 }
243  
244 # Add protocol for session resume
245 0 ▶ if ($self->protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->protocol) 0 0 0
246 0 $params{protocol} = $self->protocol;
247 }
248  
249 # Add authorization_state for fast re-auth
250 0 ▶ if ($self->authorization_state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->authorization_state) 0 0 0
251 0 $params{authorization_state} = $self->authorization_state;
252 }
253  
254 0 my $result = $self->execute('signalwire.connect', \%params);
255  
256 0 ▶ if (ref $result eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH") 0 0 0
257 0 ▶ $self->protocol($result->{protocol}) if $result->{protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"protocol"} 0 0 0
258 0 ▶ $self->session_id($result->{session_id}) if $result->{session_id};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"session_id"} 0 0 0
259 }
260  
261 0 return $result;
262 }
263  
264 # --- JSON-RPC execute ---
265  
266 0 ▶sub execute {
Subroutine execute: not called CC=7 SCAR=40.3
Pod undocumented
267 0 my ($self, $method, $params) = @_;
268 0 ▶ $params //= {};
Condition0%0/2
$params //= {}
l!l
00
MC/DC0%0/1
$params //= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
269  
270 0 my $id = _generate_uuid();
271  
272 # Add protocol to params (except for signalwire.connect itself)
273 0 ▶ if ($method ne 'signalwire.connect' && $self->protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne "signalwire.connect" and $self->protocol) 0 0 0
Condition0%0/3
$method ne "signalwire.connect" and $self->protocol
!ll&&!rl&&r
000
MC/DC0%0/2
$method ne "signalwire.connect" and $self->protocol
$method ne "signalwire.connect" $self->protocol
Truth table0%0/3
A and B
A= $method ne "signalwire.connect"B= $self->protocol
ABresult
0X0
100
111
274 0 $params->{protocol} = $self->protocol;
275 }
276  
277 0 my $request = {
278 jsonrpc => '2.0',
279 id => $id,
280 method => $method,
281 params => $params,
282 };
283  
284 # Register pending
285 0 my $result;
286 0 my $done = 0;
287 0 my $error;
288 $self->_pending->{$id} = {
289 0 ▶ resolve => sub { $result = $_[0]; $done = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
290 0 ▶ reject => sub { $error = $_[0]; $done = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
291 0 };
292  
293 0 $self->_send($request);
294  
295 # Poll for response (synchronous in this implementation)
296 0 my $timeout = 30;
297 0 my $start = time();
298 0 ▶ while (!$done && (time() - $start) < $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and time - $start < $timeout) 0 0 0
Condition0%0/3
not $done and time - $start < $timeout
!ll&&!rl&&r
000
MC/DC0%0/2
not $done and time - $start < $timeout
not $done time - $start < $timeout
Truth table0%0/3
not(A) and B
A= not $doneB= time - $start < $timeout
ABresult
0X0
100
111
299 0 $self->_read_once();
300 }
301  
302 0 delete $self->_pending->{$id};
303  
304 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
305 0 die "RELAY error: $error";
306 }
307  
308 0 return $result;
309 }
310  
311 # --- Messaging ---
312  
313 0 ▶sub send_message {
Subroutine send_message: not called CC=27 SCAR=66.3
Pod undocumented
314 0 my ($self, %opts) = @_;
315  
316 die "At least one of body or media is required"
317 unless (defined $opts{body} && length $opts{body})
318 0 ▶ || (ref $opts{media} eq 'ARRAY' && @{$opts{media}});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"body"} and length $opts{"body"} or ref $opts{"media"} eq "ARRAY" and @{$opts{"media"};} 0 0 0
Condition0%0/9
defined $opts{"body"} and length $opts{"body"} or ref $opts{"media"} eq "ARRAY" and @{$opts{"media"};}
l!l&&r!l&&!r
000
defined $opts{"body"} and length $opts{"body"}
!ll&&!rl&&r
000
ref $opts{"media"} eq "ARRAY" and @{$opts{"media"};}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $opts{"body"} and length $opts{"body"} or ref $opts{"media"} eq "ARRAY" and @{$opts{"media"};}
defined $opts{"body"} length $opts{"body"} ref $opts{"media"} eq "ARRAY" @{$opts{"media"};}
Truth table0%0/7
A and B or C and D
A= defined $opts{"body"}B= length $opts{"body"}C= ref $opts{"media"} eq "ARRAY"D= @{$opts{"media"};}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
319  
320 # Default context to the relay protocol or 'default' (matches Python).
321 0 ▶ my $msg_context = $opts{context} // $self->protocol // '';
Condition0%0/5
($opts{"context"} // $self->protocol) // ""
l!l
00
$opts{"context"} // $self->protocol
l!l&&r!l&&!r
000
MC/DC0%0/2
($opts{"context"} // $self->protocol) // ""
$opts{"context"} $self->protocol
Truth table0%0/3
A // B
A= $opts{"context"}B= $self->protocol
ABresult
1X1
011
000
322 0 ▶ $msg_context = 'default' unless length $msg_context;
Branch: 1 branch
BranchTrueFalseTotal
unless length $msg_context 0 0 0
323  
324 my %params = (
325 context => $msg_context,
326 to_number => $opts{to_number} // '',
327 0 ▶ from_number => $opts{from_number} // '',
Condition0%0/4
$opts{"to_number"} // ""
l!l
00
$opts{"from_number"} // ""
l!l
00
MC/DC0%0/1
$opts{"to_number"} // ""
$opts{"to_number"}
MC/DC0%0/1
$opts{"from_number"} // ""
$opts{"from_number"}
Truth table0%0/2
A
A= $opts{"to_number"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"from_number"}
Aresult
11
00
328 );
329 0 ▶ $params{body} = $opts{body} if defined $opts{body} && length $opts{body};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"body"} and length $opts{"body"} 0 0 0
Condition0%0/3
defined $opts{"body"} and length $opts{"body"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"body"} and length $opts{"body"}
defined $opts{"body"} length $opts{"body"}
Truth table0%0/3
A and B
A= defined $opts{"body"}B= length $opts{"body"}
ABresult
0X0
100
111
330 0 ▶ $params{media} = $opts{media} if ref $opts{media} eq 'ARRAY' && @{$opts{media}};
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts{"media"} eq "ARRAY" and @{$opts{"media"};} 0 0 0
Condition0%0/3
ref $opts{"media"} eq "ARRAY" and @{$opts{"media"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $opts{"media"} eq "ARRAY" and @{$opts{"media"};}
ref $opts{"media"} eq "ARRAY" @{$opts{"media"};}
Truth table0%0/3
A and B
A= ref $opts{"media"} eq "ARRAY"B= @{$opts{"media"};}
ABresult
0X0
100
111
331 0 ▶ $params{tags} = $opts{tags} if ref $opts{tags} eq 'ARRAY' && @{$opts{tags}};
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts{"tags"} eq "ARRAY" and @{$opts{"tags"};} 0 0 0
Condition0%0/3
ref $opts{"tags"} eq "ARRAY" and @{$opts{"tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $opts{"tags"} eq "ARRAY" and @{$opts{"tags"};}
ref $opts{"tags"} eq "ARRAY" @{$opts{"tags"};}
Truth table0%0/3
A and B
A= ref $opts{"tags"} eq "ARRAY"B= @{$opts{"tags"};}
ABresult
0X0
100
111
332 0 ▶ $params{region} = $opts{region} if defined $opts{region} && length $opts{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"region"} and length $opts{"region"} 0 0 0
Condition0%0/3
defined $opts{"region"} and length $opts{"region"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"region"} and length $opts{"region"}
defined $opts{"region"} length $opts{"region"}
Truth table0%0/3
A and B
A= defined $opts{"region"}B= length $opts{"region"}
ABresult
0X0
100
111
333  
334 0 my $result = $self->execute('messaging.send', \%params);
335  
336 0 ▶ if (ref $result eq 'HASH' && $result->{message_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH" and $result->{"message_id"}) 0 0 0
Condition0%0/3
ref $result eq "HASH" and $result->{"message_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $result eq "HASH" and $result->{"message_id"}
ref $result eq "HASH" $result->{"message_id"}
Truth table0%0/3
A and B
A= ref $result eq "HASH"B= $result->{"message_id"}
ABresult
0X0
100
111
337 my $msg = SignalWire::Relay::Message->new(
338 message_id => $result->{message_id},
339 context => $opts{context} // '',
340 direction => 'outbound',
341 from_number => $opts{from_number} // '',
342 to_number => $opts{to_number} // '',
343 body => $opts{body} // '',
344 media => $opts{media} // [],
345 0 ▶ tags => $opts{tags} // [],
Condition0%0/12
$opts{"context"} // ""
l!l
00
$opts{"from_number"} // ""
l!l
00
$opts{"to_number"} // ""
l!l
00
$opts{"body"} // ""
l!l
00
$opts{"media"} // []
l!l
00
$opts{"tags"} // []
l!l
00
MC/DC0%0/1
$opts{"context"} // ""
$opts{"context"}
MC/DC0%0/1
$opts{"from_number"} // ""
$opts{"from_number"}
MC/DC0%0/1
$opts{"to_number"} // ""
$opts{"to_number"}
MC/DC0%0/1
$opts{"body"} // ""
$opts{"body"}
MC/DC0%0/1
$opts{"media"} // []
$opts{"media"}
MC/DC0%0/1
$opts{"tags"} // []
$opts{"tags"}
Truth table0%0/2
A
A= $opts{"context"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"from_number"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"to_number"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"body"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"media"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"tags"}
Aresult
11
00
346 state => 'queued',
347 );
348 0 $self->_messages->{$result->{message_id}} = $msg;
349  
350 0 ▶ if ($opts{on_completed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"on_completed"}) 0 0 0
351 0 $msg->on_completed($opts{on_completed});
352 }
353  
354 0 return $msg;
355 }
356  
357 0 return $result;
358 }
359  
360 # --- Context management ---
361  
362 0 ▶sub receive {
Subroutine receive: not called CC=3 SCAR=24.8
Pod undocumented
363 0 my ($self, $contexts) = @_;
364 # Python parity: receive(contexts: list[str]). Canonical form takes
365 # an arrayref. Backward-compat: also accept slurpy
366 # (``$client->receive('ctx1', 'ctx2')``) — re-grab @_ when the first
367 # arg is a string and there are extras.
368 0 my @ctxs;
369 0 ▶ if (ref $contexts eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $contexts eq 'ARRAY') { } 0 0 0
370 0 @ctxs = @$contexts;
371 } else {
372 0 @ctxs = @_;
373 0 shift @ctxs; # drop $self
374 }
375 0 ▶ return unless @ctxs;
Branch: 1 branch
BranchTrueFalseTotal
unless @ctxs 0 0 0
376 0 return $self->execute('signalwire.receive', { contexts => \@ctxs });
377 }
378  
379 0 ▶sub unreceive {
Subroutine unreceive: not called CC=3 SCAR=24.8
Pod undocumented
380 0 my ($self, $contexts) = @_;
381 # Python parity: unreceive(contexts: list[str]).
382 0 my @ctxs;
383 0 ▶ if (ref $contexts eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $contexts eq 'ARRAY') { } 0 0 0
384 0 @ctxs = @$contexts;
385 } else {
386 0 @ctxs = @_;
387 0 shift @ctxs; # drop $self
388 }
389 0 ▶ return unless @ctxs;
Branch: 1 branch
BranchTrueFalseTotal
unless @ctxs 0 0 0
390 0 return $self->execute('signalwire.unreceive', { contexts => \@ctxs });
391 }
392  
393 # --- Dial ---
394  
395 0 ▶sub dial {
Subroutine dial: not called CC=12 SCAR=50.5
Pod undocumented
396 0 my ($self, %opts) = @_;
397 0 ▶ my $tag = $opts{tag} || _generate_uuid();
Condition0%0/3
$opts{'tag'} || _generate_uuid()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'tag'} || _generate_uuid()
$opts{'tag'} _generate_uuid()
Truth table0%0/3
A || B
A= $opts{'tag'}B= _generate_uuid()
ABresult
1X1
011
000
398 0 ▶ my $timeout = delete $opts{timeout} || 120;
Condition0%0/2
delete $opts{'timeout'} || 120
l!l
00
MC/DC0%0/1
delete $opts{'timeout'} || 120
delete $opts{'timeout'}
Truth table0%0/2
A
A= delete $opts{'timeout'}
Aresult
11
00
399 0 my $on_completed = delete $opts{on_completed};
400  
401 0 my %params = ( tag => $tag );
402 0 ▶ $params{devices} = $opts{devices} if $opts{devices};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"devices"} 0 0 0
403 0 ▶ $params{region} = $opts{region} if $opts{region};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"region"} 0 0 0
404 0 ▶ $params{max_price_per_minute} = $opts{max_price_per_minute} if exists $opts{max_price_per_minute};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"max_price_per_minute"} 0 0 0
405  
406 # Register pending dial BEFORE sending RPC
407 0 my $call;
408 0 my $done = 0;
409 0 my $dial_error;
410 $self->_pending_dials->{$tag} = {
411 0 ▶ resolve => sub { $call = $_[0]; $done = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 0 ▶ reject => sub { $dial_error = $_[0]; $done = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
413 0 };
414  
415 # Send the RPC -- response is just {code:200, message:"Dialing"}
416 0 eval { $self->execute('calling.dial', \%params) };
417 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
418 0 delete $self->_pending_dials->{$tag};
419 0 die $@;
420 }
421  
422 # Wait for calling.call.dial event to resolve
423 0 my $start = time();
424 0 ▶ while (!$done && (time() - $start) < $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and time - $start < $timeout) 0 0 0
Condition0%0/3
not $done and time - $start < $timeout
!ll&&!rl&&r
000
MC/DC0%0/2
not $done and time - $start < $timeout
not $done time - $start < $timeout
Truth table0%0/3
not(A) and B
A= not $doneB= time - $start < $timeout
ABresult
0X0
100
111
425 0 $self->_read_once();
426 }
427  
428 0 delete $self->_pending_dials->{$tag};
429  
430 0 ▶ if ($dial_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dial_error) 0 0 0
431 0 die "Dial failed: $dial_error";
432 }
433  
434 0 ▶ if ($call && $on_completed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call and $on_completed) 0 0 0
Condition0%0/3
$call and $on_completed
!ll&&!rl&&r
000
MC/DC0%0/2
$call and $on_completed
$call $on_completed
Truth table0%0/3
A and B
A= $callB= $on_completed
ABresult
0X0
100
111
435 0 ▶ $call->on(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
436 0 my ($c, $event) = @_;
437 0 ▶ if ($event->event_type eq 'calling.call.state' && $event->call_state eq 'ended') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->event_type eq "calling.call.state" and $event->call_state eq "ended") 0 0 0
Condition0%0/3
$event->event_type eq "calling.call.state" and $event->call_state eq "ended"
!ll&&!rl&&r
000
MC/DC0%0/2
$event->event_type eq "calling.call.state" and $event->call_state eq "ended"
$event->event_type eq "calling.call.state" $event->call_state eq "ended"
Truth table0%0/3
A and B
A= $event->event_type eq "calling.call.state"B= $event->call_state eq "ended"
ABresult
0X0
100
111
438 0 eval { $on_completed->($c) };
439 0 ▶ warn "dial on_completed error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
440 }
441 0 });
442 }
443  
444 0 return $call;
445 }
446  
447 # --- Internal: send a JSON-RPC message ---
448  
449 0 ▶sub _send {
Subroutine _send: not called CC=2 SCAR=17.9
450 0 my ($self, $msg) = @_;
451 0 my $json = encode_json($msg);
452 0 $logger->debug("SEND: $json");
453 0 my $ws = $self->_ws;
454 0 ▶ if ($ws) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ws) 0 0 0
455 0 $ws->write($json);
456 }
457 }
458  
459 # --- Internal: read one frame from WebSocket ---
460  
461 0 ▶sub _read_once {
Subroutine _read_once: not called CC=7 SCAR=40.3
462 0 my ($self) = @_;
463 0 my $socket = $self->_socket;
464 0 ▶ return unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 0 0
465  
466 0 my $buf = '';
467 0 my $ready = '';
468 0 vec($ready, fileno($socket), 1) = 1;
469 0 ▶ if (select($ready, undef, undef, 0.1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (select $ready, undef, undef, "0.1") 0 0 0
470 0 my $bytes = sysread($socket, $buf, 4096);
471 0 ▶ if ($bytes && $bytes > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bytes and $bytes > 0) { } 0 0 0
elsif (not defined $bytes or $bytes == 0) { } 0 0 0
Condition0%0/3
$bytes and $bytes > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$bytes and $bytes > 0
$bytes $bytes > 0
Truth table0%0/3
A and B
A= $bytesB= $bytes > 0
ABresult
0X0
100
111
472 0 $self->_ws->read($buf);
473 ▶ } elsif (!defined $bytes || $bytes == 0) {
Condition0%0/3
not defined $bytes or $bytes == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $bytes or $bytes == 0
not defined $bytes $bytes == 0
Truth table0%0/3
not(A) or B
A= not defined $bytesB= $bytes == 0
ABresult
1X1
011
000
474 # Connection lost
475 0 $self->connected(0);
476 }
477 }
478 }
479  
480 # --- Internal: handle an incoming WebSocket message ---
481  
482 4 ▶sub _handle_message {
Subroutine _handle_message: called CC=19 SCAR=36.4
483 4 my ($self, $raw) = @_;
484 4 $logger->debug("RECV: $raw");
485  
486 # Skip non-JSON-text frames. Protocol::WebSocket::Client doesn't
487 # surface frame opcode in on_read, so we sniff: a JSON-RPC frame
488 # always starts with '{'. Close/ping/pong control frames have a
489 # 16-bit status code or short binary payload that won't begin with
490 # '{', and decoding them as JSON would just spam the log.
491 4 ▶ return unless defined $raw && length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and length $raw 0 4 4
Condition33%1/3
defined $raw and length $raw
!ll&&!rl&&r
004
MC/DC0%0/2
defined $raw and length $raw
defined $raw length $raw
Truth table33%1/3
A and B
A= defined $rawB= length $raw
ABresult
0X0
100
111
492 4 my $first;
493 4 ▶ if (utf8::is_utf8($raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($raw)) { } 0 4 4
494 0 $first = substr($raw, 0, 1);
495 } else {
496 4 $first = substr($raw, 0, 1);
497 }
498 4 ▶ return unless defined $first && $first eq '{';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and $first eq "{" 0 4 4
Condition33%1/3
defined $first and $first eq "{"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $first and $first eq "{"
defined $first $first eq "{"
Truth table33%1/3
A and B
A= defined $firstB= $first eq "{"
ABresult
0X0
100
111
499  
500 # decode_json expects a byte string; if the payload arrived flagged
501 # utf8 (Perl saw a multibyte char in transit) we need to re-encode
502 # before parsing. Otherwise we hit "Wide character in subroutine entry".
503 4 my $msg;
504 4 ▶ if (utf8::is_utf8($raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($raw)) { } 0 4 4
505 0 my $bytes = $raw;
506 0 utf8::encode($bytes);
507 0 eval { $msg = decode_json($bytes) };
508 } else {
509 4 eval { $msg = decode_json($raw) };
510 }
511 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
512 0 $logger->error("JSON parse error: $@");
513 0 return;
514 }
515  
516 # JSON-RPC response (has "result" or "error", matched by "id")
517 4 ▶ if (exists $msg->{result} || exists $msg->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $msg->{"result"} or exists $msg->{"error"}) 2 2 4
Condition100%3/3
exists $msg->{"result"} or exists $msg->{"error"}
l!l&&r!l&&!r
112
MC/DC100%2/2
exists $msg->{"result"} or exists $msg->{"error"}
exists $msg->{"result"} exists $msg->{"error"}
Truth table100%3/3
A or B
A= exists $msg->{"result"}B= exists $msg->{"error"}
ABresult
1X1
011
000
518 2 ▶ my $id = $msg->{id} // '';
Condition50%1/2
$msg->{"id"} // ""
l!l
20
MC/DC0%0/1
$msg->{"id"} // ""
$msg->{"id"}
Truth table50%1/2
A
A= $msg->{"id"}
Aresult
11
00
519 2 ▶ if (my $pending = delete $self->_pending->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pending = delete $self->_pending->{$id}) 2 0 2
520 2 ▶ if (exists $msg->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $msg->{'error'}) { } 1 1 2
521 1 $pending->{reject}->($msg->{error});
522 } else {
523 1 $pending->{resolve}->($msg->{result});
524 }
525 }
526 2 return;
527 }
528  
529 # Server-initiated method call
530 2 ▶ my $method = $msg->{method} // '';
Condition50%1/2
$msg->{"method"} // ""
l!l
20
MC/DC0%0/1
$msg->{"method"} // ""
$msg->{"method"}
Truth table50%1/2
A
A= $msg->{"method"}
Aresult
11
00
531  
532 2 ▶ if ($method eq 'signalwire.event') {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'signalwire.event') { } 1 1 2
elsif ($method eq 'signalwire.ping') { } 1 0 1
elsif ($method eq 'signalwire.disconnect') { } 0 0 0
533 # ACK the event immediately
534 1 $self->_send_ack($msg->{id});
535 1 ▶ $self->_handle_event($msg->{params} // {});
Condition50%1/2
$msg->{"params"} // {}
l!l
10
MC/DC0%0/1
$msg->{"params"} // {}
$msg->{"params"}
Truth table50%1/2
A
A= $msg->{"params"}
Aresult
11
00
536 }
537 elsif ($method eq 'signalwire.ping') {
538 1 $self->_send_ack($msg->{id});
539 }
540 elsif ($method eq 'signalwire.disconnect') {
541 0 $self->_send_ack($msg->{id});
542 0 ▶ $self->_handle_disconnect($msg->{params} // {});
Condition0%0/2
$msg->{"params"} // {}
l!l
00
MC/DC0%0/1
$msg->{"params"} // {}
$msg->{"params"}
Truth table0%0/2
A
A= $msg->{"params"}
Aresult
11
00
543 }
544 }
545  
546 # --- Internal: send an ACK ---
547  
548 2 ▶sub _send_ack {
Subroutine _send_ack: called CC=1 SCAR=0.0
549 2 my ($self, $id) = @_;
550 2 $self->_send({
551 jsonrpc => '2.0',
552 id => $id,
553 result => {},
554 });
555 }
556  
557 # --- Internal: handle events ---
558  
559 18 ▶sub _handle_event {
Subroutine _handle_event: called CC=26 SCAR=35.0
560 18 my ($self, $outer_params) = @_;
561 18 ▶ my $event_type = $outer_params->{event_type} // '';
Condition50%1/2
$outer_params->{"event_type"} // ""
l!l
180
MC/DC0%0/1
$outer_params->{"event_type"} // ""
$outer_params->{"event_type"}
Truth table50%1/2
A
A= $outer_params->{"event_type"}
Aresult
11
00
562 18 ▶ my $inner_params = $outer_params->{params} // {};
Condition50%1/2
$outer_params->{"params"} // {}
l!l
180
MC/DC0%0/1
$outer_params->{"params"} // {}
$outer_params->{"params"}
Truth table50%1/2
A
A= $outer_params->{"params"}
Aresult
11
00
563  
564 # Parse into typed event object
565 18 my $event = SignalWire::Relay::Event->parse_event($event_type, $inner_params);
566  
567 # Fire global event callback
568 18 ▶ if (my $cb = $self->_on_event) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->_on_event) 0 18 18
569 0 eval { $cb->($event) };
570 0 ▶ warn "on_event callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
571 }
572  
573 # --- Authorization state ---
574 18 ▶ if ($event_type eq 'signalwire.authorization.state') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_type eq "signalwire.authorization.state") 1 17 18
575 1 ▶ $self->authorization_state($inner_params->{authorization_state} // '');
Condition50%1/2
$inner_params->{"authorization_state"} // ""
l!l
10
MC/DC0%0/1
$inner_params->{"authorization_state"} // ""
$inner_params->{"authorization_state"}
Truth table50%1/2
A
A= $inner_params->{"authorization_state"}
Aresult
11
00
576 1 return;
577 }
578  
579 # --- Inbound call ---
580 17 ▶ if ($event_type eq 'calling.call.receive') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_type eq "calling.call.receive") 1 16 17
581 1 $self->_handle_inbound_call($event, $inner_params);
582 1 return;
583 }
584  
585 # --- Inbound message ---
586 16 ▶ if ($event_type eq 'messaging.receive') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_type eq "messaging.receive") 2 14 16
587 2 $self->_handle_inbound_message($event);
588 2 return;
589 }
590  
591 # --- Message state ---
592 14 ▶ if ($event_type eq 'messaging.state') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_type eq "messaging.state") 4 10 14
593 4 ▶ my $message_id = $inner_params->{message_id} // '';
Condition50%1/2
$inner_params->{"message_id"} // ""
l!l
40
MC/DC0%0/1
$inner_params->{"message_id"} // ""
$inner_params->{"message_id"}
Truth table50%1/2
A
A= $inner_params->{"message_id"}
Aresult
11
00
594 4 ▶ if (my $msg = $self->_messages->{$message_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->_messages->{$message_id}) 4 0 4
595 4 $msg->dispatch_event($event);
596 4 ▶ if ($msg->completed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->completed) 2 2 4
597 2 delete $self->_messages->{$message_id};
598 }
599 }
600 4 return;
601 }
602  
603 # --- Dial completion ---
604 10 ▶ if ($event_type eq 'calling.call.dial') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_type eq "calling.call.dial") 5 5 10
605 5 $self->_handle_dial_event($event, $inner_params);
606 5 return;
607 }
608  
609 # --- State events during dial (call not registered yet) ---
610 5 ▶ my $call_id = $inner_params->{call_id} // '';
Condition50%1/2
$inner_params->{"call_id"} // ""
l!l
50
MC/DC0%0/1
$inner_params->{"call_id"} // ""
$inner_params->{"call_id"}
Truth table50%1/2
A
A= $inner_params->{"call_id"}
Aresult
11
00
611 5 ▶ my $tag = $inner_params->{tag} // '';
Condition100%2/2
$inner_params->{"tag"} // ""
l!l
23
MC/DC100%1/1
$inner_params->{"tag"} // ""
$inner_params->{"tag"}
Truth table100%2/2
A
A= $inner_params->{"tag"}
Aresult
11
00
612  
613 5 ▶ if ($event_type eq 'calling.call.state' && $tag && exists $self->_pending_dials->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_type eq "calling.call.state" and $tag and exists $self->_pending_dials->{$tag}) 2 3 5
Condition66%4/6
$event_type eq "calling.call.state" and $tag and exists $self->_pending_dials->{$tag}
!ll&&!rl&&r
302
$event_type eq "calling.call.state" and $tag
!ll&&!rl&&r
032
MC/DC33%1/3
$event_type eq "calling.call.state" and $tag and exists $self->_pending_dials->{$tag}
$event_type eq "calling.call.state" $tag exists $self->_pending_dials->{$tag}
Truth table50%2/4
A and B and C
A= $event_type eq "calling.call.state"B= $tagC= exists $self->_pending_dials->{$tag}
ABCresult
0XX0
10X0
1100
1111
614 2 ▶ if (!exists $self->_calls->{$call_id} && $call_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->_calls->{$call_id} and $call_id) 2 0 2
Condition33%1/3
not exists $self->_calls->{$call_id} and $call_id
!ll&&!rl&&r
002
MC/DC0%0/2
not exists $self->_calls->{$call_id} and $call_id
not exists $self->_calls->{$call_id} $call_id
Truth table33%1/3
not(A) and B
A= not exists $self->_calls->{$call_id}B= $call_id
ABresult
0X0
100
111
615 # Create the Call object so events route correctly
616 my $call = SignalWire::Relay::Call->new(
617 call_id => $call_id,
618 node_id => $inner_params->{node_id} // '',
619 tag => $tag,
620 device => $inner_params->{device} // {},
621 2 ▶ _client => $self,
Condition50%2/4
$inner_params->{"node_id"} // ""
l!l
20
$inner_params->{"device"} // {}
l!l
20
MC/DC0%0/1
$inner_params->{"node_id"} // ""
$inner_params->{"node_id"}
MC/DC0%0/1
$inner_params->{"device"} // {}
$inner_params->{"device"}
Truth table50%1/2
A
A= $inner_params->{"node_id"}
Aresult
11
00
Truth table50%1/2
A
A= $inner_params->{"device"}
Aresult
11
00
622 );
623 2 $self->_calls->{$call_id} = $call;
624 }
625 }
626  
627 # --- Normal routing by call_id ---
628 5 ▶ if ($call_id && (my $call = $self->_calls->{$call_id})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_id and my $call = $self->_calls->{$call_id}) 4 1 5
Condition66%2/3
$call_id and my $call = $self->_calls->{$call_id}
!ll&&!rl&&r
014
MC/DC50%1/2
$call_id and my $call = $self->_calls->{$call_id}
$call_id my $call = $self->_calls->{$call_id}
Truth table66%2/3
A and B
A= $call_idB= my $call = $self->_calls->{$call_id}
ABresult
0X0
100
111
629 4 $call->dispatch_event($event);
630  
631 # Clean up ended calls
632 4 ▶ if ($call->state eq 'ended') {
Branch: 1 branch
BranchTrueFalseTotal
if ($call->state eq "ended") 1 3 4
633 1 delete $self->_calls->{$call_id};
634 }
635 }
636 }
637  
638 # --- Internal: handle inbound call ---
639  
640 1 ▶sub _handle_inbound_call {
Subroutine _handle_inbound_call: called CC=10 SCAR=26.2
641 1 my ($self, $event, $params) = @_;
642 1 ▶ my $call_id = $params->{call_id} // '';
Condition50%1/2
$params->{"call_id"} // ""
l!l
10
MC/DC0%0/1
$params->{"call_id"} // ""
$params->{"call_id"}
Truth table50%1/2
A
A= $params->{"call_id"}
Aresult
11
00
643 1 ▶ return unless $call_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $call_id 0 1 1
644  
645 my $call = SignalWire::Relay::Call->new(
646 call_id => $call_id,
647 node_id => $params->{node_id} // '',
648 tag => $params->{tag} // '',
649 device => $params->{device} // {},
650 context => $params->{context} // '',
651 1 ▶ state => $params->{call_state} // 'ringing',
Condition50%5/10
$params->{"node_id"} // ""
l!l
10
$params->{"tag"} // ""
l!l
01
$params->{"device"} // {}
l!l
10
$params->{"context"} // ""
l!l
10
$params->{"call_state"} // "ringing"
l!l
10
MC/DC0%0/1
$params->{"node_id"} // ""
$params->{"node_id"}
MC/DC0%0/1
$params->{"tag"} // ""
$params->{"tag"}
MC/DC0%0/1
$params->{"device"} // {}
$params->{"device"}
MC/DC0%0/1
$params->{"context"} // ""
$params->{"context"}
MC/DC0%0/1
$params->{"call_state"} // "ringing"
$params->{"call_state"}
Truth table50%1/2
A
A= $params->{"node_id"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"tag"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"device"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"context"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"call_state"}
Aresult
11
00
652 _client => $self,
653 );
654 1 $self->_calls->{$call_id} = $call;
655  
656 1 ▶ if (my $cb = $self->_on_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->_on_call) 1 0 1
657 1 eval { $cb->($call) };
658 1 ▶ warn "on_call callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
659 }
660 }
661  
662 # --- Internal: handle inbound message ---
663  
664 2 ▶sub _handle_inbound_message {
Subroutine _handle_inbound_message: called CC=3 SCAR=11.3
665 2 my ($self, $event) = @_;
666  
667 2 ▶ if (my $cb = $self->_on_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->_on_message) 2 0 2
668 2 eval { $cb->($event) };
669 2 ▶ warn "on_message callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
670 }
671 }
672  
673 # --- Internal: handle dial completion event ---
674  
675 5 ▶sub _handle_dial_event {
Subroutine _handle_dial_event: called CC=11 SCAR=26.7
676 5 my ($self, $event, $params) = @_;
677 5 ▶ my $tag = $params->{tag} // '';
Condition50%1/2
$params->{"tag"} // ""
l!l
50
MC/DC0%0/1
$params->{"tag"} // ""
$params->{"tag"}
Truth table50%1/2
A
A= $params->{"tag"}
Aresult
11
00
678 5 ▶ my $dial_state = $params->{dial_state} // '';
Condition50%1/2
$params->{"dial_state"} // ""
l!l
50
MC/DC0%0/1
$params->{"dial_state"} // ""
$params->{"dial_state"}
Truth table50%1/2
A
A= $params->{"dial_state"}
Aresult
11
00
679 5 ▶ my $call_info = $params->{call} // {};
Condition100%2/2
$params->{"call"} // {}
l!l
32
MC/DC100%1/1
$params->{"call"} // {}
$params->{"call"}
Truth table100%2/2
A
A= $params->{"call"}
Aresult
11
00
680  
681 5 my $pending = $self->_pending_dials->{$tag};
682 5 ▶ return unless $pending;
Branch: 1 branch
BranchTrueFalseTotal
unless $pending 1 4 5
683  
684 4 ▶ if ($dial_state eq DIAL_STATE_ANSWERED) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dial_state eq 'answered') { } 2 2 4
elsif ($dial_state eq 'failed') { } 2 0 2
685 2 ▶ my $call_id = $call_info->{call_id} // '';
Condition50%1/2
$call_info->{"call_id"} // ""
l!l
20
MC/DC0%0/1
$call_info->{"call_id"} // ""
$call_info->{"call_id"}
Truth table50%1/2
A
A= $call_info->{"call_id"}
Aresult
11
00
686 2 my $call = $self->_calls->{$call_id};
687 2 ▶ unless ($call) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($call) 0 2 2
688 $call = SignalWire::Relay::Call->new(
689 call_id => $call_id,
690 node_id => $call_info->{node_id} // '',
691 tag => $tag,
692 device => $call_info->{device} // {},
693 0 ▶ dial_winner => 1,
Condition0%0/4
$call_info->{"node_id"} // ""
l!l
00
$call_info->{"device"} // {}
l!l
00
MC/DC0%0/1
$call_info->{"node_id"} // ""
$call_info->{"node_id"}
MC/DC0%0/1
$call_info->{"device"} // {}
$call_info->{"device"}
Truth table0%0/2
A
A= $call_info->{"node_id"}
Aresult
11
00
Truth table0%0/2
A
A= $call_info->{"device"}
Aresult
11
00
694 state => 'answered',
695 _client => $self,
696 );
697 0 $self->_calls->{$call_id} = $call;
698 }
699 2 $call->state('answered');
700 2 $call->dial_winner(1);
701 2 $pending->{resolve}->($call);
702 }
703 elsif ($dial_state eq DIAL_STATE_FAILED) {
704 2 $pending->{reject}->("Dial failed");
705 }
706 }
707  
708 # --- Internal: handle server disconnect ---
709  
710 2 ▶sub _handle_disconnect {
Subroutine _handle_disconnect: called CC=3 SCAR=11.0
711 2 my ($self, $params) = @_;
712 2 ▶ my $restart = $params->{restart} || 0;
Condition100%2/2
$params->{'restart'} || 0
l!l
11
MC/DC100%1/1
$params->{'restart'} || 0
$params->{'restart'}
Truth table100%2/2
A
A= $params->{'restart'}
Aresult
11
00
713  
714 2 ▶ if ($restart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart) 1 1 2
715 # Clear session state, connect fresh
716 1 $self->protocol('');
717 1 $self->authorization_state('');
718 }
719  
720 2 $self->connected(0);
721 # The client should reconnect (handled by the event loop)
722 }
723  
724 # --- Reconnection ---
725  
726 0 ▶sub reconnect {
Subroutine reconnect: not called CC=7 SCAR=40.3
Pod undocumented
727 0 my ($self) = @_;
728  
729 # Reject all pending requests
730 0 for my $id (keys %{$self->_pending}) {
731 0 my $p = delete $self->_pending->{$id};
732 0 ▶ $p->{reject}->("Disconnected") if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 0 0 0
733 }
734  
735 # Reject all pending dials
736 0 for my $tag (keys %{$self->_pending_dials}) {
737 0 my $p = delete $self->_pending_dials->{$tag};
738 0 ▶ $p->{reject}->("Disconnected") if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 0 0 0
739 }
740  
741 # Exponential backoff: 1s, 2s, 4s, ... up to max_backoff
742 0 my $attempts = $self->_reconnect_attempts;
743 0 my $delay = 2 ** $attempts;
744 0 ▶ $delay = $self->_max_backoff if $delay > $self->_max_backoff;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > $self->_max_backoff 0 0 0
745  
746 0 $logger->info("Reconnecting in ${delay}s (attempt " . ($attempts + 1) . ")");
747 0 select(undef, undef, undef, $delay);
748  
749 0 $self->_reconnect_attempts($attempts + 1);
750  
751 0 ▶ if ($self->connect_ws) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connect_ws) 0 0 0
752 0 return $self->authenticate;
753 }
754  
755 0 return undef;
756 }
757  
758 # --- Disconnect ---
759  
760 0 ▶sub disconnect_ws {
Subroutine disconnect_ws: not called CC=2 SCAR=17.9
Pod undocumented
761 0 my ($self) = @_;
762 0 $self->connected(0);
763 0 ▶ if ($self->_socket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_socket) 0 0 0
764 0 close($self->_socket);
765 0 $self->_socket(undef);
766 }
767 0 $self->_ws(undef);
768 }
769  
770 # --- Run event loop ---
771  
772 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
773 0 my ($self) = @_;
774 0 ▶ while ($self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connected) 0 0 0
775 0 $self->_read_once();
776 }
777 }
778  
779 3 1;
« blib/lib/SignalWire/Skills/Builtin/WebSearch.pm ↑ Summary blib/lib/SignalWire/SWAIG/FunctionResult.pm »