lib/Beekeeper/MQTT.pm

filter: Statementstmt 62.0% 542 / 874 Branchbran 36.4% 142 / 390 Conditioncond 34.2% 13 / 38 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 72.4% 71 / 98 Podpod 52.9% 9 / 17 totaltotal 54.0% 778 / 1439 CC 225 SCAR 37.2
CC
225
Cov
54%
unsubscribe
156.0
_receive_connack
143.6
_receive_unsuback
132.0
CRAP 41.3
« lib/Beekeeper/Logger.pm ↑ Summary blib/lib/auto/share/dist/Beekeeper/chat/lib/MyApp/Client.pm »
1 package Beekeeper::MQTT;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = '0.10';
7  
8 11 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use List::Util 'shuffle';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 our @EXPORT_OK;
17 11 our %EXPORT_TAGS;
18  
19 11 our $DEBUG = 0;
20  
21 EXPORT: {
22 11 my (@const, @encode);
23 11 foreach (keys %{Beekeeper::MQTT::}) {
24 1078 ▶ push @const, $_ if m/^MQTT_/;
Branch: 1 branch
BranchTrueFalseTotal
if /^MQTT_/ 462 616 1078
25 1078 ▶ push @encode, $_ if m/^_(en|de)code/;
Branch: 1 branch
BranchTrueFalseTotal
if /^_(en|de)code/ 77 1001 1078
26 }
27 11 @EXPORT_OK = (@const, @encode);
28 11 $EXPORT_TAGS{'const'} = \@const;
29 11 $EXPORT_TAGS{'decode'} = \@encode;
30 }
31  
32 # 2.1.2 Control Packet type
33  
34 11 ▶use constant MQTT_CONNECT => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 11 ▶use constant MQTT_CONNACK => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 ▶use constant MQTT_PUBLISH => 0x03;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 11 ▶use constant MQTT_PUBACK => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 11 ▶use constant MQTT_PUBREC => 0x05;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 11 ▶use constant MQTT_PUBREL => 0x06;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 11 ▶use constant MQTT_PUBCOMP => 0x07;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 11 ▶use constant MQTT_SUBSCRIBE => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 11 ▶use constant MQTT_SUBACK => 0x09;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 11 ▶use constant MQTT_UNSUBSCRIBE => 0x0A;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 11 ▶use constant MQTT_UNSUBACK => 0x0B;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 11 ▶use constant MQTT_PINGREQ => 0x0C;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 11 ▶use constant MQTT_PINGRESP => 0x0D;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 11 ▶use constant MQTT_DISCONNECT => 0x0E;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 11 ▶use constant MQTT_AUTH => 0x0F;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # 2.2.2.2 Properties
51  
52 11 ▶use constant MQTT_PAYLOAD_FORMAT_INDICATOR => 0x01; # byte PUBLISH, Will Properties
Subroutine BEGIN: called CC=1 SCAR=0.0
53 11 ▶use constant MQTT_MESSAGE_EXPIRY_INTERVAL => 0x02; # long int PUBLISH, Will Properties
Subroutine BEGIN: called CC=1 SCAR=0.0
54 11 ▶use constant MQTT_CONTENT_TYPE => 0x03; # utf8 string PUBLISH, Will Properties
Subroutine BEGIN: called CC=1 SCAR=0.0
55 11 ▶use constant MQTT_RESPONSE_TOPIC => 0x08; # utf8 string PUBLISH, Will Properties
Subroutine BEGIN: called CC=1 SCAR=0.0
56 11 ▶use constant MQTT_CORRELATION_DATA => 0x09; # binary data PUBLISH, Will Properties
Subroutine BEGIN: called CC=1 SCAR=0.0
57 11 ▶use constant MQTT_SUBSCRIPTION_IDENTIFIER => 0x0B; # variable int PUBLISH, SUBSCRIBE
Subroutine BEGIN: called CC=1 SCAR=0.0
58 11 ▶use constant MQTT_SESSION_EXPIRY_INTERVAL => 0x11; # long int CONNECT, CONNACK, DISCONNECT
Subroutine BEGIN: called CC=1 SCAR=0.0
59 11 ▶use constant MQTT_ASSIGNED_CLIENT_IDENTIFIER => 0x12; # utf8 string CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
60 11 ▶use constant MQTT_SERVER_KEEP_ALIVE => 0x13; # short int CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
61 11 ▶use constant MQTT_AUTHENTICATION_METHOD => 0x15; # utf8 string CONNECT, CONNACK, AUTH
Subroutine BEGIN: called CC=1 SCAR=0.0
62 11 ▶use constant MQTT_AUTHENTICATION_DATA => 0x16; # binary data CONNECT, CONNACK, AUTH
Subroutine BEGIN: called CC=1 SCAR=0.0
63 11 ▶use constant MQTT_REQUEST_PROBLEM_INFORMATION => 0x17; # byte CONNECT
Subroutine BEGIN: called CC=1 SCAR=0.0
64 11 ▶use constant MQTT_WILL_DELAY_INTERVAL => 0x18; # long int Will Properties
Subroutine BEGIN: called CC=1 SCAR=0.0
65 11 ▶use constant MQTT_REQUEST_RESPONSE_INFORMATION => 0x19; # byte CONNECT
Subroutine BEGIN: called CC=1 SCAR=0.0
66 11 ▶use constant MQTT_RESPONSE_INFORMATION => 0x1A; # utf8 string CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
67 11 ▶use constant MQTT_SERVER_REFERENCE => 0x1C; # utf8 string CONNACK, DISCONNECT
Subroutine BEGIN: called CC=1 SCAR=0.0
68 11 ▶use constant MQTT_REASON_STRING => 0x1F; # utf8 string CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, DISCONNECT, AUTH
Subroutine BEGIN: called CC=1 SCAR=0.0
69 11 ▶use constant MQTT_RECEIVE_MAXIMUM => 0x21; # short int CONNECT, CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
70 11 ▶use constant MQTT_TOPIC_ALIAS_MAXIMUM => 0x22; # short int CONNECT, CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
71 11 ▶use constant MQTT_TOPIC_ALIAS => 0x23; # short int PUBLISH
Subroutine BEGIN: called CC=1 SCAR=0.0
72 11 ▶use constant MQTT_MAXIMUM_QOS => 0x24; # byte CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
73 11 ▶use constant MQTT_RETAIN_AVAILABLE => 0x25; # byte CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
74 11 ▶use constant MQTT_USER_PROPERTY => 0x26; # utf8 pair CONNECT, CONNACK, PUBLISH, Will Properties, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, DISCONNECT, AUTH
Subroutine BEGIN: called CC=1 SCAR=0.0
75 11 ▶use constant MQTT_MAXIMUM_PACKET_SIZE => 0x27; # long int CONNECT, CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
76 11 ▶use constant MQTT_WILDCARD_SUBSCRIPTION_AVAILABLE => 0x28; # byte CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
77 11 ▶use constant MQTT_SUBSCRIPTION_IDENTIFIER_AVAILABLE => 0x29; # byte CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
78 11 ▶use constant MQTT_SHARED_SUBSCRIPTION_AVAILABLE => 0x2A; # byte CONNACK
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 # 2.4 Reason Code
81  
82 11 my %Reason_code = (
83 0x00 => 'Success', # CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK, AUTH
84 # 0x00 => 'Normal disconnection', # DISCONNECT
85 # 0x00 => 'Granted QoS 0', # SUBACK
86 # 0x01 => 'Granted QoS 1', # SUBACK
87 # 0x02 => 'Granted QoS 2', # SUBACK
88 0x04 => 'Disconnect with Will Message', # DISCONNECT
89 0x10 => 'No matching subscribers', # PUBACK, PUBREC
90 0x11 => 'No subscription existed', # UNSUBACK
91 0x18 => 'Continue authentication', # AUTH
92 0x19 => 'Re-authenticate', # AUTH
93 0x80 => 'Unspecified error', # CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT
94 0x81 => 'Malformed Packet', # CONNACK, DISCONNECT
95 0x82 => 'Protocol Error', # CONNACK, DISCONNECT
96 0x83 => 'Implementation specific error', # CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT
97 0x84 => 'Unsupported Protocol Version', # CONNACK
98 0x85 => 'Client Identifier not valid', # CONNACK
99 0x86 => 'Bad User Name or Password', # CONNACK
100 0x87 => 'Not authorized', # CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECT
101 0x88 => 'Server unavailable', # CONNACK
102 0x89 => 'Server busy', # CONNACK, DISCONNECT
103 0x8A => 'Banned', # CONNACK
104 0x8B => 'Server shutting down', # DISCONNECT
105 0x8C => 'Bad authentication method', # CONNACK, DISCONNECT
106 0x8D => 'Keep Alive timeout', # DISCONNECT
107 0x8E => 'Session taken over', # DISCONNECT
108 0x8F => 'Topic Filter invalid', # SUBACK, UNSUBACK, DISCONNECT
109 0x90 => 'Topic Name invalid', # CONNACK, PUBACK, PUBREC, DISCONNECT
110 0x91 => 'Packet Identifier in use', # PUBACK, PUBREC, SUBACK, UNSUBACK
111 0x92 => 'Packet Identifier not found', # PUBREL, PUBCOMP
112 0x93 => 'Receive Maximum exceeded', # DISCONNECT
113 0x94 => 'Topic Alias invalid', # DISCONNECT
114 0x95 => 'Packet too large', # CONNACK, DISCONNECT
115 0x96 => 'Message rate too high', # DISCONNECT
116 0x97 => 'Quota exceeded', # CONNACK, PUBACK, PUBREC, SUBACK, DISCONNECT
117 0x98 => 'Administrative action', # DISCONNECT
118 0x99 => 'Payload format invalid', # CONNACK, PUBACK, PUBREC, DISCONNECT
119 0x9A => 'Retain not supported', # CONNACK, DISCONNECT
120 0x9B => 'QoS not supported', # CONNACK, DISCONNECT
121 0x9C => 'Use another server', # CONNACK, DISCONNECT
122 0x9D => 'Server moved', # CONNACK, DISCONNECT
123 0x9E => 'Shared Subscriptions not supported', # SUBACK, DISCONNECT
124 0x9F => 'Connection rate exceeded', # CONNACK, DISCONNECT
125 0xA0 => 'Maximum connect time', # DISCONNECT
126 0xA1 => 'Subscription Identifiers not supported', # SUBACK, DISCONNECT
127 0xA2 => 'Wildcard Subscriptions not supported', # SUBACK, DISCONNECT
128 );
129  
130 # 3.9.3 Subscribe Reason Codes
131  
132 11 my %Subscribe_reason_code = (
133 %Reason_code,
134 0x00 => 'Granted QoS 0',
135 0x01 => 'Granted QoS 1',
136 0x02 => 'Granted QoS 2',
137 );
138  
139 # 3.14.2.1 Disconnect Reason Code
140  
141 11 my %Disconnect_reason_code = (
142 %Reason_code,
143 0x00 => 'Normal disconnection',
144 );
145  
146 76 ▶sub _decode_byte {
Subroutine _decode_byte: called CC=1 SCAR=0.0
147 76 my ($packet, $offs) = @_;
148  
149 76 my $byte = unpack("C", substr($$packet, $$offs, 1));
150 76 $$offs += 1;
151  
152 76 return $byte;
153 }
154  
155 19 ▶sub _decode_int_16 {
Subroutine _decode_int_16: called CC=1 SCAR=0.0
156 19 my ($packet, $offs) = @_;
157  
158 19 my $int = unpack("n", substr($$packet, $$offs, 2));
159 19 $$offs += 2;
160  
161 19 return $int;
162 }
163  
164 0 ▶sub _decode_int_32 {
Subroutine _decode_int_32: not called CC=1 SCAR=6.9
165 0 my ($packet, $offs) = @_;
166  
167 0 my $int = unpack("N", substr($$packet, $$offs, 4));
168 0 $$offs += 4;
169  
170 0 return $int;
171 }
172  
173 0 ▶sub _decode_utf8_str {
Subroutine _decode_utf8_str: not called CC=1 SCAR=6.9
174 0 my ($packet, $offs) = @_;
175  
176 0 my $str = unpack("n/a", substr($$packet, $$offs));
177 0 $$offs += 2 + length($str);
178 0 utf8::decode($str);
179  
180 0 return $str;
181 }
182  
183 0 ▶sub _decode_binary_data {
Subroutine _decode_binary_data: not called CC=1 SCAR=6.9
184 0 my ($packet, $offs) = @_;
185  
186 0 my $data = unpack("n/a", substr($$packet, $$offs));
187 0 $$offs += 2 + length($data);
188  
189 0 return $data;
190 }
191  
192 206 ▶sub _decode_var_int {
Subroutine _decode_var_int: called CC=2 SCAR=6.9
193 206 my ($packet, $offs) = @_;
194  
195 206 my $int = 0;
196 206 my $mult = 1;
197 206 my $byte;
198  
199 206 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$byte & 128 and do { do { $byte = unpack("C", substr($$packet, $$offs, 1)); $int += ($byte & 127) * $mult; $mult *= 128; ++$$offs } } 0 206 206
200 206 $byte = unpack("C", substr($$packet, $$offs, 1));
201 206 $int += ($byte & 0x7F) * $mult;
202 206 $mult *= 128;
203 206 $$offs++;
204 } while ($byte & 0x80);
205  
206 206 return $int;
207 }
208  
209 301 ▶sub _encode_var_int {
Subroutine _encode_var_int: called CC=2 SCAR=6.9
210 301 ▶ return pack("C", $_[0]) if ($_[0] < 128);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 128 271 30 301
211 30 my @a = unpack("C*", pack("w", $_[0]));
212 30 $a[0] &= 0x7F;
213 30 $a[-1] |= 0x80;
214 30 return pack("C*", reverse @a);
215 }
216  
217  
218 19 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
219 19 my ($class, %args) = @_;
220  
221 19 my $self = {
222 bus_id => undef,
223 bus_role => undef,
224 handle => undef, # the socket
225 hosts => undef, # list of all hosts in cluster
226 is_connected => undef, # true once connected
227 try_hosts => undef, # list of hosts to try to connect
228 connect_err => undef, # count of connection errors
229 timeout_tmr => undef, # timer used for connection timeout
230 reconnect_tmr => undef, # timer used for connection retry
231 connack_cb => undef, # connack callback
232 error_cb => undef, # error callback
233 client_id => undef, # client id
234 server_prop => {}, # server properties
235 server_alias => {}, # server topic aliases
236 client_alias => {}, # client topic aliases
237 subscriptions => {}, # topic subscriptions
238 subscr_cb => {}, # subscription callbacks
239 packet_cb => {}, # packet callbacks
240 buffers => {}, # raw mqtt buffers
241 packet_seq => 1, # sequence used for packet ids
242 subscr_seq => 1, # sequence used for subscription ids
243 alias_seq => 1, # sequence used for topic alias ids
244 use_alias => 0, # topic alias enabled
245 config => \%args,
246 };
247  
248 19 $self->{bus_id} = delete $args{'bus_id'};
249 19 ▶ $self->{bus_role} = delete $args{'bus_role'} || $self->{bus_id};
Condition33%1/3
delete $args{'bus_role'} || $self->{'bus_id'}
l!l&&r!l&&!r
0190
MC/DC0%0/2
delete $args{'bus_role'} || $self->{'bus_id'}
delete $args{'bus_role'} $self->{'bus_id'}
Truth table33%1/3
A || B
A= delete $args{'bus_role'}B= $self->{'bus_id'}
ABresult
1X1
011
000
250 19 $self->{error_cb} = delete $args{'on_error'};
251  
252 19 bless $self, $class;
253 19 return $self;
254 }
255  
256 0 ▶sub bus_id { $_[0]->{bus_id} }
Subroutine bus_id: not called CC=1 SCAR=6.9
Pod undocumented
257 0 ▶sub bus_role { $_[0]->{bus_role} }
Subroutine bus_role: not called CC=1 SCAR=6.9
Pod undocumented
258  
259 0 ▶sub _fatal {
Subroutine _fatal: not called CC=2 SCAR=17.9
260 0 my ($self, $errstr) = @_;
261 0 ▶ die "(" . __PACKAGE__ . ") $errstr\n" unless $self->{error_cb};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"error_cb"} 0 0 0
262 0 $self->{error_cb}->($errstr);
263 }
264  
265 11 our $BUSY_SINCE = undef;
266 11 our $BUSY_TIME = 0;
267  
268 19 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.2
Pod documented
269 19 my ($self, %args) = @_;
270  
271 19 $self->{connack_cb} = $args{'on_connack'};
272 19 $self->{connect_cv} = AnyEvent->condvar;
273  
274 19 $self->_connect;
275  
276 19 ▶ $self->{connect_cv}->recv if $args{'blocking'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"blocking"} 19 0 19
277 19 $self->{connect_cv} = undef;
278  
279 19 ▶ return $args{'blocking'} ? $self->{is_connected} : 1;
Branch: 1 branch
BranchTrueFalseTotal
$args{'blocking'} ? : 19 0 19
280 }
281  
282 19 ▶sub _connect {
Subroutine _connect: called CC=13 SCAR=31.1
283 19 my ($self) = @_;
284 19 weaken($self);
285  
286 19 my $config = $self->{config};
287  
288 19 my $timeout = $config->{'timeout'};
289 19 ▶ $timeout = 30 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 19 0 19
290  
291 # Ensure that timeout is set properly when the event loop was blocked
292 19 AnyEvent->now_update;
293  
294 # Connection timeout handler
295 19 ▶ if ($timeout && !$self->{timeout_tmr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and not $self->{"timeout_tmr"}) 19 0 19
Condition33%1/3
$timeout and not $self->{"timeout_tmr"}
!ll&&!rl&&r
0019
MC/DC0%0/2
$timeout and not $self->{"timeout_tmr"}
$timeout not $self->{"timeout_tmr"}
Truth table33%1/3
A and not(B)
A= $timeoutB= not $self->{"timeout_tmr"}
ABresult
0X0
100
111
296 0 ▶ $self->{timeout_tmr} = AnyEvent->timer( after => $timeout, cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
297 0 $self->_reset_connection;
298 0 $self->{connect_cv}->send;
299 0 $self->_fatal("Could not connect to MQTT broker after $timeout seconds");
300 19 });
301 }
302  
303 19 ▶ unless ($self->{hosts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"hosts"}) 19 0 19
304 # Initialize the list of cluster hosts
305 19 ▶ my $hosts = $config->{'host'} || 'localhost';
Condition50%1/2
$config->{'host'} || 'localhost'
l!l
190
MC/DC0%0/1
$config->{'host'} || 'localhost'
$config->{'host'}
Truth table50%1/2
A
A= $config->{'host'}
Aresult
11
00
306 19 ▶ my @hosts = (ref $hosts eq 'ARRAY') ? @$hosts : ( $hosts );
Branch: 1 branch
BranchTrueFalseTotal
ref $hosts eq 'ARRAY' ? : 0 19 19
307 19 $self->{hosts} = [ shuffle @hosts ];
308 }
309  
310 # Determine next host of cluster to connect to
311 19 ▶ my $try_hosts = $self->{try_hosts} ||= [];
Condition50%1/2
$self->{"try_hosts"} ||= []
l!l
019
MC/DC0%0/1
$self->{"try_hosts"} ||= []
$self->{"try_hosts"}
Truth table50%1/2
A
A= $self->{"try_hosts"}
Aresult
11
00
312 19 ▶ @$try_hosts = @{$self->{hosts}} unless @$try_hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless @$try_hosts 19 0 19
313  
314 # TCP connection args
315 19 my $host = shift @$try_hosts;
316 19 ▶ my $tls = $config->{'tls'} || 0;
Condition50%1/2
$config->{'tls'} || 0
l!l
019
MC/DC0%0/1
$config->{'tls'} || 0
$config->{'tls'}
Truth table50%1/2
A
A= $config->{'tls'}
Aresult
11
00
317 19 ▶ my $port = $config->{'port'} || ( $tls ? 8883 : 1883 );
Branch: 1 branch
BranchTrueFalseTotal
$tls ? : 0 0 0
Condition33%1/3
$config->{'port'} || ($tls ? 8883 : 1883)
l!l&&r!l&&!r
1900
MC/DC0%0/2
$config->{'port'} || ($tls ? 8883 : 1883)
$config->{'port'} ($tls ? 8883 : 1883)
Truth table33%1/3
A || B
A= $config->{'port'}B= ($tls ? 8883 : 1883)
ABresult
1X1
011
000
318  
319 19 ($host) = ($host =~ m/^([a-zA-Z0-9\-\.]+)$/s); # untaint
320 19 ($port) = ($port =~ m/^([0-9]+)$/s);
321  
322 $self->{handle} = AnyEvent::Handle->new(
323 connect => [ $host, $port ],
324 tls => $tls ? 'connect' : undef,
325 keepalive => 1,
326 no_delay => 1,
327 19 ▶ on_connect => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
328 19 my ($fh, $host, $port) = @_;
329 # Send CONNECT packet
330 19 $self->{server_prop}->{host} = $host;
331 19 $self->{server_prop}->{port} = $port;
332 19 $self->_send_connect;
333 },
334 0 ▶ on_connect_error => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
335 0 my ($fh, $errmsg) = @_;
336 # Some error occurred while connection, such as an unresolved hostname
337 # or connection refused. Try next host of cluster immediately, or retry
338 # in few seconds if all hosts of the cluster are unresponsive
339 0 $self->{connect_err}++;
340 0 ▶ warn "Could not connect to MQTT broker at $host:$port: $errmsg\n" if ($self->{connect_err} <= @{$self->{hosts}});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connect_err"} <= @{$self->{"hosts"};} 0 0 0
341 0 ▶ my $delay = @{$self->{try_hosts}} ? 0 : $self->{connect_err} / @{$self->{hosts}};
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'try_hosts'};} ? : 0 0 0
342 $self->{reconnect_tmr} = AnyEvent->timer(
343 after => ($delay < 10 ? $delay : 10),
344 0 ▶ cb => sub { $self->_connect },
Subroutine __ANON__: not called CC=1 SCAR=6.9
345 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$delay < 10 ? : 0 0 0
346 },
347 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 0 my ($fh, $fatal, $errmsg) = @_;
349 # Some error occurred, such as a read error
350 0 $self->_reset_connection;
351 0 $self->_fatal("Error on connection to MQTT broker at $host:$port: $errmsg");
352 },
353 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
354 0 my ($fh) = @_;
355 # The server has closed the connection without sending DISCONNECT
356 0 $self->_reset_connection;
357 0 $self->_fatal("MQTT broker at $host:$port has gone away");
358 },
359 270 ▶ on_read => sub {
Subroutine __ANON__: called CC=22 SCAR=34.6
360 270 my ($fh) = @_;
361  
362 270 my $packet_type;
363 my $packet_flags;
364  
365 270 my $rbuff_len;
366 270 my $packet_len;
367  
368 270 my $mult;
369 270 my $offs;
370 270 my $byte;
371  
372 270 my $timing_packets;
373  
374 270 ▶ unless (defined $BUSY_SINCE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Beekeeper::MQTT::BUSY_SINCE) 270 0 270
375 # Measure time elapsed while processing incoming packets
376 270 $BUSY_SINCE = Time::HiRes::time;
377 270 $timing_packets = 1;
378 }
379  
380 PARSE_PACKET: {
381  
382 270 $rbuff_len = length $fh->{rbuf};
383  
384 462 ▶ last PARSE_PACKET unless $rbuff_len >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $rbuff_len >= 2 176 286 462
385  
386 286 ▶ unless ($packet_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($packet_type) 286 0 286
387  
388 286 $packet_len = 0;
389 286 $mult = 1;
390 286 $offs = 1;
391  
392 PARSE_LEN: {
393 286 $byte = unpack "C", substr( $fh->{rbuf}, $offs++, 1 );
394 580 $packet_len += ($byte & 0x7f) * $mult;
395 580 ▶ last unless ($byte & 0x80);
Branch: 1 branch
BranchTrueFalseTotal
unless $byte & 128 286 294 580
396 294 ▶ last PARSE_PACKET if ($offs >= $rbuff_len); # Not enough data
Branch: 1 branch
BranchTrueFalseTotal
if $offs >= $rbuff_len 0 294 294
397 294 $mult *= 128;
398 294 ▶ redo if ($offs < 5);
Branch: 1 branch
BranchTrueFalseTotal
if $offs < 5 294 0 294
399 }
400  
401 #TODO: Check max packet size
402  
403 286 $byte = unpack('C', substr( $fh->{rbuf}, 0, 1 ));
404 286 $packet_type = $byte >> 4;
405 286 $packet_flags = $byte & 0x0F;
406 }
407  
408 286 ▶ if ($rbuff_len < ($offs + $packet_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rbuff_len < $offs + $packet_len) 94 192 286
409 # Not enough data
410 94 last PARSE_PACKET;
411 }
412  
413 # Consume packet from buffer
414 192 my $packet = substr($fh->{rbuf}, 0, ($offs + $packet_len), '');
415  
416 # Trim fixed header from packet
417 192 substr($packet, 0, $offs, '');
418  
419 192 ▶ if ($packet_type == MQTT_PUBLISH) {
Branch: 12 branches
BranchTrueFalseTotal
if ($packet_type == 3) { } 84 108 192
elsif ($packet_type == 4) { } 70 38 108
elsif ($packet_type == 5) { } 0 38 38
elsif ($packet_type == 6) { } 0 38 38
elsif ($packet_type == 7) { } 0 38 38
elsif ($packet_type == 12) { } 0 38 38
elsif ($packet_type == 13) { } 0 38 38
elsif ($packet_type == 9) { } 19 19 38
elsif ($packet_type == 11) { } 0 19 19
elsif ($packet_type == 2) { } 19 0 19
elsif ($packet_type == 14) { } 0 0 0
elsif ($packet_type == 15) { } 0 0 0
420  
421 84 $self->_receive_publish(\$packet, $packet_flags);
422 }
423 elsif ($packet_type == MQTT_PUBACK) {
424  
425 70 $self->_receive_puback(\$packet);
426 }
427 elsif ($packet_type == MQTT_PUBREC) {
428  
429 0 $self->_receive_pubrec(\$packet);
430 }
431 elsif ($packet_type == MQTT_PUBREL) {
432
433 0 $self->_receive_pubrel(\$packet);
434 }
435 elsif ($packet_type == MQTT_PUBCOMP) {
436  
437 0 $self->_receive_pubcomp(\$packet);
438 }
439 elsif ($packet_type == MQTT_PINGREQ) {
440  
441 0 $self->pingresp;
442 }
443 elsif ($packet_type == MQTT_PINGRESP) {
444  
445 # Client takes no action on receiving PINGRESP
446 }
447 elsif ($packet_type == MQTT_SUBACK) {
448  
449 19 $self->_receive_suback(\$packet);
450 }
451 elsif ($packet_type == MQTT_UNSUBACK) {
452  
453 0 $self->_receive_unsuback(\$packet);
454 }
455 elsif ($packet_type == MQTT_CONNACK) {
456  
457 19 $self->_receive_connack(\$packet);
458 }
459 elsif ($packet_type == MQTT_DISCONNECT) {
460  
461 0 $self->_receive_disconnect(\$packet);
462 }
463 elsif ($packet_type == MQTT_AUTH) {
464  
465 0 $self->_receive_auth(\$packet);
466 }
467 else {
468 # Protocol error
469 0 $self->_fatal("Received packet with unknown type $packet_type");
470 }
471  
472 # Prepare for next frame
473 192 undef $packet_type;
474  
475 # Handle could have been destroyed at this point
476 192 ▶ redo PARSE_PACKET if defined $fh->{rbuf};
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh->{"rbuf"} 192 0 192
477 }
478  
479 270 ▶ if (defined $timing_packets) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timing_packets) 270 0 270
480 270 $BUSY_TIME += Time::HiRes::time - $BUSY_SINCE;
481 270 undef $BUSY_SINCE;
482 }
483 },
484 19 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$tls ? : 0 19 19
485  
486 19 1;
487 }
488  
489 19 ▶sub _send_connect {
Subroutine _send_connect: called CC=22 SCAR=35.6
490 19 my ($self) = @_;
491  
492 19 my %prop = %{$self->{config}};
493  
494 19 my $username = delete $prop{'username'};
495 19 my $password = delete $prop{'password'};
496 19 my $client_id = delete $prop{'client_id'};
497 19 my $clean_start = delete $prop{'clean_start'};
498 19 my $keep_alive = delete $prop{'keep_alive'};
499 19 my $will = delete $prop{'will'};
500  
501 19 ▶ unless ($client_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_id) 19 0 19
502 19 $client_id = '';
503 19 $client_id .= ('0'..'9','a'..'z','A'..'Z')[rand 62] for (1..22);
504 }
505  
506 19 $self->{client_id} = $client_id;
507  
508  
509 # 3.1.2.11 Properties
510  
511 19 my $raw_prop = '';
512  
513 19 ▶ if (exists $prop{'session_expiry_interval'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"session_expiry_interval"}) 6 13 19
514 # 3.1.2.11.2 Session Expiry Interval (long int)
515 6 $raw_prop .= pack("C N", MQTT_SESSION_EXPIRY_INTERVAL, delete $prop{'session_expiry_interval'});
516 }
517  
518 19 ▶ if (exists $prop{'receive_maximum'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"receive_maximum"}) 8 11 19
519 # 3.1.2.11.3 Receive Maximum (short int)
520 8 $raw_prop .= pack("C n", MQTT_RECEIVE_MAXIMUM, delete $prop{'receive_maximum'});
521 }
522  
523 19 ▶ if (exists $prop{'maximum_packet_size'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"maximum_packet_size"}) 0 19 19
524 # 3.1.2.11.4 Maximum Packet Size (long int)
525 0 $raw_prop .= pack("C N", MQTT_MAXIMUM_PACKET_SIZE, delete $prop{'maximum_packet_size'});
526 }
527  
528 19 ▶ if (exists $prop{'topic_alias_maximum'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"topic_alias_maximum"}) 6 13 19
529 # 3.1.2.11.5 Topic Alias Maximum (short int)
530 6 $raw_prop .= pack("C n", MQTT_TOPIC_ALIAS_MAXIMUM, delete $prop{'topic_alias_maximum'});
531 }
532  
533 19 ▶ if (exists $prop{'request_response_information'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"request_response_information"}) 0 19 19
534 # 3.1.2.11.6 Request Response Information (byte)
535 0 $raw_prop .= pack("C C", MQTT_REQUEST_RESPONSE_INFORMATION, delete $prop{'request_response_information'});
536 }
537  
538 19 ▶ if (exists $prop{'request_problem_information'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"request_problem_information"}) 0 19 19
539 # 3.1.2.11.7 Request Problem Information (byte)
540 0 $raw_prop .= pack("C C", MQTT_REQUEST_PROBLEM_INFORMATION, delete $prop{'request_problem_information'});
541 }
542  
543 19 ▶ if (exists $prop{'authentication_method'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"authentication_method"}) 0 19 19
544 # 3.1.2.11.9 Authentication Method (utf8 string)
545 0 utf8::encode( $prop{'authentication_method'} );
546 0 $raw_prop .= pack("C n/a*", MQTT_AUTHENTICATION_METHOD, delete $prop{'authentication_method'});
547 }
548  
549 19 ▶ if (exists $prop{'authentication_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{"authentication_data"}) 0 19 19
550 # 3.1.2.11.10 Authentication Data (binary data)
551 0 $raw_prop .= pack("C n", MQTT_AUTHENTICATION_DATA, delete $prop{'authentication_data'});
552 }
553  
554 19 foreach my $key (keys %prop) {
555 # 3.1.2.11.8 User Property (utf8 string pair)
556 38 my $val = $prop{$key};
557 38 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 38 38
558 38 utf8::encode( $key );
559 38 utf8::encode( $val );
560 38 $raw_prop .= pack("C n/a* n/a*", MQTT_USER_PROPERTY, $key, $val);
561 }
562  
563  
564 # 3.1.2 Variable Header
565  
566 # 3.1.2.1 Protocol Name (utf8 string)
567 19 my $raw_mqtt = pack("n/a*", "MQTT");
568  
569 # 3.1.2.2 Protocol Version (byte)
570 19 $raw_mqtt .= pack("C", 5);
571  
572 # 3.1.2.3 Connect Flags (byte)
573 19 my $flags = 0;
574 19 ▶ $flags |= 0x02 if $clean_start; # 3.1.2.4 Clean Start
Branch: 1 branch
BranchTrueFalseTotal
if $clean_start 6 13 19
575 19 ▶ $flags |= 0x80 if defined $username; # 3.1.2.8 User Name Flag
Branch: 1 branch
BranchTrueFalseTotal
if defined $username 19 0 19
576 19 ▶ $flags |= 0x40 if defined $password; # 3.1.2.9 Password Flag
Branch: 1 branch
BranchTrueFalseTotal
if defined $password 19 0 19
577  
578 19 ▶ if ($will) {
Branch: 1 branch
BranchTrueFalseTotal
if ($will) 0 19 19
579 0 $flags |= 0x04; # 3.1.2.5 Will Flag
580 0 $flags |= $will->{'qos'} << 3; # 3.1.2.6 Will QoS
581 0 ▶ $flags |= 0x20 if $will->{'retain'}; # 3.1.2.7 Will Retain
Branch: 1 branch
BranchTrueFalseTotal
if $will->{"retain"} 0 0 0
582 }
583  
584 19 $raw_mqtt .= pack("C", $flags);
585  
586 # 3.1.2.10 Keep Alive (short int)
587 19 ▶ $raw_mqtt .= pack("n", $keep_alive || 0);
Condition50%1/2
$keep_alive || 0
l!l
019
MC/DC0%0/1
$keep_alive || 0
$keep_alive
Truth table50%1/2
A
A= $keep_alive
Aresult
11
00
588  
589 # 3.1.2.11 Properties
590 19 $raw_mqtt .= _encode_var_int(length $raw_prop);
591 19 $raw_mqtt .= $raw_prop;
592  
593  
594 # 3.1.3 Payload
595  
596 # 3.1.3.1 Client Identifier (utf8 string)
597 19 $raw_mqtt .= pack("n/a*", $client_id);
598  
599 19 ▶ if ($will) {
Branch: 1 branch
BranchTrueFalseTotal
if ($will) 0 19 19
600  
601 #TODO: 3.1.3.2 Will Properties
602 0 my $will_prop = '';
603  
604 0 $raw_mqtt .= _encode_var_int(length $will_prop);
605 0 $raw_mqtt .= $will_prop;
606  
607 # 3.1.3.3 Will Topic (utf8 string)
608 0 utf8::encode( $will->{'topic'});
609 0 $raw_mqtt .= pack("n/a*", $will->{'topic'});
610  
611 # 3.1.3.4 Will Payload (binary data)
612 0 $raw_mqtt .= pack("n/a*", $will->{'payload'});
613 }
614  
615 19 ▶ if (defined $username) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username) 19 0 19
616 # 3.1.3.5 Username (utf8 string)
617 19 utf8::encode( $username );
618 19 $raw_mqtt .= pack("n/a*", $username);
619 }
620
621 19 ▶ if (defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $password) 19 0 19
622 # 3.1.3.6 Password (binary data)
623 19 $raw_mqtt .= pack("n/a*", $password);
624 }
625  
626 $self->{handle}->push_write(
627 19 pack("C", MQTT_CONNECT << 4) . # 3.1.1 Packet type
628 _encode_var_int(length $raw_mqtt) . # 3.1.1 Packet length
629 $raw_mqtt
630 );
631 }
632  
633 19 ▶sub _receive_connack {
Subroutine _receive_connack: called CC=22 SCAR=49.7
634 19 my ($self, $packet) = @_;
635  
636 19 my $prop = $self->{server_prop};
637 19 my $offs = 0;
638  
639 # 3.2.2.1 Acknowledge flags (byte)
640 19 my $ack_flags = _decode_byte($packet, \$offs);
641 19 $prop->{'session_present'} = $ack_flags & 0x01;
642  
643 # 3.2.2.2 Reason code (byte)
644 19 my $reason_code = _decode_byte($packet, \$offs);
645 19 $prop->{'reason_code'} = $reason_code;
646 19 $prop->{'reason'} = $Reason_code{$reason_code};
647
648 # 3.2.2.3.1 Properties Length (variable length int)
649 19 my $prop_len = _decode_var_int($packet, \$offs);
650 19 my $prop_end = $offs + $prop_len;
651  
652 19 ▶ while ($offs < $prop_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offs < $prop_end) 19 19 38
653  
654 19 my $prop_id = _decode_byte($packet, \$offs);
655  
656 19 ▶ if ($prop_id == MQTT_SESSION_EXPIRY_INTERVAL) {
Branch: 17 branches
BranchTrueFalseTotal
if ($prop_id == 17) { } 0 19 19
elsif ($prop_id == 33) { } 0 19 19
elsif ($prop_id == 36) { } 19 0 19
elsif ($prop_id == 37) { } 0 0 0
elsif ($prop_id == 39) { } 0 0 0
elsif ($prop_id == 18) { } 0 0 0
elsif ($prop_id == 34) { } 0 0 0
elsif ($prop_id == 31) { } 0 0 0
elsif ($prop_id == 38) { } 0 0 0
elsif ($prop_id == 40) { } 0 0 0
elsif ($prop_id == 41) { } 0 0 0
elsif ($prop_id == 42) { } 0 0 0
elsif ($prop_id == 19) { } 0 0 0
elsif ($prop_id == 26) { } 0 0 0
elsif ($prop_id == 28) { } 0 0 0
elsif ($prop_id == 21) { } 0 0 0
elsif ($prop_id == 22) { } 0 0 0
657 # 3.2.2.3.2 Session Expiry Interval (long int)
658 0 $prop->{'session_expiry_interval'} = _decode_int_32($packet, \$offs);
659 }
660 elsif ($prop_id == MQTT_RECEIVE_MAXIMUM) {
661 # 3.2.2.3.3 Receive Maximum (short int)
662 0 $prop->{'receive_maximum'} = _decode_int_16($packet, \$offs);
663 }
664 elsif ($prop_id == MQTT_MAXIMUM_QOS) {
665 # 3.2.2.3.4 Maximum QoS (byte)
666 19 $prop->{'maximum_qos'} = _decode_byte($packet, \$offs);
667 }
668 elsif ($prop_id == MQTT_RETAIN_AVAILABLE) {
669 # 3.2.2.3.5 Retain Available (byte)
670 0 $prop->{'retain_available'} = _decode_byte($packet, \$offs);
671 }
672 elsif ($prop_id == MQTT_MAXIMUM_PACKET_SIZE) {
673 # 3.2.2.3.6 Maximum Packet Size (long int)
674 0 $prop->{'maximum_packet_size'} = _decode_int_32($packet, \$offs);
675 }
676 elsif ($prop_id == MQTT_ASSIGNED_CLIENT_IDENTIFIER) {
677 # 3.2.2.3.7 Assigned Client Identifier (utf8 string)
678 0 $prop->{'assigned_client_identifier'} = _decode_utf8_str($packet, \$offs);
679 }
680 elsif ($prop_id == MQTT_TOPIC_ALIAS_MAXIMUM) {
681 # 3.2.2.3.8 Topic Alias Maximum (short int)
682 0 $prop->{'topic_alias_maximum'} = _decode_int_16($packet, \$offs);
683 }
684 elsif ($prop_id == MQTT_REASON_STRING) {
685 # 3.2.2.3.9 Reason String (utf8 string)
686 0 $prop->{'reason_string'} = _decode_utf8_str($packet, \$offs);
687 }
688 elsif ($prop_id == MQTT_USER_PROPERTY) {
689 # 3.2.2.3.10 User Property (utf8 string pair)
690 0 my $key = _decode_utf8_str($packet, \$offs);
691 0 my $val = _decode_utf8_str($packet, \$offs);
692 0 $prop->{$key} = $val;
693 }
694 elsif ($prop_id == MQTT_WILDCARD_SUBSCRIPTION_AVAILABLE) {
695 # 3.2.2.3.11 Wildcard Subscription Available (byte)
696 0 $prop->{'wildcard_subscription_available'} = _decode_byte($packet, \$offs);
697 }
698 elsif ($prop_id == MQTT_SUBSCRIPTION_IDENTIFIER_AVAILABLE) {
699 # 3.2.2.3.12 Subscription Identifiers Available (byte)
700 0 $prop->{'subscription_identifier_available'} = _decode_byte($packet, \$offs);
701 }
702 elsif ($prop_id == MQTT_SHARED_SUBSCRIPTION_AVAILABLE) {
703 # 3.2.2.3.13 Shared Subscription Available (byte)
704 0 $prop->{'shared_subscription_available'} = _decode_byte($packet, \$offs);
705 }
706 elsif ($prop_id == MQTT_SERVER_KEEP_ALIVE) {
707 # 3.2.2.3.14 Server Keep Alive (short int)
708 0 $prop->{'server_keep_alive'} = _decode_int_16($packet, \$offs);
709 }
710 elsif ($prop_id == MQTT_RESPONSE_INFORMATION) {
711 # 3.2.2.3.15 Response Information (utf8 string)
712 0 $prop->{'response_information'} = _decode_utf8_str($packet, \$offs);
713 }
714 elsif ($prop_id == MQTT_SERVER_REFERENCE) {
715 # 3.2.2.3.16 Server Reference (utf8 string)
716 0 $prop->{'server_reference'} = _decode_utf8_str($packet, \$offs);
717 }
718 elsif ($prop_id == MQTT_AUTHENTICATION_METHOD) {
719 # 3.2.2.3.17 Authentication Method (utf8 string)
720 0 $prop->{'authentication_method'} = _decode_utf8_str($packet, \$offs);
721 }
722 elsif ($prop_id == MQTT_AUTHENTICATION_DATA) {
723 # 3.2.2.3.18 Authentication Data (binary data)
724 0 $prop->{'authentication_data'} = _decode_binary_data($packet, \$offs);
725 }
726 else {
727 # Protocol error
728 0 $self->_fatal("Received CONNACK with unknown property $prop_id");
729 }
730 }
731  
732 19 my $success = ($reason_code == 0x00);
733  
734 19 ▶ unless ( $success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 19 19
735 # Server will close the connection
736 # warn "Served refused CONNACK: $reason";
737 #TODO: handle
738 }
739  
740 19 $self->{is_connected} = 1;
741 19 $self->{timeout_tmr} = undef;
742 19 $self->{reconnect_tmr} = undef;
743 19 $self->{connect_err} = undef;
744  
745 #TODO: ... blocking connection
746 19 ▶ $self->{connect_cv}->send if $self->{connect_cv};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connect_cv"} 19 0 19
747  
748 # Execute CONNACK callback
749 19 my $connack_cb = $self->{connack_cb};
750 19 ▶ $connack_cb->($success, $prop) if $connack_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $connack_cb 0 19 19
751 }
752  
753  
754 13 ▶sub disconnect {
Subroutine disconnect: called CC=8 SCAR=28.5
Pod documented
755 13 my ($self, %args) = @_;
756  
757 13 ▶ unless (defined $self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"handle"}) 0 13 13
758 0 carp "Already disconnected from MQTT broker";
759 0 return;
760 }
761  
762 13 my $reason_code = delete $args{'reason_code'};
763  
764 # 3.14.2.2 Properties
765  
766 13 my $raw_prop = '';
767  
768 13 ▶ if (exists $args{'session_expiry_interval'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"session_expiry_interval"}) 0 13 13
769 # 3.14.2.2.2 Session Expiry Interval (long int)
770 0 utf8::encode( $args{'session_expiry_interval'} );
771 0 $raw_prop .= pack("C n/a*", MQTT_SESSION_EXPIRY_INTERVAL, delete $args{'session_expiry_interval'});
772 }
773  
774 13 ▶ if (exists $args{'reason_string'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"reason_string"}) 0 13 13
775 # 3.14.2.2.3 Reason String (utf8 string)
776 0 utf8::encode( $args{'reason_string'} );
777 0 $raw_prop .= pack("C n/a*", MQTT_REASON_STRING, delete $args{'reason_string'});
778 }
779  
780 13 ▶ if (exists $args{'server_reference'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"server_reference"}) 0 13 13
781 # 3.14.2.2.5 Server Reference (utf8 string)
782 0 utf8::encode( $args{'server_reference'} );
783 0 $raw_prop .= pack("C n/a*", MQTT_SERVER_REFERENCE, delete $args{'server_reference'});
784 }
785  
786 13 foreach my $key (keys %args) {
787 # 3.14.2.2.4 User Property (utf8 string pair)
788 0 my $val = $args{$key};
789 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
790 0 utf8::encode( $key );
791 0 utf8::encode( $val );
792 0 $raw_prop .= pack("C n/a* n/a*", MQTT_USER_PROPERTY, $key, $val);
793 }
794  
795 # 3.14.2 Variable Header
796  
797 # 3.14.2.1 Disconnect Reason Code (byte)
798 13 ▶ my $raw_mqtt = pack("C", $reason_code || 0);
Condition50%1/2
$reason_code || 0
l!l
013
MC/DC0%0/1
$reason_code || 0
$reason_code
Truth table50%1/2
A
A= $reason_code
Aresult
11
00
799  
800 # 3.14.2.2 Properties
801 13 $raw_mqtt .= _encode_var_int(length $raw_prop);
802 13 $raw_mqtt .= $raw_prop;
803  
804 $self->{handle}->push_write(
805 13 pack("C", MQTT_DISCONNECT << 4) . # 3.14.1 Packet type
806 _encode_var_int(length $raw_mqtt) . # 3.14.1 Packet length
807 $raw_mqtt
808 );
809  
810 13 $self->_reset_connection;
811 }
812  
813 13 ▶sub _reset_connection {
Subroutine _reset_connection: called CC=1 SCAR=0.0
814 13 my ($self) = @_;
815  
816 13 $self->{handle} = undef;
817  
818 13 $self->{is_connected} = undef;
819 13 $self->{reconnect_tmr} = undef;
820 13 $self->{timeout_tmr} = undef;
821 13 $self->{connect_err} = undef;
822  
823 13 $self->{server_prop} = {};
824 13 $self->{server_alias} = {};
825 13 $self->{client_alias} = {};
826 13 $self->{subscriptions} = {};
827 13 $self->{subscr_cb} = {};
828 13 $self->{packet_cb} = {};
829 13 $self->{buffers} = {};
830 13 $self->{packet_seq} = 1;
831 13 $self->{subscr_seq} = 1;
832 13 $self->{alias_seq} = 1;
833 13 $self->{use_alias} = 0;
834 }
835  
836 0 ▶sub _receive_disconnect {
Subroutine _receive_disconnect: not called CC=9 SCAR=45.0
837 0 my ($self, $packet) = @_;
838  
839 # Handle abbreviated packet
840 0 ▶ $$packet = "\x00\x00" if (length $$packet == 0);
Branch: 1 branch
BranchTrueFalseTotal
if length $$packet == 0 0 0 0
841 0 ▶ $$packet .= "\x00" if (length $$packet == 1);
Branch: 1 branch
BranchTrueFalseTotal
if length $$packet == 1 0 0 0
842  
843 # 3.14.2.1 Reason Code (byte)
844 0 my $offs = 0;
845 0 my $reason_code = _decode_byte($packet, \$offs);
846 0 my $reason = $Disconnect_reason_code{$reason_code};
847  
848 # 3.14.2.2.1 Property Length (variable length int)
849 0 my $prop_len = _decode_var_int($packet, \$offs);
850 0 my $prop_end = $offs + $prop_len;
851  
852 0 my %prop = (
853 'reason_code' => $reason_code,
854 'reason' => $reason,
855 );
856  
857 0 ▶ while ($offs < $prop_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offs < $prop_end) 0 0 0
858  
859 0 my $prop_id = _decode_byte($packet, \$offs);
860  
861 0 ▶ if ($prop_id == MQTT_SESSION_EXPIRY_INTERVAL) {
Branch: 4 branches
BranchTrueFalseTotal
if ($prop_id == 17) { } 0 0 0
elsif ($prop_id == 31) { } 0 0 0
elsif ($prop_id == 38) { } 0 0 0
elsif ($prop_id == 28) { } 0 0 0
862 # 3.14.2.2.2 Session Expiry Interval (long int)
863 0 $prop{'session_expiry_interval'} = _decode_int_32($packet, \$offs);
864 }
865 elsif ($prop_id == MQTT_REASON_STRING) {
866 # 3.14.2.2.3 Reason String (utf8 string)
867 0 $prop{'reason_string'} = _decode_utf8_str($packet, \$offs);
868 }
869 elsif ($prop_id == MQTT_USER_PROPERTY) {
870 # 3.14.2.2.4 User Property (utf8 string pair)
871 0 my $key = _decode_utf8_str($packet, \$offs);
872 0 my $val = _decode_utf8_str($packet, \$offs);
873 0 $prop{$key} = $val;
874 }
875 elsif ($prop_id == MQTT_SERVER_REFERENCE) {
876 # 3.14.2.2.5 Server Reference (utf8 string)
877 0 $prop{'server_reference'} = _decode_utf8_str($packet, \$offs);
878 }
879 else {
880 # Protocol error
881 0 $self->_fatal("Received DISCONNECT with unknown property $prop_id");
882 }
883 }
884  
885 0 $self->_reset_connection;
886  
887 0 my $disconn_cb = $self->{disconn_cb};
888  
889 0 ▶ if ($disconn_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disconn_cb) { } 0 0 0
890 0 $disconn_cb->(\%prop);
891 }
892 else {
893 0 $self->_fatal("Disconnected from MQTT broker: $prop{reason}");
894 }
895 }
896  
897  
898 0 ▶sub pingreq {
Subroutine pingreq: not called CC=1 SCAR=6.9
Pod undocumented
899 0 my ($self) = @_;
900  
901 $self->{handle}->push_write(
902 0 pack( "C C",
903 MQTT_PINGREQ << 4, # 3.12.1 Packet type
904 0, # 3.12.1 Remaining length
905 )
906 );
907 }
908  
909 0 ▶sub pingresp {
Subroutine pingresp: not called CC=1 SCAR=6.9
Pod undocumented
910 0 my ($self) = @_;
911  
912 $self->{handle}->push_write(
913 0 pack( "C C",
914 MQTT_PINGRESP << 4, # 3.13.1 Packet type
915 0, # 3.13.1 Remaining length
916 )
917 );
918 }
919  
920  
921 19 ▶sub subscribe {
Subroutine subscribe: called CC=16 SCAR=30.5
Pod documented
922 19 my ($self, %args) = @_;
923  
924 19 my $topic = delete $args{'topic'};
925 19 my $topics = delete $args{'topics'};
926 19 my $subscr_cb = delete $args{'on_publish'};
927 19 my $suback_cb = delete $args{'on_suback'};
928 19 my $max_qos = delete $args{'maximum_qos'};
929 19 my $no_local = delete $args{'no_local'};
930 19 my $retain_asp = delete $args{'retain_as_published'};
931 19 my $retain_hdl = delete $args{'retain_handling'};
932  
933 19 ▶ $topics = [] unless defined $topics;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topics 19 0 19
934 19 ▶ push (@$topics, $topic) if defined $topic;
Branch: 1 branch
BranchTrueFalseTotal
if defined $topic 19 0 19
935  
936 19 ▶ croak "Subscription topics were not specified" unless @$topics;
Branch: 1 branch
BranchTrueFalseTotal
unless @$topics 0 19 19
937 19 ▶ croak "on_publish callback is required" unless $subscr_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $subscr_cb 0 19 19
938  
939 19 foreach my $topic (@$topics) {
940 19 ▶ croak "Undefined subscription topic" unless defined $topic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic 0 19 19
941 19 ▶ croak "Empty subscription topic" unless length $topic;
Branch: 1 branch
BranchTrueFalseTotal
unless length $topic 0 19 19
942 }
943  
944 19 my $packet_id = $self->{packet_seq}++;
945 19 ▶ $self->{packet_seq} = 1 if $packet_id == 0xFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $packet_id == 65535 0 19 19
946  
947 # Set callback for incomings PUBLISH
948 19 my $subscr_id = $self->{subscr_seq}++;
949 19 $self->{subscr_cb}->{$subscr_id} = $subscr_cb;
950  
951 # Parameters for expected SUBACK
952 19 $self->{packet_cb}->{$packet_id} = {
953 topics => [ @$topics ], # copy
954 subscr_id => $subscr_id,
955 suback_cb => $suback_cb,
956 };
957  
958 # 3.8.2.1.2 Subscription Identifier (variable len int)
959 19 my $raw_prop = pack("C", MQTT_SUBSCRIPTION_IDENTIFIER) .
960 _encode_var_int($subscr_id);
961  
962 # 3.8.2.1.3 User Property (utf8 string pair)
963 19 foreach my $key (keys %args) {
964 0 my $val = $args{$key};
965 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
966 0 utf8::encode( $key );
967 0 utf8::encode( $val );
968 0 $raw_prop .= pack("C n/a* n/a*", MQTT_USER_PROPERTY, $key, $val);
969 }
970  
971 19 my $raw_mqtt = pack("n", $packet_id) . # 3.8.2 Packet identifier
972 _encode_var_int(length $raw_prop) . # 3.8.2.1 Properties Length
973 $raw_prop; # 3.8.2.1 Properties
974  
975 # 3.8.3.1 Subscription Options
976 19 my $options = 0;
977 19 ▶ $options |= ($max_qos & 0x03) if $max_qos; # Maximum QoS
Branch: 1 branch
BranchTrueFalseTotal
if $max_qos 2 17 19
978 19 ▶ $options |= 0x04 if $no_local; # No Local
Branch: 1 branch
BranchTrueFalseTotal
if $no_local 0 19 19
979 19 ▶ $options |= 0x08 if $retain_asp; # Retain As Published
Branch: 1 branch
BranchTrueFalseTotal
if $retain_asp 0 19 19
980 19 ▶ $options |= ($retain_hdl & 0x03) << 4 if $retain_hdl; # Retain Handling
Branch: 1 branch
BranchTrueFalseTotal
if $retain_hdl 0 19 19
981  
982 # 3.8.3 Payload
983 19 foreach my $topic (@$topics) {
984 19 utf8::encode( $topic );
985 19 $raw_mqtt .= pack("n/a* C", $topic, $options);
986 }
987  
988 $self->{handle}->push_write(
989 19 pack("C", MQTT_SUBSCRIBE << 4 | 0x02) . # 3.8.1 Packet type
990 _encode_var_int(length $raw_mqtt) . # 3.8.1 Packet length
991 $raw_mqtt
992 );
993  
994 19 1;
995 }
996  
997 19 ▶sub _receive_suback {
Subroutine _receive_suback: called CC=10 SCAR=28.1
998 19 my ($self, $packet) = @_;
999  
1000 # 3.9.2 Packet id (short int)
1001 19 my $offs = 0;
1002 19 my $packet_id = _decode_int_16($packet, \$offs);
1003  
1004 # 3.9.2.1.1 Property Length (variable length int)
1005 19 my $prop_len = _decode_var_int($packet, \$offs);
1006 19 my $prop_end = $offs + $prop_len;
1007 19 my %prop;
1008  
1009 19 ▶ while ($offs < $prop_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offs < $prop_end) 0 19 19
1010  
1011 0 my $prop_id = _decode_byte($packet, \$offs);
1012  
1013 0 ▶ if ($prop_id == MQTT_REASON_STRING) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prop_id == 31) { } 0 0 0
elsif ($prop_id == 38) { } 0 0 0
1014 # 3.9.2.1.2 Reason String (utf8 string)
1015 0 $prop{'reason_string'} = _decode_utf8_str($packet, \$offs);
1016 }
1017 elsif ($prop_id == MQTT_USER_PROPERTY) {
1018 # 3.9.2.1.3 User Property (utf8 string pair)
1019 0 my $key = _decode_utf8_str($packet, \$offs);
1020 0 my $val = _decode_utf8_str($packet, \$offs);
1021 0 $prop{$key} = $val;
1022 }
1023 else {
1024 # Protocol error
1025 0 $self->_fatal("Received SUBACK with unexpected property $prop_id");
1026 }
1027 }
1028  
1029 # 3.9.3 Payload
1030 19 my @reason_codes = unpack("C*", substr($$packet, $offs));
1031  
1032 19 my $packet_cb = delete $self->{packet_cb}->{$packet_id};
1033 19 ▶ $self->_fatal("Received unexpected SUBACK") unless $packet_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $packet_cb 0 19 19
1034  
1035 19 my $topics = $packet_cb->{topics};
1036 19 my $suback_cb = $packet_cb->{suback_cb};
1037 19 my $subscr_id = $packet_cb->{subscr_id};
1038  
1039 19 my $success = 1;
1040 19 my @properties;
1041  
1042 19 foreach my $code (@reason_codes) {
1043  
1044 19 my $topic = shift @$topics;
1045 19 my $reason = $Subscribe_reason_code{$code};
1046 19 my $granted_qos;
1047  
1048 19 ▶ if ($code <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code <= 2) { } 19 0 19
1049 # Success
1050 19 $granted_qos = $code;
1051 19 $self->{subscriptions}->{$topic} = $subscr_id;
1052 }
1053 else {
1054 # Failure
1055 0 $success = 0;
1056 0 $granted_qos = undef;
1057 0 ▶ unless ($suback_cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($suback_cb) 0 0 0
1058 0 $self->_fatal("Subscription to topic '$topic' failed: $reason");
1059 }
1060 }
1061  
1062 19 ▶ $DEBUG && warn "Subscribed to: $topic\n";
Branch: 1 branch
BranchTrueFalseTotal
$Beekeeper::MQTT::DEBUG and warn "Subscribed to: $topic\n" 0 19 19
1063  
1064 19 push @properties, {
1065 topic => $topic,
1066 reason_code => $code,
1067 granted_qos => $granted_qos,
1068 reason => $reason,
1069 %prop
1070 };
1071 }
1072  
1073 19 ▶ $suback_cb->($success, @properties) if $suback_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $suback_cb 6 13 19
1074 }
1075  
1076  
1077 0 ▶sub unsubscribe {
Subroutine unsubscribe: not called CC=12 SCAR=50.5
Pod documented
1078 0 my ($self, %args) = @_;
1079  
1080 0 my $topic = delete $args{'topic'};
1081 0 my $topics = delete $args{'topics'};
1082 0 my $unsuback_cb = delete $args{'on_unsuback'};
1083  
1084 0 ▶ $topics = [] unless defined $topics;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topics 0 0 0
1085 0 ▶ push (@$topics, $topic) if defined $topic;
Branch: 1 branch
BranchTrueFalseTotal
if defined $topic 0 0 0
1086  
1087 0 ▶ croak "Unsubscription topics were not specified" unless @$topics;
Branch: 1 branch
BranchTrueFalseTotal
unless @$topics 0 0 0
1088 0 ▶ croak "on_unsuback callback is required" unless $unsuback_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $unsuback_cb 0 0 0
1089  
1090 0 foreach my $topic (@$topics) {
1091 0 ▶ croak "Undefined unsubscription topic" unless defined $topic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic 0 0 0
1092 0 ▶ croak "Empty unsubscription topic" unless length $topic;
Branch: 1 branch
BranchTrueFalseTotal
unless length $topic 0 0 0
1093 }
1094  
1095 0 my $packet_id = $self->{packet_seq}++;
1096 0 ▶ $self->{packet_seq} = 1 if $packet_id == 0xFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $packet_id == 65535 0 0 0
1097  
1098 # Set callback for UNSUBACK
1099 0 $self->{packet_cb}->{$packet_id} = {
1100 topics => [ @$topics ], # copy
1101 unsuback_cb => $unsuback_cb,
1102 };
1103  
1104 # 3.10.2.1.2 User Property (utf8 string pair)
1105 0 my $raw_prop = '';
1106 0 foreach my $key (keys %args) {
1107 0 my $val = $args{$key};
1108 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
1109 0 utf8::encode( $key );
1110 0 utf8::encode( $val );
1111 0 $raw_prop .= pack("C n/a* n/a*", MQTT_USER_PROPERTY, $key, $val);
1112 }
1113  
1114 0 my $raw_mqtt = pack("n", $packet_id) . # 3.10.2 Packet identifier
1115 _encode_var_int(length $raw_prop) . # 3.10.2.1 Property Length
1116 $raw_prop; # 3.10.2.1 Properties
1117  
1118 # 3.10.3 Payload
1119 0 foreach my $topic (@$topics) {
1120 0 utf8::encode($topic);
1121 0 $raw_mqtt .= pack("n/a*", $topic);
1122 }
1123  
1124 $self->{handle}->push_write(
1125 0 pack("C", MQTT_UNSUBSCRIBE << 4 | 0x02) . # 3.10.1 Packet type
1126 _encode_var_int(length $raw_mqtt) . # 3.10.1 Packet length
1127 $raw_mqtt
1128 );
1129  
1130 0 1;
1131 }
1132  
1133 0 ▶sub _receive_unsuback {
Subroutine _receive_unsuback: not called CC=11 SCAR=48.8
1134 0 my ($self, $packet) = @_;
1135 0 weaken($self);
1136  
1137 # 3.11.2 Packet id (short int)
1138 0 my $offs = 0;
1139 0 my $packet_id = _decode_int_16($packet, \$offs);
1140  
1141 # 3.11.2.1.1 Property Length (variable length int)
1142 0 my $prop_len = _decode_var_int($packet, \$offs);
1143 0 my $prop_end = $offs + $prop_len;
1144 0 my %prop;
1145  
1146 0 ▶ while ($offs < $prop_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offs < $prop_end) 0 0 0
1147  
1148 0 my $prop_id = _decode_byte($packet, \$offs);
1149  
1150 0 ▶ if ($prop_id == MQTT_REASON_STRING) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prop_id == 31) { } 0 0 0
elsif ($prop_id == 38) { } 0 0 0
1151 # 3.11.2.1.2 Reason String (utf8 string)
1152 0 $prop{'reason_string'} = _decode_utf8_str($packet, \$offs);
1153 }
1154 elsif ($prop_id == MQTT_USER_PROPERTY) {
1155 # 3.11.2.1.3 User Property (utf8 string pair)
1156 0 my $key = _decode_utf8_str($packet, \$offs);
1157 0 my $val = _decode_utf8_str($packet, \$offs);
1158 0 $prop{$key} = $val;
1159 }
1160 else {
1161 # Protocol error
1162 0 $self->_fatal("Received UNSUBACK with unexpected property $prop_id");
1163 }
1164 }
1165  
1166 # 3.11.3 Payload
1167 0 my @reason_codes = unpack("C*", substr($$packet, $offs));
1168  
1169 0 my $packet_cb = delete $self->{packet_cb}->{$packet_id};
1170 0 ▶ $self->_fatal("Received unexpected UNSUBACK") unless $packet_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $packet_cb 0 0 0
1171  
1172 0 my $topics = $packet_cb->{topics};
1173 0 my $unsuback_cb = $packet_cb->{unsuback_cb};
1174  
1175 0 my $success = 1;
1176 0 my @properties;
1177  
1178 0 foreach my $code (@reason_codes) {
1179  
1180 0 my $topic = shift @$topics;
1181 0 my $reason = $Reason_code{$code};
1182  
1183 0 ▶ if ($code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 0) { } 0 0 0
1184 # Success
1185 0 my $subs = $self->{subscriptions};
1186 0 my $subscr_id = delete $subs->{$topic};
1187 0 ▶ if ($subscr_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subscr_id) 0 0 0
1188 # Free on_publish callback if not used by another subscription
1189 0 my @still_used = grep { $subs->{$_} == $subscr_id } keys %$subs;
1190 0 ▶ unless (@still_used) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@still_used) 0 0 0
1191 # But not right now, as broker may send some messages *after* unsubscription
1192 0 ▶ $self->{_timers}->{"unsub-$subscr_id"} = AnyEvent->timer( after => 60, cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1193 0 delete $self->{_timers}->{"unsub-$subscr_id"};
1194 0 delete $self->{subscr_cb}->{$subscr_id};
1195 0 });
1196 }
1197 }
1198 }
1199 else {
1200 # Failure
1201 0 $success = 0;
1202 0 ▶ unless ($unsuback_cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unsuback_cb) 0 0 0
1203 0 $self->_fatal("Unsubscription to topic '$topic' failed: $reason");
1204 }
1205 }
1206  
1207 0 push @properties, {
1208 topic => $topic,
1209 reason_code => $code,
1210 reason => $reason,
1211 %prop
1212 };
1213 }
1214  
1215 0 ▶ $unsuback_cb->($success, @properties) if $unsuback_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $unsuback_cb 0 0 0
1216 }
1217  
1218 11 our $AE_WAITING;
1219  
1220 90 ▶sub publish {
Subroutine publish: called CC=30 SCAR=40.3
Pod documented
1221 90 my ($self, %args) = @_;
1222  
1223 90 my $topic = delete $args{'topic'};
1224 90 my $payload = delete $args{'payload'};
1225 90 my $qos = delete $args{'qos'};
1226 90 my $dup = delete $args{'duplicate'};
1227 90 my $retain = delete $args{'retain'};
1228 90 my $on_puback = delete $args{'on_puback'};
1229 90 my $buffer_id = delete $args{'buffer_id'};
1230  
1231 90 ▶ croak "Message topic was not specified" unless defined $topic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic 0 90 90
1232  
1233 90 ▶ $DEBUG && warn "Sent message to: $topic\n";
Branch: 1 branch
BranchTrueFalseTotal
$Beekeeper::MQTT::DEBUG and warn "Sent message to: $topic\n" 0 90 90
1234  
1235 90 ▶ $payload = '' unless defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 0 90 90
1236 90 ▶ my $payload_ref = (ref $payload eq 'SCALAR') ? $payload : \$payload;
Branch: 1 branch
BranchTrueFalseTotal
ref $payload eq 'SCALAR' ? : 74 16 90
1237  
1238 # 3.3.2.3.4 Topic Alias
1239 90 my $topic_alias;
1240 90 ▶ if ($self->{use_alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_alias"}) 0 90 90
1241 0 $topic_alias = $self->{client_alias}->{$topic};
1242 0 ▶ if ($topic_alias) {
Branch: 2 branches
BranchTrueFalseTotal
if ($topic_alias) { } 0 0 0
elsif ($self->{'server_prop'}{'topic_alias_maximum'}) { } 0 0 0
1243 # Send topic alias only
1244 0 $topic = '';
1245 }
1246 elsif ($self->{server_prop}->{'topic_alias_maximum'}) {
1247 #TODO: Honor maximum
1248 0 $topic_alias = $self->{alias_seq}++;
1249 0 $self->{client_alias}->{$topic} = $topic_alias;
1250 }
1251 }
1252  
1253 # 3.3.1.2 QoS level
1254 90 my $flags = 0;
1255 90 ▶ $flags |= $qos << 1 if $qos;
Branch: 1 branch
BranchTrueFalseTotal
if $qos 70 20 90
1256 90 ▶ $flags |= 0x04 if $dup;
Branch: 1 branch
BranchTrueFalseTotal
if $dup 0 90 90
1257 90 ▶ $flags |= 0x01 if $retain;
Branch: 1 branch
BranchTrueFalseTotal
if $retain 0 90 90
1258  
1259 90 my $packet_id;
1260 90 ▶ if ($qos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qos) 70 20 90
1261 70 $packet_id = $self->{packet_seq}++;
1262 70 ▶ $self->{packet_seq} = 1 if $packet_id == 0xFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $packet_id == 65535 0 70 70
1263 }
1264  
1265 90 my $raw_prop = '';
1266  
1267 90 ▶ if (utf8::is_utf8( $$payload_ref )) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($$payload_ref)) 1 89 90
1268 # 3.3.2.3.2 Payload Format Indicator (byte)
1269 1 $raw_prop .= pack("C C", MQTT_PAYLOAD_FORMAT_INDICATOR, 0x01);
1270 1 utf8::encode( $$payload_ref );
1271 }
1272  
1273 90 ▶ if (exists $args{'message_expiry_interval'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"message_expiry_interval"}) 0 90 90
1274 # 3.3.2.3.3 Message Expiry Interval (long int)
1275 0 $raw_prop .= pack("C N", MQTT_MESSAGE_EXPIRY_INTERVAL, delete $args{'message_expiry_interval'});
1276 }
1277  
1278 90 ▶ if ($topic_alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topic_alias) 0 90 90
1279 # 3.3.2.3.4 Topic Alias (short int)
1280 0 $raw_prop .= pack("C n", MQTT_TOPIC_ALIAS, $topic_alias);
1281 }
1282  
1283 90 ▶ if (exists $args{'response_topic'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"response_topic"}) 68 22 90
1284 # 3.3.2.3.5 Response Topic (utf8 string)
1285 68 utf8::encode( $args{'response_topic'} );
1286 68 $raw_prop .= pack("C n/a*", MQTT_RESPONSE_TOPIC, delete $args{'response_topic'});
1287 }
1288  
1289 90 ▶ if (exists $args{'correlation_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"correlation_data"}) 0 90 90
1290 # 3.3.2.3.6 Correlation Data (binary data)
1291 0 $raw_prop .= pack("C n/a*", MQTT_CORRELATION_DATA, delete $args{'correlation_data'});
1292 }
1293  
1294 # if (exists $args{'subscription_identifier'}) {
1295 # # 3.3.2.3.8 Subscription Identifier (variable int)
1296 # my $id = delete $args{'subscription_identifier'};
1297 # $raw_prop .= pack("C", MQTT_SUBSCRIPTION_IDENTIFIER) . _encode_var_int($id);
1298 # }
1299  
1300 90 ▶ if (exists $args{'content_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"content_type"}) 0 90 90
1301 # 3.3.2.3.9 Content Type (utf8 string)
1302 0 utf8::encode( $args{'content_type'} );
1303 0 $raw_prop .= pack("C n/a*", MQTT_CONTENT_TYPE, delete $args{'content_type'});
1304 }
1305  
1306 90 foreach my $key (keys %args) {
1307 # 3.3.2.3.7 User Property (utf8 string pair)
1308 20 my $val = $args{$key};
1309 20 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 20 20
1310 20 utf8::encode( $key );
1311 20 utf8::encode( $val );
1312 20 $raw_prop .= pack("C n/a* n/a*", MQTT_USER_PROPERTY, $key, $val);
1313 }
1314  
1315 # 3.3.2.1 Topic name (utf8 string)
1316 90 utf8::encode( $topic );
1317 90 my $raw_mqtt = pack("n/a*", $topic);
1318  
1319 # 3.3.2.2 Packet identifier (short int)
1320 90 ▶ $raw_mqtt .= pack("n", $packet_id) if $packet_id;
Branch: 1 branch
BranchTrueFalseTotal
if $packet_id 70 20 90
1321  
1322 # 3.3.2.3 Properties
1323 90 $raw_mqtt .= _encode_var_int(length $raw_prop);
1324 90 $raw_mqtt .= $raw_prop;
1325  
1326 # 3.3.3 Payload
1327 90 $raw_mqtt .= $$payload_ref;
1328  
1329 90 $raw_mqtt = pack("C", MQTT_PUBLISH << 4 | $flags) . # 3.3.1 Packet type
1330 _encode_var_int(length $raw_mqtt) . # 3.3.1 Packet length
1331 $raw_mqtt;
1332  
1333 90 ▶ if ($qos && $on_puback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qos and $on_puback) 0 90 90
Condition66%2/3
$qos and $on_puback
!ll&&!rl&&r
20700
MC/DC0%0/2
$qos and $on_puback
$qos $on_puback
Truth table66%2/3
A and B
A= $qosB= $on_puback
ABresult
0X0
100
111
1334 # Set PUBACK callback
1335 0 $self->{packet_cb}->{$packet_id} = $on_puback;
1336 }
1337  
1338 90 ▶ if ($buffer_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer_id) 0 90 90
1339 # Do not send right now, wait until flush_buffer
1340 0 ▶ my $buffer = $self->{buffers}->{$buffer_id} ||= {};
Condition0%0/2
$self->{"buffers"}{$buffer_id} ||= {}
l!l
00
MC/DC0%0/1
$self->{"buffers"}{$buffer_id} ||= {}
$self->{"buffers"}{$buffer_id}
Truth table0%0/2
A
A= $self->{"buffers"}{$buffer_id}
Aresult
11
00
1341 0 $buffer->{raw_mqtt} .= $raw_mqtt;
1342 0 ▶ $buffer->{packets}->{$packet_id} = 1 if $packet_id;
Branch: 1 branch
BranchTrueFalseTotal
if $packet_id 0 0 0
1343 0 return 1;
1344 }
1345  
1346 90 $self->{handle}->push_write( $raw_mqtt );
1347  
1348 90 ▶ if (defined $self->{handle}->{wbuf} && length $self->{handle}->{wbuf} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"handle"}{"wbuf"} and length $self->{"handle"}{"wbuf"} > 0) 1 89 90
Condition66%2/3
defined $self->{"handle"}{"wbuf"} and length $self->{"handle"}{"wbuf"} > 0
!ll&&!rl&&r
0891
MC/DC50%1/2
defined $self->{"handle"}{"wbuf"} and length $self->{"handle"}{"wbuf"} > 0
defined $self->{"handle"}{"wbuf"} length $self->{"handle"}{"wbuf"} > 0
Truth table66%2/3
A and B
A= defined $self->{"handle"}{"wbuf"}B= length $self->{"handle"}{"wbuf"} > 0
ABresult
0X0
100
111
1349 # push_write could not send all data to the handle because the kernel
1350 # write buffer is full. The size of kernel write bufer (which can be
1351 # queried with 'sysctl net.ipv4.tcp_wmem') is choosed by the kernel
1352 # based on available memory, and is 4MB in known production servers.
1353 # This will happen after sending more that 4MB of data very quickly.
1354 # As client may be syncronous, wait until entire message is sent.
1355  
1356 # Make AnyEvent allow one level of recursive condvar blocking
1357 1 ▶ $AE_WAITING && Carp::confess "Recursive condvar blocking wait attempted";
Branch: 1 branch
BranchTrueFalseTotal
$Beekeeper::MQTT::AE_WAITING and Carp::confess("Recursive condvar blocking wait attempted") 0 1 1
1358 1 local $AE_WAITING = 1;
1359 1 local $AnyEvent::CondVar::Base::WAITING = 0;
1360  
1361 1 my $flushed = AnyEvent->condvar;
1362 1 $self->{handle}->on_drain( $flushed );
1363 1 $flushed->recv;
1364 1 $self->{handle}->on_drain(); # clear
1365 }
1366 }
1367  
1368 84 ▶sub _receive_publish {
Subroutine _receive_publish: called CC=15 SCAR=30.9
1369 84 my ($self, $packet, $flags) = @_;
1370  
1371 # 3.3.2.1 Topic Name (utf8 str)
1372 84 my $topic = unpack("n/a", $$packet);
1373 84 my $offs = 2 + length $topic;
1374 84 utf8::decode($topic);
1375  
1376 84 ▶ $DEBUG && warn "Got message from: $topic\n";
Branch: 1 branch
BranchTrueFalseTotal
$Beekeeper::MQTT::DEBUG and warn "Got message from: $topic\n" 0 84 84
1377  
1378 84 my %prop = (
1379 'topic' => $topic,
1380 'qos' => ($flags & 0x6) >> 1,
1381 'dup' => ($flags & 0x8) >> 3,
1382 );
1383  
1384 # 3.3.2.2 Packet Identifier (short int)
1385 84 ▶ if ($prop{'qos'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop{"qos"} > 0) 3 81 84
1386 3 $prop{'packet_id'} = unpack("n", substr($$packet, $offs, 2));
1387 3 $offs += 2;
1388 }
1389  
1390 # 3.3.2.3.1 Properties Length (variable length int)
1391 84 my $prop_len = _decode_var_int($packet, \$offs);
1392 84 my $prop_end = $offs + $prop_len;
1393  
1394 84 my @subscr_ids;
1395 my $prop_id;
1396  
1397 84 ▶ while ($offs < $prop_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offs < $prop_end) 173 84 257
1398  
1399 173 $prop_id = unpack("C", substr($$packet, $offs, 1));
1400 173 $offs += 1;
1401  
1402 173 ▶ if ($prop_id == MQTT_PAYLOAD_FORMAT_INDICATOR) {
Branch: 8 branches
BranchTrueFalseTotal
if ($prop_id == 1) { } 1 172 173
elsif ($prop_id == 2) { } 0 172 172
elsif ($prop_id == 35) { } 0 172 172
elsif ($prop_id == 8) { } 1 171 172
elsif ($prop_id == 9) { } 0 171 171
elsif ($prop_id == 38) { } 87 84 171
elsif ($prop_id == 11) { } 84 0 84
elsif ($prop_id == 3) { } 0 0 0
1403 # 3.3.2.3.2 Payload Format Indicator (byte)
1404 1 $prop{'payload_format'} = unpack("C", substr($$packet, $offs, 1));
1405 1 $offs += 1;
1406 }
1407 elsif ($prop_id == MQTT_MESSAGE_EXPIRY_INTERVAL) {
1408 # 3.3.2.3.3 Message Expiry Interval (long int)
1409 0 $prop{'message_expiry_interval'} = unpack("N", substr($$packet, $offs, 4));
1410 0 $offs += 4;
1411 }
1412 elsif ($prop_id == MQTT_TOPIC_ALIAS) {
1413 # 3.3.2.3.4 Topic Alias (short int)
1414 0 my $alias = unpack("n", substr($$packet, $offs, 2));
1415 0 $offs += 2;
1416 0 ▶ if (length $topic) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $topic) { } 0 0 0
1417 0 $self->{server_alias}->{$alias} = $topic;
1418 }
1419 else {
1420 0 $prop{'topic'} = $self->{server_alias}->{$alias};
1421 }
1422 }
1423 elsif ($prop_id == MQTT_RESPONSE_TOPIC) {
1424 # 3.3.2.3.5 Response Topic (utf8 string)
1425 1 my $resp_topic = unpack("n/a", substr($$packet, $offs));
1426 1 $offs += 2 + length $resp_topic;
1427 1 utf8::decode( $resp_topic );
1428 1 $prop{'response_topic'} = $resp_topic;
1429 }
1430 elsif ($prop_id == MQTT_CORRELATION_DATA) {
1431 # 3.3.2.3.6 Correlation Data (binary data)
1432 0 $prop{'correlation_data'} = unpack("n/a", substr($$packet, $offs));
1433 0 $offs += 2 + length $prop{'correlation_data'};
1434 }
1435 elsif ($prop_id == MQTT_USER_PROPERTY) {
1436 # 3.3.2.3.7 User Property (utf8 string pair)
1437 87 my ($key, $val) = unpack("n/a n/a", substr($$packet, $offs));
1438 87 $offs += 4 + length($key) + length($val);
1439 87 utf8::decode( $key );
1440 87 utf8::decode( $val );
1441 87 $prop{$key} = $val;
1442 }
1443 elsif ($prop_id == MQTT_SUBSCRIPTION_IDENTIFIER) {
1444 # 3.3.2.3.8 Subscription Identifier (variable int)
1445 84 push @subscr_ids, _decode_var_int($packet, \$offs);
1446 }
1447 elsif ($prop_id == MQTT_CONTENT_TYPE) {
1448 # 3.3.2.3.9 Content Type (utf8 string)
1449 0 my $content_type = unpack("n/a", substr($$packet, $offs));
1450 0 $offs += 2 + length $content_type;
1451 0 utf8::decode( $content_type );
1452 0 $prop{'content_type'} = $content_type;
1453 }
1454 else {
1455 # Protocol error
1456 0 $self->_fatal("Received PUBLISH with unknown property $prop_id");
1457 }
1458 }
1459  
1460 # Trim variable header from packet, the remaining is the payload
1461 84 substr($$packet, 0, $prop_end, '');
1462  
1463 84 ▶ if ($prop{'payload_format'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop{"payload_format"}) 1 83 84
1464 # Payload is UTF-8 Encoded Character Data
1465 1 utf8::decode( $$packet );
1466 }
1467  
1468 84 foreach (@subscr_ids) {
1469 # Execute subscriptions callbacks
1470  
1471 84 $self->{subscr_cb}->{$_}->($packet, \%prop);
1472 }
1473 }
1474  
1475  
1476 3 ▶sub puback {
Subroutine puback: called CC=4 SCAR=15.5
Pod documented
1477 3 my ($self, %args) = @_;
1478  
1479 3 ▶ croak "Missing packet_id" unless $args{'packet_id'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"packet_id"} 0 3 3
1480  
1481 my $raw_mqtt = pack(
1482 "C C n C",
1483 MQTT_PUBACK << 4, # 3.4.1 Packet type
1484 3, # 3.4.1 Remaining length
1485 $args{'packet_id'}, # 3.4.2 Packet identifier
1486 3 ▶ $args{'reason_code'} || 0, # 3.4.2.1 Reason code
Condition50%1/2
$args{'reason_code'} || 0
l!l
03
MC/DC0%0/1
$args{'reason_code'} || 0
$args{'reason_code'}
Truth table50%1/2
A
A= $args{'reason_code'}
Aresult
11
00
1487 );
1488  
1489 3 ▶ if ($args{'buffer_id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"buffer_id"}) 0 3 3
1490 # Do not send right now, wait until flush_buffer
1491 0 $self->{buffers}->{$args{'buffer_id'}}->{raw_mqtt} .= $raw_mqtt;
1492 0 return 1;
1493 }
1494  
1495 3 $self->{handle}->push_write( $raw_mqtt );
1496  
1497 3 1;
1498 }
1499  
1500 70 ▶sub _receive_puback {
Subroutine _receive_puback: called CC=3 SCAR=11.8
1501 70 my ($self, $packet) = @_;
1502  
1503 70 my ($packet_id, $reason_code) = unpack("n C", $$packet);
1504 70 ▶ $reason_code = 0 unless defined $reason_code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason_code 0 70 70
1505  
1506 #TODO: 3.5.2.2 Properties
1507  
1508 70 my $puback_cb = delete $self->{packet_cb}->{$packet_id};
1509 70 ▶ return unless defined $puback_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $puback_cb 70 0 70
1510  
1511 0 $puback_cb->($reason_code);
1512 }
1513  
1514 0 ▶sub pubrec {
Subroutine pubrec: not called CC=3 SCAR=24.8
Pod undocumented
1515 0 my ($self, %args) = @_;
1516  
1517 0 ▶ croak "Missing packet_id" unless $args{'packet_id'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"packet_id"} 0 0 0
1518  
1519 my $raw_mqtt = pack(
1520 "C C n C",
1521 MQTT_PUBREC << 4, # 3.5.1 Packet type
1522 3, # 3.5.1 Remaining length
1523 $args{'packet_id'}, # 3.5.2 Packet identifier
1524 0 ▶ $args{'reason_code'} || 0, # 3.5.2.1 Reason code
Condition0%0/2
$args{'reason_code'} || 0
l!l
00
MC/DC0%0/1
$args{'reason_code'} || 0
$args{'reason_code'}
Truth table0%0/2
A
A= $args{'reason_code'}
Aresult
11
00
1525 );
1526  
1527 #TODO: set PUBREL callback
1528  
1529 0 $self->{handle}->push_write( $raw_mqtt );
1530  
1531 0 1;
1532 }
1533  
1534 0 ▶sub _receive_pubrec {
Subroutine _receive_pubrec: not called CC=3 SCAR=24.8
1535 0 my ($self, $packet) = @_;
1536  
1537 0 my ($packet_id, $reason_code) = unpack("n C", $$packet);
1538 0 ▶ $reason_code = 0 unless defined $reason_code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason_code 0 0 0
1539  
1540 #TODO: 3.5.2.2 Properties
1541  
1542 0 my $pubrec_cb = delete $self->{packet_cb}->{$packet_id};
1543 0 ▶ return unless defined $pubrec_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pubrec_cb 0 0 0
1544  
1545 0 $pubrec_cb->($packet_id, $reason_code);
1546 }
1547  
1548 0 ▶sub pubrel {
Subroutine pubrel: not called CC=3 SCAR=24.8
Pod undocumented
1549 0 my ($self, %args) = @_;
1550  
1551 0 ▶ croak "Missing packet_id" unless $args{'packet_id'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"packet_id"} 0 0 0
1552  
1553 my $raw_mqtt = pack(
1554 "C C n C",
1555 MQTT_PUBREL << 4, # 3.6.1 Packet type
1556 3, # 3.6.1 Remaining length
1557 $args{'packet_id'}, # 3.6.2 Packet identifier
1558 0 ▶ $args{'reason_code'} || 0, # 3.6.2.1 Reason code
Condition0%0/2
$args{'reason_code'} || 0
l!l
00
MC/DC0%0/1
$args{'reason_code'} || 0
$args{'reason_code'}
Truth table0%0/2
A
A= $args{'reason_code'}
Aresult
11
00
1559 );
1560  
1561 #TODO: set PUBREC callback
1562  
1563 0 $self->{handle}->push_write( $raw_mqtt );
1564  
1565 0 1;
1566 }
1567  
1568 0 ▶sub _receive_pubrel {
Subroutine _receive_pubrel: not called CC=3 SCAR=24.8
1569 0 my ($self, $packet) = @_;
1570  
1571 0 my ($packet_id, $reason_code) = unpack("n C", $$packet);
1572 0 ▶ $reason_code = 0 unless defined $reason_code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason_code 0 0 0
1573  
1574 #TODO: 3.6.2.2 Properties
1575  
1576 0 my $pubrel_cb = delete $self->{packet_cb}->{$packet_id};
1577 0 ▶ return unless defined $pubrel_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pubrel_cb 0 0 0
1578  
1579 0 $pubrel_cb->($packet_id, $reason_code);
1580 }
1581  
1582 0 ▶sub pubcomp {
Subroutine pubcomp: not called CC=3 SCAR=24.8
Pod undocumented
1583 0 my ($self, %args) = @_;
1584  
1585 0 ▶ croak "Missing packet_id" unless $args{'packet_id'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"packet_id"} 0 0 0
1586  
1587 my $raw_mqtt = pack(
1588 "C C n C",
1589 MQTT_PUBCOMP << 4, # 3.7.1 Packet type
1590 3, # 3.7.1 Remaining length
1591 $args{'packet_id'}, # 3.7.2 Packet identifier
1592 0 ▶ $args{'reason_code'} || 0, # 3.7.2.1 Reason code
Condition0%0/2
$args{'reason_code'} || 0
l!l
00
MC/DC0%0/1
$args{'reason_code'} || 0
$args{'reason_code'}
Truth table0%0/2
A
A= $args{'reason_code'}
Aresult
11
00
1593 );
1594  
1595 0 $self->{handle}->push_write( $raw_mqtt );
1596  
1597 0 1;
1598 }
1599  
1600 0 ▶sub _receive_pubcomp {
Subroutine _receive_pubcomp: not called CC=3 SCAR=24.8
1601 0 my ($self, $packet) = @_;
1602  
1603 0 my ($packet_id, $reason_code) = unpack("n C", $$packet);
1604 0 ▶ $reason_code = 0 unless defined $reason_code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason_code 0 0 0
1605  
1606 #TODO: 3.7.2.2 Properties
1607  
1608 0 my $pubcomp_cb = delete $self->{packet_cb}->{$packet_id};
1609 0 ▶ return unless defined $pubcomp_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pubcomp_cb 0 0 0
1610  
1611 0 $pubcomp_cb->($reason_code);
1612 }
1613  
1614  
1615 0 ▶sub auth {
Subroutine auth: not called CC=7 SCAR=40.3
Pod undocumented
1616 0 my ($self, %args) = @_;
1617  
1618 0 my $reason_code = delete $args{'reason_code'};
1619 0 my $auth_cb = delete $args{'on_auth'};
1620  
1621 # Set callback to be executed when the server answers with an AUTH packet
1622 0 ▶ $self->{packet_cb}->{'auth'} = $auth_cb if $auth_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $auth_cb 0 0 0
1623  
1624 0 my $raw_prop = '';
1625  
1626 0 ▶ if (exists $args{'authentication_method'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"authentication_method"}) 0 0 0
1627 # 3.15.2.2.2 Authentication Method (utf8 string)
1628 0 utf8::encode( $args{'authentication_method'} );
1629 0 $raw_prop .= pack("C n/a*", MQTT_AUTHENTICATION_METHOD, delete $args{'authentication_method'});
1630 }
1631  
1632 0 ▶ if (exists $args{'authentication_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"authentication_data"}) 0 0 0
1633 # 3.15.2.2.3 Authentication Data (binary data)
1634 0 $raw_prop .= pack("C n/a*", MQTT_AUTHENTICATION_DATA, delete $args{'authentication_data'});
1635 }
1636  
1637 0 ▶ if (exists $args{'reason_string'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"reason_string"}) 0 0 0
1638 # 3.15.2.2.4 Reason String (utf8 string)
1639 0 utf8::encode( $args{'reason_string'} );
1640 0 $raw_prop .= pack("C n/a*", MQTT_REASON_STRING, delete $args{'reason_string'});
1641 }
1642  
1643 0 foreach my $key (keys %args) {
1644 # 3.15.2.2.5 User Property (utf8 string pair)
1645 0 my $val = $args{$key};
1646 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
1647 0 utf8::encode( $key );
1648 0 utf8::encode( $val );
1649 0 $raw_prop .= pack("C n/a* n/a*", MQTT_USER_PROPERTY, $key, $val);
1650 }
1651  
1652 # 3.15.2.1 Authenticate Reason Code (byte)
1653 0 my $raw_mqtt = pack("C", $reason_code);
1654  
1655 # 3.15.2.2 Properties
1656 0 $raw_mqtt .= _encode_var_int(length $raw_prop);
1657 0 $raw_mqtt .= $raw_prop;
1658  
1659 $self->{handle}->push_write(
1660 0 pack("C", MQTT_AUTH << 4) . # 3.15.1 Packet type
1661 _encode_var_int(length $raw_mqtt) . # 3.15.1 Packet length
1662 $raw_mqtt
1663 );
1664  
1665 0 1;
1666 }
1667  
1668 0 ▶sub _receive_auth {
Subroutine _receive_auth: not called CC=8 SCAR=42.8
1669 0 my ($self, $packet) = @_;
1670  
1671 # Handle abbreviated packet
1672 0 ▶ $$packet = "\x00\x00" if (length $$packet == 0);
Branch: 1 branch
BranchTrueFalseTotal
if length $$packet == 0 0 0 0
1673  
1674 # 3.15.2.1 Authenticate Reason Code (byte)
1675 0 my $offs = 0;
1676 0 my $reason_code = _decode_byte($packet, \$offs);
1677 0 my $reason = $Reason_code{$reason_code};
1678  
1679 # 3.15.2.2.1 Property Length (variable length int)
1680 0 my $prop_len = _decode_var_int($packet, \$offs);
1681 0 my $prop_end = $offs + $prop_len;
1682  
1683 0 my %prop = (
1684 reason_code => $reason_code,
1685 reason => $reason,
1686 );
1687  
1688 0 ▶ while ($offs < $prop_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offs < $prop_end) 0 0 0
1689  
1690 0 my $prop_id = _decode_byte($packet, \$offs);
1691  
1692 0 ▶ if ($prop_id == MQTT_AUTHENTICATION_METHOD) {
Branch: 4 branches
BranchTrueFalseTotal
if ($prop_id == 21) { } 0 0 0
elsif ($prop_id == 22) { } 0 0 0
elsif ($prop_id == 31) { } 0 0 0
elsif ($prop_id == 38) { } 0 0 0
1693 # 3.15.2.2.2 Authentication Method (utf8 string)
1694 0 $prop{'authentication_method'} = _decode_utf8_str($packet, \$offs);
1695 }
1696 elsif ($prop_id == MQTT_AUTHENTICATION_DATA) {
1697 # 3.15.2.2.3 Authentication Data (binary data)
1698 0 $prop{'authentication_data'} = _decode_binary_data($packet, \$offs);
1699 }
1700 elsif ($prop_id == MQTT_REASON_STRING) {
1701 # 3.15.2.2.4 Reason String (utf8 string)
1702 0 $prop{'reason_string'} = _decode_utf8_str($packet, \$offs);
1703 }
1704 elsif ($prop_id == MQTT_USER_PROPERTY) {
1705 # 3.15.2.2.5 User Property (utf8 string pair)
1706 0 my $key = _decode_utf8_str($packet, \$offs);
1707 0 my $val = _decode_utf8_str($packet, \$offs);
1708 0 $prop{$key} = $val;
1709 }
1710 else {
1711 # Protocol error
1712 0 $self->_fatal("Received AUTH with unexpected property $prop_id");
1713 }
1714 }
1715  
1716 0 my $auth_cb = delete $self->{packet_cb}->{'auth'};
1717  
1718 0 ▶ $auth_cb->(\%prop) if $auth_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $auth_cb 0 0 0
1719 }
1720  
1721  
1722 0 ▶sub flush_buffer {
Subroutine flush_buffer: not called CC=5 SCAR=34.0
Pod documented
1723 0 my ($self, %args) = @_;
1724  
1725 0 my $buffer = delete $self->{buffers}->{$args{'buffer_id'}};
1726  
1727 # Nothing to do if nothing was buffered
1728 0 ▶ return unless $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer 0 0 0
1729  
1730 0 $self->{handle}->push_write( $buffer->{raw_mqtt} );
1731  
1732 0 ▶ if (defined $self->{handle}->{wbuf} && length $self->{handle}->{wbuf} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"handle"}{"wbuf"} and length $self->{"handle"}{"wbuf"} > 0) 0 0 0
Condition0%0/3
defined $self->{"handle"}{"wbuf"} and length $self->{"handle"}{"wbuf"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"handle"}{"wbuf"} and length $self->{"handle"}{"wbuf"} > 0
defined $self->{"handle"}{"wbuf"} length $self->{"handle"}{"wbuf"} > 0
Truth table0%0/3
A and B
A= defined $self->{"handle"}{"wbuf"}B= length $self->{"handle"}{"wbuf"} > 0
ABresult
0X0
100
111
1733  
1734 # Kernel write buffer is full, see publish() above
1735  
1736 # Make AnyEvent allow one level of recursive condvar blocking
1737 0 ▶ $AE_WAITING && Carp::confess "Recursive condvar blocking wait attempted";
Branch: 1 branch
BranchTrueFalseTotal
$Beekeeper::MQTT::AE_WAITING and Carp::confess("Recursive condvar blocking wait attempted") 0 0 0
1738 0 local $AE_WAITING = 1;
1739 0 local $AnyEvent::CondVar::Base::WAITING = 0;
1740  
1741 0 my $flushed = AnyEvent->condvar;
1742 0 $self->{handle}->on_drain( $flushed );
1743 0 $flushed->recv;
1744 0 $self->{handle}->on_drain(); # clear
1745 }
1746  
1747 0 1;
1748 }
1749  
1750 0 ▶sub discard_buffer {
Subroutine discard_buffer: not called CC=3 SCAR=24.8
Pod documented
1751 0 my ($self, %args) = @_;
1752  
1753 0 my $buffer = delete $self->{buffers}->{$args{'buffer_id'}};
1754  
1755 # Nothing to do if nothing was buffered
1756 0 ▶ return unless $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer 0 0 0
1757  
1758 # Remove all pending puback callbacks, as those will never be executed
1759 0 foreach my $packet_id (keys %{$buffer->{packet_ids}}) {
1760 0 delete $self->{packet_cb}->{$packet_id};
1761 }
1762  
1763 0 1;
1764 }
1765  
1766  
1767 13 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=8.1
1768 13 my $self = shift;
1769 # Disconnect gracefully from server if already connected
1770 13 ▶ return unless defined $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"handle"} 13 0 13
1771 0 $self->disconnect;
1772 }
1773  
1774 11 1;
1775  
1776 __END__
« lib/Beekeeper/Logger.pm ↑ Summary blib/lib/auto/share/dist/Beekeeper/chat/lib/MyApp/Client.pm »