| 1 | package Net::MQTT::Simple; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # use strict; # might not be available (e.g. on openwrt) | ||||||||||||||||||||||||||||
| 4 | # use warnings; # same. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '1.21'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # Please note that these are not documented and are subject to change: | ||||||||||||||||||||||||||||
| 9 | 2 | our $KEEPALIVE_INTERVAL = 60; | |||||||||||||||||||||||||||
| 10 | 2 | our $PING_TIMEOUT = 10; | |||||||||||||||||||||||||||
| 11 | 2 | our $RECONNECT_INTERVAL = 5; | |||||||||||||||||||||||||||
| 12 | 2 | our $MAX_LENGTH = 2097152; # 2 MB | |||||||||||||||||||||||||||
| 13 | 2 | our $READ_BYTES = 16 * 1024; # 16 kB per IO::Socket::SSL recommendation | |||||||||||||||||||||||||||
| 14 | 2 | our $PROTOCOL_NAME = "MQIsdp"; # MQIsdp in v3.1, MQTT in v3.1.1 | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | my $global; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | BEGIN { | ||||||||||||||||||||||||||||
| 19 | *_socket_class = | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | eval { require IO::Socket::IP; 1 } ? sub { "IO::Socket::IP" } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | : eval { require IO::Socket::INET; 1 } ? sub { "IO::Socket::INET" } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | : die "Neither IO::Socket::IP nor IO::Socket::INET found"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub _default_port { 1883 } | ||||||||||||||||||||||||||
|
Subroutine
_default_port: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub _socket_error { "$@" } | ||||||||||||||||||||||||||
|
Subroutine
_socket_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub _client_identifier { "Net::MQTT::Simple[$$]" } | ||||||||||||||||||||||||||
|
Subroutine
_client_identifier: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Carp might not be available either. | ||||||||||||||||||||||||||||
| 31 | sub _croak { | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | die sprintf "%s at %s line %d.\n", "@_", (caller 1)[1, 2]; | ||||||||||||||||||||||||||
|
Subroutine
_croak: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub filter_as_regex { | ||||||||||||||||||||||||||||
| 36 | 555 | ▶ | my ($filter) = @_; | ||||||||||||||||||||||||||
|
Subroutine
filter_as_regex: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 555 | ▶ | return "^(?!\\\$)" if $filter eq '#'; # Match everything except /^\$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 535 | ▶ | return "^/" if $filter eq '/#'; # Parent (empty topic) is invalid | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 518 | $filter = quotemeta $filter; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 518 | $filter =~ s{ \z (?<! \\ \/ \\ \# ) } {\\z}x; # Anchor unless /#$/ | |||||||||||||||||||||||||||
| 44 | 518 | $filter =~ s{ \\ \/ \\ \# } {}x; | |||||||||||||||||||||||||||
| 45 | 518 | $filter =~ s{ \\ \+ } {[^/]*+}xg; | |||||||||||||||||||||||||||
| 46 | 518 | $filter =~ s{ ^ (?= \[ \^ / \] \* ) } {(?!\\\$)}x; # No /^\$/ if /^\+/ | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 518 | return "^$filter"; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub import { | ||||||||||||||||||||||||||||
| 52 | 2 | ▶ | my ($class, $server) = @_; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=15.7
| |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | @_ <= 2 or _croak "Too many arguments for use " . __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | $server or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | $global = $class->new($server); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 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
Condition0%0/3
@_ == 2 or @_ == 3
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2
@_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
| |||||||||||||||||||||||||||||
| 66 | 0 | my $port = $class->_default_port; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Add port for bare IPv6 address | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $server = "[$server]:$port" if $server =~ /:.*:/ and not $server =~ /\[/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$server =~ /:.*:/ and not $server =~ /\[/
MC/DC0%0/2
$server =~ /:.*:/ and not $server =~ /\[/
$server =~ /:.*:/
not $server =~ /\[/
Truth table0%0/3
A and not(B)
A= $server =~ /:.*:/B= not $server =~ /\[/
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Add port for bare IPv4 address or bracketed IPv6 address | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $server .= ":$port" if $server !~ /:/ or $server =~ /^\[.*\]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $server =~ /:/ or $server =~ /^\[.*\]$/
MC/DC0%0/2
not $server =~ /:/ or $server =~ /^\[.*\]$/
not $server =~ /:/
$server =~ /^\[.*\]$/
Truth table0%0/3
not(A) or B
A= not $server =~ /:/B= $server =~ /^\[.*\]$/
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return bless { | ||||||||||||||||||||||||||
|
Condition0%0/2
$sockopts // {}
MC/DC0%0/1
$sockopts // {}
$sockopts
Truth table0%0/2
A
A= $sockopts
| |||||||||||||||||||||||||||||
| 75 | server => $server, | ||||||||||||||||||||||||||||
| 76 | last_connect => 0, | ||||||||||||||||||||||||||||
| 77 | sockopts => $sockopts // {}, | ||||||||||||||||||||||||||||
| 78 | }, $class; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub _connect { | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_connect: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | return if $self->{socket} and $self->{socket}->connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"socket"} and $self->{"socket"}->connected
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
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($self->{last_connect} > time() - $RECONNECT_INTERVAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | select undef, undef, undef, .01; | |||||||||||||||||||||||||||
| 88 | 0 | return; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Reset state | ||||||||||||||||||||||||||||
| 92 | 0 | $self->{last_connect} = time; | |||||||||||||||||||||||||||
| 93 | 0 | $self->{buffer} = ""; | |||||||||||||||||||||||||||
| 94 | 0 | delete $self->{ping}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Connect | ||||||||||||||||||||||||||||
| 97 | 0 | my $socket_class = $self->_socket_class; | |||||||||||||||||||||||||||
| 98 | my %socket_options = ( | ||||||||||||||||||||||||||||
| 99 | PeerAddr => $self->{server}, | ||||||||||||||||||||||||||||
| 100 | 0 | %{ $self->{sockopts} } | |||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $self->{socket} = $socket_class->new( %socket_options ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | or warn "$0: connect: " . $self->_socket_error . "\n"; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Say hello | ||||||||||||||||||||||||||||
| 106 | 0 | local $self->{skip_connect} = 1; # avoid infinite recursion :-) | |||||||||||||||||||||||||||
| 107 | 0 | $self->_send_connect; | |||||||||||||||||||||||||||
| 108 | 0 | $self->_send_subscribe; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub _prepend_variable_length { | ||||||||||||||||||||||||||||
| 112 | # Copied from Net::MQTT::Constants | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_prepend_variable_length: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 114 | 0 | my $v = length $data; | |||||||||||||||||||||||||||
| 115 | 0 | my $o = ""; | |||||||||||||||||||||||||||
| 116 | 0 | my $d; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $d = $v % 128; | |||||||||||||||||||||||||||
| 119 | 0 | $v = int($v/128); | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | $d |= 0x80 if $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | $o .= pack "C", $d; | |||||||||||||||||||||||||||
| 122 | } while $d & 0x80; | ||||||||||||||||||||||||||||
| 123 | 0 | return "$o$data"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub _send { | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_send: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $self->_connect unless exists $self->{skip_connect}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $socket = $self->{socket} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | syswrite $socket, $data | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | or delete $self->{socket}; # reconnect on next message | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | $self->{last_send} = time; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub _send_connect { | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_send_connect: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | $self->_send("\x10" . _prepend_variable_length(pack( | |||||||||||||||||||||||||||
| 142 | "x C/a* C C n n/a*", | ||||||||||||||||||||||||||||
| 143 | $PROTOCOL_NAME, | ||||||||||||||||||||||||||||
| 144 | 0x03, | ||||||||||||||||||||||||||||
| 145 | 0x02, | ||||||||||||||||||||||||||||
| 146 | $KEEPALIVE_INTERVAL, | ||||||||||||||||||||||||||||
| 147 | $self->_client_identifier | ||||||||||||||||||||||||||||
| 148 | ))); | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub _send_subscribe { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my ($self, @topics) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_send_subscribe: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if (not @topics) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | @topics = keys %{ $self->{sub} } or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return if not @topics; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | utf8::encode($_) for @topics; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Hardcoded "packet identifier" \0\0 for now. | ||||||||||||||||||||||||||||
| 162 | 0 | $self->_send("\x82" . _prepend_variable_length("\0\0" . | |||||||||||||||||||||||||||
| 163 | pack("(n/a* x)*", @topics) # x = QoS 0 | ||||||||||||||||||||||||||||
| 164 | )); | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | sub _send_unsubscribe { | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | my ($self, @topics) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_send_unsubscribe: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | return if not @topics; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | utf8::encode($_) for @topics; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # Hardcoded "packet identifier" \0\0 for now. | ||||||||||||||||||||||||||||
| 175 | 0 | $self->_send("\xa2" . _prepend_variable_length("\0\0" . | |||||||||||||||||||||||||||
| 176 | pack("(n/a*)*", @topics) | ||||||||||||||||||||||||||||
| 177 | )); | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | sub _parse { | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | my $bufref = \$self->{buffer}; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | return if length $$bufref < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | my $offset = 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | my $length = do { | |||||||||||||||||||||||||||
| 190 | 0 | my $multiplier = 1; | |||||||||||||||||||||||||||
| 191 | 0 | my $v = 0; | |||||||||||||||||||||||||||
| 192 | 0 | my $d; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | return if $offset >= length $$bufref; # not enough data yet | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | $d = unpack "C", substr $$bufref, $offset++, 1; | |||||||||||||||||||||||||||
| 196 | 0 | $v += ($d & 0x7f) * $multiplier; | |||||||||||||||||||||||||||
| 197 | 0 | $multiplier *= 128; | |||||||||||||||||||||||||||
| 198 | } while ($d & 0x80); | ||||||||||||||||||||||||||||
| 199 | 0 | $v; | |||||||||||||||||||||||||||
| 200 | }; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ($length > $MAX_LENGTH) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | # On receiving an enormous packet, just disconnect to avoid exhausting | ||||||||||||||||||||||||||||
| 204 | # RAM on tiny systems. | ||||||||||||||||||||||||||||
| 205 | # TODO: just slurp and drop the data | ||||||||||||||||||||||||||||
| 206 | 0 | delete $self->{socket}; | |||||||||||||||||||||||||||
| 207 | 0 | return; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | return if $length > (length $$bufref) + $offset; # not enough data yet | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | my $first_byte = unpack "C", substr $$bufref, 0, 1; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | my $packet = { | |||||||||||||||||||||||||||
| 215 | type => ($first_byte & 0xF0) >> 4, | ||||||||||||||||||||||||||||
| 216 | dup => ($first_byte & 0x08) >> 3, | ||||||||||||||||||||||||||||
| 217 | qos => ($first_byte & 0x06) >> 1, | ||||||||||||||||||||||||||||
| 218 | retain => ($first_byte & 0x01), | ||||||||||||||||||||||||||||
| 219 | data => substr($$bufref, $offset, $length), | ||||||||||||||||||||||||||||
| 220 | }; | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | substr $$bufref, 0, $offset + $length, ""; # remove the parsed bits. | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | return $packet; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | sub _incoming_publish { | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my ($self, $packet) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_incoming_publish: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # Because QoS is not supported, no packed ID in the data. It would | ||||||||||||||||||||||||||||
| 231 | # have been 16 bits between $topic and $message. | ||||||||||||||||||||||||||||
| 232 | 0 | my ($topic, $message) = unpack "n/a a*", $packet->{data}; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | utf8::decode($topic); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | for my $cb (@{ $self->{callbacks} }) { | |||||||||||||||||||||||||||
| 237 | 0 | ▶ | if ($topic =~ /$cb->{regex}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | $cb->{callback}->($topic, $message, $packet->{retain}); | |||||||||||||||||||||||||||
| 239 | 0 | return; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | sub _publish { | ||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my ($self, $retain, $topic, $message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_publish: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | ▶ | $message //= "" if $retain; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$message //= ""
MC/DC0%0/1
$message //= ""
$message
Truth table0%0/2
A
A= $message
| |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | utf8::encode($topic); | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | utf8::downgrade($message, 1) or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 0 | my ($file, $line, $method) = (caller 1)[1, 2, 3]; | |||||||||||||||||||||||||||
| 252 | 0 | warn "Wide character in $method at $file line $line.\n"; | |||||||||||||||||||||||||||
| 253 | 0 | utf8::encode($message); | |||||||||||||||||||||||||||
| 254 | }; | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | ▶ | $self->_send( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | ($retain ? "\x31" : "\x30") | ||||||||||||||||||||||||||||
| 258 | . _prepend_variable_length( | ||||||||||||||||||||||||||||
| 259 | pack("n/a*", $topic) . $message | ||||||||||||||||||||||||||||
| 260 | ) | ||||||||||||||||||||||||||||
| 261 | ); | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | sub publish { | ||||||||||||||||||||||||||||
| 265 | 0 | ▶ | my $method = UNIVERSAL::isa($_[0], __PACKAGE__); | ||||||||||||||||||||||||||
|
Subroutine
publish: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | @_ == ($method ? 3 : 2) or _croak "Wrong number of arguments for publish"; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | ($method ? shift : $global)->_publish(0, @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | sub retain { | ||||||||||||||||||||||||||||
| 272 | 0 | ▶ | my $method = UNIVERSAL::isa($_[0], __PACKAGE__); | ||||||||||||||||||||||||||
|
Subroutine
retain: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | @_ == ($method ? 3 : 2) or _croak "Wrong number of arguments for retain"; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | ($method ? shift : $global)->_publish(1, @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | sub run { | ||||||||||||||||||||||||||||
| 279 | 0 | ▶ | my ($self, @subscribe_args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | ▶ | $self->subscribe(@subscribe_args) if @subscribe_args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 0 | ▶ | until ($self->{stop_loop}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | my @timeouts; | |||||||||||||||||||||||||||
| 285 | push @timeouts, $KEEPALIVE_INTERVAL - (time() - $self->{last_send}) | ||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if exists $self->{last_send}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | push @timeouts, $PING_TIMEOUT - (time() - $self->{ping}) | ||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if exists $self->{ping}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | my $timeout = @timeouts | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | ? (sort { $a <=> $b } @timeouts)[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | : 1; # default to 1 | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | $self->tick($timeout); | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | delete $self->{stop_loop}; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | sub subscribe { | ||||||||||||||||||||||||||||
| 301 | 0 | ▶ | my ($self, @kv) = @_; | ||||||||||||||||||||||||||
|
Subroutine
subscribe: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | ▶ | while (my ($topic, $callback) = splice @kv, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 0 | $self->{sub}->{ $topic } = 1; | |||||||||||||||||||||||||||
| 305 | 0 | push @{ $self->{callbacks} }, { | |||||||||||||||||||||||||||
| 306 | topic => $topic, | ||||||||||||||||||||||||||||
| 307 | regex => filter_as_regex($topic), | ||||||||||||||||||||||||||||
| 308 | callback => $callback, | ||||||||||||||||||||||||||||
| 309 | }; | ||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 0 | ▶ | $self->_send_subscribe() if $self->{socket}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | sub unsubscribe { | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | my ($self, @topics) = @_; | ||||||||||||||||||||||||||
|
Subroutine
unsubscribe: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 0 | $self->_send_unsubscribe(@topics); | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 0 | my $cb = $self->{callbacks}; | |||||||||||||||||||||||||||
| 321 | @$cb = grep $_->{topic} ne $_, @$cb | ||||||||||||||||||||||||||||
| 322 | 0 | for @topics; | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 0 | delete @{ $self->{sub} }{ @topics }; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | sub tick { | ||||||||||||||||||||||||||||
| 328 | 0 | ▶ | my ($self, $timeout) = @_; | ||||||||||||||||||||||||||
|
Subroutine
tick: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 0 | $self->_connect; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | ▶ | my $socket = $self->{socket} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 0 | my $bufref = \$self->{buffer}; | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 0 | my $r = ''; | |||||||||||||||||||||||||||
| 336 | 0 | vec($r, fileno($socket), 1) = 1; | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 0 | ▶ | if (select $r, undef, undef, $timeout // 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$timeout // 0
MC/DC0%0/1
$timeout // 0
$timeout
Truth table0%0/2
A
A= $timeout
| |||||||||||||||||||||||||||||
| 339 | sysread $socket, $$bufref, $READ_BYTES, length $$bufref | ||||||||||||||||||||||||||||
| 340 | 0 | ▶ | or delete $self->{socket}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | ▶ | while (length $$bufref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 0 | ▶ | my $packet = $self->_parse() or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 0 | ▶ | $self->_incoming_publish($packet) if $packet->{type} == 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | delete $self->{ping} if $packet->{type} == 13; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if (time() >= $self->{last_send} + $KEEPALIVE_INTERVAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 0 | $self->_send("\xc0\0"); # PINGREQ | |||||||||||||||||||||||||||
| 351 | 0 | $self->{ping} = time; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | 0 | ▶ | if ($self->{ping} and time() >= $self->{ping} + $PING_TIMEOUT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"ping"} and time >= $self->{"ping"} + $PING_TIMEOUT
MC/DC0%0/2
$self->{"ping"} and time >= $self->{"ping"} + $PING_TIMEOUT
$self->{"ping"}
time >= $self->{"ping"} + $PING_TIMEOUT
Truth table0%0/3
A and B
A= $self->{"ping"}B= time >= $self->{"ping"} + $PING_TIMEOUT
| |||||||||||||||||||||||||||||
| 354 | 0 | delete $self->{socket}; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | return !! $self->{socket}; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 2 | 1; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | __END__ | ||||||||||||||||||||||||||||