package InfoSys::FreeDB; use 5.006; use strict; use warnings; use AutoLoader qw(AUTOLOAD); use Error qw(:try); use Sys::Hostname; # Package version our ($VERSION) = '$Revision: 0.92 $' =~ /\$Revision:\s+([^\s]+)/; 1; __END__ =head1 NAME InfoSys::FreeDB - FreeDB connection factory =head1 SYNOPSIS require InfoSys::FreeDB; require InfoSys::FreeDB::Entry; # Read entry from the default CD device my $entry = InfoSys::FreeDB::Entry->new_from_cdparanoia(); # Create a HTTP connection my $fact = InfoSys::FreeDB->new(); my $conn = $fact->create_connection( { client_name => 'testing-InfoSys::FreeDB', client_version => $InfoSys::FreeDB::VERSION, } ); # Query FreeDB my $res_q = $conn->query( $entry ); scalar( $res_q->get_match() ) || die 'no matches found for the disck in the default CD-Rom drive'; # Read the first match my $res_r = $conn->read( ( $res_q->get_match() )[0] ); # Write the entry to STDERR use IO::Handle; my $fh = IO::Handle->new_from_fd( fileno(STDERR), 'w' ); $res_r->get_entry()->write_fh( $fh ); =head1 ABSTRACT FreeDB connection factory =head1 DESCRIPTION C is the connection factory of the C module hierarchy. This class creates connections using the protocols supported by FreeDB*. =over =item (*) Currently CDDBP and HTTP protocols are supported. =back =head1 CONSTRUCTOR =over =item new() Creates a new C object. =back =head1 METHODS =over =item create_connection(OPT_HASH_REF) Creates a C object. C is a hash reference used to pass connection creation options. On error an exception C is thrown. =over =item SPEED-UP NOTE If protocol level C<1> is specified, the C method tries to use the highest available protocol level. To do so, it queries the FreeDB to find out exaclty which level is supported. On C connections this doesn't take that long. On C connections it does. To speed up C connections specify a higher C -say C<5>. =back Options for C may include: =over =item B> Connect the created object just after instantiation. Defaults to C<1>. =item B> The hostname of the client. Defaults to C<&Sys::Hostname::hostname()>. =item B> Mandatory option to name the connecting client software. =item B> The user name of the client. Defaults to C) )>. =item B> Mandatory option with the client software version string. =item B>* The FreeDB C to use. Defaults to C<~cddb/cddb.cgi>. =item B> The FreeDB host. Defaults to C. =item B> The port on the FreeDB host. Defaults to C<80> for C and to C<888> for C connection types. =item B> The protocol to use. Either C or C. Defaults to C. =item B> The FreeDB protocol level. Defaults to B<1>. =item B>** The proxy host to use. =item B>** The proxy password to use. =item B>** The port on the proxy host. Defaults to 8080. =item B>** The proxy user name to use. =back =over =item (*) Only supported for the HTTP protocol. =item (**) Proxy is only supported for the HTTP protocol. =back =back =head1 SEE ALSO L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L =head1 BUGS None known (yet.) =head1 TODO =head2 Implement =over =item log() =item update() =back =head2 Test =over =item write() =back =head2 Analyse =over =item CDDBP through firewall =back =head1 HISTORY First development: September 2003 Last update: October 2003 =head1 AUTHOR Vincenzo Zocca =head1 COPYRIGHT Copyright 2003 by Vincenzo Zocca =head1 LICENSE This file is part of the C module hierarchy for Perl by Vincenzo Zocca. The InfoSys::FreeDB module hierarchy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The InfoSys::FreeDB module hierarchy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the InfoSys::FreeDB module hierarchy; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA =cut sub new { my $class = shift; my $self = {}; bless( $self, ( ref($class) || $class ) ); return( $self->_initialize(@_) ); } sub _initialize { my $self = shift; my $opt = defined($_[0]) ? shift : {}; # Check $opt ref($opt) eq 'HASH' || throw Error::Simple("ERROR: InfoSys::FreeDB::_initialize, first argument must be 'HASH' reference."); # Return $self return($self); } sub create_connection { my $self = shift; my $opt = defined($_[0]) ? shift : {}; # Check $opt ref($opt) eq 'HASH' || throw Error::Simple("ERROR: InfoSys::FreeDB::create_connection, first argument must be 'HASH' reference."); # Set default values for $opt $opt->{client_host} = &Sys::Hostname::hostname() if (! $opt->{client_host} ); $opt->{client_user} = scalar( getpwuid($>) ) if (! $opt->{client_user} ); $opt->{freedb_host} = 'freedb.freedb.org' if (! $opt->{freedb_host} ); # Set default value to protocol $opt->{protocol} = 'HTTP' if ( ! $opt->{protocol} ); # Select the correct connection class my $conn = undef; if ( $opt->{protocol} eq 'HTTP' ) { $opt->{freedb_port} = 80 if (! $opt->{freedb_port} ); require InfoSys::FreeDB::Connection::HTTP; $conn = InfoSys::FreeDB::Connection::HTTP->new($opt); } elsif ( $opt->{protocol} eq 'CDDBP' ){ $opt->{freedb_port} = 888 if (! $opt->{freedb_port} ); require InfoSys::FreeDB::Connection::CDDBP; $conn = InfoSys::FreeDB::Connection::CDDBP->new($opt); } else { throw Error::Simple("ERROR: InfoSys::FreeDB::create_connection, protocol '$opt->{protocol}' is not supported. Only 'HTTP' and 'CDDBP' are."); } # Connect if necessary $opt->{auto_connected} = 1 if ( !exists( $opt->{auto_connected} ) ); $opt->{auto_connected} && $conn->connect(); # Return the connection return($conn); }