blib/lib/POE/Filter/Block.pm

filter: Statementstmt 100% 86 / 86 Branchbran 76.0% 38 / 50 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 92.5% 148 / 160 CC 28 SCAR 19.5
CC
28
Cov
91%
new
14.0
get_one
8.0
_default_decoder
3.0
CRAP 7.0
« blib/lib/POE/Kernel.pm ↑ Summary mylib/ForkingDaemon.pm »
1 package POE::Filter::Block;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use POE::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
8 4 @ISA = qw(POE::Filter);
9  
10 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub FRAMING_BUFFER () { 0 }
13 sub BLOCK_SIZE () { 1 }
14 sub EXPECTED_SIZE () { 2 }
15 sub ENCODER () { 3 }
16 sub DECODER () { 4 }
17 sub MAX_LENGTH () { 5 }
18 sub MAX_BUFFER () { 6 }
19 sub FIRST_UNUSED () { 7 }
20  
21 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 our @EXPORT_OK = qw( FIRST_UNUSED );
23  
24 #------------------------------------------------------------------------------
25  
26 48 ▶sub _default_decoder {
Subroutine _default_decoder: called CC=3 SCAR=11.0
27 48 my $stuff = shift;
28 48 ▶ unless ($$stuff =~ s/^(\d+)\0//s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$stuff =~ s/^(\d+)\0//s) 20 28 48
29 20 ▶ warn length($1), " strange bytes removed from stream"
Branch: 1 branch
BranchTrueFalseTotal
if $$stuff =~ s/^(\D+)//s 0 20 20
30 if $$stuff =~ s/^(\D+)//s;
31 20 return;
32 }
33 28 return $1;
34 }
35  
36 30 ▶sub _default_encoder {
Subroutine _default_encoder: called CC=1 SCAR=0.0
37 30 my $stuff = shift;
38 30 substr($$stuff, 0, 0) = length($$stuff) . "\0";
39 30 return;
40 }
41  
42 30 ▶sub new {
Subroutine new: called CC=13 SCAR=26.4
Pod documented
43 30 my $type = shift;
44 30 ▶ croak "$type must be given an even number of parameters" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 30 30
45 30 my %params = @_;
46  
47 30 my $max_buffer = $type->__param_max( MaxBuffer => 512*1024*1024, \%params );
48  
49 28 my ($encoder, $decoder, $max_length);
50 28 my $block_size = delete $params{BlockSize};
51 28 ▶ if (defined $block_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $block_size) { } 7 21 28
52 7 ▶ croak "$type doesn't support zero or negative block sizes"
Branch: 1 branch
BranchTrueFalseTotal
if $block_size < 1 0 7 7
53 if $block_size < 1;
54 croak "Can't use both LengthCodec and BlockSize at the same time"
55 7 ▶ if exists $params{LengthCodec};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"LengthCodec"} 0 7 7
56 croak "Can't use both MaxLength and BlockSize at the same time"
57 7 ▶ if exists $params{MaxLength};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"MaxLength"} 0 7 7
58 }
59 else {
60 21 my $codec = delete $params{LengthCodec};
61 21 ▶ if ($codec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($codec) { } 1 20 21
62 1 ▶ croak "LengthCodec must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $codec eq "ARRAY" 0 1 1
63 unless ref($codec) eq "ARRAY";
64 1 ▶ croak "LengthCodec must contain two items"
Branch: 1 branch
BranchTrueFalseTotal
unless @$codec == 2 0 1 1
65 unless @$codec == 2;
66 1 ($encoder, $decoder) = @$codec;
67 1 ▶ croak "LengthCodec encoder must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $encoder eq "CODE" 0 1 1
68 unless ref($encoder) eq "CODE";
69 1 ▶ croak "LengthCodec decoder must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $decoder eq "CODE" 0 1 1
70 unless ref($decoder) eq "CODE";
71 }
72 else {
73 20 $encoder = \&_default_encoder;
74 20 $decoder = \&_default_decoder;
75 }
76 21 $max_length = $type->__param_max( MaxLength => 64*1024*1024, \%params );
77 19 ▶ croak "MaxBuffer is not large enough for MaxLength blocks"
Branch: 1 branch
BranchTrueFalseTotal
unless $max_buffer >= $max_length + length($max_length) + 1 1 18 19
78 unless $max_buffer >= $max_length + length( $max_length ) + 1;
79 }
80  
81 25 delete @params{qw(MaxLength MaxBuffer LengthCode BlockSize)};
82 25 ▶ carp("$type ignores unknown parameters: ", join(', ', sort keys %params))
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %params 0 25 25
83 if scalar keys %params;
84  
85 25 my $self = bless [
86 '', # FRAMING_BUFFER
87 $block_size, # BLOCK_SIZE
88 undef, # EXPECTED_SIZE
89 $encoder, # ENCODER
90 $decoder, # DECODER
91 $max_length, # MAX_LENGTH
92 $max_buffer # MAX_BUFFER
93 ], $type;
94  
95 25 $self;
96 }
97  
98  
99 #------------------------------------------------------------------------------
100 # get() is inherited from POE::Filter.
101  
102 #------------------------------------------------------------------------------
103 # 2001-07-27 RCC: The get_one() variant of get() allows Wheel::Xyz to
104 # retrieve one filtered block at a time. This is necessary for filter
105 # changing and proper input flow control.
106  
107 57 ▶sub get_one_start {
Subroutine get_one_start: called CC=2 SCAR=6.9
Pod documented
108 57 my ($self, $stream) = @_;
109 57 $self->[FRAMING_BUFFER] .= join '', @$stream;
110 57 ▶ die "Framing buffer exceeds the limit"
Branch: 1 branch
BranchTrueFalseTotal
if $self->[6] < length $self->[0] 1 56 57
111 if $self->[MAX_BUFFER] < length( $self->[FRAMING_BUFFER] );
112 }
113  
114 110 ▶sub get_one {
Subroutine get_one: called CC=8 SCAR=20.8
Pod documented
115 110 my $self = shift;
116  
117 # Need to check lengths in octets, not characters.
118 4 ▶ BEGIN { eval { require bytes } and bytes->import; }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require bytes } } and "bytes"->import 4 0 4
119  
120 # If a block size is specified, then pull off a block of that many
121 # bytes.
122  
123 110 ▶ if (defined $self->[BLOCK_SIZE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[1]) 48 62 110
124 48 ▶ return [ ] unless length($self->[FRAMING_BUFFER]) >= $self->[BLOCK_SIZE];
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->[0] >= $self->[1] 22 26 48
125 26 my $block = substr($self->[FRAMING_BUFFER], 0, $self->[BLOCK_SIZE]);
126 26 substr($self->[FRAMING_BUFFER], 0, $self->[BLOCK_SIZE]) = '';
127 26 return [ $block ];
128 }
129  
130 # Otherwise we're doing the variable-length block thing. Look for a
131 # length marker, and then pull off a chunk of that length. Repeat.
132  
133 62 ▶ unless( defined($self->[EXPECTED_SIZE]) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[2]) 58 4 62
134 58 $self->[EXPECTED_SIZE] = $self->[DECODER]->(\$self->[FRAMING_BUFFER]);
135 58 ▶ die "Expected size of next block exceeds the limit"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[2] and $self->[2] > $self->[5] 1 57 58
Condition100%3/3
defined $self->[2] and $self->[2] > $self->[5]
!ll&&!rl&&r
25321
MC/DC100%2/2
defined $self->[2] and $self->[2] > $self->[5]
defined $self->[2] $self->[2] > $self->[5]
Truth table100%3/3
A and B
A= defined $self->[2]B= $self->[2] > $self->[5]
ABresult
0X0
100
111
136 if defined($self->[EXPECTED_SIZE]) and
137 $self->[EXPECTED_SIZE] > $self->[MAX_LENGTH];
138 }
139 61 ▶ if ( defined($self->[EXPECTED_SIZE]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[2]) 36 25 61
140 36 ▶ return [ ] if length($self->[FRAMING_BUFFER]) < $self->[EXPECTED_SIZE];
Branch: 1 branch
BranchTrueFalseTotal
if length $self->[0] < $self->[2] 4 32 36
141  
142 # Four-arg substr() would be better here, but it's not compatible
143 # with Perl as far back as we support.
144 32 my $block = substr($self->[FRAMING_BUFFER], 0, $self->[EXPECTED_SIZE]);
145 32 substr($self->[FRAMING_BUFFER], 0, $self->[EXPECTED_SIZE]) = '';
146 32 $self->[EXPECTED_SIZE] = undef;
147  
148 32 return [ $block ];
149 }
150  
151 25 return [ ];
152 }
153  
154 #------------------------------------------------------------------------------
155  
156 42 ▶sub put {
Subroutine put: called CC=3 SCAR=11.0
Pod documented
157 42 my ($self, $blocks) = @_;
158 42 my @raw;
159  
160 # Need to check lengths in octets, not characters.
161 4 ▶ BEGIN { eval { require bytes } and bytes->import; }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require bytes } } and "bytes"->import 4 0 4
162  
163 # If a block size is specified, then just assume the put is right.
164 # This will cause quiet framing errors on the receiving side. Then
165 # again, we'll have quiet errors if the block sizes on both ends
166 # differ. Ah, well!
167  
168 42 ▶ if (defined $self->[BLOCK_SIZE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[1]) { } 16 26 42
169 16 @raw = join '', @$blocks;
170 }
171  
172 # No specified block size. Do the variable-length block thing. This
173 # steals a lot of Artur's code from the Reference filter.
174  
175 else {
176 26 @raw = @$blocks;
177 26 foreach (@raw) {
178 38 $self->[ENCODER]->(\$_);
179 }
180 }
181  
182 42 \@raw;
183 }
184  
185 #------------------------------------------------------------------------------
186  
187 12 ▶sub get_pending {
Subroutine get_pending: called CC=2 SCAR=6.9
Pod documented
188 12 my $self = shift;
189 12 ▶ return undef unless length $self->[FRAMING_BUFFER];
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->[0] 8 4 12
190 4 [ $self->[FRAMING_BUFFER] ];
191 }
192  
193 4 1;
194  
195 __END__
« blib/lib/POE/Kernel.pm ↑ Summary mylib/ForkingDaemon.pm »