| File: | lib/Net/MQTT/Message/Publish.pm |
| Coverage: | 96.1% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | 3 3 3 | 3335 19 212 | use strict; | ||||
| 2 | 3 3 3 | 37 17 376 | use warnings; | ||||
| 3 | package Net::MQTT::Message::Publish; | ||||||
| 4 | |||||||
| 5 | # ABSTRACT: Perl module to represent an MQTT Publish message | ||||||
| 6 | |||||||
| 7 - 17 | =head1 SYNOPSIS # instantiated by Net::MQTT::Message =head1 DESCRIPTION This module encapsulates a single MQTT Publish 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 | 38 15 584 | use base 'Net::MQTT::Message'; | ||||
| 20 | 3 3 3 | 39 24 90 | use Net::MQTT::Constants qw/:all/; | ||||
| 21 | |||||||
| 22 | sub message_type { | ||||||
| 23 | 11 | 293 | 3 | ||||
| 24 | } | ||||||
| 25 | |||||||
| 26 | sub attributes { | ||||||
| 27 | 0 | 0 | (shift->SUPER::attributes, qw/topic message_id message/) | ||||
| 28 | } | ||||||
| 29 | |||||||
| 30 | =method C<topic()> | ||||||
| 31 | |||||||
| 32 | Returns the topic field of the MQTT Publish message. | ||||||
| 33 | |||||||
| 34 | =cut | ||||||
| 35 | |||||||
| 36 | 8 | 231 | sub topic { shift->{topic} } | ||||
| 37 | |||||||
| 38 | =method C<message_id()> | ||||||
| 39 | |||||||
| 40 | Returns the message id field of the MQTT Publish message. | ||||||
| 41 | |||||||
| 42 | =cut | ||||||
| 43 | |||||||
| 44 | 4 | 88 | sub message_id { shift->{message_id} } | ||||
| 45 | |||||||
| 46 | =method C<message()> | ||||||
| 47 | |||||||
| 48 | Returns the message field of the MQTT Publish message. | ||||||
| 49 | |||||||
| 50 | =cut | ||||||
| 51 | |||||||
| 52 | 4 | 50 | sub message { shift->{message} } | ||||
| 53 | |||||||
| 54 | 4 | 115 | sub _message_string { shift->{message} } | ||||
| 55 | |||||||
| 56 | sub _remaining_string { | ||||||
| 57 | 4 | 27 | my $self = shift; | ||||
| 58 | 4 | 36 | $self->topic. | ||||
| 59 | ($self->qos ? '/'.$self->message_id : ''). | ||||||
| 60 | ' '.dump_string($self->_message_string) | ||||||
| 61 | } | ||||||
| 62 | |||||||
| 63 | sub _parse_remaining { | ||||||
| 64 | 2 | 20 | my $self = shift; | ||||
| 65 | 2 | 15 | my $offset = 0; | ||||
| 66 | 2 | 65 | $self->{topic} = decode_string($self->{remaining}, \$offset); | ||||
| 67 | 2 | 82 | $self->{message_id} = decode_short($self->{remaining}, \$offset) | ||||
| 68 | if ($self->qos); | ||||||
| 69 | 2 | 29 | $self->{message} = substr $self->{remaining}, $offset; | ||||
| 70 | 2 | 59 | $self->{remaining} = ''; | ||||
| 71 | } | ||||||
| 72 | |||||||
| 73 | sub _remaining_bytes { | ||||||
| 74 | 4 | 36 | my $self = shift; | ||||
| 75 | 4 | 43 | my $o = encode_string($self->topic); | ||||
| 76 | 4 | 113 | if ($self->qos) { | ||||
| 77 | 2 | 25 | $o .= encode_short($self->message_id); | ||||
| 78 | } | ||||||
| 79 | 4 | 45 | $o .= $self->message; | ||||
| 80 | 4 | 118 | $o; | ||||
| 81 | } | ||||||
| 82 | |||||||
| 83 | 1; | ||||||