Message.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 96.4% 27 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 9 / 9 totaltotal 98.4% 125 / 127 CC 14 SCAR 9.8
CC
14
Cov
98%
new_from_bytes
5.0
string
5.0
new
2.0
CRAP 2.7
↑ Summary TopicStore.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Net::MQTT::Message;
4 3 $Net::MQTT::Message::VERSION = '1.143250';
5 # ABSTRACT: Perl module to represent MQTT messages
6  
7  
8 3 ▶use Net::MQTT::Constants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Module::Pluggable search_path => __PACKAGE__, require => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our %types;
12 3 foreach (plugins()) {
13 45 my $m = $_.'::message_type';
14 45 ▶ next unless (defined &{$m}); # avoid super classes
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$m;} 3 42 45
15 42 my $t = $_->message_type;
16 42 ▶ if (exists $types{$t}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Net::MQTT::Message::types{$t}) 0 42 42
17 die 'Duplicate message_type number ', $t, ":\n",
18 ' ', $_, " and\n",
19 0 ' ', $types{$t}, "\n";
20 }
21 42 $types{$t} = $_;
22 }
23  
24  
25 sub new {
26 43 ▶ my ($pkg, %p) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
27 my $type_pkg =
28 43 ▶ exists $types{$p{message_type}} ? $types{$p{message_type}} : $pkg;
Branch: 1 branch
BranchTrueFalseTotal
exists $Net::MQTT::Message::types{$p{'message_type'}} ? : 41 2 43
29 43 bless { %p }, $type_pkg;
30 }
31  
32  
33 sub new_from_bytes {
34 26 ▶ my ($pkg, $bytes, $splice) = @_;
Subroutine new_from_bytes: called CC=5 SCAR=16.1
Pod documented
35 26 my %p;
36 26 ▶ return if (length $bytes < 2);
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes < 2 1 25 26
37 25 my $offset = 0;
38 25 my $b = decode_byte($bytes, \$offset);
39 25 $p{message_type} = ($b&0xf0) >> 4;
40 25 $p{dup} = ($b&0x8)>>3;
41 25 $p{qos} = ($b&0x6)>>1;
42 25 $p{retain} = ($b&0x1);
43 25 my $length;
44 25 eval {
45 25 $length = decode_remaining_length($bytes, \$offset);
46 };
47 25 ▶ return if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 23 25
48 23 ▶ if (length $bytes < $offset+$length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes < $offset + $length) 2 21 23
49 return
50 2 }
51 21 ▶ substr $_[1], 0, $offset+$length, '' if ($splice);
Branch: 1 branch
BranchTrueFalseTotal
if $splice 1 20 21
52 21 $p{remaining} = substr $bytes, $offset, $length;
53 21 my $self = $pkg->new(%p);
54 21 $self->_parse_remaining();
55 21 $self;
56 }
57  
58 ▶sub _parse_remaining {
Subroutine _parse_remaining: called CC=1 SCAR=0.0
59 }
60  
61  
62 4 ▶sub message_type { shift->{message_type} }
Subroutine message_type: called CC=1 SCAR=0.0
Pod documented
63  
64  
65 84 ▶sub dup { shift->{dup} || 0 }
Subroutine dup: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"dup"} or 0 80 4 84
66  
67  
68 sub qos {
69 94 ▶ my $self = shift;
Subroutine qos: called CC=2 SCAR=6.9
Pod documented
70 94 ▶ defined $self->{qos} ? $self->{qos} : $self->_default_qos
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'qos'} ? : 52 42 94
71 }
72  
73 sub _default_qos {
74 36 ▶ MQTT_QOS_AT_MOST_ONCE
Subroutine _default_qos: called CC=1 SCAR=0.0
75 }
76  
77  
78 84 ▶sub retain { shift->{retain} || 0 }
Subroutine retain: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"retain"} or 0 80 4 84
79  
80  
81 50 ▶sub remaining { shift->{remaining} || '' }
Subroutine remaining: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"remaining"} or "" 38 12 50
82  
83 sub _remaining_string {
84 38 ▶ my ($self, $prefix) = @_;
Subroutine _remaining_string: called CC=1 SCAR=0.0
85 38 dump_string($self->remaining, $prefix);
86 }
87  
88 12 ▶sub _remaining_bytes { shift->remaining }
Subroutine _remaining_bytes: called CC=1 SCAR=0.0
89  
90  
91 sub string {
92 42 ▶ my ($self, $prefix) = @_;
Subroutine string: called CC=5 SCAR=16.1
Pod documented
93 42 ▶ $prefix = '' unless (defined $prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 41 1 42
94 42 my @attr;
95 42 push @attr, qos_string($self->qos);
96 42 foreach (qw/dup retain/) {
97 84 my $bool = $self->$_;
98 84 ▶ push @attr, $_ if ($bool);
Branch: 1 branch
BranchTrueFalseTotal
if $bool 4 80 84
99 }
100 42 my $r = $self->_remaining_string($prefix);
101 42 ▶ $prefix.message_type_string($self->message_type).
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 36 6 42
102 '/'.(join ',', @attr).($r ? ' '.$r : '')
103 }
104  
105  
106 sub bytes {
107 42 ▶ my ($self) = shift;
Subroutine bytes: called CC=1 SCAR=0.0
Pod documented
108 42 my $o = '';
109 42 my $b =
110 ($self->message_type << 4) | ($self->dup << 3) |
111 ($self->qos << 1) | $self->retain;
112 42 $o .= encode_byte($b);
113 42 my $remaining = $self->_remaining_bytes;
114 42 $o .= encode_remaining_length(length $remaining);
115 42 $o .= $remaining;
116 42 $o;
117 }
118  
119 3 1;
120  
121 __END__
↑ Summary TopicStore.pm »