r10018 - in /branches/upstream/libaudio-mpd-perl/current: Changes META.yml bin/mpd-dynamic lib/Audio/MPD.pm t/21-new.t

joeyh at users.alioth.debian.org joeyh at users.alioth.debian.org
Thu Nov 29 22:49:46 UTC 2007


Author: joeyh
Date: Thu Nov 29 22:49:46 2007
New Revision: 10018

URL: http://svn.debian.org/wsvn/?sc=1&rev=10018
Log:
Load Audio-MPD-0.19.0 into ..

Modified:
    branches/upstream/libaudio-mpd-perl/current/Changes
    branches/upstream/libaudio-mpd-perl/current/META.yml
    branches/upstream/libaudio-mpd-perl/current/bin/mpd-dynamic
    branches/upstream/libaudio-mpd-perl/current/lib/Audio/MPD.pm
    branches/upstream/libaudio-mpd-perl/current/t/21-new.t

Modified: branches/upstream/libaudio-mpd-perl/current/Changes
URL: http://svn.debian.org/wsvn/branches/upstream/libaudio-mpd-perl/current/Changes?rev=10018&op=diff
==============================================================================
--- branches/upstream/libaudio-mpd-perl/current/Changes (original)
+++ branches/upstream/libaudio-mpd-perl/current/Changes Thu Nov 29 22:49:46 2007
@@ -1,6 +1,7 @@
 High-level changelog
 ====================
 
+0.19.x  possibility to change connection scheme
 0.18.x  using common classes in audio::mpd::common
 0.17.x  introducing AM::Playlist + AM::Time
 0.16.x  utf-8 support + introducing mpd-dynamic script
@@ -17,6 +18,16 @@
 
 Low-level changelog
 ===================
+
+0.19.0 Thu Nov 29 20:10:18 CET 2007
+ - changed constructor api: using a hash for options instead of
+   positional paraemters
+ - new conntype param for constructor to change the way connection is
+   handled
+ - MPD_HOST env var supports password at host
+ - mpd-dynamic daemonizes after mpd connection to trap problems
+
+**
 
 0.18.3 Mon Nov 26 12:51:40 CET 2007
  - kwalitee/cpants release

Modified: branches/upstream/libaudio-mpd-perl/current/META.yml
URL: http://svn.debian.org/wsvn/branches/upstream/libaudio-mpd-perl/current/META.yml?rev=10018&op=diff
==============================================================================
--- branches/upstream/libaudio-mpd-perl/current/META.yml (original)
+++ branches/upstream/libaudio-mpd-perl/current/META.yml Thu Nov 29 22:49:46 2007
@@ -1,6 +1,6 @@
 ---
 name: Audio-MPD
-version: 0.18.3
+version: 0.19.0
 author: []
 abstract: class to talk to MPD (Music Player Daemon) servers
 license: perl
@@ -29,7 +29,7 @@
 provides:
   Audio::MPD:
     file: lib/Audio/MPD.pm
-    version: 0.18.3
+    version: 0.19.0
   Audio::MPD::Collection:
     file: lib/Audio/MPD/Collection.pm
   Audio::MPD::Playlist:

Modified: branches/upstream/libaudio-mpd-perl/current/bin/mpd-dynamic
URL: http://svn.debian.org/wsvn/branches/upstream/libaudio-mpd-perl/current/bin/mpd-dynamic?rev=10018&op=diff
==============================================================================
--- branches/upstream/libaudio-mpd-perl/current/bin/mpd-dynamic (original)
+++ branches/upstream/libaudio-mpd-perl/current/bin/mpd-dynamic Thu Nov 29 22:49:46 2007
@@ -18,12 +18,13 @@
 use Proc::Daemon;
 use Time::HiRes    qw[ usleep ];
 
-Proc::Daemon::Init unless $ARGV{debug};
 
 #
 my $song     = 0; # song currently playing
 my $playlist = 0; # playlist version
 my $mpd = Audio::MPD->new;
+
+Proc::Daemon::Init unless $ARGV{debug};
 
 # fetch list of songs known by mpd.
 my @files = $mpd->collection->all_pathes;

Modified: branches/upstream/libaudio-mpd-perl/current/lib/Audio/MPD.pm
URL: http://svn.debian.org/wsvn/branches/upstream/libaudio-mpd-perl/current/lib/Audio/MPD.pm?rev=10018&op=diff
==============================================================================
--- branches/upstream/libaudio-mpd-perl/current/lib/Audio/MPD.pm (original)
+++ branches/upstream/libaudio-mpd-perl/current/lib/Audio/MPD.pm Thu Nov 29 22:49:46 2007
@@ -19,44 +19,60 @@
 use Audio::MPD::Playlist;
 use Encode;
 use IO::Socket;
-
-
-use base qw[ Class::Accessor::Fast ];
+use Readonly;
+
+
+use base qw[ Class::Accessor::Fast Exporter ];
 __PACKAGE__->mk_accessors(
-    qw[ _host _password _port
+    qw[ _conntype _host _password _port _socket
         collection playlist version ] );
 
 
-our $VERSION = '0.18.3';
+our $VERSION = '0.19.0';
+
+Readonly our $REUSE => 0;
+Readonly our $ONCE  => 1;
+
+our @EXPORT = qw[ $REUSE $ONCE ];
 
 
 #--
 # Constructor
 
 #
-# my $mpd = Audio::MPD->new( [$hostname], [$port], [$password] )
-#
-# This is the constructor for Audio::MPD. One can specify a $hostname, a
-# $port, and a $password.
-# If none is specified then defaults to environment vars MPD_HOST, MPD_PORT
-# and MPD_PASSWORD. If those aren't set, defaults to 'localhost', 6600 and ''.
-#
+# my $mpd = Audio::MPD->new( [%opts] )
+#
+# This is the constructor for Audio::MPD. One can specify the following
+# options:
+#   - hostname => $hostname : defaults to environment var MPD_HOST, then to 'localhost'
+#   - port     => $port     : defaults to env var MPD_PORT, then to 6600
+#   - password => $password : defaults to env var MPD_PASSWORD, then to ''
+#   - conntype => $type     : how the connection to mpd server is handled. it can be
+#               either $REUSE: reuse the same connection
+#                    or $ONCE: open a new connection per command (default)
+#   
 sub new {
-    my $class = shift;
-    my ($host, $port, $password) = @_;
+    my ($class, %opts) = @_;
 
     # use mpd defaults.
-    $host     = $ENV{MPD_HOST}     || 'localhost' unless defined $host;
-    $port     = $ENV{MPD_PORT}     || '6600'      unless defined $port;
-    $password = $ENV{MPD_PASSWORD} || ''          unless defined $password;
+    my ($default_password, $default_host) = split( '@', $ENV{MPD_HOST} )
+       if exists $ENV{MPD_HOST} && $ENV{MPD_HOST} =~ /@/;
+    my $host     = $opts{host}     || $default_host      || $ENV{MPD_HOST} || 'localhost';
+    my $port     = $opts{port}     || $ENV{MPD_PORT}     || '6600';
+    my $password = $opts{password} || $ENV{MPD_PASSWORD} || $default_password || '';
 
     # create & bless the object.
     my $self = {
         _host     => $host,
         _port     => $port,
         _password => $password,
+        _conntype => exists $opts{conntype} ? $opts{conntype} : $ONCE,
     };
     bless $self, $class;
+
+    # create the connection if conntype is set to $REUSE
+    $self->_connect_to_mpd_server if $self->_conntype == $REUSE;
+
 
     # create the helper objects and store them.
     $self->collection( Audio::MPD::Collection->new($self) );
@@ -74,20 +90,48 @@
 
 
 #
+# $mpd->_connect_to_mpd_server;
+#
+# This method connects to the mpd server. It can die on several conditions:
+#  - if the server cannot be reached,
+#  - if it's not an mpd server,
+#  - or if the password is incorrect,
+#
+sub _connect_to_mpd_server {
+    my ($self) = @_;
+
+    # try to connect to mpd.
+    my $socket = IO::Socket::INET->new(
+        PeerAddr => $self->_host,
+        PeerPort => $self->_port,
+    )
+    or die "Could not create socket: $!\n";
+
+    # parse version information.
+    my $line = $socket->getline;
+    chomp $line;
+    die "Not a mpd server - welcome string was: [$line]\n"
+        if $line !~ /^OK MPD (.+)$/;
+    $self->version($1);
+
+    # send password.
+    if ( $self->_password ) {
+        $socket->print( 'password ' . encode('utf-8', $self->_password) . "\n" );
+        $line = $socket->getline;
+        die $line if $line =~ s/^ACK //;
+    }
+
+    # save socket
+    $self->_socket($socket);
+}
+
+
+#
 # my @result = $mpd->_send_command( $command );
 #
 # This method is central to the module. It is responsible for interacting with
 # mpd by sending the $command and reading output - that will be returned as an
 # array of chomped lines (status line will not be returned).
-#
-# Note that currently, this method will connect to mpd before sending any
-# command, and will disconnect after the command has been issued. This scheme
-# is far from optimal, but allows us not to care about timeout disconnections.
-#
-# /!\ Note that we're using high-level, blocking sockets. This means that if
-# the mpd server is slow, or hangs for whatever reason, or even crash abruptly,
-# the program will be hung forever in this sub. The POE::Component::Client::MPD
-# module is way safer - you're advised to use it instead of Audio::MPD.
 #
 # This method can die on several conditions:
 #  - if the server cannot be reached,
@@ -99,32 +143,13 @@
 sub _send_command {
     my ($self, $command) = @_;
 
-    # try to connect to mpd.
-    my $socket = IO::Socket::INET->new(
-        PeerAddr => $self->_host,
-        PeerPort => $self->_port
-    )
-    or die "Could not create socket: $!\n";
-    my $line;
-
-    # parse version information.
-    $line = $socket->getline;
-    chomp $line;
-    die "Not a mpd server - welcome string was: [$line]\n"
-        if $line !~ /^OK MPD (.+)$/;
-    $self->version($1);
-
-    # send password.
-    if ( $self->_password ) {
-        $socket->print( 'password ' . encode('utf-8', $self->_password) . "\n" );
-        $line = $socket->getline;
-        die $line if $line =~ s/^ACK //;
-    }
+    $self->_connect_to_mpd_server if $self->_conntype == $ONCE;
+    my $socket = $self->_socket;
 
     # ok, now we're connected - let's issue the command.
     $socket->print( encode('utf-8', $command) );
     my @output;
-    while (defined ( $line = $socket->getline ) ) {
+    while (defined ( my $line = $socket->getline ) ) {
         chomp $line;
         die $line if $line =~ s/^ACK //; # oops - error.
         last if $line =~ /^OK/;          # end of output.
@@ -132,7 +157,7 @@
     }
 
     # close the socket.
-    $socket->close;
+    $socket->close if $self->_conntype == $ONCE;
 
     return @output;
 }
@@ -567,7 +592,18 @@
 Audio::MPD gives a clear object-oriented interface for talking to and
 controlling MPD (Music Player Daemon) servers. A connection to the MPD
 server is established as soon as a new Audio::MPD object is created.
-Commands are then sent to the server as the class's methods are called.
+
+Note that the module will by default connect to mpd before sending any
+command, and will disconnect after the command has been issued. This scheme
+is far from optimal, but allows us not to care about timeout disconnections.
+
+B</!\> Note that Audio::MPD is using high-level, blocking sockets. This
+means that if the mpd server is slow, or hangs for whatever reason, or
+even crash abruptly, the program will be hung forever in this sub. The
+POE::Component::Client::MPD module is way safer - you're advised to use
+it instead of Audio::MPD. Or you can try to set C<conntype> to C<$REUSE>
+(see Audio::MPD constructor for more details), but you would be then on
+your own to deal with disconnections.
 
 
 =head1 METHODS
@@ -576,13 +612,34 @@
 
 =over 4
 
-=item new( [$host] [, $port] [, $password] )
-
-This is the constructor for Audio::MPD. One can specify a $hostname, a
-$port, and a $password.
-
-If none is specified then defaults to environment vars MPD_HOST, MPD_PORT
-and MPD_PASSWORD. If those aren't set, defaults to 'localhost', 6600 and ''.
+=item new( [%opts] )
+
+This is the constructor for Audio::MPD. One can specify the following
+options:
+
+=over 4
+
+=item hostname => C<$hostname>
+
+defaults to environment var MPD_HOST, then to 'localhost'. Note that
+MPD_HOST can be of the form password at host.
+
+=item port => C<$port>
+
+defaults to environment var MPD_PORT, then to 6600.
+
+=item password => $password
+
+defaults to environment var MPD_PASSWORD, then to ''.
+
+=item conntype => $type
+
+change how the connection to mpd server is handled. It can be either
+C<$REUSE> to reuse the same connection or C<$ONCE> to open a new
+connection per command (default)
+
+=back
+
 
 =back
 

Modified: branches/upstream/libaudio-mpd-perl/current/t/21-new.t
URL: http://svn.debian.org/wsvn/branches/upstream/libaudio-mpd-perl/current/t/21-new.t?rev=10018&op=diff
==============================================================================
--- branches/upstream/libaudio-mpd-perl/current/t/21-new.t (original)
+++ branches/upstream/libaudio-mpd-perl/current/t/21-new.t Thu Nov 29 22:49:46 2007
@@ -18,7 +18,7 @@
 eval 'use Audio::MPD::Test';
 plan skip_all => $@ if $@ =~ s/\n+Compilation failed.*//s;
 
-plan tests => 10;
+plan tests => 13;
 my $mpd;
 
 #
@@ -40,7 +40,7 @@
 
 #
 # testing constructor params.
-$mpd = Audio::MPD->new('127.0.0.1', $port, 'foobar' );
+$mpd = Audio::MPD->new( host=>'127.0.0.1', port=>$port, password=>'foobar' );
 is( $mpd->_host,     '127.0.0.1', 'host set to param' );
 is( $mpd->_port,     $port,       'port set to param' );
 is( $mpd->_password, 'foobar',    'password set to param' );
@@ -53,10 +53,26 @@
 $mpd = Audio::MPD->new;
 is( $mpd->_host,     $ENV{MPD_HOST},     'host default to $ENV{MPD_HOST}' );
 is( $mpd->_port,     $ENV{MPD_PORT},     'port default to $ENV{MPD_PORT}' );
-is( $mpd->_password, $ENV{MPD_PASSWORD}, 'port default to $ENV{MPD_PASSWORD}' );
+is( $mpd->_password, $ENV{MPD_PASSWORD}, 'password default to $ENV{MPD_PASSWORD}' );
+
+delete $ENV{MPD_HOST};
+delete $ENV{MPD_PASSWORD};
+$ENV{MPD_HOST} = 'foobar at 127.0.0.1';
+is( $mpd->_host,     '127.0.0.1', 'host detected when $ENV{MPD_HOST} is passwd at host' );
+is( $mpd->_password, 'foobar',    'password detected when $ENV{MPD_HOST} is passwd at host' );
+
+$mpd = Audio::MPD->new;
 
 delete $ENV{MPD_HOST};
 delete $ENV{MPD_PORT};
-delete $ENV{MPD_PASSWORD};
+
+
+#
+# testing connection type
+$mpd = Audio::MPD->new( port=>16600,conntype=>$REUSE );
+$mpd->ping;
+$mpd->ping;
+$mpd->ping;
+isa_ok( $mpd->_socket, 'IO::Socket', 'socket is created and retained' );
 
 exit;




More information about the Pkg-perl-cvs-commits mailing list