AnyEvent-Net-MPD

 view release on metacpan or  search on metacpan

lib/AnyEvent/Net/MPD.pm  view on Meta::CPAN

package AnyEvent::Net::MPD;

use strict;
use warnings;

our $VERSION = '0.002';

use Moo;
use MooX::HandlesVia;
extends 'AnyEvent::Emitter';

use AnyEvent;
use AnyEvent::Socket;
use AnyEvent::Handle;

use Types::Standard qw(
  InstanceOf Int ArrayRef HashRef Str Maybe Bool CodeRef
);

use Log::Any;
my $log = Log::Any->get_logger( category => __PACKAGE__ );

has version => (
  is => 'ro',
  isa => Str,
  lazy => 1,
  init_arg => undef,
);

has auto_connect => (
  is => 'ro',
  isa => Bool,
  default => 0,
);

has state => (
  is => 'rw',
  isa => Str,
  init_arg => undef,
  default => 'created',
  trigger => sub {
    $_[0]->emit( state => $_[0]->{state} );
  },
);

has read_queue => (
  is => 'ro',
  isa => ArrayRef [CodeRef],
  lazy => 1,
  init_arg => undef,
  default => sub { [] },
  handles_via => 'Array',
  handles => {
    push_read    => 'push',
    pop_read     => 'pop',
    shift_read   => 'shift',
    unshift_read => 'unshift',
  },
);

has password => (
  is => 'ro',
  isa => Maybe[Str],
  lazy => 1,
);

has port => (
  is => 'ro',
  isa => Int,
  lazy => 1,
  default => sub { $ENV{MPD_PORT} // 6600 },
);

has host => (
  is => 'ro',
  isa => Str,
  lazy => 1,
  default => sub { $ENV{MPD_HOST} // 'localhost' },
);

has _uri => (
  is => 'ro',
  init_arg => undef,
  lazy => 1,
  default => sub {
    my $self = shift;
      ( $self->password ? $self->password . '@' : q{} )
    . $self->host
    . ( $self->port     ? ':' . $self->port     : q{} )
  },
);

has [qw( handle socket )] => ( is => 'rw', init_arg => undef, );

{
  my @buffer;
  sub _parse_block {
    my $self = shift;
    return sub {
      my ($handle, $line) = @_;

      if ($line =~ /\w/) {
        $log->tracef('< %s', $line);
        if ($line =~ /^OK/) {
          if ($line =~ /OK MPD (.*)/) {
            $log->trace('Connection established');
            $self->{version} = $1;

            $self->send( password => $self->password )
              if $self->password and $self->state ne 'ready';

            $self->state( 'ready' );
          }
          else {
            $self->shift_read->( \@buffer );
            @buffer = ();
          }
        }
        elsif ($line =~ /^ACK/) {
          return $self->emit(error => $line );
          @buffer = ();
        }
        else {
          push @buffer, $line;
        }
      }

      $handle->push_read( line => $self->_parse_block );
    };
  }
}

# Set up response parsers for each command
my $parsers = { none => sub { @_ } };
{
  my $item = sub {
    return { map {
      my ($key, $value) = split /: /, $_, 2;
      $key => $value;
    } @{$_[0]} };
  };

  my $flat_list = sub { [ map { (split /: /, $_, 2)[1] } @{$_[0]} ] };

  my $base_list = sub {
    my @main_keys = @{shift()};
    my @list_keys = @{shift()};
    my @lines     = @{shift()};

    my @return;
    my $item = {};

    foreach my $line (@lines) {
      my ($key, $value) = split /: /, $line, 2;

      if ( grep { /$key/ } @main_keys ) {
        push @return, $item if defined $item->{$key};
        $item = { $key => $value };
      }
      elsif ( grep { /$key/ } @list_keys ) {
        unless (defined $item->{$key}) {
          $item->{$key} = []
        }
        push @{$item->{$key}}, $value;
      }
      else {
        $item->{$key} = $value;
      }
    }
    push @return, $item if keys %{$item};

lib/AnyEvent/Net/MPD.pm  view on Meta::CPAN


=head1 SYNOPSIS

  use AnyEvent::Net::MPD;

  my $mpd = AnyEvent::Net::MPD->new( host => $ARGV[0] )->connect;

  my @subsystems = qw( player mixer database );

  # Register a listener
  foreach my $subsystem (@subsystems) {
    $mpd->on( $subsystem => sub {
      my ($self) = @_;
      print "$subsystem has changed\n";

      # Stop listening if mixer changes
      $mpd->noidle if $subsystem eq 'mixer';
    });
  }

  # Send a command
  my $stats = $mpd->send( 'stats' );

  # Or in blocking mode
  my $status = $mpd->send( 'status' )->recv;

  # Which is the same as
  $status = $mpd->get( 'status' );

  print "Server is ", $status->{state}, " state\n";
  print "Server has ", $stats->recv->{albums}, " albums in the database\n";

  # Put the client in looping idle mode
  my $idle = $mpd->idle( @subsystems );

  # Set the emitter in motion, until the next call to noidle
  $idle->recv;

=head1 DESCRIPTION

AnyEvent::Net::MPD provides a non-blocking interface to an MPD server.

=head1 NOTE

Although in what is mostly a usable state, AnyEvent::Net::MPD is currently
B<DEPRECATED>, in favour of L<Net::Async::MPD>. If you want an async interface
to MPD, please consider using that distribution instead.

=head1 ATTRIBUTES

=over 4

=item B<host>

The host to connect to. Defaults to B<localhost>.

=item B<port>

The port to connect to. Defaults to B<6600>.

=item B<password>

The password to use to connect to the server. Defaults to undefined, which
means to use no password.

=item B<auto_connect>

If set to true, the constructor will block until the connection to the MPD
server has been established. Defaults to false.

=back

=head1 METHODS

=over 4

=item B<connect>

If the client is not connected, wait until it is. Otherwise, do nothing.
Returns the client itself;

=item B<send> $cmd

=item B<send> $cmd => @args

=item B<send> [ $cmd1 $cmd2 $cmd3 ]

Send a command to the server in a non-blocking way. This command always returns
an L<AnyEvent> condvar.

If called with a single string, then that string will be sent as the command.

If called with a list, the list will be joined with spaces and sent as the
command.

If called with an array reference, then the value of each of item in that array
will be processed as above (with array references instead of plain lists). If
the referenced array contains more than one command, then these will be sent to
the server as a command list.

An optional subroutine reference passed as the last argument will be passed to
the condvar constructor, and fire when the condvar is ready (= when there is a
response from the server).

The response from the server will be parsed with a command-specific parser, to
provide some structure to the flat lists returned by MPD. If no parser is
found, or if the user specifically asks for no parser to be used (see below),
then the response will be an array reference with the raw lines from the server.

Finally, a hash reference with additional options can be passed as the I<first>
argument. Valid keys to use are:

=over 4

=item B<parser>

Specify the parser to use for the response. Parser labels are MPD commands. If
the requested parser is not found, the fallback C<none> will be used.

Alternatively, if the value itself is a code reference, then that will be
called with a reference to the raw list of lines as its only argument.

=back



( run in 0.501 second using v1.01-cache-2.11-cpan-524268b4103 )