Client.pm

filter: Statementstmt 53.0% 131 / 247 Branchbran 27.2% 30 / 110 Conditioncond 19.6% 10 / 51 MC/DCmcdc 3.1% 1 / 32 Subroutinesub 65.9% 29 / 44 Podpod 0.0% 0 / 19 totaltotal 39.9% 201 / 503 CC 76 SCAR 31.8
CC
76
Cov
42%
ping
72.0
request_sync
56.0
read
56.0
CRAP 24.0
« Connection.pm ↑ Summary URI.pm »
1 package Net::NATS2::Client;
2  
3 5 our $VERSION = '0.3.5';
4  
5 5 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use constant CRLF => "\r\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use constant _0E0 => '0E0';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use Net::NATS2::URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Net::NATS2::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Net::NATS2::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Net::NATS2::ServerInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Net::NATS2::ConnectInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Net::NATS2::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Net::NATS2::Base qw(-no_new);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 has $_ for qw(connection server_info socket_args auto_reconnect reconnect_attempts reconnect_delay subscriptions uri current_sid message_count nkey nkey_sig_cb);
28  
29 7 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
30 7 my $class = shift;
31  
32 7 my $self = bless {@_}, $class;
33 7 ▶ $self->socket_args({}) unless defined $self->socket_args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->socket_args 5 2 7
34 7 $self->subscriptions({});
35 7 $self->current_sid(0);
36 7 $self->message_count(0);
37 7 ▶ $self->reconnect_attempts(3) unless defined $self->reconnect_attempts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reconnect_attempts 6 1 7
38 7 ▶ $self->reconnect_delay(1) unless defined $self->reconnect_delay;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reconnect_delay 5 2 7
39  
40 7 return $self;
41 }
42  
43 4 ▶sub connect {
Subroutine connect: called CC=6 SCAR=30.5
Pod undocumented
44 4 my $self = shift;
45  
46 4 ▶ my $uri = Net::NATS2::URI->new($self->uri) || return;
Condition50%1/2
'Net::NATS2::URI'->new($self->uri) || (return)
l!l
40
MC/DC0%0/1
'Net::NATS2::URI'->new($self->uri) || (return)
'Net::NATS2::URI'->new($self->uri)
Truth table50%1/2
A
A= 'Net::NATS2::URI'->new($self->uri)
Aresult
11
00
47  
48 4 $self->socket_args->{PeerAddr} = $uri->host;
49 4 $self->socket_args->{PeerPort} = $uri->port;
50  
51 4 ▶ my $connection = Net::NATS2::Connection->new(socket_args => $self->socket_args) || return;
Condition50%1/2
'Net::NATS2::Connection'->new('socket_args', $self->socket_args) || (return)
l!l
04
MC/DC0%0/1
'Net::NATS2::Connection'->new('socket_args', $self->socket_args) || (return)
'Net::NATS2::Connection'->new('socket_args', $self->socket_args)
Truth table50%1/2
A
A= 'Net::NATS2::Connection'->new('socket_args', $self->socket_args)
Aresult
11
00
52 0 $self->connection($connection);
53  
54 # Get INFO line
55 0 my ($op, @args) = $self->read_line;
56 0 my $info = Net::NATS2::ServerInfo->new(%{decode_json($args[0])});
57 0 $self->server_info($info);
58  
59 0 my $connect_info = $self->_connect_info($info, $uri);
60  
61 0 ▶ if ($info->ssl_required || $info->tls_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->ssl_required or $info->tls_required) 0 0 0
Condition0%0/3
$info->ssl_required or $info->tls_required
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->ssl_required or $info->tls_required
$info->ssl_required $info->tls_required
Truth table0%0/3
A or B
A= $info->ssl_requiredB= $info->tls_required
ABresult
1X1
011
000
62 0 ▶ $connection->upgrade() || return;
Branch: 1 branch
BranchTrueFalseTotal
$connection->upgrade or return 0 0 0
63 0 $self->connection($connection);
64 }
65  
66 0 my $connect = 'CONNECT ' . to_json($connect_info, {convert_blessed => 1});
67 0 $self->connection->send($connect);
68  
69 0 return 1;
70 }
71  
72 1 ▶sub _connect_info {
Subroutine _connect_info: called CC=9 SCAR=28.1
73 1 my ($self, $info, $uri) = @_;
74  
75 1 ▶ my $connect_info = Net::NATS2::ConnectInfo->new(
Condition33%1/3
$info->ssl_required || $info->tls_required
l!l&&r!l&&!r
001
MC/DC0%0/2
$info->ssl_required || $info->tls_required
$info->ssl_required $info->tls_required
Truth table33%1/3
A || B
A= $info->ssl_requiredB= $info->tls_required
ABresult
1X1
011
000
76 lang => 'perl',
77 version => $VERSION,
78 headers => 1,
79 tls_required => $info->ssl_required || $info->tls_required,
80 );
81  
82 1 ▶ if ($info->auth_required && !defined $self->nkey) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->auth_required and not defined $self->nkey) 0 1 1
Condition33%1/3
$info->auth_required and not defined $self->nkey
!ll&&!rl&&r
100
MC/DC0%0/2
$info->auth_required and not defined $self->nkey
$info->auth_required not defined $self->nkey
Truth table33%1/3
A and not(B)
A= $info->auth_requiredB= not defined $self->nkey
ABresult
0X0
100
111
83 0 ▶ if (!defined $uri->password) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uri->password) { } 0 0 0
84 0 $connect_info->auth_token($uri->user);
85 }
86 else {
87 0 $connect_info->user($uri->user);
88 0 $connect_info->pass($uri->password);
89 }
90 }
91  
92 1 ▶ if (defined $self->nkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->nkey) 1 0 1
93 1 ▶ die 'NKey authentication requires a server nonce' unless defined $info->nonce;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->nonce 0 1 1
94 1 ▶ die 'NKey authentication requires nkey_sig_cb' unless ref $self->nkey_sig_cb eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->nkey_sig_cb eq "CODE" 0 1 1
95  
96 1 my $signature = $self->nkey_sig_cb->($info->nonce);
97 1 ▶ die 'NKey signature callback returned undef' unless defined $signature;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $signature 0 1 1
98  
99 1 $connect_info->nkey($self->nkey);
100 1 $connect_info->sig(_base64url($signature));
101 }
102  
103 1 return $connect_info;
104 }
105  
106 1 ▶sub _base64url {
Subroutine _base64url: called CC=1 SCAR=0.0
107 1 my ($value) = @_;
108 1 $value = encode_base64($value, '');
109 1 $value =~ tr!+/!-_!;
110 1 $value =~ s/=+\z//;
111 1 return $value;
112 }
113  
114 0 ▶sub subscribe {
Subroutine subscribe: not called CC=3 SCAR=24.8
Pod undocumented
115 0 my $self = shift;
116  
117 0 my ($subject, $group, $callback);
118  
119 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
120 0 ($subject, $callback) = @_;
121 }
122 else {
123 0 ($subject, $group, $callback) = @_;
124 }
125  
126 0 my $sid = $self->next_sid;
127 0 ▶ $group = (defined $group) ? " $group" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $group ? : 0 0 0
128  
129 0 $self->connection->send("SUB $subject$group $sid");
130  
131 0 my $subscription = Net::NATS2::Subscription->new(
132 subject => $subject,
133 group => $group,
134 sid => $sid,
135 callback => $callback,
136 client => $self,
137 );
138  
139 0 $self->subscriptions->{$sid} = $subscription;
140 0 return $subscription;
141 }
142  
143 0 ▶sub unsubscribe {
Subroutine unsubscribe: not called CC=3 SCAR=24.8
Pod undocumented
144 0 my $self = shift;
145 0 my ($subscription, $max_msgs) = @_;
146  
147 0 $subscription->max_msgs($max_msgs);
148  
149 0 my $sid = $subscription->sid;
150 0 ▶ $sid .= " $max_msgs" if defined $max_msgs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_msgs 0 0 0
151  
152 0 $self->connection->send("UNSUB $sid");
153  
154 0 ▶ $self->_remove_subscription($subscription) unless defined $max_msgs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_msgs 0 0 0
155 }
156  
157 # 0:$self 1:$subject 2:$data 3:$reply_to
158 # Returns 1 on success, undef on failure
159 1 ▶sub publish {
Subroutine publish: called CC=2 SCAR=7.2
Pod undocumented
160 1 ▶ my $reply_to = defined $_[3] ? "$_[3] " : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 1 1
161 1 return $_[0]->connection->send("PUB $_[1] $reply_to" . _wire_length($_[2]) . CRLF . $_[2]);
162 }
163  
164 # Publish a message with a raw NATS header block.
165 #
166 # $headers must include the NATS header version and its terminating empty line,
167 # for example: "NATS/1.0\r\nX-Request-ID: 42\r\n\r\n".
168 # Returns 1 on success, undef on failure.
169 3 ▶sub hpublish {
Subroutine hpublish: called CC=4 SCAR=13.9
Pod undocumented
170 3 my ($self, $subject, $headers, $data, $reply_to) = @_;
171 3 ▶ return if defined $self->server_info && !$self->server_info->headers;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->server_info and not $self->server_info->headers 1 2 3
Condition66%2/3
defined $self->server_info and not $self->server_info->headers
!ll&&!rl&&r
201
MC/DC50%1/2
defined $self->server_info and not $self->server_info->headers
defined $self->server_info not $self->server_info->headers
Truth table66%2/3
A and not(B)
A= defined $self->server_infoB= not $self->server_info->headers
ABresult
0X0
100
111
172  
173 2 my $header_length = _wire_length($headers);
174 2 my $total_length = $header_length + _wire_length($data);
175 2 ▶ my $reply = defined $reply_to ? "$reply_to " : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $reply_to ? : 1 1 2
176  
177 2 return $self->connection->send("HPUB $subject $reply$header_length $total_length" . CRLF . $headers . $data);
178 }
179  
180 5 ▶sub _wire_length {
Subroutine _wire_length: called CC=2 SCAR=6.9
181 5 my $data = shift;
182 5 ▶ return utf8::is_utf8($data) ? length(encode_utf8($data)) : length($data);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($data) ? : 2 3 5
183 }
184  
185 0 ▶sub publish_with_headers {
Subroutine publish_with_headers: not called CC=1 SCAR=6.9
Pod undocumented
186 0 goto &hpublish;
187 }
188  
189 0 ▶sub request {
Subroutine request: not called CC=1 SCAR=6.9
Pod undocumented
190 0 my ($self, $subject, $data, $callback) = @_;
191  
192 0 my $inbox = new_inbox();
193 0 my $sub = $self->subscribe($inbox, $callback);
194 0 $self->unsubscribe($sub, 1);
195 0 $self->publish($subject, $data, $inbox);
196 }
197  
198 # Sends a request and waits for one response. Returns a Message on success or
199 # undef if the publish fails, the connection closes, or the timeout expires.
200 0 ▶sub request_sync {
Subroutine request_sync: not called CC=7 SCAR=40.3
Pod undocumented
201 0 my ($self, $subject, $data, $timeout) = @_;
202 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
203  
204 0 my $response;
205 0 my $inbox = new_inbox();
206 0 ▶ my $subscription = $self->subscribe($inbox, sub { ($response) = @_ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 $self->unsubscribe($subscription, 1);
208 0 ▶ return unless $self->publish($subject, $data, $inbox);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->publish($subject, $data, $inbox) 0 0 0
209  
210 0 my $deadline = time + $timeout;
211 0 ▶ while (!defined $response) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response) 0 0 0
212 0 my $remaining = $deadline - time;
213 0 ▶ last if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 0 0
214 0 ▶ last unless $self->wait_for_op($remaining);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->wait_for_op($remaining) 0 0 0
215 }
216  
217 0 ▶ $self->unsubscribe($subscription) unless defined $response;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response 0 0 0
218 0 return $response;
219 }
220  
221 2 ▶sub _reconnect {
Subroutine _reconnect: called CC=8 SCAR=22.1
222 2 my $self = shift;
223 2 ▶ return unless defined $self->auto_reconnect;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->auto_reconnect 1 1 2
224  
225 1 my $attempt = 0;
226 1 ▶ while ($self->auto_reconnect == 0 || $attempt < $self->reconnect_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_reconnect == 0 or $attempt < $self->reconnect_attempts) 2 0 2
Condition33%1/3
$self->auto_reconnect == 0 or $attempt < $self->reconnect_attempts
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->auto_reconnect == 0 or $attempt < $self->reconnect_attempts
$self->auto_reconnect == 0 $attempt < $self->reconnect_attempts
Truth table33%1/3
A or B
A= $self->auto_reconnect == 0B= $attempt < $self->reconnect_attempts
ABresult
1X1
011
000
227 2 ++$attempt;
228 2 ▶ if ($self->connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connect) 1 1 2
229 1 for my $subscription (values %{$self->subscriptions}) {
230 1 my $sid = $subscription->sid;
231 1 $self->connection->send("SUB " . $subscription->subject . $subscription->group . " $sid");
232 }
233 1 return 1;
234 }
235 1 ▶ sleep($self->reconnect_delay) if $self->auto_reconnect == 0 || $attempt < $self->reconnect_attempts;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_reconnect == 0 or $attempt < $self->reconnect_attempts 1 0 1
Condition33%1/3
$self->auto_reconnect == 0 or $attempt < $self->reconnect_attempts
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->auto_reconnect == 0 or $attempt < $self->reconnect_attempts
$self->auto_reconnect == 0 $attempt < $self->reconnect_attempts
Truth table33%1/3
A or B
A= $self->auto_reconnect == 0B= $attempt < $self->reconnect_attempts
ABresult
1X1
011
000
236 }
237 0 return;
238 }
239  
240 0 ▶sub _remove_subscription {
Subroutine _remove_subscription: not called CC=1 SCAR=6.9
241 0 delete $_[0]->subscriptions->{$_[1]->sid};
242 }
243  
244 # blocking read built upon non-blocking read
245 0 ▶sub read {
Subroutine read: not called CC=7 SCAR=40.3
Pod undocumented
246 0 my ($self, $length) = @_;
247  
248 0 my $data;
249 0 ▶ my $rv = $self->connection->nb_read($data, $length) || return; # EOF or error
Condition0%0/2
$self->connection->nb_read($data, $length) || (return)
l!l
00
MC/DC0%0/1
$self->connection->nb_read($data, $length) || (return)
$self->connection->nb_read($data, $length)
Truth table0%0/2
A
A= $self->connection->nb_read($data, $length)
Aresult
11
00
250 0 ▶ if ($rv eq _0E0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv eq "0E0") 0 0 0
251 0 ▶ while ($rv eq _0E0 && $self->connection->can_read()) { # keep tryingi until we get the data we need.
Branch: 1 branch
BranchTrueFalseTotal
if ($rv eq "0E0" and $self->connection->can_read) 0 0 0
Condition0%0/3
$rv eq "0E0" and $self->connection->can_read
!ll&&!rl&&r
000
MC/DC0%0/2
$rv eq "0E0" and $self->connection->can_read
$rv eq "0E0" $self->connection->can_read
Truth table0%0/3
A and B
A= $rv eq "0E0"B= $self->connection->can_read
ABresult
0X0
100
111
252 # EOF or error. should report error somewhere...
253 0 ▶ $rv = $self->connection->nb_read($data, $length) || return;
Condition0%0/2
$self->connection->nb_read($data, $length) || (return)
l!l
00
MC/DC0%0/1
$self->connection->nb_read($data, $length) || (return)
$self->connection->nb_read($data, $length)
Truth table0%0/2
A
A= $self->connection->nb_read($data, $length)
Aresult
11
00
254 }
255 0 ▶ return if $rv eq _0E0; # got timeout from can_read
Branch: 1 branch
BranchTrueFalseTotal
if $rv eq "0E0" 0 0 0
256 }
257 0 return _chomp($data);
258 }
259  
260 # non-blocking version of read_line. if no timeout passed, will block
261 0 ▶sub read_line {
Subroutine read_line: not called CC=7 SCAR=40.3
Pod undocumented
262 0 my ($self, $timeout) = @_;
263 0 my $line;
264  
265 0 ▶ my $rv = $self->connection->nb_getline($line) || return; # EOF or error
Condition0%0/2
$self->connection->nb_getline($line) || (return)
l!l
00
MC/DC0%0/1
$self->connection->nb_getline($line) || (return)
$self->connection->nb_getline($line)
Truth table0%0/2
A
A= $self->connection->nb_getline($line)
Aresult
11
00
266 0 ▶ if ($rv eq _0E0) { # we do not have a full line
Branch: 1 branch
BranchTrueFalseTotal
if ($rv eq "0E0") 0 0 0
267 0 ▶ while ($rv eq _0E0 && $self->connection->can_read($timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv eq "0E0" and $self->connection->can_read($timeout)) 0 0 0
Condition0%0/3
$rv eq "0E0" and $self->connection->can_read($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$rv eq "0E0" and $self->connection->can_read($timeout)
$rv eq "0E0" $self->connection->can_read($timeout)
Truth table0%0/3
A and B
A= $rv eq "0E0"B= $self->connection->can_read($timeout)
ABresult
0X0
100
111
268  
269 # EOF or error. should report error somewhere...
270 0 ▶ $rv = $self->connection->nb_getline($line) || return;
Condition0%0/2
$self->connection->nb_getline($line) || (return)
l!l
00
MC/DC0%0/1
$self->connection->nb_getline($line) || (return)
$self->connection->nb_getline($line)
Truth table0%0/2
A
A= $self->connection->nb_getline($line)
Aresult
11
00
271 }
272 0 ▶ return if $rv eq _0E0; # got timeout from can_read
Branch: 1 branch
BranchTrueFalseTotal
if $rv eq "0E0" 0 0 0
273 }
274 0 return split(' ', _chomp($line));
275 }
276  
277 0 ▶sub parse_msg {
Subroutine parse_msg: not called CC=5 SCAR=34.0
Pod undocumented
278 0 my $self = shift;
279  
280 0 my ($subject, $sid, $length, $reply_to);
281  
282 0 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) { } 0 0 0
283 0 ($subject, $sid, $length) = @_;
284 }
285 else {
286 0 ($subject, $sid, $reply_to, $length) = @_;
287 }
288  
289 0 my $data = $self->read($length + 2);
290 0 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
291 0 my $subscription = $self->subscriptions->{$sid};
292 0 my $message = Net::NATS2::Message->new(
293 subject => $subject,
294 sid => $sid,
295 reply_to => $reply_to,
296 length => $length,
297 data => $data,
298 subscription => $subscription,
299 );
300  
301 0 $subscription->message_count($subscription->message_count + 1);
302 0 $self->message_count($self->message_count + 1);
303  
304 0 ▶ if ($subscription->defined_max && $subscription->message_count >= $subscription->max_msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subscription->defined_max and $subscription->message_count >= $subscription->max_msgs) 0 0 0
Condition0%0/3
$subscription->defined_max and $subscription->message_count >= $subscription->max_msgs
!ll&&!rl&&r
000
MC/DC0%0/2
$subscription->defined_max and $subscription->message_count >= $subscription->max_msgs
$subscription->defined_max $subscription->message_count >= $subscription->max_msgs
Truth table0%0/3
A and B
A= $subscription->defined_maxB= $subscription->message_count >= $subscription->max_msgs
ABresult
0X0
100
111
305 0 $self->_remove_subscription($subscription);
306 }
307  
308 0 &{$subscription->callback}($message);
309 0 return 1;
310 }
311  
312 1 ▶sub parse_hmsg {
Subroutine parse_hmsg: called CC=5 SCAR=16.9
Pod undocumented
313 1 my $self = shift;
314  
315 1 my ($subject, $sid, $header_length, $total_length, $reply_to);
316  
317 1 ▶ if (@_ == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 4) { } 0 1 1
318 0 ($subject, $sid, $header_length, $total_length) = @_;
319 }
320 else {
321 1 ($subject, $sid, $reply_to, $header_length, $total_length) = @_;
322 }
323  
324 1 my $content = $self->read($total_length + 2);
325 1 ▶ return unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 1 1
326 1 my $headers = substr($content, 0, $header_length);
327 1 my $data = substr($content, $header_length, $total_length - $header_length);
328 1 my $subscription = $self->subscriptions->{$sid};
329 1 my $message = Net::NATS2::Message->new(
330 subject => $subject,
331 sid => $sid,
332 reply_to => $reply_to,
333 header_length => $header_length,
334 length => $total_length - $header_length,
335 headers => $headers,
336 data => $data,
337 subscription => $subscription,
338 );
339  
340 1 $subscription->message_count($subscription->message_count + 1);
341 1 $self->message_count($self->message_count + 1);
342  
343 1 ▶ if ($subscription->defined_max && $subscription->message_count >= $subscription->max_msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subscription->defined_max and $subscription->message_count >= $subscription->max_msgs) 0 1 1
Condition33%1/3
$subscription->defined_max and $subscription->message_count >= $subscription->max_msgs
!ll&&!rl&&r
100
MC/DC0%0/2
$subscription->defined_max and $subscription->message_count >= $subscription->max_msgs
$subscription->defined_max $subscription->message_count >= $subscription->max_msgs
Truth table33%1/3
A and B
A= $subscription->defined_maxB= $subscription->message_count >= $subscription->max_msgs
ABresult
0X0
100
111
344 0 $self->_remove_subscription($subscription);
345 }
346  
347 1 &{$subscription->callback}($message);
348 1 return 1;
349 }
350  
351 2 ▶sub wait_for_op {
Subroutine wait_for_op: called CC=2 SCAR=7.4
Pod undocumented
352 2 my $self = shift;
353 2 my $timeout = shift; # in seconds; can be fractional
354  
355 2 my ($op, @args) = $self->read_line($timeout);
356 2 ▶ return $self->_reconnect unless defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 2 0 2
357  
358 0 return $self->_handle_op($op, @args);
359 }
360  
361 # Sends a client PING and waits for its PONG response.
362 # Returns 1 on success and 0 if the response is not received before timeout.
363 0 ▶sub ping {
Subroutine ping: not called CC=8 SCAR=42.8
Pod undocumented
364 0 my ($self, $timeout) = @_;
365 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
366  
367 0 ▶ return 0 if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 0 0 0
368 0 ▶ return 0 unless $self->connection->send('PING');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->connection->send("PING") 0 0 0
369  
370 0 my $deadline = time + $timeout;
371 0 while (1) {
372 0 my $remaining = $deadline - time;
373 0 ▶ return 0 if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 0 0
374  
375 0 my ($op, @args) = $self->read_line($remaining);
376 0 ▶ return 0 unless defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 0 0 0
377 0 ▶ return 1 if $op eq 'PONG';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "PONG" 0 0 0
378  
379 0 ▶ return 0 unless $self->_handle_op($op, @args);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_handle_op($op, @args) 0 0 0
380 }
381 }
382  
383 1 ▶sub _handle_op {
Subroutine _handle_op: called CC=9 SCAR=38.3
384 1 my ($self, $op, @args) = @_;
385  
386 1 ▶ if ($op eq 'MSG') {
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq 'MSG') { } 0 1 1
elsif ($op eq 'HMSG') { } 1 0 1
elsif ($op eq 'PING') { } 0 0 0
elsif ($op eq 'PONG' or $op eq '+OK') { } 0 0 0
elsif ($op eq '-ERR') { } 0 0 0
387 0 ▶ return $self->parse_msg(@args) || $self->_reconnect;
Condition0%0/3
$self->parse_msg(@args) || $self->_reconnect
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->parse_msg(@args) || $self->_reconnect
$self->parse_msg(@args) $self->_reconnect
Truth table0%0/3
A || B
A= $self->parse_msg(@args)B= $self->_reconnect
ABresult
1X1
011
000
388 }
389 elsif ($op eq 'HMSG') {
390 1 ▶ return $self->parse_hmsg(@args) || $self->_reconnect;
Condition33%1/3
$self->parse_hmsg(@args) || $self->_reconnect
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->parse_hmsg(@args) || $self->_reconnect
$self->parse_hmsg(@args) $self->_reconnect
Truth table33%1/3
A || B
A= $self->parse_hmsg(@args)B= $self->_reconnect
ABresult
1X1
011
000
391 }
392 elsif ($op eq 'PING') {
393 0 $self->handle_ping;
394 }
395 ▶ elsif ($op eq 'PONG' || $op eq '+OK') {
Condition0%0/3
$op eq "PONG" or $op eq "+OK"
l!l&&r!l&&!r
000
MC/DC0%0/2
$op eq "PONG" or $op eq "+OK"
$op eq "PONG" $op eq "+OK"
Truth table0%0/3
A or B
A= $op eq "PONG"B= $op eq "+OK"
ABresult
1X1
011
000
396 }
397 elsif ($op eq '-ERR') {
398 0 return;
399 }
400 0 return 1;
401 }
402  
403 0 ▶sub _chomp {
Subroutine _chomp: not called CC=2 SCAR=17.9
404 0 my $data = shift;
405 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
406 0 local $/ = CRLF;
407 0 chomp($data);
408 }
409 0 return $data;
410 }
411  
412 0 ▶sub handle_ping {
Subroutine handle_ping: not called CC=1 SCAR=6.9
Pod undocumented
413 0 $_[0]->connection->send('PONG');
414 }
415  
416 1 ▶sub next_sid {
Subroutine next_sid: called CC=1 SCAR=0.0
Pod undocumented
417 1 my $self = shift;
418 1 $self->current_sid($self->current_sid + 1);
419 1 return $self->current_sid;
420 }
421  
422 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
423 0 $_[0]->connection->_socket->close;
424 }
425  
426 0 ▶sub new_inbox {
Subroutine new_inbox: not called CC=1 SCAR=6.9
Pod undocumented
427 0 sprintf('_INBOX.%08X%08X%06X', rand(2**32), rand(2**32), rand(2**24));
428 }
429  
430 5 1;
431  
432 __END__
« Connection.pm ↑ Summary URI.pm »