Simple.pm

filter: Statementstmt 15.5% 35 / 225 Branchbran 4.2% 6 / 140 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 18.7% 6 / 32 Podpod 100% 11 / 11 totaltotal 11.7% 58 / 492 SCAR 88.1
CC
95
Cov
10%
last_will
210.0
_send_connect
182.0
tick
156.0
CRAP 6721.4
↑ Summary Simple/SSL.pm »
1 package Net::MQTT::Simple;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.29';
9  
10 # Please note that these are not documented and are subject to change:
11 2 our $KEEPALIVE_INTERVAL = 60;
12 2 our $PING_TIMEOUT = 10;
13 2 our $RECONNECT_INTERVAL = 5;
14 2 our $MAX_LENGTH = 2097152; # 2 MB
15 2 our $READ_BYTES = 16 * 1024; # 16 kB per IO::Socket::SSL recommendation
16 2 our $WRITE_BYTES = 16 * 1024; # 16 kB per IO::Socket::SSL maximum
17 2 our $PROTOCOL_LEVEL = 0x04; # 0x03 in v3.1, 0x04 in v3.1.1
18 2 our $PROTOCOL_NAME = "MQTT"; # MQIsdp in v3.1, MQTT in v3.1.1
19  
20 2 my $global;
21  
22 0 ▶sub _default_port { 1883 }
Subroutine _default_port: not called CC=1 SCAR=6.9
23 0 ▶sub _socket_class { 'IO::Socket::IP' }
Subroutine _socket_class: not called CC=1 SCAR=6.9
24 0 ▶sub _socket_error { "$@" }
Subroutine _socket_error: not called CC=1 SCAR=6.9
25 0 ▶sub _secure { 0 }
Subroutine _secure: not called CC=1 SCAR=6.9
26  
27 0 ▶sub _client_identifier { my ($class) = @_; return "Net::MQTT::Simple[" . $class->{random_id} . "]"; }
Subroutine _client_identifier: not called CC=1 SCAR=6.9
28  
29 # Carp might not be available either.
30 sub _croak {
31 0 ▶ die sprintf "%s at %s line %d.\n", "@_", (caller 1)[1, 2];
Subroutine _croak: not called CC=1 SCAR=6.9
32 }
33  
34 sub filter_as_regex {
35 555 ▶ my ($filter) = @_;
Subroutine filter_as_regex: called CC=3 SCAR=11.0
Pod documented
36  
37 555 ▶ return "^(?!\\\$)" if $filter eq '#'; # Match everything except /^\$/
Branch: 1 branch
BranchTrueFalseTotal
if $filter eq "#" 20 535 555
38 535 ▶ return "^/" if $filter eq '/#'; # Parent (empty topic) is invalid
Branch: 1 branch
BranchTrueFalseTotal
if $filter eq "/#" 17 518 535
39  
40 518 $filter = quotemeta $filter;
41  
42 518 $filter =~ s{ \z (?<! \\ \/ \\ \# ) } {\\z}x; # Anchor unless /#$/
43 518 $filter =~ s{ \\ \/ \\ \# } {}x;
44 518 $filter =~ s{ \\ \+ } {[^/]*+}xg;
45 518 $filter =~ s{ ^ (?= \[ \^ / \] \* ) } {(?!\\\$)}x; # No /^\$/ if /^\+/
46  
47 518 return "^$filter";
48 }
49  
50 sub import {
51 2 ▶ my ($class, $server) = @_;
Subroutine import: called CC=3 SCAR=13.6
52 2 ▶ @_ <= 2 or _croak "Too many arguments for use " . __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 2 or _croak("Too many arguments for use Net::MQTT::Simple") 0 2 2
53  
54 2 ▶ $server or return;
Branch: 1 branch
BranchTrueFalseTotal
$server or return 2 0 2
55  
56 0 $global = $class->new($server);
57  
58 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 0 *{ (caller)[0] . "::publish" } = \&publish;
60 0 *{ (caller)[0] . "::retain" } = \&retain;
61 }
62  
63 sub new {
64 0 ▶ my ($class, $server, $sockopts) = @_;
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
65 0 ▶ @_ == 2 or @_ == 3 or _croak "Wrong number of arguments for $class->new";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or _croak("Wrong number of arguments for $class->new") 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
66  
67 0 my $port = $class->_default_port;
68  
69 # Add port for bare IPv6 address
70 0 ▶ $server = "[$server]:$port" if $server =~ /:.*:/ and not $server =~ /\[/;
Branch: 1 branch
BranchTrueFalseTotal
if $server =~ /:.*:/ and not $server =~ /\[/ 0 0 0
Condition0%0/3
$server =~ /:.*:/ and not $server =~ /\[/
!ll&&!rl&&r
000
MC/DC0%0/2
$server =~ /:.*:/ and not $server =~ /\[/
$server =~ /:.*:/ not $server =~ /\[/
Truth table0%0/3
A and not(B)
A= $server =~ /:.*:/B= not $server =~ /\[/
ABresult
0X0
100
111
71  
72 # Add port for bare IPv4 address or bracketed IPv6 address
73 0 ▶ $server .= ":$port" if $server !~ /:/ or $server =~ /^\[.*\]$/;
Branch: 1 branch
BranchTrueFalseTotal
if not $server =~ /:/ or $server =~ /^\[.*\]$/ 0 0 0
Condition0%0/3
not $server =~ /:/ or $server =~ /^\[.*\]$/
l!l&&r!l&&!r
000
MC/DC0%0/2
not $server =~ /:/ or $server =~ /^\[.*\]$/
not $server =~ /:/ $server =~ /^\[.*\]$/
Truth table0%0/3
not(A) or B
A= not $server =~ /:/B= $server =~ /^\[.*\]$/
ABresult
1X1
011
000
74  
75 # Create a random ID for the instance of the object
76 0 my $random_id = join "", map chr 65 + int rand 26, 1 .. 10;
77
78 0 ▶ return bless {
Condition0%0/2
$sockopts // {}
l!l
00
MC/DC0%0/1
$sockopts // {}
$sockopts
Truth table0%0/2
A
A= $sockopts
Aresult
11
00
79 server => $server,
80 last_connect => 0,
81 sockopts => $sockopts // {},
82 random_id => $random_id
83 }, $class;
84 }
85  
86 sub last_will {
87 0 ▶ my ($self, $topic, $message, $retain) = @_;
Subroutine last_will: not called CC=14 SCAR=53.5
Pod documented
88  
89 0 my %old;
90 0 ▶ %old = %{ $self->{will} } if $self->{will};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"will"} 0 0 0
91  
92 0 ▶ _croak "Wrong number of arguments for last_will" if @_ > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 4 0 0 0
93  
94 0 ▶ if (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 0 0 0
95 0 ▶ if (not defined $topic and not defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $topic || defined $message) { } 0 0 0
Condition0%0/3
defined $topic || defined $message
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $topic || defined $message
defined $topic defined $message
Truth table0%0/3
A || B
A= defined $topicB= defined $message
ABresult
1X1
011
000
96 0 delete $self->{will};
97 0 delete $self->{encoded_will};
98  
99 0 return;
100 } else {
101 $self->{will} = {
102 topic => $topic // $old{topic} // '',
103 message => $message // $old{message} // '',
104 0 ▶ retain => !!$retain // $old{retain} // 0,
Condition0%0/15
($topic // $old{"topic"}) // ""
l!l
00
$topic // $old{"topic"}
l!l&&r!l&&!r
000
($message // $old{"message"}) // ""
l!l
00
$message // $old{"message"}
l!l&&r!l&&!r
000
(!!$retain // $old{"retain"}) // 0
l!l
00
!!$retain // $old{"retain"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($topic // $old{"topic"}) // ""
$topic $old{"topic"}
MC/DC0%0/2
($message // $old{"message"}) // ""
$message $old{"message"}
MC/DC0%0/2
(!!$retain // $old{"retain"}) // 0
!!$retain $old{"retain"}
Truth table0%0/3
A // B
A= $topicB= $old{"topic"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $messageB= $old{"message"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= !!$retainB= $old{"retain"}
ABresult
1X1
011
000
105 };
106 0 ▶ _croak("Topic is empty") if not length $self->{will}->{topic};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"will"}{"topic"} 0 0 0
107  
108 0 my $e = $self->{encoded_will} = { %{ $self->{will} } };
109 0 utf8::encode($e->{topic});
110 0 ▶ utf8::downgrade($e->{message}, 1) or do {
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($e->{"message"}, 1) or do { () = (caller 1)[1, 2, 3]; warn "Wide character in $method at $file line $line.\n"; utf8::encode($e->{"message"}) } 0 0 0
111 0 my ($file, $line, $method) = (caller 1)[1, 2, 3];
112 0 warn "Wide character in $method at $file line $line.\n";
113 0 utf8::encode($e->{message});
114 };
115 }
116 }
117  
118 0 return @{ $self->{will} }{qw/topic message retain/};
119 }
120  
121 sub login {
122 0 ▶ my ($self, $username, $password) = @_;
Subroutine login: not called CC=5 SCAR=34.0
Pod documented
123  
124  
125 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
126 _croak "Password login is disabled for insecure connections"
127 if defined $password
128 0 ▶ and not $self->_secure || $ENV{MQTT_SIMPLE_ALLOW_INSECURE_LOGIN};
Branch: 1 branch
BranchTrueFalseTotal
if defined $password and not $self->_secure || $ENV{"MQTT_SIMPLE_ALLOW_INSECURE_LOGIN"} 0 0 0
Condition0%0/6
defined $password and not $self->_secure || $ENV{"MQTT_SIMPLE_ALLOW_INSECURE_LOGIN"}
!ll&&!rl&&r
000
$self->_secure || $ENV{'MQTT_SIMPLE_ALLOW_INSECURE_LOGIN'}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $password and not $self->_secure || $ENV{"MQTT_SIMPLE_ALLOW_INSECURE_LOGIN"}
defined $password $self->_secure $ENV{'MQTT_SIMPLE_ALLOW_INSECURE_LOGIN'}
Truth table0%0/4
A and not(B || C)
A= defined $passwordB= $self->_secureC= $ENV{'MQTT_SIMPLE_ALLOW_INSECURE_LOGIN'}
ABCresult
0XX0
11X0
1010
1001
129  
130 0 utf8::encode($username);
131 0 $self->{username} = $username;
132 0 $self->{password} = $password;
133 }
134  
135 0 return $username;
136 }
137  
138 sub _connect {
139 0 ▶ my ($self) = @_;
Subroutine _connect: not called CC=5 SCAR=34.0
140  
141 0 ▶ return if $self->{socket} and $self->{socket}->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} and $self->{"socket"}->connected 0 0 0
Condition0%0/3
$self->{"socket"} and $self->{"socket"}->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"socket"} and $self->{"socket"}->connected
$self->{"socket"} $self->{"socket"}->connected
Truth table0%0/3
A and B
A= $self->{"socket"}B= $self->{"socket"}->connected
ABresult
0X0
100
111
142  
143 0 ▶ if ($self->{last_connect} > time() - $RECONNECT_INTERVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last_connect"} > time - $Net::MQTT::Simple::RECONNECT_INTERVAL) 0 0 0
144 0 select undef, undef, undef, .01;
145 0 return;
146 }
147  
148 # Reset state
149 0 $self->{last_connect} = time;
150 0 $self->{buffer} = "";
151 0 delete $self->{ping};
152  
153 # Connect
154 0 my $socket_class = $self->_socket_class;
155 my %socket_options = (
156 PeerAddr => $self->{server},
157 0 %{ $self->{sockopts} }
158 );
159 0 ▶ $self->{socket} = $socket_class->new( %socket_options )
Branch: 1 branch
BranchTrueFalseTotal
$self->{"socket"} = $socket_class->new(%socket_options) or warn "$0: connect: " . $self->_socket_error . "\n" 0 0 0
160 or warn "$0: connect: " . $self->_socket_error . "\n";
161  
162 # Say hello
163 0 local $self->{skip_connect} = 1; # avoid infinite recursion :-)
164 0 $self->_send_connect;
165 0 $self->_send_subscribe;
166 }
167  
168 sub _prepend_variable_length {
169 # Copied from Net::MQTT::Constants
170 0 ▶ my ($data) = @_;
Subroutine _prepend_variable_length: not called CC=3 SCAR=24.8
171 0 my $v = length $data;
172 0 my $o = "";
173 0 my $d;
174 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$d & 128 and do { do { $d = $v % 128; $v = int $v / 128; $d |= 128 if $v; $o .= pack("C", $d) } } 0 0 0
175 0 $d = $v % 128;
176 0 $v = int($v/128);
177 0 ▶ $d |= 0x80 if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
178 0 $o .= pack "C", $d;
179 } while $d & 0x80;
180 0 return "$o$data";
181 }
182  
183 sub _send {
184 0 ▶ my ($self, $data) = @_;
Subroutine _send: not called CC=5 SCAR=34.0
185  
186 0 ▶ $self->_connect unless exists $self->{skip_connect};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"skip_connect"} 0 0 0
187 0 delete $self->{skip_connect};
188  
189 0 ▶ my $socket = $self->{socket} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $socket = $self->{"socket"} or return 0 0 0
190  
191 0 ▶ while (my $chunk = substr $data, 0, $WRITE_BYTES, "") {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chunk = substr($data, 0, $Net::MQTT::Simple::WRITE_BYTES, '')) 0 0 0
192 0 ▶ syswrite $socket, $chunk
Branch: 1 branch
BranchTrueFalseTotal
syswrite $socket, $chunk or $self->_drop_connection 0 0 0
193 or $self->_drop_connection; # reconnect on next message
194 }
195  
196 0 $self->{last_send} = time;
197 }
198  
199 sub _send_connect {
200 0 ▶ my ($self) = @_;
Subroutine _send_connect: not called CC=13 SCAR=52.0
201  
202 0 my $will = $self->{encoded_will};
203 0 my $flags = 0x02;
204 0 ▶ $flags |= 0x04 if $will;
Branch: 1 branch
BranchTrueFalseTotal
if $will 0 0 0
205 0 ▶ $flags |= 0x20 if $will and $will->{retain};
Branch: 1 branch
BranchTrueFalseTotal
if $will and $will->{"retain"} 0 0 0
Condition0%0/3
$will and $will->{"retain"}
!ll&&!rl&&r
000
MC/DC0%0/2
$will and $will->{"retain"}
$will $will->{"retain"}
Truth table0%0/3
A and B
A= $willB= $will->{"retain"}
ABresult
0X0
100
111
206  
207 0 ▶ $flags |= 0x80 if defined $self->{username};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"username"} 0 0 0
208 0 ▶ $flags |= 0x40 if defined $self->{username} and defined $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"username"} and defined $self->{"password"} 0 0 0
Condition0%0/3
defined $self->{"username"} and defined $self->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"username"} and defined $self->{"password"}
defined $self->{"username"} defined $self->{"password"}
Truth table0%0/3
A and B
A= defined $self->{"username"}B= defined $self->{"password"}
ABresult
0X0
100
111
209  
210 $self->_send("\x10" . _prepend_variable_length(pack(
211 "x C/a* C C n n/a*"
212 . ($flags & 0x04 ? "n/a* n/a*" : "")
213 . ($flags & 0x80 ? "n/a*" : "")
214 . ($flags & 0x40 ? "n/a*" : ""),
215 $PROTOCOL_NAME,
216 $PROTOCOL_LEVEL,
217 $flags,
218 $KEEPALIVE_INTERVAL,
219 $self->_client_identifier,
220 ($flags & 0x04 ? ($will->{topic}, $will->{message}) : ()),
221 ($flags & 0x80 ? $self->{username} : ()),
222 0 ▶ ($flags & 0x40 ? $self->{password} : ()),
Branch: 6 branches
BranchTrueFalseTotal
$flags & 4 ? : 0 0 0
$flags & 128 ? : 0 0 0
$flags & 64 ? : 0 0 0
$flags & 4 ? : 0 0 0
$flags & 128 ? : 0 0 0
$flags & 64 ? : 0 0 0
223 )));
224 }
225  
226 sub _send_subscribe {
227 0 ▶ my ($self, @topics) = @_;
Subroutine _send_subscribe: not called CC=5 SCAR=34.0
228  
229 0 ▶ if (not @topics) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@topics) 0 0 0
230 0 ▶ @topics = keys %{ $self->{sub} } or return;
Branch: 1 branch
BranchTrueFalseTotal
@topics = keys %{$$self{"sub"};} or return 0 0 0
231 }
232 0 ▶ return if not @topics;
Branch: 1 branch
BranchTrueFalseTotal
unless @topics 0 0 0
233  
234 0 utf8::encode($_) for @topics;
235  
236 # Hardcoded "packet identifier" \0\x01 for now (was \0\0 but the spec
237 # disallows it for subscribe packets and mosquitto started enforcing that.)
238 0 $self->_send("\x82" . _prepend_variable_length("\0\x01" .
239 pack("(n/a* x)*", @topics) # x = QoS 0
240 ));
241 }
242  
243 sub _send_unsubscribe {
244 0 ▶ my ($self, @topics) = @_;
Subroutine _send_unsubscribe: not called CC=3 SCAR=24.8
245  
246 0 ▶ return if not @topics;
Branch: 1 branch
BranchTrueFalseTotal
unless @topics 0 0 0
247  
248 0 utf8::encode($_) for @topics;
249  
250 # Hardcoded "packet identifier" \0\0x01 for now; see above.
251 0 $self->_send("\xa2" . _prepend_variable_length("\0\x01" .
252 pack("(n/a*)*", @topics)
253 ));
254 }
255  
256 sub _parse {
257 0 ▶ my ($self) = @_;
Subroutine _parse: not called CC=6 SCAR=37.4
258  
259 0 my $bufref = \$self->{buffer};
260  
261 0 ▶ return if length $$bufref < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $$bufref < 2 0 0 0
262  
263 0 my $offset = 1;
264  
265 0 my $length = do {
266 0 my $multiplier = 1;
267 0 my $v = 0;
268 0 my $d;
269 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$d & 128 and do { do { return if $offset >= length $$bufref; $d = unpack("C", substr($$bufref, $offset++, 1)); $v += ($d & 127) * $multiplier; $multiplier *= 128 } } 0 0 0
270 0 ▶ return if $offset >= length $$bufref; # not enough data yet
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= length $$bufref 0 0 0
271 0 $d = unpack "C", substr $$bufref, $offset++, 1;
272 0 $v += ($d & 0x7f) * $multiplier;
273 0 $multiplier *= 128;
274 } while ($d & 0x80);
275 0 $v;
276 };
277  
278 0 ▶ if ($length > $MAX_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > $Net::MQTT::Simple::MAX_LENGTH) 0 0 0
279 # On receiving an enormous packet, just disconnect to avoid exhausting
280 # RAM on tiny systems.
281 # TODO: just slurp and drop the data
282 0 $self->_drop_connection;
283 0 return;
284 }
285  
286 0 ▶ return if length($$bufref) < $offset + $length; # not enough data yet
Branch: 1 branch
BranchTrueFalseTotal
if length $$bufref < $offset + $length 0 0 0
287  
288 0 my $first_byte = unpack "C", substr $$bufref, 0, 1;
289  
290 0 my $packet = {
291 type => ($first_byte & 0xF0) >> 4,
292 dup => ($first_byte & 0x08) >> 3,
293 qos => ($first_byte & 0x06) >> 1,
294 retain => ($first_byte & 0x01),
295 data => substr($$bufref, $offset, $length),
296 };
297  
298 0 substr $$bufref, 0, $offset + $length, ""; # remove the parsed bits.
299  
300 0 return $packet;
301 }
302  
303 sub _incoming_publish {
304 0 ▶ my ($self, $packet) = @_;
Subroutine _incoming_publish: not called CC=3 SCAR=24.8
305  
306 # Because QoS is not supported, no packed ID in the data. It would
307 # have been 16 bits between $topic and $message.
308 0 my ($topic, $message) = unpack "n/a a*", $packet->{data};
309  
310 0 utf8::decode($topic);
311  
312 0 for my $cb (@{ $self->{callbacks} }) {
313 0 ▶ if ($topic =~ /$cb->{regex}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topic =~ /$cb->{"regex"}/) 0 0 0
314 0 $cb->{callback}->($topic, $message, $packet->{retain});
315 0 return;
316 }
317 }
318 }
319  
320 sub _publish {
321 0 ▶ my ($self, $retain, $topic, $message) = @_;
Subroutine _publish: not called CC=5 SCAR=34.0
322  
323 0 ▶ $message //= "" if $retain;
Branch: 1 branch
BranchTrueFalseTotal
if $retain 0 0 0
Condition0%0/2
$message //= ""
l!l
00
MC/DC0%0/1
$message //= ""
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
324  
325 0 utf8::encode($topic);
326 0 ▶ utf8::downgrade($message, 1) or do {
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($message, 1) or do { () = (caller 1)[1, 2, 3]; warn "Wide character in $method at $file line $line.\n"; utf8::encode($message) } 0 0 0
327 0 my ($file, $line, $method) = (caller 1)[1, 2, 3];
328 0 warn "Wide character in $method at $file line $line.\n";
329 0 utf8::encode($message);
330 };
331  
332 0 ▶ $self->_send(
Branch: 1 branch
BranchTrueFalseTotal
$retain ? : 0 0 0
333 ($retain ? "\x31" : "\x30")
334 . _prepend_variable_length(
335 pack("n/a*", $topic) . $message
336 )
337 );
338 }
339  
340 sub publish {
341 0 ▶ my $method = UNIVERSAL::isa($_[0], __PACKAGE__);
Subroutine publish: not called CC=4 SCAR=30.0
Pod documented
342 0 ▶ @_ == ($method ? 3 : 2) or _croak "Wrong number of arguments for publish";
Branch: 2 branches
BranchTrueFalseTotal
@_ == ($method ? 3 : 2) or _croak("Wrong number of arguments for publish") 0 0 0
$method ? : 0 0 0
343  
344 0 ▶ ($method ? shift : $global)->_publish(0, @_);
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 0 0 0
345 }
346  
347 sub retain {
348 0 ▶ my $method = UNIVERSAL::isa($_[0], __PACKAGE__);
Subroutine retain: not called CC=4 SCAR=30.0
Pod documented
349 0 ▶ @_ == ($method ? 3 : 2) or _croak "Wrong number of arguments for retain";
Branch: 2 branches
BranchTrueFalseTotal
@_ == ($method ? 3 : 2) or _croak("Wrong number of arguments for retain") 0 0 0
$method ? : 0 0 0
350  
351 0 ▶ ($method ? shift : $global)->_publish(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 0 0 0
352 }
353  
354 sub run {
355 0 ▶ my ($self, @subscribe_args) = @_;
Subroutine run: not called CC=6 SCAR=37.4
Pod documented
356  
357 0 ▶ $self->subscribe(@subscribe_args) if @subscribe_args;
Branch: 1 branch
BranchTrueFalseTotal
if @subscribe_args 0 0 0
358  
359 0 ▶ until ($self->{stop_loop}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"stop_loop"}) 0 0 0
360 0 my @timeouts;
361 push @timeouts, $KEEPALIVE_INTERVAL - (time() - $self->{last_send})
362 0 ▶ if exists $self->{last_send};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"last_send"} 0 0 0
363 push @timeouts, $PING_TIMEOUT - (time() - $self->{ping})
364 0 ▶ if exists $self->{ping};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ping"} 0 0 0
365  
366 my $timeout = @timeouts
367 0 ▶ ? (sort { $a <=> $b } @timeouts)[0]
Branch: 1 branch
BranchTrueFalseTotal
@timeouts ? : 0 0 0
368 : 1; # default to 1
369  
370 0 $self->tick($timeout);
371 }
372  
373 0 delete $self->{stop_loop};
374 }
375  
376 sub subscribe {
377 0 ▶ my ($self, @kv) = @_;
Subroutine subscribe: not called CC=3 SCAR=24.8
Pod documented
378  
379 0 ▶ while (my ($topic, $callback) = splice @kv, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@kv, 0, 2)) 0 0 0
380 0 $self->{sub}->{ $topic } = 1;
381 0 push @{ $self->{callbacks} }, {
382 topic => $topic,
383 regex => filter_as_regex($topic),
384 callback => $callback,
385 };
386 }
387  
388 0 ▶ $self->_send_subscribe() if $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} 0 0 0
389 }
390  
391 sub unsubscribe {
392 0 ▶ my ($self, @topics) = @_;
Subroutine unsubscribe: not called CC=2 SCAR=17.9
Pod documented
393  
394 0 $self->_send_unsubscribe(@topics);
395  
396 0 my $cb = $self->{callbacks};
397 0 for my $topic ( @topics ) {
398 0 @$cb = grep {$_->{topic} ne $topic} @$cb;
399 }
400  
401 0 delete @{ $self->{sub} }{ @topics };
402 }
403  
404 sub tick {
405 0 ▶ my ($self, $timeout) = @_;
Subroutine tick: not called CC=12 SCAR=50.5
Pod documented
406  
407 0 $self->_connect;
408  
409 0 ▶ my $socket = $self->{socket} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $socket = $self->{"socket"} or return 0 0 0
410 0 my $bufref = \$self->{buffer};
411  
412 0 my $r = '';
413 0 vec($r, fileno($socket), 1) = 1;
414  
415 0 ▶ if (select($r, undef, undef, $timeout // 0) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (select($r, undef, undef, $timeout // 0) > 0) 0 0 0
Condition0%0/2
$timeout // 0
l!l
00
MC/DC0%0/1
$timeout // 0
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
416 sysread $socket, $$bufref, $READ_BYTES, length $$bufref
417 0 ▶ or delete $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
sysread $socket, $$bufref, $Net::MQTT::Simple::READ_BYTES, length $$bufref or delete $self->{"socket"} 0 0 0
418  
419 0 ▶ while (length $$bufref) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$bufref) 0 0 0
420 0 ▶ my $packet = $self->_parse() or last;
Branch: 1 branch
BranchTrueFalseTotal
my $packet = $self->_parse or last 0 0 0
421 0 ▶ $self->_incoming_publish($packet) if $packet->{type} == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $packet->{"type"} == 3 0 0 0
422 0 ▶ delete $self->{ping} if $packet->{type} == 13;
Branch: 1 branch
BranchTrueFalseTotal
if $packet->{"type"} == 13 0 0 0
423 }
424 }
425  
426 0 ▶ if (time() >= $self->{last_send} + $KEEPALIVE_INTERVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (time >= $self->{"last_send"} + $Net::MQTT::Simple::KEEPALIVE_INTERVAL) 0 0 0
427 0 $self->_send("\xc0\0"); # PINGREQ
428 0 $self->{ping} = time;
429 }
430 0 ▶ if ($self->{ping} and time() >= $self->{ping} + $PING_TIMEOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ping"} and time >= $self->{"ping"} + $Net::MQTT::Simple::PING_TIMEOUT) 0 0 0
Condition0%0/3
$self->{"ping"} and time >= $self->{"ping"} + $Net::MQTT::Simple::PING_TIMEOUT
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"ping"} and time >= $self->{"ping"} + $Net::MQTT::Simple::PING_TIMEOUT
$self->{"ping"} time >= $self->{"ping"} + $Net::MQTT::Simple::PING_TIMEOUT
Truth table0%0/3
A and B
A= $self->{"ping"}B= time >= $self->{"ping"} + $Net::MQTT::Simple::PING_TIMEOUT
ABresult
0X0
100
111
431 0 $self->_drop_connection;
432 }
433  
434 0 return !! $self->{socket};
435 }
436  
437 sub disconnect {
438 0 ▶ my ($self) = @_;
Subroutine disconnect: not called CC=3 SCAR=24.8
Pod documented
439  
440 $self->_send(pack "C x", 0xe0)
441 0 ▶ if $self->{socket} and $self->{socket}->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} and $self->{"socket"}->connected 0 0 0
Condition0%0/3
$self->{"socket"} and $self->{"socket"}->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"socket"} and $self->{"socket"}->connected
$self->{"socket"} $self->{"socket"}->connected
Truth table0%0/3
A and B
A= $self->{"socket"}B= $self->{"socket"}->connected
ABresult
0X0
100
111
442  
443 0 $self->_drop_connection;
444 }
445  
446 sub _drop_connection {
447 0 ▶ my ($self) = @_;
Subroutine _drop_connection: not called CC=1 SCAR=6.9
448  
449 0 delete $self->{socket};
450 0 $self->{last_connect} = 0;
451 }
452  
453 2 1;
454  
455 __END__
↑ Summary Simple/SSL.pm »