RRP/Codec.pm

filter: Statementstmt 19.8% 23 / 116 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 50.0% 7 / 14 Podpod 80.0% 4 / 5 totaltotal 14.6% 34 / 232 CC 38 SCAR 50.7
CC
38
Cov
12%
decodeRequest
342.0
decodeResponse
132.0
encodeRequest
56.0
CRAP 158.9
« RRP/Toolkit.pm ↑ Summary RRP/Protocol.pm »
1 package Net::RRP::Codec;
2  
3 =head1 NAME
4  
5 Net::RRP::Codec - codec class for serialization/deserialization of Net::RRP::Request/Response
6  
7 =head1 SYNOPSIS
8  
9 use Net::RRP::RRP::Codec;
10 my $codec = new Net::RRP::Codec();
11  
12 =head1 DESCRIPTION
13  
14 Net::RRP::Codec - codec class for serialization/deserialization of Net::RRP::Request/Response
15  
16 =cut
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Net::RRP::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Net::RRP::Exception::InvalidCommandName;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Net::RRP::Exception::InvalidResponseFormat;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 $Net::RRP::Codec::VERSION = (split " ", '# $Id: Codec.pm,v 1.12 2000/09/30 16:04:41 mkul Exp $ ')[3];
26  
27 1 ▶use constant CRLF => "\r\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head2 new
30  
31 Constructor of this class.
32  
33 Example:
34  
35 use Net::RRP::RRP::Codec;
36 my $codec = new Net::RRP::Codec();
37  
38 =cut
39  
40 sub new
41 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
42 0 my $class = shift;
43 0 bless {}, $class;
44 }
45  
46 =head2 decodeRequest
47  
48 This method get the buffer with unparsed rrp request && decode it and return Net::RRP::Request object.
49 The real return object is a instance of Net::RRP::Request::$requestName class. This method dynamic loads
50 required class ( package ). Next, we a parse the Entity part of RRP request and construct the instance of
51 Net::RRP::Entity::$entityName class ( with dynamic loading of this class ) and add entity attributes to
52 this object. After this, parser process the rrp request options and add it's to request object. When all
53 done, method return a constructed rrp request object. This method say throw() at any errors.
54  
55 Example:
56  
57 my $request = $codec->decodeRequest ( $buffer );
58  
59 =cut
60  
61 #'
62  
63 1 ▶use constant NAME_SUBSCRIBE => { 'nameserver' => 'NameServer' };
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 sub decodeRequest
66 0 ▶{
Subroutine decodeRequest: not called CC=18 SCAR=58.3
Pod documented
67 0 my ( $this, $buffer ) = @_;
68 0 my @lines = split CRLF, $buffer;
69  
70 0 my $requestName = lc ( shift @lines );
71 0 ▶ $requestName = NAME_SUBSCRIBE->{$requestName} if NAME_SUBSCRIBE->{$requestName};
Branch: 1 branch
BranchTrueFalseTotal
if {"nameserver" => "NameServer"}->{$requestName} 0 0 0
72 0 my $requestPackageName = 'Net::RRP::Request::' . ucfirst ( $requestName );
73 0 eval "use $requestPackageName";
74 0 ▶ throw Net::RRP::Exception::InvalidCommandName ( $buffer ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
75 0 my $request = $requestPackageName->new();
76  
77 0 my $entity;
78  
79 0 ▶ if ( ( $lines[0] ne '.' ) && ( $lines[0] !~ m/^\-/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] ne "." and not $lines[0] =~ /^\-/) 0 0 0
Condition0%0/3
$lines[0] ne "." and not $lines[0] =~ /^\-/
!ll&&!rl&&r
000
MC/DC0%0/2
$lines[0] ne "." and not $lines[0] =~ /^\-/
$lines[0] ne "." not $lines[0] =~ /^\-/
Truth table0%0/3
A and not(B)
A= $lines[0] ne "."B= not $lines[0] =~ /^\-/
ABresult
0X0
100
111
80 {
81 0 my $entityLine = shift @lines;
82 0 ▶ $entityLine =~ /^EntityName:(.*)/i || throw Net::RRP::Exception::InvalidEntityValue ( $buffer );
Branch: 1 branch
BranchTrueFalseTotal
$entityLine =~ /^EntityName:(.*)/i or "Net::RRP::Exception::InvalidEntityValue"->throw($buffer) 0 0 0
83 0 my $entityName = lc ( $1 );
84 0 ▶ $entityName = NAME_SUBSCRIBE->{$entityName} if NAME_SUBSCRIBE->{$entityName};
Branch: 1 branch
BranchTrueFalseTotal
if {"nameserver" => "NameServer"}->{$entityName} 0 0 0
85 0 my $entityPackageName = 'Net::RRP::Entity::' . ucfirst ( $entityName );
86 0 eval "use $entityPackageName";
87 0 ▶ throw Net::RRP::Exception::InvalidEntityValue ( $buffer ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
88 0 $entity = $entityPackageName->new ();
89 0 $request->setEntity ( $entity );
90 }
91  
92 0 my ( $index, $line ) = 0;
93  
94 0 ▶ while ( ( ( $line = $lines [ $index++ ] ) =~ m/^[^-]/ ) && ( $line ne '.' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (($line = $lines[$index++]) =~ /^[^-]/ and $line ne ".") 0 0 0
Condition0%0/3
($line = $lines[$index++]) =~ /^[^-]/ and $line ne "."
!ll&&!rl&&r
000
MC/DC0%0/2
($line = $lines[$index++]) =~ /^[^-]/ and $line ne "."
($line = $lines[$index++]) =~ /^[^-]/ $line ne "."
Truth table0%0/3
A and B
A= ($line = $lines[$index++]) =~ /^[^-]/B= $line ne "."
ABresult
0X0
100
111
95 {
96 0 ▶ $line =~ m/:/ || throw Net::RRP::Exception::InvalidAttributeValueSyntax ( $buffer );
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /:/ or "Net::RRP::Exception::InvalidAttributeValueSyntax"->throw($buffer) 0 0 0
97 0 my $old = eval { $entity->getAttribute ( $` ); };
98 0 ▶ if ( $old )
Branch: 1 branch
BranchTrueFalseTotal
if ($old) { } 0 0 0
99 {
100 0 ▶ $old = [ $old ] unless ref ( $old );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $old 0 0 0
101 0 push @$old, $';
102 }
103 else
104 {
105 0 $old = $';
106 }
107 0 $entity->setAttribute ( $` => $old );
108 }
109  
110 0 $index--;
111  
112 0 ▶ while ( ( ( $line = $lines [ $index++ ] ) =~ m/^-/ ) && ( $line ne '.' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (($line = $lines[$index++]) =~ /^-/ and $line ne ".") 0 0 0
Condition0%0/3
($line = $lines[$index++]) =~ /^-/ and $line ne "."
!ll&&!rl&&r
000
MC/DC0%0/2
($line = $lines[$index++]) =~ /^-/ and $line ne "."
($line = $lines[$index++]) =~ /^-/ $line ne "."
Truth table0%0/3
A and B
A= ($line = $lines[$index++]) =~ /^-/B= $line ne "."
ABresult
0X0
100
111
113 {
114 0 ▶ $line =~ m/-(.*?):(.*)/ || throw Net::RRP::Exception::InvalidCommandOption ( $buffer );
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /-(.*?):(.*)/ or "Net::RRP::Exception::InvalidCommandOption"->throw($buffer) 0 0 0
115 0 $request->setOption ( $1 => $2 );
116 }
117  
118 0 $index--;
119  
120 0 ▶ throw Net::RRP::Exception::InvalidCommandSequence ( $buffer ) if ( $lines [ $index ] ne '.' || $#lines > $index );
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$index] ne "." or $#lines > $index 0 0 0
Condition0%0/3
$lines[$index] ne "." or $#lines > $index
l!l&&r!l&&!r
000
MC/DC0%0/2
$lines[$index] ne "." or $#lines > $index
$lines[$index] ne "." $#lines > $index
Truth table0%0/3
A or B
A= $lines[$index] ne "."B= $#lines > $index
ABresult
1X1
011
000
121  
122 0 $request;
123 }
124  
125 =head2
126  
127 This method encode the rrp request to buffer for send it's to any stream.
128  
129 Example:
130  
131 my $buffer = $codec->encodeRequest ( $request );
132  
133 =cut
134  
135 #'
136  
137 sub encodeRequest
138 0 ▶{
Subroutine encodeRequest: not called CC=7 SCAR=40.3
Pod undocumented
139 0 my ( $this, $request ) = @_;
140 0 my $buffer = lc ( $request->getName ) . CRLF;
141 0 my $entity = undef;
142  
143 try
144 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 $entity = $request->getEntity;
146 }
147 catch Net::RRP::Exception with
148 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
149 0 my $exception = shift;
150 0 ▶ $exception->throw unless $exception->isa ( 'Net::RRP::Exception::MissingRequiredEntity' );
Branch: 1 branch
BranchTrueFalseTotal
unless $exception->isa("Net::RRP::Exception::MissingRequiredEntity") 0 0 0
151 0 };
152  
153 0 ▶ if ( $entity )
Branch: 1 branch
BranchTrueFalseTotal
if ($entity) 0 0 0
154 {
155 0 $buffer .= 'EntityName:' . $entity->getName . CRLF;
156 0 ▶ if ( my $attributes = $entity->getAttributes )
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = $entity->getAttributes) 0 0 0
157 {
158 0 ▶ $buffer .= join ( CRLF, map { my $key = $_;
Branch: 1 branch
BranchTrueFalseTotal
if %$attributes 0 0 0
159 0 my $value = $attributes->{$key};
160 0 ▶ map { "$key:$_" } @{ ( ref $value ? $value : [ $value ] ) } } keys %$attributes ) . CRLF
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 0 0
161 if ( %$attributes );
162 }
163 }
164 0 my $options = $request->getOptions;
165 0 ▶ $buffer .= join ( CRLF, map { "-$_:" . $options->{$_} } keys %$options ) . CRLF if ( ( $options ) and ( %$options ) );
Branch: 1 branch
BranchTrueFalseTotal
if $options and %$options 0 0 0
Condition0%0/3
$options and %$options
!ll&&!rl&&r
000
MC/DC0%0/2
$options and %$options
$options %$options
Truth table0%0/3
A and B
A= $optionsB= %$options
ABresult
0X0
100
111
166 0 $buffer .= '.' . CRLF;
167 0 $buffer;
168 }
169  
170 =head2 decodeResponse
171  
172 This method constructs the instance of Net::RRP::Response::n$NNN class from input buffer,
173 where the $NNN is a response number ( the Net::RRP::Response::n$NNN loads dynamic ). This
174 method say throw() at any errors;
175  
176 Example:
177 my $response = $codec->decodeResponse ( $buffer );
178  
179 =cut
180  
181 sub decodeResponse
182 0 ▶{
Subroutine decodeResponse: not called CC=11 SCAR=48.8
Pod documented
183 0 my ( $this, $buffer ) = @_;
184  
185 0 my @lines = split CRLF, $buffer;
186  
187 0 my $responseHeader = shift @lines;
188 0 ▶ $responseHeader =~ /^(\d+) (.+)/ || throw Net::RRP::Exception::InvalidResponseFormat ( $buffer );
Branch: 1 branch
BranchTrueFalseTotal
$responseHeader =~ /^(\d+) (.+)/ or "Net::RRP::Exception::InvalidResponseFormat"->throw($buffer) 0 0 0
189  
190 0 my $responsePackageName = 'Net::RRP::Response::n' . $1;
191 0 eval "use $responsePackageName";
192 0 ▶ throw Net::RRP::Exception::InvalidCommandSequence ( $buffer ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
193  
194 0 my $response = $responsePackageName->new();
195 0 $response->setDescription ( $2 );
196  
197 0 my $index = 0;
198 0 my $line;
199  
200 0 ▶ while ( ( $line = $lines [ $index++ ] ) && ( $line ne '.' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $lines[$index++] and $line ne ".") 0 0 0
Condition0%0/3
$line = $lines[$index++] and $line ne "."
!ll&&!rl&&r
000
MC/DC0%0/2
$line = $lines[$index++] and $line ne "."
$line = $lines[$index++] $line ne "."
Truth table0%0/3
A and B
A= $line = $lines[$index++]B= $line ne "."
ABresult
0X0
100
111
201 {
202 0 ▶ $line =~ m/:/ || throw Net::RRP::Exception::InvalidAttributeValueSyntax ( $buffer );
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /:/ or "Net::RRP::Exception::InvalidAttributeValueSyntax"->throw($buffer) 0 0 0
203 0 my $result = eval { $response->getAttribute ( $` ) };
204 0 ▶ if ( $result )
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
205 {
206 0 ▶ $result = [ $result ] unless ref ( $result );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result 0 0 0
207 0 push @$result, $';
208 }
209 else
210 {
211 0 $result = $';
212 }
213 0 $response->setAttribute ( $` => $result );
214 }
215  
216 0 $index--;
217  
218 0 ▶ throw Net::RRP::Exception::InvalidResponseFormat ( $buffer ) if ( ( ! $line ) || ( $line ne '.' ) || ( $#lines > $index ) );
Branch: 1 branch
BranchTrueFalseTotal
if not $line or $line ne "." or $#lines > $index 0 0 0
Condition0%0/6
not $line or $line ne "." or $#lines > $index
l!l&&r!l&&!r
000
not $line or $line ne "."
l!l&&r!l&&!r
000
MC/DC0%0/3
not $line or $line ne "." or $#lines > $index
not $line $line ne "." $#lines > $index
Truth table0%0/4
not(A) or B or C
A= not $lineB= $line ne "."C= $#lines > $index
ABCresult
1XX1
01X1
0011
0000
219  
220 0 $response;
221 }
222  
223 =head2 encodeResponse
224  
225 This method get the instance of Net::RRP::Response child class and encodes it's to rrp format.
226  
227 Example:
228  
229 my $buffer = $codec->encodeResponse ( $response );
230  
231 =cut
232  
233 sub encodeResponse
234 0 ▶{
Subroutine encodeResponse: not called CC=4 SCAR=30.0
Pod documented
235 0 my ( $this, $response ) = @_;
236 0 my $buffer = $response->getCode . ' ' . $response->getDescription . CRLF;
237 0 ▶ if ( my $attributes = $response->getAttributes )
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = $response->getAttributes) 0 0 0
238 {
239 0 ▶ $buffer .= join ( CRLF, map { my $key = $_;
Branch: 1 branch
BranchTrueFalseTotal
if %$attributes 0 0 0
240 0 my $value = $attributes->{$key};
241 0 ▶ map { "$key:$_" } @{ ( ref $value ? $value : [ $value ] ) } } keys %$attributes ) . CRLF
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 0 0
242 if ( %$attributes );
243 #$buffer .= join ( CRLF, map { "$_:" . $attributes->{$_} } keys %$attributes ) . CRLF if ( scalar ( %$attributes ) );
244 }
245 0 $buffer .= '.' . CRLF;
246 0 $buffer;
247 }
248  
249 1 1;
250  
251 =head1 AUTHOR AND COPYRIGHT
252  
253 Net::RRP::Codec (C) Michael Kulakov, Zenon N.S.P. 2000
254 125124, 19, 1-st Jamskogo polja st,
255 Moscow, Russian Federation
256  
257 mkul@cpan.org
258  
259 All rights reserved.
260  
261 You may distribute this package under the terms of either the GNU
262 General Public License or the Artistic License, as specified in the
263 Perl README file.
264  
265 =head1 SEE ALSO
266  
267 L<Net::RRP::Request(3)>, L<Net::RRP::Response(3)>, L<Net::RRP::Entity(3)>, RFC 2832
268  
269 =cut
270  
271 __END__
« RRP/Toolkit.pm ↑ Summary RRP/Protocol.pm »