blib/lib/Net/FreeDB2/Connection/CDDBP.pm

filter: Statementstmt 29.4% 43 / 146 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 11 / 33 Podpod 100% 21 / 21 totaltotal 32.0% 75 / 234 CC 19 SCAR 22.8
CC
19
Cov
24%
waitCommandReply
30.0
mkHello
30.0
_initialize
12.0
CRAP 9.8
« blib/lib/Net/FreeDB2/Match.pm ↑ Summary blib/lib/Net/FreeDB2/Response.pm »
1 package Net::FreeDB2::Connection::CDDBP;
2  
3 # Copyright 2002, Vincenzo Zocca.
4  
5 # See LICENSE section for usage and distribution rights.
6  
7 1 require 5.005_62;
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Error qw (:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 require Exporter;
13 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #our @ISA = qw(Net::FreeDB2::Connection Exporter);
16 1 ▶use base qw(Net::FreeDB2::Connection Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Items to export into callers namespace by default. Note: do not export
19 # names by default without a very good reason. Use EXPORT_OK instead.
20 # Do not simply export all your public functions/methods/constants.
21  
22 # This allows declaration use Net::FreeDB2::Connection::CDDBP ':all';
23 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
24 # will save memory.
25 1 our %EXPORT_TAGS = ( 'all' => [ qw(
26
27 ) ] );
28  
29 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
30  
31 1 our @EXPORT = qw(
32
33 );
34 1 our ( $VERSION ) = '$Revision: 0.8.2.4 $ ' =~ /\$Revision:\s+([^\s]+)/;
35  
36 1 my $FINAL_EOL_RX = '[\r\n]';
37 1 my $FINAL_DOT_RX = '[\r\n]\.[\r\n]';
38  
39 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
40 # Call constructor of super class
41 0 my $self = &Net::FreeDB2::Connection::new (@_);
42  
43 # Shift out this class specification
44 0 shift;
45  
46 # Return object
47 0 return ($self);
48 }
49  
50 0 ▶sub _initialize {
Subroutine _initialize: not called CC=3 SCAR=24.8
51 0 my $self = shift;
52  
53 # Get options
54 0 ▶ my $opt = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
55  
56 # Proxy on CDDBP not yet defined
57 0 ▶ defined ($opt->{proxy_host}) &&
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{"proxy_host"} and "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::_initialize, CDDBP access through proxy not (yet) implemented.") 0 0 0
58 throw Error::Simple ("ERROR: Net::FreeDB2::Connection::CDDBP::_initialize, CDDBP access through proxy not (yet) implemented.");
59  
60 # Initialize super class
61 0 return ($self->SUPER::_initialize ($opt));
62 }
63  
64 0 ▶sub connect {
Subroutine connect: not called CC=4 SCAR=23.2
Pod documented
65 0 my $self = shift;
66  
67 # Make socket connection
68 1 ▶ use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 0 ▶ my $connection = IO::Socket::INET->new (
Condition0%0/2
$self->getFreeDBPort || 8880
l!l
00
MC/DC0%0/1
$self->getFreeDBPort || 8880
$self->getFreeDBPort
Truth table0%0/2
A
A= $self->getFreeDBPort
Aresult
11
00
70 PeerAddr => $self->getFreeDBHost (),
71 PeerPort => $self->getFreeDBPort () || 8880,
72 );
73 0 ▶ defined ($connection) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::CDDBP::connect, Failed to instanciate an \'IO::Socket::INET\' object.');
Branch: 1 branch
BranchTrueFalseTotal
defined $connection or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::connect, Failed to instanciate an 'IO::Socket::INET' object.") 0 0 0
74  
75 # Set the connection
76 0 $self->setConnection ($connection);
77  
78 # Send command and wait for reply
79 0 my $content_ref = $self->waitCommandReply (undef, {
80 200 => $FINAL_EOL_RX,
81 201 => $FINAL_EOL_RX,
82 432 => $FINAL_EOL_RX,
83 433 => $FINAL_EOL_RX,
84 434 => $FINAL_EOL_RX,
85 });
86  
87 # Parse the result and store it
88 1 ▶ use Net::FreeDB2::Response::SignOn;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 0 $self->setSignOnResponse (Net::FreeDB2::Response::SignOn->new ({
90 content_ref => $content_ref
91 }));
92  
93 # Send a hello
94 0 my $res = $self->hello ();
95  
96 # Disconnect and throw exception if error
97 0 ▶ if ($res->hasError ()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->hasError) 0 0 0
98 0 $self->setConnection ();
99 0 throw Error::Simple ('ERROR: Net::FreeDB2::Connection::CDDBP::connect, handshake failed.');
100 }
101 }
102  
103 0 ▶sub lscat {
Subroutine lscat: not called CC=3 SCAR=24.8
Pod documented
104 0 my $self = shift;
105  
106 # Send command and wait for reply
107 0 my $content_ref = $self->waitCommandReply ('cddb lscat', {
108 210 => $FINAL_DOT_RX,
109 });
110  
111 # Parse the result
112 0 my @content = split (/[\n\r]+/, ${$content_ref});
113 0 shift (@content);
114 0 my @cat = ();
115 0 foreach my $cat (@content) {
116 0 ▶ last if ($cat eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if $cat eq "." 0 0 0
117 0 push (@cat, $cat);
118 }
119 0 return (@cat);
120 }
121  
122 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=2.2
Pod documented
123 0 my $self = shift;
124 0 my $entity = shift;
125  
126 # Send command and wait for reply
127 0 my $cmd = 'cddb query ' . $entity->mkQuery ();
128 0 my $content_ref = $self->waitCommandReply ($cmd, {
129 200 => $FINAL_EOL_RX,
130 211 => $FINAL_DOT_RX,
131 202 => $FINAL_EOL_RX,
132 403 => $FINAL_EOL_RX,
133 409 => $FINAL_EOL_RX,
134 });
135  
136 # Parse the result
137 1 ▶ use Net::FreeDB2::Response::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 0 return Net::FreeDB2::Response::Query->new ({
139 content_ref => $content_ref,
140 });
141 }
142  
143 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=2.2
Pod documented
144 0 my $self = shift;
145 0 my $match = shift;
146  
147 # Send command and wait for reply
148 0 my $cmd = 'cddb read ' . $match->getCateg () . ' ' . $match->getDiscid ();
149 0 my $content_ref = $self->waitCommandReply ($cmd, {
150 210 => $FINAL_DOT_RX,
151 401 => $FINAL_EOL_RX,
152 402 => $FINAL_EOL_RX,
153 403 => $FINAL_EOL_RX,
154 409 => $FINAL_EOL_RX,
155 });
156  
157 # Parse the result
158 1 ▶ use Net::FreeDB2::Response::Read;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 0 return Net::FreeDB2::Response::Read->new ({
160 content_ref => $content_ref,
161 });
162 }
163  
164 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
165 0 throw Error::Simple ("ERROR: Net::FreeDB2::Connection::CDDBP::write, to be implemented.");
166 }
167  
168 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
169 0 throw Error::Simple ("ERROR: Net::FreeDB2::CDDBP::log, to be implemented.");
170 }
171  
172 0 ▶sub motd {
Subroutine motd: not called CC=3 SCAR=24.8
Pod documented
173 0 my $self = shift;
174  
175 # Send command and wait for reply
176 0 my $content_ref = $self->waitCommandReply ('motd', {
177 210 => $FINAL_DOT_RX,
178 401 => $FINAL_EOL_RX,
179 });
180  
181 # Parse the result
182 0 my @content = split (/[\n\r]+/, ${$content_ref});
183 0 my $head = shift (@content);
184 0 my @motd = ();
185 0 foreach my $motd (@content) {
186 0 ▶ last if ($motd eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if $motd eq "." 0 0 0
187 0 push (@motd, $motd);
188 }
189 0 return (@motd);
190 }
191  
192 0 ▶sub discid {
Subroutine discid: not called CC=2 SCAR=17.9
Pod documented
193 0 my $self = shift;
194 0 my $entity = shift;
195  
196 # Send command and wait for reply
197 0 my $cmd = 'discid ' . $entity->mkQuery ();
198 0 my $content_ref = $self->waitCommandReply ($cmd, {
199 200 => $FINAL_EOL_RX,
200 500 => $FINAL_EOL_RX,
201 });
202  
203 # Parse the result
204 0 my @content = split (/[\n\r]+/, ${$content_ref});
205 0 my $head = shift (@content);
206 0 my ($code) = $head =~ /^\s*(\d{3})\s+/;
207 0 ▶ $code == 500 && throw Error::Simple ("ERROR: Net::FreeDB2::Connection::CDDBP::discid, Command Syntax error.");
Branch: 1 branch
BranchTrueFalseTotal
$code == 500 and "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::discid, Command Syntax error.") 0 0 0
208 0 my @head = split (/\s+/, $head);
209 0 return ($head[4]);
210 }
211  
212 0 ▶sub proto {
Subroutine proto: not called CC=1 SCAR=6.9
Pod documented
213 0 throw Error::Simple ("ERROR: Net::FreeDB2::CDDBP::proto, to be implemented.");
214 }
215  
216 0 ▶sub sites {
Subroutine sites: not called CC=1 SCAR=1.2
Pod documented
217 0 my $self = shift;
218  
219 # Send command and wait for reply
220 0 my $content_ref = $self->waitCommandReply ('sites', {
221 210 => $FINAL_DOT_RX,
222 401 => $FINAL_EOL_RX,
223 });
224  
225 # Parse the result
226 1 ▶ use Net::FreeDB2::Response::Sites;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 0 return Net::FreeDB2::Response::Sites->new ({
228 content_ref => $content_ref,
229 });
230 }
231  
232 0 ▶sub stat {
Subroutine stat: not called CC=1 SCAR=6.9
Pod documented
233 0 throw Error::Simple ("ERROR: Net::FreeDB2::CDDBP::stat, to be implemented.");
234 }
235  
236 0 ▶sub ver {
Subroutine ver: not called CC=1 SCAR=6.9
Pod documented
237 0 throw Error::Simple ("ERROR: Net::FreeDB2::CDDBP::ver, to be implemented.");
238 }
239  
240 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
241 0 throw Error::Simple ("ERROR: Net::FreeDB2::CDDBP::update, to be implemented.");
242 }
243  
244 0 ▶sub whom {
Subroutine whom: not called CC=1 SCAR=6.9
Pod documented
245 0 throw Error::Simple ("ERROR: Net::FreeDB2::CDDBP::whom, to be implemented.");
246 }
247  
248 0 ▶sub setSignOnResponse {
Subroutine setSignOnResponse: not called CC=1 SCAR=6.9
Pod documented
249 0 my $self = shift;
250  
251 # Set FreeDB/CDDB sign-on response
252 0 $self->{Net_FreeDB2_Connection_CDDBP}{sign_on_response} = shift;
253 }
254  
255 0 ▶sub getSignOnResponse {
Subroutine getSignOnResponse: not called CC=1 SCAR=6.9
Pod documented
256 0 my $self = shift;
257  
258 # Return FreeDB/CDDB sign-on response
259 0 return ($self->{Net_FreeDB2_Connection_CDDBP}{sign_on_response});
260 }
261  
262 0 ▶sub hello {
Subroutine hello: not called CC=1 SCAR=1.7
Pod documented
263 0 my $self = shift;
264  
265 # Send command and wait for reply
266 0 my $cmd = 'cddb ' . $self->mkHello ();
267 0 my $content_ref = $self->waitCommandReply ($cmd, {
268 200 => $FINAL_EOL_RX,
269 431 => $FINAL_EOL_RX,
270 402 => $FINAL_EOL_RX,
271 });
272  
273 # Parse the result and return it
274 1 ▶ use Net::FreeDB2::Response::Hello;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 0 return (Net::FreeDB2::Response::Hello->new ({
276 content_ref => $content_ref
277 }));
278 }
279  
280 0 ▶sub quit {
Subroutine quit: not called CC=1 SCAR=6.9
Pod documented
281 0 my $self = shift;
282  
283 # Send command and wait for reply
284 0 my $content_ref = $self->waitCommandReply ('quit', {
285 230 => $FINAL_EOL_RX,
286 });
287  
288 # Disconnect
289 0 $self->setConnection ();
290 }
291  
292 0 ▶sub waitCommandReply {
Subroutine waitCommandReply: not called CC=5 SCAR=34.0
Pod documented
293 0 my $self = shift;
294 0 my $cmd = shift;
295 0 my $rx = shift;
296  
297 # Check if connection is defined
298 0 ▶ defined ($self->getConnection ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::CDDBP::waitCommandReply, no connection available.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getConnection or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::waitCommandReply, no connection available.") 0 0 0
299  
300 # Set blocking
301 0 $self->getConnection->blocking (1);
302  
303 # Send command
304 0 ▶ defined ($cmd) && $self->getConnection ()->write ($cmd . "\r\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $cmd and $self->getConnection->write($cmd . "\r\n") 0 0 0
305  
306 # Wait for code
307 0 $self->getConnection->read (my $head, 5);
308 0 $head =~ s/^\s+//;
309 0 my ($code) = $head =~ /(\d{3})/;
310 0 ▶ exists ($rx->{$code}) || throw Error::Simple ("ERROR: Net::FreeDB2::Connection::CDDBP::waitCommandReply, unknown code '$code' returned.");
Branch: 1 branch
BranchTrueFalseTotal
exists $rx->{$code} or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::waitCommandReply, unknown code '${code}' returned.") 0 0 0
311  
312 # Wait for the final DOT or EOL
313 0 my $content .= $head;
314 0 $self->getConnection->blocking (0);
315 0 while (1) {
316 0 $self->getConnection->read (my $rest, 1024);
317 0 $content .= $rest;
318 0 ▶ $content =~ /$rx->{$code}/ && last;
Branch: 1 branch
BranchTrueFalseTotal
$content =~ /$rx->{$code}/ and last 0 0 0
319 0 sleep (1);
320 }
321  
322 # Return the content reference
323 0 return (\$content);
324 }
325  
326 0 ▶sub mkHello {
Subroutine mkHello: not called CC=5 SCAR=34.0
Pod documented
327 0 my $self = shift;
328  
329 0 ▶ defined ($self->getClientName ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, \'client_name\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientName or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, 'client_name' not set.") 0 0 0
330 0 ▶ defined ($self->getClientVersion ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, \'client_version\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientVersion or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, 'client_version' not set.") 0 0 0
331 0 ▶ defined ($self->getClientHost ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, \'client_host\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientHost or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, 'client_host' not set.") 0 0 0
332 0 ▶ defined ($self->getClientUser ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, \'client_user\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientUser or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::CDDBP::mkHello, 'client_user' not set.") 0 0 0
333  
334 0 return ('hello ' . join (' ',
335 $self->getClientUser (),
336 $self->getClientHost (),
337 $self->getClientName (),
338 $self->getClientVersion (),
339 )
340 );
341 }
342  
343 1 1;
344 __END__
« blib/lib/Net/FreeDB2/Match.pm ↑ Summary blib/lib/Net/FreeDB2/Response.pm »