File: | lib/Net/MQTT/Message/Subscribe.pm |
Coverage: | 96.0% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | 3 3 3 | 3284 19 226 | use strict; | ||||
2 | 3 3 3 | 37 15 406 | use warnings; | ||||
3 | package Net::MQTT::Message::Subscribe; | ||||||
4 | |||||||
5 | # ABSTRACT: Perl module to represent an MQTT Subscribe message | ||||||
6 | |||||||
7 - 17 | =head1 SYNOPSIS # instantiated by Net::MQTT::Message =head1 DESCRIPTION This module encapsulates a single MQTT Subscribe message. It is a specific subclass used by L<Net::MQTT::Message> and should not need to be instantiated directly. =cut | ||||||
18 | |||||||
19 | 3 3 3 | 36 18 602 | use base 'Net::MQTT::Message'; | ||||
20 | 3 3 3 | 40 24 93 | use Net::MQTT::Constants qw/:all/; | ||||
21 | |||||||
22 | sub message_type { | ||||||
23 | 7 | 183 | 8 | ||||
24 | } | ||||||
25 | |||||||
26 | sub attributes { | ||||||
27 | 0 | 0 | (shift->SUPER::attributes, qw/message_id topics/) | ||||
28 | } | ||||||
29 | |||||||
30 | sub _default_qos { | ||||||
31 | 2 | 65 | MQTT_QOS_AT_LEAST_ONCE | ||||
32 | } | ||||||
33 | |||||||
34 | =method C<message_id()> | ||||||
35 | |||||||
36 | Returns the message id field of the MQTT Subscribe message. | ||||||
37 | |||||||
38 | =cut | ||||||
39 | |||||||
40 | 4 | 96 | sub message_id { shift->{message_id} } | ||||
41 | |||||||
42 | =method C<topics()> | ||||||
43 | |||||||
44 | Returns the list of topics of the MQTT Subscribe message. Each | ||||||
45 | element of the list is a 2-ple containing the topic and its associated | ||||||
46 | requested QoS level. | ||||||
47 | |||||||
48 | =cut | ||||||
49 | |||||||
50 | 2 | 24 | sub topics { shift->{topics} } | ||||
51 | |||||||
52 | sub _topics_string { | ||||||
53 | 2 2 2 | 16 75 23 | join ',', map { $_->[0].'/'.qos_string($_->[1]) } @{shift->{topics}} | ||||
54 | } | ||||||
55 | |||||||
56 | sub _remaining_string { | ||||||
57 | 2 | 24 | my ($self, $prefix) = @_; | ||||
58 | 2 | 22 | $self->message_id.' '.$self->_topics_string.' '. | ||||
59 | $self->SUPER::_remaining_string($prefix) | ||||||
60 | } | ||||||
61 | |||||||
62 | sub _parse_remaining { | ||||||
63 | 1 | 12 | my $self = shift; | ||||
64 | 1 | 9 | my $offset = 0; | ||||
65 | 1 | 36 | $self->{message_id} = decode_short($self->{remaining}, \$offset); | ||||
66 | 1 | 21 | while ($offset < length $self->{remaining}) { | ||||
67 | 1 1 | 8 40 | push @{$self->{topics}}, [ decode_string($self->{remaining}, \$offset), | ||||
68 | decode_byte($self->{remaining}, \$offset) ]; | ||||||
69 | } | ||||||
70 | 1 | 37 | substr $self->{remaining}, 0, $offset, ''; | ||||
71 | } | ||||||
72 | |||||||
73 | sub _remaining_bytes { | ||||||
74 | 2 | 18 | my $self = shift; | ||||
75 | 2 | 28 | my $o = encode_short($self->message_id); | ||||
76 | 2 2 | 17 23 | foreach my $r (@{$self->topics}) { | ||||
77 | 2 | 19 | my ($name, $qos) = @$r; | ||||
78 | 2 | 58 | $o .= encode_string($name); | ||||
79 | 2 | 62 | $o .= encode_byte($qos); | ||||
80 | } | ||||||
81 | $o | ||||||
82 | 2 | 60 | } | ||||
83 | |||||||
84 | 1; |