FastPack.pm

filter: Statementstmt 94.1% 129 / 137 Branchbran 75.0% 24 / 32 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 87.5% 14 / 16 Podpod 50.0% 3 / 6 totaltotal 87.0% 181 / 208 CC 26 SCAR 22.4
CC
26
Cov
89%
encode_message
16.2
decode_message
10.0
size_message
6.0
CRAP 9.4
↑ Summary FastPack/Meta.pm »
1 package Data::FastPack;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION="v0.3.0";
6  
7 2 ▶use feature ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Export::These qw<decode_fastpack encode_fastpack decode_message encode_message create_namespace name_for_id id_for_name FP_MSG_TIME FP_MSG_ID FP_MSG_PAYLOAD FP_MSG_TOTAL_LEN>;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 2 ▶use constant::more <FP_MSG_{TIME=0,ID,PAYLOAD,TOTAL_LEN}>;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use constant::more <FP_NS_{ID=0,SPACE,PARENT,NAME,USER}>;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use constant::more qw<N2E=0 I2E NEXT_ID FREE_ID>;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use constant::more DEBUG=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # routine indicating the required size of a buffer to store the requested
21 # payload length
22 #
23 0 ▶sub size_message {
Subroutine size_message: not called CC=2 SCAR=17.9
Pod undocumented
24 0 my $payload_size=shift;
25 0 my$padding=($payload_size%8);
26 0 ▶ $padding= 8-$padding if $padding;
Branch: 1 branch
BranchTrueFalseTotal
if $padding 0 0 0
27  
28 #return the total length of the head, payload and padding
29 0 16+$payload_size+$padding;
30 }
31  
32 #passing arrays of arrays, [time, info, payload] Computes padding, appends
33 #the serialized data to the supplied buffer, and sets the length. If id
34 #has MORE bit set, at least one more messags to follow (at some point).
35 #$buf, [$time, $id, $data]
36 #
37 2 my $pbuf= pack "x8";
38  
39  
40 =over
41  
42 =item encode_message
43  
44 =item encode_fastpack
45  
46 Encodes an array of fast pack message structures into an buffer
47  
48 Buffer is aliased and is an in/out parameter. Encoded data is appended to the buffer.
49  
50 Inputs is an array of message structure (also array refs). Array is consumed
51  
52 An optional limit can be sepecified on how many messages to encode in a single call
53  
54 Returns the number of bytes encoded
55  
56  
57 =back
58  
59 =cut
60  
61 2 my @_ids; #original name cache
62 my $i;
63  
64 sub encode_message;
65 14 ▶sub encode_message {
Subroutine encode_message: called CC=16 SCAR=27.9
Pod undocumented
66 14 \my $buf=\$_[0]; shift;
67 14 my $inputs=shift;
68 14 my $limit=shift;
69 14 my $ns=shift; # if name space is present. the id is taken as a name. Even though this could be numeric,
70 # It is then looked up from a dynamic table
71 # If it found, the allocated number is use.
72 # if is not, a system message with the table entry is created and set along with it.
73  
74 14 ▶ $limit//=@$inputs;
Condition33%1/3
$limit //= @$inputs
l!l&&r!l&&!r
0140
MC/DC0%0/2
$limit //= @$inputs
$limit @$inputs
Truth table33%1/3
A //= B
A= $limitB= @$inputs
ABresult
1X1
011
000
75 14 my $bytes=0;
76 14 my $processed=0;
77 14 my $padding;
78 my $tmp;
79
80 14 my $flags=0;
81 # Loop through and do namespace name to id conversion
82 14 ▶ if($ns){
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) { } 7 7 14
83 7 $i=0;
84 7 for(@$inputs){
85 # NOTE: messages with "0" and 0 and undef are ignored
86 7 ▶ if($_->[FP_MSG_ID]){
Branch: 1 branch
BranchTrueFalseTotal
if ($$_[1]) { } 6 1 7
87 #DEBUG and
88 6 my $name=$_->[FP_MSG_ID];
89 # Convert to id if ns is present and ID is NOT 0
90 6 my $id=$ns->[N2E]{$name};
91  
92 6 ▶ if(!defined $id){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 6 0 6
93 6 DEBUG and print STDERR $$.__PACKAGE__ . "id for name $name does NOT existes in table: $id\n";
94 6 DEBUG and print STDERR $$.__PACKAGE__ . "first payload is name... so we want to send/register\n";
95 # Update id tracking and lookup tables
96 6 ▶ $id=pop($ns->[FREE_ID]->@*)//$ns->[NEXT_ID]++;
Condition50%1/2
pop @{$$ns[3]} // $$ns[2]++
l!l
06
MC/DC0%0/1
pop @{$$ns[3]} // $$ns[2]++
pop @{$$ns[3]}
Truth table50%1/2
A
A= pop @{$$ns[3]}
Aresult
11
00
97 6 $ns->[N2E]{$name}=$id;
98 6 $ns->[I2E]{$id}=$name;
99  
100 6 DEBUG and print STDERR __PACKAGE__ . "encode registration $id $name\n";
101 # encode definition into buffer
102 6 $bytes+=encode_message $buf, [[$_->[FP_MSG_TIME], $id, $name ]];
103 }
104  
105 # Same the new name for encoding
106 6 $_ids[$i++]=$id;
107 }
108 else {
109 1 ▶ unless($_->[FP_MSG_PAYLOAD]){
Branch: 1 branch
BranchTrueFalseTotal
unless ($$_[2]) 1 0 1
110 # No payload, and 0 id, and name space active...
111 # This is a name space reset message
112 1 $ns->[N2E]->%*=();
113 1 $ns->[I2E]->%*=();
114 1 $ns->[FREE_ID]->@*=();
115 1 $ns->[NEXT_ID]=1;
116 }
117 1 $_ids[$i++]=$_->[FP_MSG_ID];
118 # If the msg id is 0 this is passed thourgh un modified and not name translated.
119 #
120 #warn 'FastPack encode: Ignoring message. Named FastPack Messages cannot be named 0 or "0" or undefined';
121 }
122  
123 }
124 7 $i=0;
125 7 for(@$inputs){
126 7 ▶ $padding=((length($_->[FP_MSG_PAYLOAD]//""))%8);
Condition100%2/2
$$_[2] // ""
l!l
61
MC/DC100%1/1
$$_[2] // ""
$$_[2]
Truth table100%2/2
A
A= $$_[2]
Aresult
11
00
127 7 ▶ $padding= 8-$padding if $padding;
Branch: 1 branch
BranchTrueFalseTotal
if $padding 6 1 7
128  
129 7 ▶ my $s=pack("d V V/a*", $_->[FP_MSG_TIME], $_ids[$i++], $_->[FP_MSG_PAYLOAD]//"" );
Condition100%2/2
$$_[2] // ""
l!l
61
MC/DC100%1/1
$$_[2] // ""
$$_[2]
Truth table100%2/2
A
A= $$_[2]
Aresult
11
00
130 7 $tmp=$s.substr $pbuf, 0, $padding;
131 7 $bytes+=length $tmp;
132 7 $buf.=$tmp;
133 7 ▶ last if ++$processed == $limit;
Branch: 1 branch
BranchTrueFalseTotal
if ++$processed == $limit 7 0 7
134 }
135 }
136 else {
137 7 $i=0;
138 7 for(@$inputs){
139 8 $padding=((length $_->[FP_MSG_PAYLOAD])%8);
140 8 ▶ $padding= 8-$padding if $padding;
Branch: 1 branch
BranchTrueFalseTotal
if $padding 8 0 8
141  
142 8 my $s=pack("d V V/a*", @$_);
143 8 $tmp=$s.substr $pbuf, 0, $padding;
144 8 $bytes+=length $tmp;
145 8 $buf.=$tmp;
146 8 ▶ last if ++$processed == $limit;
Branch: 1 branch
BranchTrueFalseTotal
if ++$processed == $limit 7 1 8
147 }
148  
149 }
150  
151 # Remove the messages from the input array
152 14 splice @$inputs, 0, $processed;
153
154 14 $bytes;
155 }
156  
157 2 *encode_fastpack=\&encode_message;
158  
159 # Decode a message from a buffer. Buffer is aliased
160 =over
161  
162 =item decode_message
163  
164 =item decode_fastpack
165  
166 Consumes data from an input buffer and decodes it into 0 or more messages.
167 Buffer is aliased and is an in/out parameter
168 Decoded messages are added to the dereferenced output array
169 An optional limit of message count can be specified.
170  
171 Returns the number of bytes consumed during decoding. I a message could not be
172 decoded, 0 bytes are consumed.
173  
174 buffer (aliased)
175 output (array ref)
176 limit (numeric)
177  
178 return (byte count)
179  
180  
181 =back
182  
183 =cut
184  
185 3 ▶sub decode_message {
Subroutine decode_message: called CC=10 SCAR=23.0
Pod undocumented
186 3 \my $buf=\$_[0]; shift;
187 3 my $output=shift;
188 3 ▶ my $limit=shift//4096;
Condition100%2/2
shift() // 4096
l!l
12
MC/DC100%1/1
shift() // 4096
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
189 3 my $ns=shift;
190  
191 3 my $byte_count=0;
192 3 for(1..$limit){
193 # Minimum message length 8 bytes long (header)
194 18 ▶ last if length($buf)<16;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < 16 3 15 18
195  
196 # Decode header. Leave length for in buffer
197 15 my @message= unpack "d V V", substr($buf, 0, 16);
198  
199  
200  
201 # Calculate pad. Payload in message here is actuall just length atm
202 15 my $pad= $message[FP_MSG_PAYLOAD]%8;
203 15 ▶ $pad=8-$pad if $pad;
Branch: 1 branch
BranchTrueFalseTotal
if $pad 14 1 15
204  
205 # Calculate total length
206 15 my $total=$message[FP_MSG_PAYLOAD]+16+$pad;
207  
208 15 ▶ last if(length($buf)<$total);
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < $total 0 15 15
209  
210  
211  
212  
213 15 $byte_count+=$total;
214  
215  
216 15 ($message[FP_MSG_PAYLOAD],undef)=unpack "V/a* ", substr($buf,12);
217 15 push @message, $total;
218  
219 # remove from buffer
220 15 substr($buf, 0, $total,"");
221  
222 # Process name space definitions and lookups
223 15 ▶ if($ns ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) { } 13 2 15
224 #print STDERR __PACKAGE__. "-------- ID of $message[FP_MSG_ID]=> $message[FP_MSG_PAYLOAD]\n";
225 13 ▶ if($message[FP_MSG_ID]){
Branch: 1 branch
BranchTrueFalseTotal
if ($message[1]) { } 12 1 13
226 # This is a non system message which needs conversion
227 #
228 12 my $id=$message[FP_MSG_ID];
229 12 my $name= $ns->[I2E]{$id};
230 12 ▶ unless($name){
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 6 6 12
231 # id not found, so this is the first message for this id
232 # The payload contains the name
233 6 DEBUG and print STDERR __PACKAGE__ . " message id has not been seen before: $id\n";
234 # This id has not been seen before. so we know the payload it the name
235 6 $name=$message[FP_MSG_PAYLOAD];
236  
237 6 DEBUG and print STDERR __PACKAGE__ . " use payload ans name: $name\n";
238 6 $ns->[I2E]{$id}=$name;
239 6 $ns->[N2E]{$name}=$id;
240 }
241 else {
242 6 DEBUG and print STDERR __PACKAGE__ . " message name exisits: $name\n";
243 6 DEBUG and print STDERR __PACKAGE__ . " has payload.. process as normal. push to output\n";
244 # only push the message to output if this code has been seen before
245 6 push @$output, \@message;
246  
247 # Finally convert id to name as we asked for named message
248 6 $message[FP_MSG_ID]=$name;
249 }
250 }
251 else {
252 #id of 0
253 1 ▶ unless($message[FP_MSG_PAYLOAD]){
Branch: 1 branch
BranchTrueFalseTotal
if ($message[2]) { } 0 1 1
254 # no payload, and 0 id,, reset name space, and consume message
255 #
256 1 $ns->[I2E]->%*=();
257 1 $ns->[N2E]->%*=();
258 1 $ns->[FREE_ID]->@*=();
259 1 $ns->[NEXT_ID]=1;
260 }
261 else {
262 # Direct pass through if a payload present
263 0 push @$output, \@message;
264 }
265 }
266 }
267 else {
268 # Direct pass through if no name space used
269 #
270 2 push @$output, \@message;
271 }
272 }
273 3 $byte_count;
274 }
275  
276 2 *decode_fastpack=\&decode_message;
277  
278 4 ▶sub create_namespace {
Subroutine create_namespace: called CC=1 SCAR=0.0
Pod documented
279 4 [{},{}, 1, []];
280 }
281  
282 12 ▶sub id_for_name {
Subroutine id_for_name: called CC=1 SCAR=0.0
Pod documented
283  
284 12 my $ns=shift;
285 12 my $name=shift;
286 12 $ns->[N2E]{$name};
287 }
288  
289 0 ▶sub name_for_id {
Subroutine name_for_id: not called CC=1 SCAR=6.9
Pod documented
290 0 my $ns=shift;
291 0 my $id=shift;
292 0 $ns->[I2E]{$id};
293  
294 }
295  
296  
297 2 1;
↑ Summary FastPack/Meta.pm »