File Coverage

blib/lib/Net/MQTT/Message/Connect.pm
Criterion Covered Total %
statement 67 67 100.0
branch 42 42 100.0
condition n/a
subroutine 23 23 100.0
pod 16 16 100.0
total 148 148 100.0


line stmt bran cond sub pod time code
1 3     3   1896 use strict;
  3         4  
  3         89  
2 3     3   13 use warnings;
  3         5  
  3         158  
3             package Net::MQTT::Message::Connect;
4             $Net::MQTT::Message::Connect::VERSION = '1.163170';
5             # ABSTRACT: Perl module to represent an MQTT Connect message
6              
7              
8 3     3   12 use base 'Net::MQTT::Message';
  3         5  
  3         260  
9 3     3   15 use Net::MQTT::Constants qw/:all/;
  3         6  
  3         19  
10              
11             sub message_type {
12 15     15 1 49 1
13             }
14              
15              
16 12 100   12 1 69 sub protocol_name { shift->{protocol_name} || 'MQIsdp' }
17              
18              
19 12 100   12 1 66 sub protocol_version { shift->{protocol_version} || 3 }
20              
21              
22             sub user_name_flag {
23 21     21 1 27 my $self = shift;
24 21 100       134 $self->{user_name_flag} || defined $self->{user_name};
25             }
26              
27              
28             sub password_flag {
29 21     21 1 24 my $self = shift;
30 21 100       114 $self->{password_flag} || defined $self->{password};
31             }
32              
33              
34 8 100   8 1 45 sub will_retain { shift->{will_retain} || 0 }
35              
36              
37 8 100   8 1 44 sub will_qos { shift->{will_qos} || 0 }
38              
39              
40             sub will_flag {
41 27     27 1 34 my $self = shift;
42             $self->{will_flag} || defined $self->{will_topic}
43 27 100       157 }
44              
45              
46             sub clean_session {
47 6     6 1 7 my $self = shift;
48 6 100       38 defined $self->{clean_session} ? $self->{clean_session} : 1
49             }
50              
51              
52 6 100   6 1 42 sub connect_reserved_flag { shift->{connect_reserved_flag} || 0 }
53              
54              
55 6 100   6 1 34 sub keep_alive_timer { shift->{keep_alive_timer} || 60 }
56              
57              
58 14 100   14 1 568 sub client_id { shift->{client_id} || 'NetMQTTpm'.$$ }
59              
60              
61 4     4 1 19 sub will_topic { shift->{will_topic} }
62              
63              
64 4     4 1 17 sub will_message { shift->{will_message} }
65              
66              
67 4     4 1 19 sub user_name { shift->{user_name} }
68              
69              
70 4     4 1 15 sub password { shift->{password} }
71              
72             sub _remaining_string {
73 6     6   12 my ($self, $prefix) = @_;
74 6 100       16 $self->protocol_name.'/'.$self->protocol_version.'/'.$self->client_id.
    100          
    100          
75             ($self->user_name_flag ? ' user='.$self->user_name : '').
76             ($self->password_flag ? ' pass='.$self->password : '').
77             ($self->will_flag
78             ? ' will='.$self->will_topic.',"'.$self->will_message.'",'.
79             $self->will_retain.','.qos_string($self->will_qos) : '').
80             ' '.$self->SUPER::_remaining_string($prefix)
81             }
82              
83             sub _parse_remaining {
84 3     3   4 my $self = shift;
85 3         6 my $offset = 0;
86 3         20 $self->{protocol_name} = decode_string($self->{remaining}, \$offset);
87 3         15 $self->{protocol_version} = decode_byte($self->{remaining}, \$offset);
88 3         13 my $b = decode_byte($self->{remaining}, \$offset);
89 3         11 $self->{user_name_flag} = ($b&0x80) >> 7;
90 3         9 $self->{password_flag} = ($b&0x40) >> 6;
91 3         8 $self->{will_retain} = ($b&0x20) >> 5;
92 3         9 $self->{will_qos} = ($b&0x18) >> 3;
93 3         7 $self->{will_flag} = ($b&0x4) >> 2;
94 3         9 $self->{clean_session} = ($b&0x2) >> 1;
95 3         6 $self->{connect_reserved_flag} = $b&0x1;
96 3         11 $self->{keep_alive_timer} = decode_short($self->{remaining}, \$offset);
97 3         9 $self->{client_id} = decode_string($self->{remaining}, \$offset);
98 3 100       15 if ($self->will_flag) {
99 1         5 $self->{will_topic} = decode_string($self->{remaining}, \$offset);
100 1         7 $self->{will_message} = decode_string($self->{remaining}, \$offset);
101             }
102 3 100       12 if ($self->user_name_flag) {
103 1         6 $self->{user_name} = decode_string($self->{remaining}, \$offset);
104             }
105 3 100       16 if ($self->password_flag) {
106 1         6 $self->{password} = decode_string($self->{remaining}, \$offset);
107             }
108 3         16 substr $self->{remaining}, 0, $offset, '';
109             }
110              
111             sub _remaining_bytes {
112 6     6   12 my $self = shift;
113 6         15 my $o = encode_string($self->protocol_name);
114 6         15 $o .= encode_byte($self->protocol_version);
115 6         19 $o .= encode_byte(
116             ($self->user_name_flag << 7) |
117             ($self->password_flag << 6) |
118             ($self->will_retain << 5) | ($self->will_qos << 3) |
119             ($self->will_flag << 2) |
120             ($self->clean_session << 1) |
121             $self->connect_reserved_flag);
122 6         22 $o .= encode_short($self->keep_alive_timer);
123 6         15 $o .= encode_string($self->client_id);
124 6 100       15 $o .= encode_string($self->will_topic) if ($self->will_flag);
125 6 100       15 $o .= encode_string($self->will_message) if ($self->will_flag);
126 6 100       18 $o .= encode_string($self->user_name) if ($self->user_name_flag);
127 6 100       18 $o .= encode_string($self->password) if ($self->password_flag);
128 6         19 $o;
129             }
130              
131             1;
132              
133             __END__