« blib/lib/POE/Component/Client/MPD/Connection.pm
↑ Summary
blib/lib/POE/Component/Client/MPD/Commands.pm »
| 1 | # | ||||||||||||||||||||||||
| 2 | # This file is part of POE-Component-Client-MPD | ||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2007 by Jerome Quelin. | ||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||
| 9 | 15 | ▶ | use 5.010; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | 15 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 11 | 15 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||
| 13 | package POE::Component::Client::MPD; | ||||||||||||||||||||||||
| 14 | # ABSTRACT: full-blown poe-aware mpd client library | ||||||||||||||||||||||||
| 15 | 15 | $POE::Component::Client::MPD::VERSION = '2.000'; | |||||||||||||||||||||||
| 16 | 15 | ▶ | use Audio::MPD::Common::Stats; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | 15 | ▶ | use Audio::MPD::Common::Status; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 18 | 15 | ▶ | use Carp; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 19 | 15 | ▶ | use List::AllUtils qw{ any }; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 20 | 15 | ▶ | use Moose; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 21 | 15 | ▶ | use MooseX::Has::Sugar; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 22 | 15 | ▶ | use MooseX::POE; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 23 | 15 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 24 | 15 | ▶ | use MooseX::Types::Moose qw{ Int Str }; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 25 | 15 | ▶ | use POE; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 26 | 15 | ▶ | use Readonly; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||
| 28 | 15 | ▶ | use POE::Component::Client::MPD::Commands; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 29 | 15 | ▶ | use POE::Component::Client::MPD::Collection; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 30 | 15 | ▶ | use POE::Component::Client::MPD::Connection; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 31 | 15 | ▶ | use POE::Component::Client::MPD::Message; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 32 | 15 | ▶ | use POE::Component::Client::MPD::Playlist; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||
| 34 | 15 | Readonly my $K => $poe_kernel; | |||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||
| 37 | # -- attributes | ||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | 15 | has host => ( ro, lazy_build, isa=>Str ); | |||||||||||||||||||||||
| 41 | 15 | has password => ( ro, lazy_build ); | |||||||||||||||||||||||
| 42 | 15 | has port => ( ro, lazy_build, isa=>Int ); | |||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||
| 44 | 15 | has alias => ( ro, isa=>Str, default=>'mpd' ); | |||||||||||||||||||||||
| 45 | 15 | has status_msgs_to => ( ro, isa=>Str, predicate=>'has_peer' ); | |||||||||||||||||||||||
| 46 | 15 | has version => ( rw, isa=>Str ); | |||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||
| 48 | 15 | has _collection => ( ro, lazy_build, isa=>'POE::Component::Client::MPD::Collection' ); | |||||||||||||||||||||||
| 49 | 15 | has _commands => ( ro, lazy_build, isa=>'POE::Component::Client::MPD::Commands' ); | |||||||||||||||||||||||
| 50 | 15 | has _playlist => ( ro, lazy_build, isa=>'POE::Component::Client::MPD::Playlist' ); | |||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 15 | has _socket => ( rw, isa=>Str ); | |||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||
| 56 | # -- builder & initializers | ||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||
| 59 | # my ($passwd, $host, $port) = _parse_env_var(); | ||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||
| 61 | # parse MPD_HOST environment variable, and extract its components. the | ||||||||||||||||||||||||
| 62 | # canonical format of MPD_HOST is passwd@host:port. | ||||||||||||||||||||||||
| 63 | # | ||||||||||||||||||||||||
| 64 | sub _parse_env_var { | ||||||||||||||||||||||||
| 65 | 0 | ▶ | return (undef, undef, undef) unless defined $ENV{MPD_HOST}; | ||||||||||||||||||||||
|
Subroutine
_parse_env_var: not called CC=5 SCAR=34.0
Branch: 1 branch
| |||||||||||||||||||||||||
| 66 | 0 | ▶ | return ($1, $2, $3) if $ENV{MPD_HOST} =~ /^([^@]+)\@([^:@]+):(\d+)$/; # passwd@host:port | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 67 | 0 | ▶ | return ($1, $2, undef) if $ENV{MPD_HOST} =~ /^([^@]+)\@([^:@]+)$/; # passwd@host | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 68 | 0 | ▶ | return (undef, $1, $2) if $ENV{MPD_HOST} =~ /^([^:@]+):(\d+)$/; # host:port | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 69 | 0 | return (undef, $ENV{MPD_HOST}, undef); | |||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||
| 72 | 0 | ▶ | sub _build_host { return ( _parse_env_var() )[1] || 'localhost'; } | ||||||||||||||||||||||
|
Subroutine
_build_host: not called CC=2 SCAR=17.9
Condition0%0/2
(_parse_env_var())[1] || 'localhost'
MC/DC0%0/1
(_parse_env_var())[1] || 'localhost'
(_parse_env_var())[1]
Truth table0%0/2
A
A= (_parse_env_var())[1]
| |||||||||||||||||||||||||
| 73 | 0 | ▶ | sub _build_port { return $ENV{MPD_PORT} || ( _parse_env_var() )[2] || 6600; } | ||||||||||||||||||||||
|
Subroutine
_build_port: not called CC=3 SCAR=24.8
Condition0%0/5
$ENV{'MPD_PORT'} || (_parse_env_var())[2] || 6600
$ENV{"MPD_PORT"} or (_parse_env_var())[2]
MC/DC0%0/2
$ENV{'MPD_PORT'} || (_parse_env_var())[2] || 6600
$ENV{"MPD_PORT"}
(_parse_env_var())[2]
Truth table0%0/3
A or B
A= $ENV{"MPD_PORT"}B= (_parse_env_var())[2]
| |||||||||||||||||||||||||
| 74 | 0 | ▶ | sub _build_password { return $ENV{MPD_PASSWORD} || ( _parse_env_var() )[0] || ''; } | ||||||||||||||||||||||
|
Subroutine
_build_password: not called CC=3 SCAR=24.8
Condition0%0/5
$ENV{'MPD_PASSWORD'} || (_parse_env_var())[0] || ''
$ENV{"MPD_PASSWORD"} or (_parse_env_var())[0]
MC/DC0%0/2
$ENV{'MPD_PASSWORD'} || (_parse_env_var())[0] || ''
$ENV{"MPD_PASSWORD"}
(_parse_env_var())[0]
Truth table0%0/3
A or B
A= $ENV{"MPD_PASSWORD"}B= (_parse_env_var())[0]
| |||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | 0 | ▶ | sub _build__collection { POE::Component::Client::MPD::Collection->new(mpd=>$_[0]); } | ||||||||||||||||||||||
|
Subroutine
_build__collection: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 77 | 0 | ▶ | sub _build__commands { POE::Component::Client::MPD::Commands ->new(mpd=>$_[0]); } | ||||||||||||||||||||||
|
Subroutine
_build__commands: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 78 | 0 | ▶ | sub _build__playlist { POE::Component::Client::MPD::Playlist ->new(mpd=>$_[0]); } | ||||||||||||||||||||||
|
Subroutine
_build__playlist: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | # -- public methods | ||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||
| 84 | sub spawn { | ||||||||||||||||||||||||
| 85 | 0 | ▶ | my $self = shift->new(@_); | ||||||||||||||||||||||
|
Subroutine
spawn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 86 | 0 | return $self->{session_id}; | |||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||
| 91 | # -- private methods | ||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||
| 93 | sub _dispatch { | ||||||||||||||||||||||||
| 94 | 0 | ▶ | my ($self, $event, $msg) = @_; | ||||||||||||||||||||||
|
Subroutine
_dispatch: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | # dispatch the event. | ||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $event =~ /^pl\.(.*)$/ ) { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 98 | # playlist commands | ||||||||||||||||||||||||
| 99 | 0 | my $meth = "_do_$1"; | |||||||||||||||||||||||
| 100 | 0 | $self->_playlist->$meth($msg); | |||||||||||||||||||||||
| 101 | } elsif ( $event =~ /^coll\.(.*)$/ ) { | ||||||||||||||||||||||||
| 102 | # collection commands | ||||||||||||||||||||||||
| 103 | 0 | my $meth = "_do_$1"; | |||||||||||||||||||||||
| 104 | 0 | $self->_collection->$meth($msg); | |||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||
| 106 | # basic commands | ||||||||||||||||||||||||
| 107 | 0 | my $meth = "_do_$event"; | |||||||||||||||||||||||
| 108 | 0 | $self->_commands->$meth($msg); | |||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||
| 114 | # $mpd->_send_to_mpd( $msg ); | ||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||
| 116 | # send $msg to mpd using pococm. | ||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||
| 118 | sub _send_to_mpd { | ||||||||||||||||||||||||
| 119 | 0 | ▶ | my ($self, $msg) = @_; | ||||||||||||||||||||||
|
Subroutine
_send_to_mpd: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 120 | 0 | $K->post( $self->_socket => send => $msg ); | |||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||
| 126 | # -- public events. | ||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||
| 129 | # | ||||||||||||||||||||||||
| 130 | # catch-all handler for pococm events that drive mpd. | ||||||||||||||||||||||||
| 131 | # | ||||||||||||||||||||||||
| 132 | event _default => sub { | ||||||||||||||||||||||||
| 133 | 0 | ▶ | my ($self, $event, $params) = @_[OBJECT, ARG0, ARG1]; | ||||||||||||||||||||||
|
Subroutine
_default: not called CC=2 SCAR=17.9
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||
| 135 | # check if event is handled. | ||||||||||||||||||||||||
| 136 | 0 | my @events_commands = qw{ | |||||||||||||||||||||||
| 137 | password version kill updatedb urlhandlers | ||||||||||||||||||||||||
| 138 | volume output_enable output_disable | ||||||||||||||||||||||||
| 139 | stats status current song songid | ||||||||||||||||||||||||
| 140 | repeat fade random | ||||||||||||||||||||||||
| 141 | play playid pause stop next prev seek seekid | ||||||||||||||||||||||||
| 142 | }; | ||||||||||||||||||||||||
| 143 | 0 | my @events_playlist = qw{ | |||||||||||||||||||||||
| 144 | pl.as_items pl.items_changed_since | ||||||||||||||||||||||||
| 145 | pl.add pl.delete pl.deleteid pl.clear pl.crop | ||||||||||||||||||||||||
| 146 | pl.shuffle pl.swap pl.swapid pl.move pl.moveid | ||||||||||||||||||||||||
| 147 | pl.load pl.save pl.rm | ||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||
| 149 | 0 | my @events_collection = qw{ | |||||||||||||||||||||||
| 150 | coll.all_items coll.all_items_simple coll.items_in_dir | ||||||||||||||||||||||||
| 151 | coll.all_albums coll.all_artists coll.all_titles coll.all_files | ||||||||||||||||||||||||
| 152 | coll.song coll.songs_with_filename_partial | ||||||||||||||||||||||||
| 153 | coll.albums_by_artist coll.songs_by_artist coll.songs_by_artist_partial | ||||||||||||||||||||||||
| 154 | coll.songs_from_album coll.songs_from_album_partial | ||||||||||||||||||||||||
| 155 | coll.songs_with_title coll.songs_with_title_partial | ||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||
| 157 | 0 | my @ok_events = ( @events_commands, @events_playlist, @events_collection ); | |||||||||||||||||||||||
| 158 | 0 | ▶ | return unless any { $event eq $_ } @ok_events; | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 2 branches
| |||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||
| 160 | # create the message that will hold | ||||||||||||||||||||||||
| 161 | 0 | my $msg = POE::Component::Client::MPD::Message->new( { | |||||||||||||||||||||||
| 162 | _from => $_[SENDER]->ID, | ||||||||||||||||||||||||
| 163 | request => $event, # /!\ $_[STATE] eq 'default' | ||||||||||||||||||||||||
| 164 | params => $params, | ||||||||||||||||||||||||
| 165 | #_commands => <to be set by handler> | ||||||||||||||||||||||||
| 166 | #_cooking => <to be set by handler> | ||||||||||||||||||||||||
| 167 | #_transform => <to be set by handler> | ||||||||||||||||||||||||
| 168 | #_post => <to be set by handler> | ||||||||||||||||||||||||
| 169 | } ); | ||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||
| 171 | # dispatch the event so it is handled by the correct object/method. | ||||||||||||||||||||||||
| 172 | 0 | $self->_dispatch($event, $msg); | |||||||||||||||||||||||
| 173 | 15 | }; | |||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||
| 178 | event disconnect => sub { | ||||||||||||||||||||||||
| 179 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||
|
Subroutine
disconnect: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 180 | 0 | $K->alias_remove( $self->alias ); # refcount-- | |||||||||||||||||||||||
| 181 | 0 | $K->post( $self->_socket, 'disconnect' ); # pococm-conn | |||||||||||||||||||||||
| 182 | 15 | }; | |||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||
| 185 | # -- protected events fired by pococm-conn | ||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||
| 187 | # | ||||||||||||||||||||||||
| 188 | # event: mpd_connect_error_retriable( $reason ) | ||||||||||||||||||||||||
| 189 | # event: mpd_connect_error_fatal( $reason ) | ||||||||||||||||||||||||
| 190 | 15 | event mpd_connect_error_retriable => \&_mpd_connect_error; | |||||||||||||||||||||||
| 191 | 15 | event mpd_connect_error_fatal => \&_mpd_connect_error; | |||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||
| 193 | # Called when pococm-conn could not connect to a mpd server. It can be | ||||||||||||||||||||||||
| 194 | # either retriable, or fatal. In bth case, we just need to forward the | ||||||||||||||||||||||||
| 195 | # error to our peer session. | ||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||
| 197 | sub _mpd_connect_error { | ||||||||||||||||||||||||
| 198 | 0 | ▶ | my ($self, $reason) = @_[OBJECT, ARG0]; | ||||||||||||||||||||||
|
Subroutine
_mpd_connect_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||
| 200 | 0 | ▶ | return unless $self->has_peer; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 201 | 0 | $K->post($self->status_msgs_to, 'mpd_connect_error', $reason); | |||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||
| 205 | # | ||||||||||||||||||||||||
| 206 | # event: mpd_connected( $version ) | ||||||||||||||||||||||||
| 207 | # | ||||||||||||||||||||||||
| 208 | # Called when pococm-conn made sure we're talking to a mpd server. | ||||||||||||||||||||||||
| 209 | # | ||||||||||||||||||||||||
| 210 | event mpd_connected => sub { | ||||||||||||||||||||||||
| 211 | 0 | ▶ | my ($self, $version) = @_[OBJECT, ARG0]; | ||||||||||||||||||||||
|
Subroutine
mpd_connected: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||
| 212 | 0 | $self->set_version( $version ); | |||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||
| 214 | 0 | ▶ | return unless $self->has_peer; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 215 | 0 | $K->post($self->status_msgs_to, 'mpd_connected'); | |||||||||||||||||||||||
| 216 | 0 | ▶ | $K->yield(password => $self->password) if $self->password; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 217 | # FIXME: send status information to peer | ||||||||||||||||||||||||
| 218 | 15 | }; | |||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||
| 223 | # event: mpd_disconnected() | ||||||||||||||||||||||||
| 224 | # | ||||||||||||||||||||||||
| 225 | # Called when pococm-conn got disconnected by mpd. | ||||||||||||||||||||||||
| 226 | # | ||||||||||||||||||||||||
| 227 | event mpd_disconnected => sub { | ||||||||||||||||||||||||
| 228 | 0 | ▶ | my ($self, $version) = @_[OBJECT, ARG0]; | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 229 | 0 | ▶ | return unless $self->has_peer; | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 230 | 0 | $K->post($self->status_msgs_to, 'mpd_disconnected'); | |||||||||||||||||||||||
| 231 | 15 | }; | |||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||
| 235 | # | ||||||||||||||||||||||||
| 236 | # Event: mpd_data( $msg ) | ||||||||||||||||||||||||
| 237 | # | ||||||||||||||||||||||||
| 238 | # Received when mpd finished to send back some data. | ||||||||||||||||||||||||
| 239 | # | ||||||||||||||||||||||||
| 240 | event mpd_data => sub { | ||||||||||||||||||||||||
| 241 | 0 | ▶ | my ($self, $msg) = @_[OBJECT, ARG0]; | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||
| 243 | # transform data if needed. | ||||||||||||||||||||||||
| 244 | 0 | ▶ | if ( defined $msg->_transform ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 245 | 0 | ▶ | if ( $msg->_transform eq "as_scalar" ) { | ||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||
| 246 | 0 | my $data = $msg->_data->[0]; | |||||||||||||||||||||||
| 247 | 0 | $msg->_set_data($data); | |||||||||||||||||||||||
| 248 | } elsif ( $msg->_transform eq "as_stats" ) { | ||||||||||||||||||||||||
| 249 | 0 | my %stats = @{ $msg->_data }; | |||||||||||||||||||||||
| 250 | 0 | my $stats = Audio::MPD::Common::Stats->new( \%stats ); | |||||||||||||||||||||||
| 251 | 0 | $msg->_set_data($stats); | |||||||||||||||||||||||
| 252 | } elsif ( $msg->_transform eq "as_status" ) { | ||||||||||||||||||||||||
| 253 | 0 | my %status = @{ $msg->_data }; | |||||||||||||||||||||||
| 254 | 0 | my $status = Audio::MPD::Common::Status->new( \%status ); | |||||||||||||||||||||||
| 255 | 0 | $msg->_set_data($status); | |||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||
| 260 | # check for post-callback. | ||||||||||||||||||||||||
| 261 | 0 | ▶ | if ( defined $msg->_post ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 262 | 0 | my $event = $msg->_post; # save postback. | |||||||||||||||||||||||
| 263 | 0 | $msg->_set_post( undef ); # remove postback. | |||||||||||||||||||||||
| 264 | 0 | $self->_dispatch($event, $msg); | |||||||||||||||||||||||
| 265 | 0 | return; | |||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||
| 268 | # send result. | ||||||||||||||||||||||||
| 269 | 0 | $K->post($msg->_from, 'mpd_result', $msg, $msg->_data); | |||||||||||||||||||||||
| 270 | 15 | }; | |||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||
| 273 | # | ||||||||||||||||||||||||
| 274 | # Event: mpd_error( $msg, $errstr ) | ||||||||||||||||||||||||
| 275 | # | ||||||||||||||||||||||||
| 276 | # Received when mpd didn't understood a command. | ||||||||||||||||||||||||
| 277 | # | ||||||||||||||||||||||||
| 278 | event mpd_error => sub { | ||||||||||||||||||||||||
| 279 | 0 | ▶ | my ($msg, $errstr) = @_[ARG0, ARG1]; | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||
| 281 | 0 | $msg->set_status(0); # failure | |||||||||||||||||||||||
| 282 | 0 | $K->post( $msg->_from, 'mpd_error', $msg, $errstr ); | |||||||||||||||||||||||
| 283 | 15 | }; | |||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||
| 287 | # -- private events | ||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||
| 289 | # | ||||||||||||||||||||||||
| 290 | # Event: _start( \%params ) | ||||||||||||||||||||||||
| 291 | # | ||||||||||||||||||||||||
| 292 | # Called when the poe session gets initialized. Receive a reference | ||||||||||||||||||||||||
| 293 | # to %params, same as spawn() received. | ||||||||||||||||||||||||
| 294 | # | ||||||||||||||||||||||||
| 295 | sub START { | ||||||||||||||||||||||||
| 296 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||
|
Subroutine
START: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 297 | 0 | $K->alias_set( $self->alias ); # refcount++ | |||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||
| 299 | # create the connection to mpd. we *cannot* do this with a | ||||||||||||||||||||||||
| 300 | # lazy_build, otherwise the connection will be started too late... | ||||||||||||||||||||||||
| 301 | 0 | my $socket = POE::Component::Client::MPD::Connection->spawn( { | |||||||||||||||||||||||
| 302 | host => $self->host, | ||||||||||||||||||||||||
| 303 | port => $self->port, | ||||||||||||||||||||||||
| 304 | password => $self->password, | ||||||||||||||||||||||||
| 305 | id => $self->alias, | ||||||||||||||||||||||||
| 306 | } ); | ||||||||||||||||||||||||
| 307 | 0 | $self->_set_socket( $socket ); | |||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||
| 311 | 15 | ▶ | no Moose; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 312 | 15 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||
| 313 | 15 | 1; | |||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||
| 315 | __END__ | ||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||
| 317 | =pod | ||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||
| 319 | =head1 NAME | ||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||
| 321 | POE::Component::Client::MPD - full-blown poe-aware mpd client library | ||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||
| 323 | =head1 VERSION | ||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||
| 325 | version 2.000 | ||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||
| 327 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||
| 329 | use POE qw{ Component::Client::MPD }; | ||||||||||||||||||||||||
| 330 | POE::Component::Client::MPD->spawn( { | ||||||||||||||||||||||||
| 331 | host => 'localhost', | ||||||||||||||||||||||||
| 332 | port => 6600, | ||||||||||||||||||||||||
| 333 | password => 's3kr3t', # mpd password | ||||||||||||||||||||||||
| 334 | alias => 'mpd', # poe alias | ||||||||||||||||||||||||
| 335 | status_msgs_to => 'myapp', # session to send status info to | ||||||||||||||||||||||||
| 336 | } ); | ||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||
| 338 | # ... later on ... | ||||||||||||||||||||||||
| 339 | $_[KERNEL]->post( mpd => 'next' ); | ||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||
| 341 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||
| 343 | POCOCM gives a clear message-passing interface (sitting on top of POE) | ||||||||||||||||||||||||
| 344 | for talking to and controlling MPD (Music Player Daemon) servers. A | ||||||||||||||||||||||||
| 345 | connection to the MPD server is established as soon as a new POCOCM | ||||||||||||||||||||||||
| 346 | object is created. | ||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||
| 348 | Commands are then sent to the server as messages are passed. | ||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||
| 350 | =head1 ATTRIBUTES | ||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||
| 352 | =head2 host | ||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||
| 354 | The hostname where MPD is running. Defaults to environment var | ||||||||||||||||||||||||
| 355 | C<MPD_HOST>, then to 'localhost'. Note that C<MPD_HOST> can be of | ||||||||||||||||||||||||
| 356 | the form C<password@host:port> (each of C<password@> or C<:port> can | ||||||||||||||||||||||||
| 357 | be omitted). | ||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||
| 359 | =head2 port | ||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||
| 361 | The port that MPD server listens to. Defaults to environment var | ||||||||||||||||||||||||
| 362 | C<MPD_PORT>, then to parsed C<MPD_HOST> (cf above), then to 6600. | ||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||
| 364 | =head2 password | ||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||
| 366 | The password to access special MPD functions. Defaults to environment | ||||||||||||||||||||||||
| 367 | var C<MPD_PASSWORD>, then to parsed C<MPD_HOST> (cf above), then to | ||||||||||||||||||||||||
| 368 | empty string. | ||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||
| 370 | =head2 alias | ||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||
| 372 | A string to alias the newly created POE session. Defaults to C<mpd>. | ||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||
| 374 | =head2 status_msgs_to | ||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||
| 376 | A session (name or id) to whom to send connection status to. Optional, | ||||||||||||||||||||||||
| 377 | although recommended. No default. When this is done, pococm will send | ||||||||||||||||||||||||
| 378 | *additional* events to the session, such as: C<mpd_connected> when | ||||||||||||||||||||||||
| 379 | pococm is connected, C<mpd_disconnected> when pococm is disconnected, | ||||||||||||||||||||||||
| 380 | etc. You thus need to register some handlers for those events. | ||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||
| 382 | =head1 METHODS | ||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||
| 384 | =head2 my $id = POE::Component::Client::MPD->spawn( \%params ); | ||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||
| 386 | This method will create a POE session responsible for communicating with | ||||||||||||||||||||||||
| 387 | mpd. It will return the poe id of the session newly created. You can | ||||||||||||||||||||||||
| 388 | tune it by passing some arguments as a hash reference. See the | ||||||||||||||||||||||||
| 389 | attributes for allowed values. | ||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||
| 391 | =head1 PUBLIC EVENTS ACCEPTED | ||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||
| 393 | =head2 MPD-related events | ||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||
| 395 | The goal of a POCOCM session is to drive a remote MPD server. This can | ||||||||||||||||||||||||
| 396 | be achieved by a lot of events. Due to their sheer number, they have | ||||||||||||||||||||||||
| 397 | been regrouped logically in modules. | ||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||
| 399 | However, note that to use those events, you need to send them to the | ||||||||||||||||||||||||
| 400 | POCOCM session that you created with C<spawn()> (see above). Indeed, the | ||||||||||||||||||||||||
| 401 | logical split is only internal: you are to use the same peer. | ||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||
| 403 | For a list of public events that update and/or query MPD, see embedded | ||||||||||||||||||||||||
| 404 | pod in: | ||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||
| 406 | =over 4 | ||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||
| 408 | =item * L<POE::Component::Client::MPD::Commands> for general commands | ||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||
| 410 | =item * L<POE::Component::Client::MPD::Playlist> for playlist-related | ||||||||||||||||||||||||
| 411 | commands. Those events begin with C<pl.>. | ||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||
| 413 | =item * L<POE::Component::Client::MPD::Collection> for collection- | ||||||||||||||||||||||||
| 414 | related commands. Those events begin with C<coll.>. | ||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||
| 416 | =back | ||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||
| 418 | =head2 disconnect( ) | ||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||
| 420 | Request the POCOCM to be shutdown. Leave mpd running. Generally sent | ||||||||||||||||||||||||
| 421 | when one wants to exit her program. | ||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||
| 423 | =for Pod::Coverage::TrustPod START | ||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||
| 425 | =head1 EVENTS FIRED | ||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||
| 427 | A POCOCM session will fire events, either to answer an incoming event, | ||||||||||||||||||||||||
| 428 | or to inform about some changes regarding the remote MPD server. | ||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||
| 430 | =head2 Answer events | ||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||
| 432 | For each incoming event received by the POCOCM session, it will fire | ||||||||||||||||||||||||
| 433 | back one of the following answers: | ||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||
| 435 | =over 4 | ||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||
| 437 | =item * mpd_result( $msg, $answer ) | ||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||
| 439 | Indicates a success. C<$msg> is a | ||||||||||||||||||||||||
| 440 | L<POE::Component::Client::MPD::Message> object with the original | ||||||||||||||||||||||||
| 441 | request, to identify the issued command (see | ||||||||||||||||||||||||
| 442 | L<POE::Component::Client::MPD::Message> pod for more information). Its | ||||||||||||||||||||||||
| 443 | C<status()> attribute is true, further confirming success. | ||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||
| 445 | C<$answer> is what has been answered by the MPD server. Depending on the | ||||||||||||||||||||||||
| 446 | command, it can be either: | ||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||
| 448 | =over 4 | ||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||
| 450 | =item * C<undef>: commands C<play>, etc. | ||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||
| 452 | =item * an L<Audio::MPD::Common::Stats> object: command C<stats> | ||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||
| 454 | =item * an L<Audio::MPD::Common::Status> object: command C<status> | ||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||
| 456 | =item * an L<Audio::MPD::Common::Item> object: commands C<song>, etc. | ||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||
| 458 | =item * an array reference: commands C<coll.files>, etc. | ||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||
| 460 | =item * etc. | ||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||
| 462 | =back | ||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||
| 464 | Refer to the documentation of each event to know what type of answer you | ||||||||||||||||||||||||
| 465 | can expect. | ||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||
| 467 | =item * mpd_error( $msg, $errstr ) | ||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||
| 469 | Indicates a failure. C<$msg> is a | ||||||||||||||||||||||||
| 470 | L<POE::Component::Client::MPD::Message> object with the original | ||||||||||||||||||||||||
| 471 | request, to identify the issued command (see | ||||||||||||||||||||||||
| 472 | L<POE::Component::Client::MPD::Message> pod for more information). Its | ||||||||||||||||||||||||
| 473 | C<status()> attribute is false, further confirming failure. | ||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||
| 475 | C<$errstr> is what the error message as returned been answered by the | ||||||||||||||||||||||||
| 476 | MPD server. | ||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||
| 478 | =back | ||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||
| 480 | =head2 Auto-generated events | ||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||
| 482 | If you supplied the C<status_msgs_to> attribute, the following events | ||||||||||||||||||||||||
| 483 | are fired to this peer by pococm: | ||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||
| 485 | =over 4 | ||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||
| 487 | =item * mpd_connect_error( $reason ) | ||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||
| 489 | Called when pococm-conn could not connect to a mpd server. It can be | ||||||||||||||||||||||||
| 490 | either retriable, or fatal. Check C<$reason> for more information. | ||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||
| 492 | =item * mpd_connected( ) | ||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||
| 494 | Called when pococm-conn made sure we're talking to a mpd server. | ||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||
| 496 | =item * mpd_disconnected( ) | ||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||
| 498 | Called when pococm-conn has been disconnected from mpd server. | ||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||
| 500 | =back | ||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||
| 502 | =head1 SEE ALSO | ||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||
| 504 | You can find more information on the mpd project on its homepage at | ||||||||||||||||||||||||
| 505 | L<http://www.musicpd.org>, or its wiki L<http://mpd.wikia.com>. You may | ||||||||||||||||||||||||
| 506 | want to have a look at L<Audio::MPD>, a non-L<POE> aware module to | ||||||||||||||||||||||||
| 507 | access MPD. | ||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||
| 509 | You can look for information on this module at: | ||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||
| 511 | =over 4 | ||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||
| 513 | =item * Search CPAN | ||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||
| 515 | L<http://search.cpan.org/dist/POE-Component-Client-MPD> | ||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||
| 517 | =item * See open / report bugs | ||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||
| 519 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Component-Client-MPD> | ||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||
| 521 | =item * Mailing-list (same as L<Audio::MPD>) | ||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||
| 523 | L<http://groups.google.com/group/audio-mpd> | ||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||
| 525 | =item * Git repository | ||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||
| 527 | L<http://github.com/jquelin/poe-component-client-mpd> | ||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||
| 529 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||
| 531 | L<http://annocpan.org/dist/POE-Component-Client-MPD> | ||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||
| 533 | =item * CPAN Ratings | ||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||
| 535 | L<http://cpanratings.perl.org/d/POE-Component-Client-MPD> | ||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||
| 537 | =back | ||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||
| 539 | =head1 AUTHOR | ||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||
| 541 | Jerome Quelin | ||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||
| 543 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||
| 545 | This software is copyright (c) 2007 by Jerome Quelin. | ||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||
| 547 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||
| 548 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||
| 550 | =cut | ||||||||||||||||||||||||