| File: | lib/Net/MQTT/Message/Connect.pm |
| Coverage: | 98.5% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | 3 3 3 | 3320 21 233 | use strict; | ||||
| 2 | 3 3 3 | 36 61 423 | use warnings; | ||||
| 3 | package Net::MQTT::Message::Connect; | ||||||
| 4 | |||||||
| 5 | # ABSTRACT: Perl module to represent an MQTT Connect message | ||||||
| 6 | |||||||
| 7 - 17 | =head1 SYNOPSIS # instantiated by Net::MQTT::Message =head1 DESCRIPTION This module encapsulates a single MQTT Connection Request 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 | 39 14 596 | use base 'Net::MQTT::Message'; | ||||
| 20 | 3 3 3 | 56 16 88 | use Net::MQTT::Constants qw/:all/; | ||||
| 21 | |||||||
| 22 | sub message_type { | ||||||
| 23 | 15 | 429 | 1 | ||||
| 24 | } | ||||||
| 25 | |||||||
| 26 | sub attributes { | ||||||
| 27 | 0 | 0 | (shift->SUPER::attributes, qw/protocol_name protocol_version | ||||
| 28 | user_name_flag user_name | ||||||
| 29 | password_flag password | ||||||
| 30 | will_flag will_retain will_qos | ||||||
| 31 | will_topic will_message | ||||||
| 32 | clean_session connect_reserved_flag | ||||||
| 33 | keep_alive_timer client_id/) | ||||||
| 34 | } | ||||||
| 35 | |||||||
| 36 | =method C<protocol_name()> | ||||||
| 37 | |||||||
| 38 | Returns the protocol name field of the MQTT Connect message. The | ||||||
| 39 | default is 'C<MQIsdp>'. | ||||||
| 40 | |||||||
| 41 | =cut | ||||||
| 42 | |||||||
| 43 | 12 | 411 | sub protocol_name { shift->{protocol_name} || 'MQIsdp' } | ||||
| 44 | |||||||
| 45 | =method C<protocol_version()> | ||||||
| 46 | |||||||
| 47 | Returns the protocol version field of the MQTT Connect message. The | ||||||
| 48 | default is 3. | ||||||
| 49 | |||||||
| 50 | =cut | ||||||
| 51 | |||||||
| 52 | 12 | 377 | sub protocol_version { shift->{protocol_version} || 3 } | ||||
| 53 | |||||||
| 54 | =method C<user_name_flag()> | ||||||
| 55 | |||||||
| 56 | Returns the user name flag field of the MQTT Connect message. The | ||||||
| 57 | default is true if and only if a user name is defined. | ||||||
| 58 | |||||||
| 59 | =cut | ||||||
| 60 | |||||||
| 61 | sub user_name_flag { | ||||||
| 62 | 21 | 125 | my $self = shift; | ||||
| 63 | 21 | 507 | $self->{user_name_flag} || defined $self->{user_name}; | ||||
| 64 | } | ||||||
| 65 | |||||||
| 66 | =method C<password_flag()> | ||||||
| 67 | |||||||
| 68 | Returns the password flag field of the MQTT Connect message. The | ||||||
| 69 | default is true if and only if a password is defined. | ||||||
| 70 | |||||||
| 71 | =cut | ||||||
| 72 | |||||||
| 73 | sub password_flag { | ||||||
| 74 | 21 | 121 | my $self = shift; | ||||
| 75 | 21 | 467 | $self->{password_flag} || defined $self->{password}; | ||||
| 76 | } | ||||||
| 77 | |||||||
| 78 | =method C<will_retain()> | ||||||
| 79 | |||||||
| 80 | Returns the will retain field of the MQTT Connect message. The | ||||||
| 81 | default is 0. | ||||||
| 82 | |||||||
| 83 | =cut | ||||||
| 84 | |||||||
| 85 | 8 | 189 | sub will_retain { shift->{will_retain} || 0 } | ||||
| 86 | |||||||
| 87 | =method C<will_qos()> | ||||||
| 88 | |||||||
| 89 | Returns the will QoS field of the MQTT Connect message. The default | ||||||
| 90 | is 0. | ||||||
| 91 | |||||||
| 92 | =cut | ||||||
| 93 | |||||||
| 94 | 8 | 203 | sub will_qos { shift->{will_qos} || 0 } | ||||
| 95 | |||||||
| 96 | =method C<will_flag()> | ||||||
| 97 | |||||||
| 98 | Returns the will flag field of the MQTT Connect message. The default | ||||||
| 99 | is 0. | ||||||
| 100 | |||||||
| 101 | =cut | ||||||
| 102 | |||||||
| 103 | 27 | 791 | sub will_flag { shift->{will_flag} || 0 } | ||||
| 104 | |||||||
| 105 | =method C<clean_session()> | ||||||
| 106 | |||||||
| 107 | Returns the clean session flag field of the MQTT Connect message. The | ||||||
| 108 | default is 1. | ||||||
| 109 | |||||||
| 110 | =cut | ||||||
| 111 | |||||||
| 112 | sub clean_session { | ||||||
| 113 | 6 | 40 | my $self = shift; | ||||
| 114 | 6 | 98 | defined $self->{clean_session} ? $self->{clean_session} : 1 | ||||
| 115 | } | ||||||
| 116 | |||||||
| 117 | =method C<connect_reserved_flag()> | ||||||
| 118 | |||||||
| 119 | Returns the reserved flag field of the MQTT Connect message. The | ||||||
| 120 | default is 0. | ||||||
| 121 | |||||||
| 122 | =cut | ||||||
| 123 | |||||||
| 124 | 6 | 279 | sub connect_reserved_flag { shift->{connect_reserved_flag} || 0 } | ||||
| 125 | |||||||
| 126 | =method C<keep_alive_timer()> | ||||||
| 127 | |||||||
| 128 | Returns the keep alive timer field of the MQTT Connect message. The | ||||||
| 129 | units are seconds. The default is 60. | ||||||
| 130 | |||||||
| 131 | =cut | ||||||
| 132 | |||||||
| 133 | 6 | 232 | sub keep_alive_timer { shift->{keep_alive_timer} || 60 } | ||||
| 134 | |||||||
| 135 | =method C<client_id()> | ||||||
| 136 | |||||||
| 137 | Returns the client identifier field of the MQTT Connect message. The | ||||||
| 138 | default is 'C<Net::MQTT::Message[$$]>' where 'C<$$>' is the | ||||||
| 139 | current process id. | ||||||
| 140 | |||||||
| 141 | =cut | ||||||
| 142 | |||||||
| 143 | 13 | 1626 | sub client_id { shift->{client_id} || 'Net::MQTT::Message['.$$.']' } | ||||
| 144 | |||||||
| 145 | =method C<will_topic()> | ||||||
| 146 | |||||||
| 147 | Returns the will topic field of the MQTT Connect message. The default | ||||||
| 148 | is undefined. | ||||||
| 149 | |||||||
| 150 | =cut | ||||||
| 151 | |||||||
| 152 | 4 | 94 | sub will_topic { shift->{will_topic} } | ||||
| 153 | |||||||
| 154 | =method C<will_message()> | ||||||
| 155 | |||||||
| 156 | Returns the will message field of the MQTT Connect message. The | ||||||
| 157 | default is undefined. | ||||||
| 158 | |||||||
| 159 | =cut | ||||||
| 160 | |||||||
| 161 | 4 | 74 | sub will_message { shift->{will_message} } | ||||
| 162 | |||||||
| 163 | =method C<user_name()> | ||||||
| 164 | |||||||
| 165 | Returns the user name field of the MQTT Connect message. The default | ||||||
| 166 | is undefined. | ||||||
| 167 | |||||||
| 168 | =cut | ||||||
| 169 | |||||||
| 170 | 4 | 76 | sub user_name { shift->{user_name} } | ||||
| 171 | |||||||
| 172 | =method C<password()> | ||||||
| 173 | |||||||
| 174 | Returns the password field of the MQTT Connect message. The default | ||||||
| 175 | is undefined. | ||||||
| 176 | |||||||
| 177 | =cut | ||||||
| 178 | |||||||
| 179 | 4 | 74 | sub password { shift->{password} } | ||||
| 180 | |||||||
| 181 | sub _remaining_string { | ||||||
| 182 | 6 | 47 | my ($self, $prefix) = @_; | ||||
| 183 | 6 | 53 | $self->protocol_name.'/'.$self->protocol_version.'/'.$self->client_id. | ||||
| 184 | ($self->user_name_flag ? ' user='.$self->user_name : ''). | ||||||
| 185 | ($self->password_flag ? ' pass='.$self->password : ''). | ||||||
| 186 | ($self->will_flag | ||||||
| 187 | ? ' will='.$self->will_topic.',"'.$self->will_message.'",'. | ||||||
| 188 | $self->will_retain.','.qos_string($self->will_qos) : ''). | ||||||
| 189 | ' '.$self->SUPER::_remaining_string($prefix) | ||||||
| 190 | } | ||||||
| 191 | |||||||
| 192 | sub _parse_remaining { | ||||||
| 193 | 3 | 23 | my $self = shift; | ||||
| 194 | 3 | 19 | my $offset = 0; | ||||
| 195 | 3 | 95 | $self->{protocol_name} = decode_string($self->{remaining}, \$offset); | ||||
| 196 | 3 | 89 | $self->{protocol_version} = decode_byte($self->{remaining}, \$offset); | ||||
| 197 | 3 | 87 | my $b = decode_byte($self->{remaining}, \$offset); | ||||
| 198 | 3 | 41 | $self->{user_name_flag} = ($b&0x80) >> 7; | ||||
| 199 | 3 | 25 | $self->{password_flag} = ($b&0x40) >> 6; | ||||
| 200 | 3 | 27 | $self->{will_retain} = ($b&0x20) >> 5; | ||||
| 201 | 3 | 22 | $self->{will_qos} = ($b&0x18) >> 3; | ||||
| 202 | 3 | 19 | $self->{will_flag} = ($b&0x4) >> 2; | ||||
| 203 | 3 | 24 | $self->{clean_session} = ($b&0x2) >> 1; | ||||
| 204 | 3 | 20 | $self->{connect_reserved_flag} = $b&0x1; | ||||
| 205 | 3 | 80 | $self->{keep_alive_timer} = decode_short($self->{remaining}, \$offset); | ||||
| 206 | 3 | 69 | $self->{client_id} = decode_string($self->{remaining}, \$offset); | ||||
| 207 | 3 | 28 | if ($self->will_flag) { | ||||
| 208 | 1 | 26 | $self->{will_topic} = decode_string($self->{remaining}, \$offset); | ||||
| 209 | 1 | 25 | $self->{will_message} = decode_string($self->{remaining}, \$offset); | ||||
| 210 | } | ||||||
| 211 | 3 | 28 | if ($self->user_name_flag) { | ||||
| 212 | 1 | 24 | $self->{user_name} = decode_string($self->{remaining}, \$offset); | ||||
| 213 | } | ||||||
| 214 | 3 | 28 | if ($self->password_flag) { | ||||
| 215 | 1 | 23 | $self->{password} = decode_string($self->{remaining}, \$offset); | ||||
| 216 | } | ||||||
| 217 | 3 | 385 | substr $self->{remaining}, 0, $offset, ''; | ||||
| 218 | } | ||||||
| 219 | |||||||
| 220 | sub _remaining_bytes { | ||||||
| 221 | 6 | 50 | my $self = shift; | ||||
| 222 | 6 | 371 | my $o = encode_string($self->protocol_name); | ||||
| 223 | 6 | 70 | $o .= encode_byte($self->protocol_version); | ||||
| 224 | 6 | 76 | $o .= encode_byte( | ||||
| 225 | ($self->user_name_flag << 7) | | ||||||
| 226 | ($self->password_flag << 6) | | ||||||
| 227 | ($self->will_retain << 5) | ($self->will_qos << 3) | | ||||||
| 228 | ($self->will_flag << 2) | | ||||||
| 229 | ($self->clean_session << 1) | | ||||||
| 230 | $self->connect_reserved_flag); | ||||||
| 231 | 6 | 70 | $o .= encode_short($self->keep_alive_timer); | ||||
| 232 | 6 | 61 | $o .= encode_string($self->client_id); | ||||
| 233 | 6 | 60 | $o .= encode_string($self->will_topic) if ($self->will_flag); | ||||
| 234 | 6 | 47 | $o .= encode_string($self->will_message) if ($self->will_flag); | ||||
| 235 | 6 | 51 | $o .= encode_string($self->user_name) if ($self->user_name_flag); | ||||
| 236 | 6 | 54 | $o .= encode_string($self->password) if ($self->password_flag); | ||||
| 237 | 6 | 255 | $o; | ||||
| 238 | } | ||||||
| 239 | |||||||
| 240 | 1; | ||||||