blib/lib/AnyEvent/MQTT.pm

filter: Statementstmt 93.9% 419 / 446 Branchbran 87.1% 129 / 148 Conditioncond 57.1% 12 / 21 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 62 / 62 Podpod 100% 8 / 8 totaltotal 90.9% 633 / 696 CC 90 SCAR 15.4
CC
90
Cov
91%
_confirm_subscription
14.0
publish
11.0
_publish_locally
7.9
CRAP 4.7
↑ Summary
1 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package AnyEvent::MQTT;
4 15 $AnyEvent::MQTT::VERSION = '1.212810';
5 # ABSTRACT: AnyEvent module for an MQTT client
6  
7  
8 15 ▶use constant DEBUG => $ENV{ANYEVENT_MQTT_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Net::MQTT::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Net::MQTT::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Net::MQTT::TopicStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 15 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 15 my ($pkg, %p) = @_;
21 15 my $self =
22 bless {
23 socket => undef,
24 host => '127.0.0.1',
25 port => '1883',
26 timeout => 30,
27 wait => 'nothing',
28 keep_alive_timer => 120,
29 qos => MQTT_QOS_AT_MOST_ONCE,
30 message_id => 1,
31 user_name => undef,
32 password => undef,
33 tls => undef,
34 will_topic => undef,
35 will_qos => MQTT_QOS_AT_MOST_ONCE,
36 will_retain => 0,
37 will_message => '',
38 client_id => undef,
39 clean_session => 1,
40 handle_args => [],
41 write_queue => [],
42 inflight => {},
43 _sub_topics => Net::MQTT::TopicStore->new(),
44 %p,
45 }, $pkg;
46 }
47  
48 15 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
49 15 $_[0]->cleanup;
50 }
51  
52  
53 20 ▶sub cleanup {
Subroutine cleanup: called CC=2 SCAR=6.9
Pod documented
54 20 my $self = shift;
55 20 print STDERR "cleanup\n" if DEBUG;
56 20 ▶ if ($self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handle"}) 18 2 20
57 18 my $cv = AnyEvent->condvar;
58 18 my $handle = $self->{handle};
59 18 weaken $handle;
60 18 ▶ $cv->cb(sub { $handle->destroy });
Subroutine __ANON__: called CC=1 SCAR=0.0
61 18 $self->_send(message_type => MQTT_DISCONNECT, cv => $cv);
62 }
63 20 delete $self->{handle};
64 20 delete $self->{connected};
65 20 delete $self->{wait};
66 20 delete $self->{_keep_alive_handle};
67 20 delete $self->{_keep_alive_waiting};
68 20 $self->{write_queue} = [];
69 }
70  
71 5 ▶sub _error {
Subroutine _error: called CC=3 SCAR=11.0
72 5 my ($self, $fatal, $message, $reconnect) = @_;
73 5 $self->cleanup($message);
74 5 ▶ $self->{on_error}->($fatal, $message) if ($self->{on_error});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_error"} 5 0 5
75 5 ▶ $self->_reconnect() if ($reconnect);
Branch: 1 branch
BranchTrueFalseTotal
if $reconnect 3 2 5
76 }
77  
78  
79 8 ▶sub publish {
Subroutine publish: called CC=11 SCAR=24.0
Pod documented
80 8 my ($self, %p) = @_;
81 my $topic = exists $p{topic} ? $p{topic} :
82 8 ▶ croak ref $self, '->publish requires "topic" parameter';
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'topic'} ? : 7 1 8
83 7 ▶ my $qos = exists $p{qos} ? $p{qos} : MQTT_QOS_AT_MOST_ONCE;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'qos'} ? : 4 3 7
84 7 ▶ my $cv = exists $p{cv} ? delete $p{cv} : AnyEvent->condvar;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'cv'} ? : 1 6 7
85 7 my $expect;
86 7 ▶ if ($qos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qos) 3 4 7
87 3 ▶ $expect = ($qos == MQTT_QOS_AT_LEAST_ONCE ? MQTT_PUBACK : MQTT_PUBREC);
Branch: 1 branch
BranchTrueFalseTotal
$qos == 1 ? : 2 1 3
88 }
89 7 my $message = $p{message};
90 7 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) 4 3 7
91 4 print STDERR "publish: message[$message] => $topic\n" if DEBUG;
92 4 $self->_send_with_ack({
93 message_type => MQTT_PUBLISH,
94 %p,
95 }, $cv, $expect);
96 4 return $cv;
97 }
98 my $handle = exists $p{handle} ? $p{handle} :
99 3 ▶ croak ref $self, '->publish requires "message" or "handle" parameter';
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'handle'} ? : 2 1 3
100 2 ▶ unless ($handle->isa('AnyEvent::Handle')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->isa("AnyEvent::Handle")) 1 1 2
101 1 ▶ my @args = @{$p{handle_args}||[]};
Condition50%1/2
$p{'handle_args'} || []
l!l
10
MC/DC0%0/1
$p{'handle_args'} || []
$p{'handle_args'}
Truth table50%1/2
A
A= $p{'handle_args'}
Aresult
11
00
102 1 print STDERR "publish: IO[$handle] => $topic @args\n" if DEBUG;
103 1 $handle = AnyEvent::Handle->new(fh => $handle, @args);
104 }
105 2 my $error_sub = $handle->{on_error}; # Hack: There is no accessor api
106 $handle->on_error(subname 'on_error_for_read_publish_'.$topic =>
107 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
108 2 my ($hdl, $fatal, $msg) = @_;
109 2 ▶ $error_sub->(@_) if ($error_sub);
Branch: 1 branch
BranchTrueFalseTotal
if $error_sub 2 0 2
110 2 $hdl->destroy;
111 2 undef $hdl;
112 2 $cv->send(1);
113 2 });
114 2 my $weak_self = $self;
115 2 weaken $weak_self;
116 2 ▶ my @push_read_args = @{$p{push_read_args}||['line']};
Condition100%2/2
$p{'push_read_args'} || ['line']
l!l
11
MC/DC100%1/1
$p{'push_read_args'} || ['line']
$p{'push_read_args'}
Truth table100%2/2
A
A= $p{'push_read_args'}
Aresult
11
00
117 2 ▶ my $sub; $sub = subname 'push_read_cb_for_'.$topic => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
118 2 my ($hdl, $chunk, @args) = @_;
119 2 print STDERR "publish: $chunk => $topic\n" if DEBUG;
120 2 my $send_cv = AnyEvent->condvar;
121 2 print STDERR "publish: message[$chunk] => $topic\n" if DEBUG;
122 $weak_self->_send_with_ack({
123 message_type => MQTT_PUBLISH,
124 qos => $qos,
125 retain => $p{retain},
126 2 topic => $topic,
127 message => $chunk,
128 }, $send_cv, $expect);
129 $send_cv->cb(subname 'publish_ack_'.$topic =>
130 2 ▶ sub { $handle->push_read(@push_read_args => $sub ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
131 2 return;
132 2 };
133 2 $handle->push_read(@push_read_args => $sub);
134 2 return $cv;
135 }
136  
137  
138 70018 ▶sub next_message_id {
Subroutine next_message_id: called CC=2 SCAR=6.9
Pod documented
139 70018 my $self = shift;
140 70018 my $res = $self->{message_id};
141 70018 $self->{message_id}++;
142 70018 ▶ $self->{message_id} = 1 if $self->{message_id} >= 65536;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"message_id"} >= 65536 1 70017 70018
143 70018 $res;
144 }
145  
146 8 ▶sub _send_with_ack {
Subroutine _send_with_ack: called CC=4 SCAR=13.9
147 8 my ($self, $args, $cv, $expect, $dup) = @_;
148 8 ▶ if ($args->{qos}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'qos'}) { } 5 3 8
149 5 ▶ unless (exists $args->{message_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"message_id"}) 3 2 5
150 3 $args->{message_id} = $self->next_message_id();
151 }
152 5 my $mid = $args->{message_id};
153 5 my $send_cv = AnyEvent->condvar;
154 5 ▶ $send_cv->cb(subname 'ack_cb_for_'.$mid => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
155 $self->{inflight}->{$mid} =
156 {
157 expect => $expect,
158 message => $args,
159 cv => $cv,
160 timeout =>
161 AnyEvent->timer(after => $self->{keep_alive_timer},
162 cb => subname 'ack_timeout_for_'.$mid =>
163 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
164 1 print ref $self, " timeout waiting for ",
165 message_type_string($expect), "\n" if DEBUG;
166 1 delete $self->{inflight}->{$mid};
167 1 $self->_send_with_ack($args, $cv, $expect, 1);
168 5 }),
169 };
170 5 });
171 5 $args->{cv} = $send_cv;
172 } else {
173 3 $args->{cv} = $cv;
174 }
175 8 ▶ $args->{dup} = 1 if ($dup);
Branch: 1 branch
BranchTrueFalseTotal
if $dup 1 7 8
176 8 return $self->_send(%$args);
177 }
178  
179  
180 19 ▶sub subscribe {
Subroutine subscribe: called CC=6 SCAR=17.9
Pod documented
181 19 my ($self, %p) = @_;
182 my $topic = exists $p{topic} ? $p{topic} :
183 19 ▶ croak ref $self, '->subscribe requires "topic" parameter';
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'topic'} ? : 18 1 19
184 my $sub = exists $p{callback} ? $p{callback} :
185 18 ▶ croak ref $self, '->subscribe requires "callback" parameter';
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'callback'} ? : 17 1 18
186 17 ▶ my $qos = exists $p{qos} ? $p{qos} : MQTT_QOS_AT_MOST_ONCE;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'qos'} ? : 5 12 17
187 17 ▶ my $cv = exists $p{cv} ? delete $p{cv} : AnyEvent->condvar;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'cv'} ? : 1 16 17
188 17 my $mid = $self->_add_subscription($topic, $cv, $sub);
189 17 ▶ if (defined $mid) { # not already subscribed/subscribing
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mid) 12 5 17
190 12 $self->_send(message_type => MQTT_SUBSCRIBE,
191 message_id => $mid,
192 topics => [[$topic, $qos]]);
193 }
194 $cv
195 17 }
196  
197  
198 7 ▶sub unsubscribe {
Subroutine unsubscribe: called CC=4 SCAR=13.9
Pod documented
199 7 my ($self, %p) = @_;
200 my $topic = exists $p{topic} ? $p{topic} :
201 7 ▶ croak ref $self, '->unsubscribe requires "topic" parameter';
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'topic'} ? : 6 1 7
202 6 ▶ my $cv = exists $p{cv} ? delete $p{cv} : AnyEvent->condvar;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'cv'} ? : 1 5 6
203 6 my $mid = $self->_remove_subscription($topic, $cv, $p{callback});
204 6 ▶ if (defined $mid) { # not already subscribed/subscribing
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mid) 2 4 6
205 2 $self->_send(message_type => MQTT_UNSUBSCRIBE,
206 message_id => $mid,
207 topics => [$topic]);
208 }
209 $cv
210 6 }
211  
212 17 ▶sub _add_subscription {
Subroutine _add_subscription: called CC=4 SCAR=13.9
213 17 my ($self, $topic, $cv, $sub) = @_;
214 17 my $rec = $self->{_sub}->{$topic};
215 17 ▶ if ($rec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec) 2 15 17
216 2 print STDERR "Add $sub to existing $topic subscription\n" if DEBUG;
217 2 $rec->{cb}->{$sub} = $sub;
218 2 $cv->send($rec->{qos});
219 2 foreach my $msg (values %{$rec->{retained}}) {
220 0 $sub->($msg->topic, $msg->message, $msg);
221 }
222 2 return;
223 }
224 15 $rec = $self->{_sub_pending}->{$topic};
225 15 ▶ if ($rec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec) 3 12 15
226 3 print STDERR "Add $sub to existing pending $topic subscription\n" if DEBUG;
227 3 $rec->{cb}->{$sub} = $sub;
228 3 push @{$rec->{cv}}, $cv;
229 3 return;
230 }
231 12 my $mid = $self->next_message_id();
232 12 print STDERR "Add $sub as pending $topic subscription (mid=$mid)\n" if DEBUG;
233 12 $self->{_sub_pending_by_message_id}->{$mid} = $topic;
234 12 $self->{_sub_pending}->{$topic} =
235 { cb => { $sub => $sub }, cv => [ $cv ], retained => {} };
236 12 $mid;
237 }
238  
239 6 ▶sub _remove_subscription {
Subroutine _remove_subscription: called CC=6 SCAR=18.0
240 6 my ($self, $topic, $cv, $sub) = @_;
241 6 my $rec = $self->{_unsub_pending}->{$topic};
242 6 ▶ if ($rec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec) 1 5 6
243 1 print STDERR "Remove of $topic with pending unsubscribe\n" if DEBUG;
244 1 push @{$rec->{cv}}, $cv;
245 1 return;
246 }
247 5 $rec = $self->{_sub}->{$topic};
248 5 ▶ unless ($rec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec) 1 4 5
249 1 print STDERR "Remove of $topic with no subscription\n" if DEBUG;
250 1 $cv->send(0);
251 1 return;
252 }
253  
254 4 ▶ if (defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub) 3 1 4
255 3 ▶ unless (exists $rec->{cb}->{$sub}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $rec->{"cb"}{$sub}) 0 3 3
256 0 print STDERR "Remove of $topic for $sub with no subscription\n"
257 if DEBUG;
258 0 $cv->send(0);
259 0 return;
260 }
261 3 delete $rec->{cb}->{$sub};
262 3 ▶ if (keys %{$rec->{cb}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$rec{"cb"};}) 2 1 3
263 2 print STDERR "Remove of $topic for $sub\n" if DEBUG;
264 2 $cv->send(1);
265 2 return;
266 }
267 }
268 2 print STDERR "Remove of $topic\n" if DEBUG;
269 2 my $mid = $self->next_message_id();
270 2 delete $self->{_sub}->{$topic};
271 2 $self->{_sub_topics}->delete($topic);
272 2 $self->{_unsub_pending_by_message_id}->{$mid} = $topic;
273 2 $self->{_unsub_pending}->{$topic} = { cv => [ $cv ] };
274 2 return $mid;
275 }
276  
277 13 ▶sub _confirm_subscription {
Subroutine _confirm_subscription: called CC=8 SCAR=26.4
278 13 my ($self, $mid, $qos) = @_;
279 13 my $topic = delete $self->{_sub_pending_by_message_id}->{$mid};
280 13 ▶ unless (defined $topic) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $topic) 1 12 13
281 1 carp 'SubAck with no pending subscription for message id: ', $mid, "\n";
282 1 return;
283 }
284 12 my $rec = $self->{_sub}->{$topic} = delete $self->{_sub_pending}->{$topic};
285 12 $self->{_sub_topics}->add($topic);
286 12 $rec->{qos} = $qos;
287  
288 12 foreach my $cv (@{$rec->{cv}}) {
289 15 $cv->send($qos);
290 }
291  
292 # publish any matching queued QoS messages
293 12 ▶ if (!$self->{clean_session} && $qos && $self->{_qos_msg_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"clean_session"} and $qos and $self->{"_qos_msg_cache"}) 0 12 12
Condition33%2/6
not $self->{"clean_session"} and $qos and $self->{"_qos_msg_cache"}
!ll&&!rl&&r
1200
not $self->{"clean_session"} and $qos
!ll&&!rl&&r
1200
MC/DC0%0/3
not $self->{"clean_session"} and $qos and $self->{"_qos_msg_cache"}
not $self->{"clean_session"} $qos $self->{"_qos_msg_cache"}
Truth table25%1/4
not(A) and B and C
A= not $self->{"clean_session"}B= $qosC= $self->{"_qos_msg_cache"}
ABCresult
0XX0
10X0
1100
1111
294 0 my $cache = $self->{_qos_msg_cache};
295 0 my $ts = Net::MQTT::TopicStore->new($topic);
296 0 for my $i (grep { $ts->values($cache->[$_]->topic) } reverse(0..$#$cache)) {
297 0 my $msg = delete $cache->[$i];
298 0 print STDERR "Processing cached message for topic '", $msg->topic, "' with subscription to topic '$topic'\n" if DEBUG;
299 0 $self->_process_publish($self->{handle}, $msg);
300 }
301 0 ▶ delete $self->{_qos_msg_cache} unless @$cache;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cache 0 0 0
302 }
303 12 delete $rec->{cv};
304 }
305  
306 3 ▶sub _confirm_unsubscribe {
Subroutine _confirm_unsubscribe: called CC=3 SCAR=11.0
307 3 my ($self, $mid) = @_;
308 3 my $topic = delete $self->{_unsub_pending_by_message_id}->{$mid};
309 3 ▶ unless (defined $topic) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $topic) 1 2 3
310 1 carp 'UnSubAck with no pending unsubscribe for message id: ', $mid, "\n";
311 1 return;
312 }
313 2 my $rec = delete $self->{_unsub_pending}->{$topic};
314 2 foreach my $cv (@{$rec->{cv}}) {
315 3 $cv->send(1);
316 }
317 }
318  
319 56 ▶sub _send {
Subroutine _send: called CC=2 SCAR=6.9
320 56 my $self = shift;
321 56 my %p = @_;
322 56 my $cv = delete $p{cv};
323 56 my $msg = Net::MQTT::Message->new(%p);
324 $self->{connected} ?
325 56 ▶ $self->_queue_write($msg, $cv) : $self->connect($msg, $cv);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'connected'} ? : 35 21 56
326 }
327  
328 56 ▶sub _queue_write {
Subroutine _queue_write: called CC=2 SCAR=6.9
329 56 my ($self, $msg, $cv) = @_;
330 56 my $queue = $self->{write_queue};
331 56 print STDERR 'Queuing: ', ($cv||'no cv'), ' ', $msg->string, "\n" if DEBUG;
332 56 push @{$queue}, [$msg, $cv];
333 56 ▶ $self->_write_now unless (defined $self->{_waiting});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_waiting"} 35 21 56
334 56 $cv;
335 }
336  
337 114 ▶sub _write_now {
Subroutine _write_now: called CC=4 SCAR=13.9
338 114 my $self = shift;
339 114 my ($msg, $cv);
340 114 undef $self->{_waiting};
341 114 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 16 98 114
342 16 ($msg, $cv) = @_;
343 } else {
344 98 ▶ my $args = shift @{$self->{write_queue}} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = shift @{$$self{"write_queue"};} or return 50 48 98
345 50 ($msg, $cv) = @$args;
346 }
347 66 $self->_reset_keep_alive_timer();
348 66 print STDERR "Sending: ", $msg->string, "\n" if DEBUG;
349 66 ▶ $self->{message_log_callback}->('>', $msg) if ($self->{message_log_callback});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"message_log_callback"} 5 61 66
350 66 $self->{_waiting} = [$msg, $cv];
351 66 print ' ', (unpack 'H*', $msg->bytes), "\n" if DEBUG;
352 66 $self->{handle}->push_write($msg->bytes);
353 66 $cv;
354 }
355  
356 71 ▶sub _reset_keep_alive_timer {
Subroutine _reset_keep_alive_timer: called CC=2 SCAR=6.9
357 71 my ($self, $wait) = @_;
358 71 undef $self->{_keep_alive_handle};
359 71 ▶ my $method = $wait ? '_keep_alive_timeout' : '_send_keep_alive';
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 3 68 71
360 71 $self->{_keep_alive_waiting} = $wait;
361 71 my $weak_self = $self;
362 71 weaken $weak_self;
363 $self->{_keep_alive_handle} =
364 AnyEvent->timer(after => $self->{keep_alive_timer},
365 cb => subname((substr $method, 1).'_cb' =>
366 71 ▶ sub { $weak_self->$method(@_) }));
Subroutine __ANON__: called CC=1 SCAR=0.0
367 }
368  
369 3 ▶sub _send_keep_alive {
Subroutine _send_keep_alive: called CC=1 SCAR=0.0
370 3 my $self = shift;
371 3 print STDERR "Sending: keep alive\n" if DEBUG;
372 3 $self->_send(message_type => MQTT_PINGREQ);
373 3 $self->_reset_keep_alive_timer(1);
374 }
375  
376 1 ▶sub _keep_alive_timeout {
Subroutine _keep_alive_timeout: called CC=1 SCAR=0.0
377 1 my $self = shift;
378 1 print STDERR "keep alive timeout\n" if DEBUG;
379 1 undef $self->{_keep_alive_waiting};
380 1 $self->{handle}->destroy;
381 1 $self->_error(0, 'keep alive timeout', 1);
382 }
383  
384 3 ▶sub _keep_alive_received {
Subroutine _keep_alive_received: called CC=2 SCAR=6.9
385 3 my $self = shift;
386 3 print STDERR "keep alive received\n" if DEBUG;
387 3 ▶ return unless (defined $self->{_keep_alive_waiting});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_keep_alive_waiting"} 2 1 3
388 1 $self->_reset_keep_alive_timer();
389 }
390  
391  
392 30 ▶sub connect {
Subroutine connect: called CC=6 SCAR=17.9
Pod documented
393 30 my ($self, $msg, $cv) = @_;
394 30 print STDERR "connect\n" if DEBUG;
395 30 $self->{_waiting} = 'connect';
396 30 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 21 9 30
397 21 ▶ $cv = AnyEvent->condvar unless ($cv);
Branch: 1 branch
BranchTrueFalseTotal
unless $cv 12 9 21
398 21 $self->_queue_write($msg, $cv);
399 } else {
400 9 ▶ $self->{connect_cv} = AnyEvent->condvar unless (exists $self->{connect_cv});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"connect_cv"} 7 2 9
401 9 $cv = $self->{connect_cv};
402 }
403 30 ▶ return $cv if ($self->{handle});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 12 18 30
404  
405 18 my $weak_self = $self;
406 18 weaken $weak_self;
407  
408 18 my $hd;
409 $hd = $self->{handle} =
410 AnyEvent::Handle->new(connect => [$self->{host}, $self->{port}],
411 ($self->{tls} ? (tls => "connect") : ()),
412 1 ▶ on_error => subname('on_error_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
413 1 my ($handle, $fatal, $message) = @_;
414 1 print STDERR "handle error $_[1]\n" if DEBUG;
415 1 $handle->destroy;
416 1 $weak_self->_error($fatal, 'Error: '.$message, 0);
417 }),
418 1 ▶ on_eof => subname('on_eof_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
419 1 my ($handle) = @_;
420 1 print STDERR "handle eof\n" if DEBUG;
421 1 $handle->destroy;
422 1 $weak_self->_error(1, 'EOF', 1);
423 }),
424 1 ▶ on_timeout => subname('on_timeout_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
425 1 $weak_self->_error(0, $weak_self->{wait}.' timeout', 1);
426 1 $weak_self->{wait} = 'nothing';
427 }),
428 16 ▶ on_connect => subname('on_connect_cb' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
429 16 my ($handle, $host, $port, $retry) = @_;
430 16 print STDERR "TCP handshake complete\n" if DEBUG;
431 # call user-defined on_connect function.
432 16 ▶ $weak_self->{on_connect}->($handle, $retry) if $weak_self->{on_connect};
Branch: 1 branch
BranchTrueFalseTotal
if $weak_self->{"on_connect"} 0 16 16
433 my $msg =
434 Net::MQTT::Message->new(
435 message_type => MQTT_CONNECT,
436 keep_alive_timer => $weak_self->{keep_alive_timer},
437 client_id => $weak_self->{client_id},
438 clean_session => $weak_self->{clean_session},
439 will_topic => $weak_self->{will_topic},
440 will_qos => $weak_self->{will_qos},
441 will_retain => $weak_self->{will_retain},
442 will_message => $weak_self->{will_message},
443 user_name => $weak_self->{user_name},
444 password => $weak_self->{password},
445 16 );
446 16 $weak_self->_write_now($msg);
447 16 $handle->timeout($weak_self->{timeout});
448 16 $weak_self->{wait} = 'connack';
449 14 ▶ $handle->on_read(subname 'on_read_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
450 14 my ($hdl) = @_;
451 $hdl->push_read(ref $weak_self =>
452 58 ▶ subname 'reader_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
453 58 $weak_self->_handle_message(@_);
454 58 1;
455 14 });
456 16 });
457 }),
458 18 ▶ @{$self->{handle_args}},
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tls'} ? : 0 18 18
459 );
460 18 return $cv
461 }
462  
463 3 ▶sub _reconnect {
Subroutine _reconnect: called CC=3 SCAR=11.2
464 3 my $self = shift;
465 3 print STDERR "reconnecting:\n" if DEBUG;
466  
467 # must resubscribe everything
468 3 ▶ if ($self->{clean_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"clean_session"}) 3 0 3
469 3 $self->{_sub_topics} = Net::MQTT::TopicStore->new();
470 3 ▶ $self->{_sub_reconnect} = delete $self->{_sub} || {};
Condition50%1/2
delete $self->{'_sub'} || {}
l!l
03
MC/DC0%0/1
delete $self->{'_sub'} || {}
delete $self->{'_sub'}
Truth table50%1/2
A
A= delete $self->{'_sub'}
Aresult
11
00
471 }
472  
473 3 $self->connect(@_);
474 }
475  
476 58 ▶sub _handle_message {
Subroutine _handle_message: called CC=6 SCAR=18.0
477 58 my $self = shift;
478 58 my ($handle, $msg, $error) = @_;
479 58 ▶ return $self->_error(0, $error, 1) if ($error);
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 58 58
480 58 ▶ $self->{message_log_callback}->('<', $msg) if ($self->{message_log_callback});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"message_log_callback"} 1 57 58
481 58 ▶ $self->_call_callback('before_msg_callback' => $msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_call_callback("before_msg_callback", $msg) or return 58 0 58
482 58 my $msg_type = lc ref $msg;
483 58 $msg_type =~ s/^.*:://;
484 58 ▶ $self->_call_callback('before_'.$msg_type.'_callback' => $msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_call_callback('before_' . $msg_type . '_callback', $msg) or return 58 0 58
485 58 my $method = '_process_'.$msg_type;
486 58 ▶ unless ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($method)) 1 57 58
487 1 carp 'Unsupported message ', $msg->string(), "\n";
488 1 return;
489 }
490 57 my $res = $self->$method(@_);
491 57 $self->_call_callback('after_'.$msg_type.'_callback' => $msg, $res);
492 57 $res;
493 }
494  
495 173 ▶sub _call_callback {
Subroutine _call_callback: called CC=2 SCAR=7.6
496 173 my $self = shift;
497 173 my $cb_name = shift;
498 173 ▶ return 1 unless (exists $self->{$cb_name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$cb_name} 173 0 173
499 0 $self->{$cb_name}->(@_);
500 }
501  
502 13 ▶sub _process_connack {
Subroutine _process_connack: called CC=5 SCAR=16.2
503 13 my ($self, $handle, $msg, $error) = @_;
504 13 $handle->timeout(undef);
505 13 ▶ unless ($msg->return_code == MQTT_CONNECT_ACCEPTED) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->return_code == 0) 1 12 13
506 1 return $self->_error(1, 'Connection refused: '.$msg->string, 0);
507 }
508 12 print STDERR "Connection ready:\n", $msg->string(' '), "\n" if DEBUG;
509 12 $self->_write_now();
510 12 $self->{connected} = 1;
511 12 ▶ $self->{connect_cv}->send(1) if ($self->{connect_cv});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connect_cv"} 3 9 12
512 12 delete $self->{connect_cv};
513  
514 12 my $weak_self = $self;
515 12 weaken $weak_self;
516  
517 51 ▶ $handle->on_drain(subname 'on_drain_cb' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
518 51 print STDERR "drained\n" if DEBUG;
519 51 my $w = $weak_self->{_waiting};
520 51 ▶ $w->[1]->send(1) if (ref $w && defined $w->[1]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $w and defined $w->[1] 31 20 51
Condition100%3/3
ref $w and defined $w->[1]
!ll&&!rl&&r
21831
MC/DC100%2/2
ref $w and defined $w->[1]
ref $w defined $w->[1]
Truth table100%3/3
A and B
A= ref $wB= defined $w->[1]
ABresult
0X0
100
111
521 51 $weak_self->_write_now;
522 51 1;
523 12 });
524  
525 # handle reconnect
526 12 ▶ while (my ($topic, $rec) = each %{$self->{_sub_reconnect}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_sub_reconnect"};}) 0 12 12
527 0 print STDERR "Resubscribing to '$topic':\n" if DEBUG;
528 0 for my $cb (values %{$rec->{cb}}) {
529 0 $self->subscribe(topic => $topic, callback => $cb, qos => $rec->{qos});
530 }
531 }
532 12 delete $self->{_sub_reconnect};
533 return
534 12 }
535  
536 3 ▶sub _process_pingresp {
Subroutine _process_pingresp: called CC=1 SCAR=0.0
537 3 shift->_keep_alive_received();
538 }
539  
540 13 ▶sub _process_suback {
Subroutine _process_suback: called CC=1 SCAR=0.0
541 13 my ($self, $handle, $msg, $error) = @_;
542 13 print STDERR "Confirmed subscription:\n", $msg->string(' '), "\n" if DEBUG;
543 13 $self->_confirm_subscription($msg->message_id, $msg->qos_levels->[0]);
544 return
545 13 }
546  
547 3 ▶sub _process_unsuback {
Subroutine _process_unsuback: called CC=1 SCAR=0.0
548 3 my ($self, $handle, $msg, $error) = @_;
549 3 print STDERR "Confirmed unsubscribe:\n", $msg->string(' '), "\n" if DEBUG;
550 3 $self->_confirm_unsubscribe($msg->message_id);
551 return
552 3 }
553  
554 16 ▶sub _publish_locally {
Subroutine _publish_locally: called CC=7 SCAR=20.7
555 16 my ($self, $msg) = @_;
556 16 my $msg_topic = $msg->topic;
557 16 my $msg_data = $msg->message;
558 16 my $matches = $self->{_sub_topics}->values($msg_topic);
559 16 ▶ unless (scalar @$matches) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$matches) 1 15 16
560 1 carp "Unexpected publish:\n", $msg->string(' '), "\n";
561 1 return;
562 }
563 15 my %matched;
564 15 my $msg_retain = $msg->retain;
565 15 foreach my $topic (@$matches) {
566 19 my $rec = $self->{_sub}->{$topic};
567 19 ▶ if ($msg_retain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_retain) 0 19 19
568 0 ▶ if ($msg_data eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_data eq '') { } 0 0 0
569 0 delete $rec->{retained}->{$msg_topic};
570 0 print STDERR " retained cleared\n" if DEBUG;
571 } else {
572 0 $rec->{retained}->{$msg_topic} = $msg;
573 0 print STDERR " retained '", $msg_data, "'\n" if DEBUG;
574 }
575 }
576 19 foreach my $cb (values %{$rec->{cb}}) {
577 27 ▶ next if ($matched{$cb}++);
Branch: 1 branch
BranchTrueFalseTotal
if $matched{$cb}++ 2 25 27
578 25 $cb->($msg_topic, $msg_data, $msg);
579 }
580 }
581 15 1;
582 }
583  
584 16 ▶sub _process_publish {
Subroutine _process_publish: called CC=6 SCAR=19.7
585 16 my ($self, $handle, $msg, $error) = @_;
586 16 my $qos = $msg->qos;
587  
588 # assuming this was intended for a subscription not yet restored
589 0 ▶ if ($qos && !$self->{clean_session} && !@{$self->{_sub_topics}->values($msg->topic)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qos and not $self->{"clean_session"} and not @{$self->{"_sub_topics"}->values($msg->topic);}) 0 16 16
Condition50%3/6
$qos and not $self->{"clean_session"} and not @{$self->{"_sub_topics"}->values($msg->topic);}
!ll&&!rl&&r
1600
$qos and not $self->{"clean_session"}
!ll&&!rl&&r
1420
MC/DC0%0/3
$qos and not $self->{"clean_session"} and not @{$self->{"_sub_topics"}->values($msg->topic);}
$qos not $self->{"clean_session"} not @{$self->{"_sub_topics"}->values($msg->topic);}
Truth table50%2/4
A and not(B) and not(C)
A= $qosB= not $self->{"clean_session"}C= not @{$self->{"_sub_topics"}->values($msg->topic);}
ABCresult
0XX0
10X0
1100
1111
590 0 print STDERR "Caching message for '", $msg->topic, "'\n" if DEBUG;
591 0 push(@{$self->{_qos_msg_cache}}, $msg);
592 0 return;
593 }
594  
595 16 ▶ if ($qos == MQTT_QOS_EXACTLY_ONCE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qos == 2) 1 15 16
596 1 my $mid = $msg->message_id;
597 1 $self->{messages}->{$mid} = $msg;
598 1 $self->_send(message_type => MQTT_PUBREC, message_id => $mid);
599 1 return;
600 }
601 15 $self->_publish_locally($msg);
602 15 ▶ $self->_send(message_type => MQTT_PUBACK, message_id => $msg->message_id)
Branch: 1 branch
BranchTrueFalseTotal
if $qos == 1 1 14 15
603 if ($qos == MQTT_QOS_AT_LEAST_ONCE);
604 return
605 15 }
606  
607 7 ▶sub _inflight_record {
Subroutine _inflight_record: called CC=3 SCAR=11.0
608 7 my ($self, $msg) = @_;
609 7 my $mid = $msg->message_id;
610 7 ▶ unless (exists $self->{inflight}->{$mid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"inflight"}{$mid}) 2 5 7
611 2 carp "Unexpected message for message id $mid\n ".$msg->string;
612 2 return;
613 }
614 5 my $exp_type = $self->{inflight}->{$mid}->{expect};
615 5 my $got_type = $msg->message_type;
616 5 ▶ unless ($got_type == $exp_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($got_type == $exp_type) 1 4 5
617 1 carp 'Received ', message_type_string($got_type), ' but expected ',
618 message_type_string($exp_type), " for message id $mid\n";
619 1 return;
620 }
621 4 return delete $self->{inflight}->{$mid};
622 }
623  
624 3 ▶sub _process_puback {
Subroutine _process_puback: called CC=2 SCAR=6.9
625 3 my ($self, $handle, $msg, $error) = @_;
626 3 ▶ my $rec = $self->_inflight_record($msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->_inflight_record($msg) or return 2 1 3
627 2 my $mid = $msg->message_id;
628 2 print STDERR 'PubAck: ', $mid, ' ', $rec->{cv}, "\n" if DEBUG;
629 2 $rec->{cv}->send(1);
630 2 return 1;
631 }
632  
633 2 ▶sub _process_pubrec {
Subroutine _process_pubrec: called CC=2 SCAR=6.9
634 2 my ($self, $handle, $msg, $error) = @_;
635 2 ▶ my $rec = $self->_inflight_record($msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->_inflight_record($msg) or return 1 1 2
636 1 my $mid = $msg->message_id;
637 1 print STDERR 'PubRec: ', $mid, ' ', $rec->{cv}, "\n" if DEBUG;
638 $self->_send_with_ack({
639 message_type => MQTT_PUBREL,
640 qos => MQTT_QOS_AT_LEAST_ONCE,
641 message_id => $mid,
642 1 }, $rec->{cv}, MQTT_PUBCOMP);
643 }
644  
645 2 ▶sub _process_pubrel {
Subroutine _process_pubrel: called CC=2 SCAR=6.9
646 2 my ($self, $handle, $msg, $error) = @_;
647 2 my $mid = $msg->message_id;
648 2 print STDERR 'PubRel: ', $mid, "\n" if DEBUG;
649 2 my $pubmsg = delete $self->{messages}->{$mid};
650 2 ▶ unless ($pubmsg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pubmsg) 1 1 2
651 1 carp "Unexpected message for message id $mid\n ".$msg->string;
652 1 return;
653 }
654 1 $self->_publish_locally($pubmsg);
655 1 $self->_send(message_type => MQTT_PUBCOMP, message_id => $mid);
656 }
657  
658 2 ▶sub _process_pubcomp {
Subroutine _process_pubcomp: called CC=2 SCAR=6.9
659 2 my ($self, $handle, $msg, $error) = @_;
660 2 ▶ my $rec = $self->_inflight_record($msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->_inflight_record($msg) or return 1 1 2
661 1 my $mid = $msg->message_id;
662 1 print STDERR 'PubComp: ', $mid, ' ', $rec->{cv}, "\n" if DEBUG;
663 1 $rec->{cv}->send(1);
664 1 return 1;
665 }
666  
667  
668 14 ▶sub anyevent_read_type {
Subroutine anyevent_read_type: called CC=1 SCAR=0.0
Pod documented
669 14 my ($handle, $cb) = @_;
670 51 ▶ subname 'anyevent_read_type_reader' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
671 51 my ($handle) = @_;
672 51 my $rbuf = \$handle->{rbuf};
673 51 weaken $rbuf;
674 51 ▶ return unless (defined $$rbuf);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$rbuf 0 51 51
675 51 while (1) {
676 109 my $msg = Net::MQTT::Message->new_from_bytes($$rbuf, 1);
677 109 ▶ last unless ($msg);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 51 58 109
678 58 $cb->($handle, $msg);
679 }
680 51 return;
681 14 };
682 }
683  
684 15 1;
685  
686 __END__
↑ Summary