Client.pm

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