/usr/share/perl5/POE/Component/Client/MPD/Connection.pm is in libpoe-component-client-mpd-perl 1.100430-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | #
# This file is part of POE-Component-Client-MPD
#
# This software is copyright (c) 2007 by Jerome Quelin.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#
use 5.010;
use strict;
use warnings;
package POE::Component::Client::MPD::Connection;
our $VERSION = '1.100430';
# ABSTRACT: module handling the tcp connection with mpd
use Audio::MPD::Common::Item;
use POE;
use POE::Component::Client::TCP;
use Readonly;
use POE::Component::Client::MPD::Message; # for exported constants
# -- attributes
# -- public methods
sub spawn {
my ($type, $args) = @_;
# connect to mpd server.
my $id = POE::Component::Client::TCP->new(
RemoteAddress => $args->{host},
RemotePort => $args->{port},
Filter => 'POE::Filter::Line',
Args => [ $args ],
Alias => '_mpd_conn',
ServerError => sub { }, # quiet errors
Started => \&_Started,
Connected => \&_Connected,
ConnectError => \&_ConnectError,
Disconnected => \&_Disconnected,
ServerInput => \&_ServerInput,
InlineStates => {
send => \&send, # send data
disconnect => \&disconnect, # force quit
},
);
return $id;
}
# -- public events
sub disconnect {
$_[HEAP]->{auto_reconnect} = 0; # no more auto-reconnect.
$_[KERNEL]->yield( 'shutdown' ); # shutdown socket.
}
sub send {
my ($k, $h, $msg) = @_[KERNEL, HEAP, ARG0];
# Test to see if we're currently connected to MPD...
if ($h->{connected}) {
# ... if we are, it's all good, so send messages ...
$h->{server}->put( @{ $msg->_commands } );
push @{ $h->{fifo} }, $msg;
} elsif ($h->{auto_reconnect} == 1) {
# ... and if not, retry the send in 2 seconds.
$k->delay_set(send => 2, $msg);
}
}
# -- private events
#
# event: Started($id)
#
# Called whenever the session is started, but before the tcp connection is
# established. Receives the session $id of the poe-session that will be our
# peer during the life of this session.
#
sub _Started {
my ($h, $args) = @_[HEAP, ARG0];
# storing params
$h->{session} = $args->{id}; # poe-session peer
$h->{max_retries} = $args->{max_retries} // 5; # max retries before giving up
$h->{retry_wait} = $args->{retry_wait} // 2; # sleep time before retry
# setting session vars
$h->{auto_reconnect} = 1; # on-disconnect policy
$h->{retries_left} = $h->{max_retries}; # how much chances is there still?
}
#
# event: Connected()
#
# Called whenever the tcp connection is established.
#
sub _Connected {
my $h = $_[HEAP];
$h->{fifo} = []; # reset current messages
$h->{incoming} = []; # reset incoming data
$h->{is_mpd} = 0; # is remote server a mpd sever?
$h->{retries_left} = $h->{max_retries}; # reset connection retries count
}
#
# event: ConnectError($syscall, $errno, $errstr)
#
# Called whenever the tcp connection fails to be established. Generally
# due to mpd server not started, or wrong host / port, etc. Receives
# the $syscall that failed, as well as $errno and $errstr.
#
sub _ConnectError {
my ($k, $h, $syscall, $errno, $errstr) = @_[KERNEL, HEAP, ARG0, ARG1, ARG2];
return unless $h->{auto_reconnect};
# check if this is the last allowed error.
$h->{retries_left}--;
my ($event, $msg);
if ( $h->{retries_left} > 0 ) {
# nope, we can reconnect
$event = 'mpd_connect_error_retriable';
$msg = '';
# auto-reconnect in $retry_wait seconds
$k->delay_add('reconnect' => $h->{retry_wait});
} else {
# yup, it was our last chance.
$event = 'mpd_connect_error_fatal';
$msg = 'Too many failed attempts! error was: ';
}
# signal that there was a problem during connection
my $error = $msg . "$syscall: ($errno) $errstr";
$k->post( $h->{session}, $event, $error );
}
#
# event: Disconnected()
#
# Called whenever the tcp connection is broken / finished.
#
sub _Disconnected {
my ($k, $h) = @_[KERNEL, HEAP];
# signal that we're disconnected
$k->post($h->{session}, 'mpd_disconnected');
# auto-reconnect in $retry_wait seconds
return unless $h->{auto_reconnect};
$k->delay_add('reconnect' => $h->{retry_wait});
}
#
# event: ServerInput($input)
#
# Called whenever the tcp peer sends data over the wires, with the $input
# transmitted given as param.
#
sub _ServerInput {
my ($k, $h, $input) = @_[KERNEL, HEAP, ARG0];
# did we check we were talking to a mpd server?
if ( not $h->{is_mpd} ) {
_got_first_input_line($k, $h, $input);
return;
}
# table of dispatch: check input against regex, and process it.
given ($input) {
when ( /^OK$/ ) { _got_data_eot($k, $h); }
when ( /^ACK (.*)/ ) { _got_error($k, $h, $1); }
default { _got_data($k, $h, $input); }
}
}
# -- private subs
#
# _got_data($kernel, $heap, $input);
#
# called when receiving another piece of data.
#
sub _got_data {
my ($k, $h, $input) = @_;
# regular data, to be cooked (if needed) and stored.
my $msg = $h->{fifo}[0];
given ($msg->_cooking) {
when ('raw') {
# nothing to do, just push the data.
push @{ $h->{incoming} }, $input;
}
when ('as_items') {
# Lots of POCOCM methods are sending commands and then parse the
# output to build an amc-item.
my ($k,$v) = split /:\s+/, $input, 2;
$k = lc $k;
$k =~ s/-/_/;
if ( $k eq 'file' || $k eq 'directory' || $k eq 'playlist' ) {
# build a new amc-item
my $item = Audio::MPD::Common::Item->new( $k => $v );
push @{ $h->{incoming} }, $item;
}
# just complete the current amc-item
$h->{incoming}[-1]->$k($v);
}
when ('as_kv') {
# Lots of POCOCM methods are sending commands and then parse the
# output to get a list of key / value (with the colon ":" acting
# as separator).
my @data = split(/:\s+/, $input, 2);
push @{ $h->{incoming} }, @data;
}
when ('strip_first') {
# Lots of POCOCM methods are sending commands and then parse the
# output to remove the first field (with the colon ":" acting as
# separator).
$input = ( split(/:\s+/, $input, 2) )[1];
push @{ $h->{incoming} }, $input;
}
}
}
#
# _got_data_eot($kernel, $heap)
#
# called when the stream of data is finished. used to send the received
# data.
#
sub _got_data_eot {
my ($k, $h) = @_;
my $session = $h->{session};
my $msg = shift @{ $h->{fifo} }; # remove completed msg
$msg->_set_data($h->{incoming}); # complete message with data
$msg->set_status(1); # success
$k->post($session, 'mpd_data', $msg); # signal poe session
$h->{incoming} = []; # reset incoming data
}
#
# _got_error($kernel, $heap, $errstr);
#
# called when the mpd server reports an error. used to report the error
# to the pococm.
#
sub _got_error {
my ($k, $h, $errstr) = @_;
my $session = $h->{session};
my $msg = shift @{ $h->{fifo} };
$k->post($session, 'mpd_error', $msg, $errstr);
}
#
# _got_first_input_line($kernel, $heap, $input);
#
# called when the mpd server fires the first line. used to check whether
# we are talking to a regular mpd server.
#
sub _got_first_input_line {
my ($k, $h, $input) = @_;
if ( $input =~ /^OK MPD (.*)$/ ) {
$h->{is_mpd} = 1; # remote server *is* a mpd sever
$k->post($h->{session}, 'mpd_connected', $1);
} else {
# oops, it appears that it's not a mpd server...
$k->post(
$h->{session}, 'mpd_connect_error_fatal',
"Not a mpd server - welcome string was: '$input'",
);
}
}
1;
=pod
=head1 NAME
POE::Component::Client::MPD::Connection - module handling the tcp connection with mpd
=head1 VERSION
version 1.100430
=head1 DESCRIPTION
This module will spawn a poe session responsible for low-level
communication with mpd. It is written as a
L<POE::Component::Client::TCP>, which is taking care of
everything needed.
Note that you're B<not> supposed to use this class directly: it's one of
the helper class for L<POE::Component::Client::MPD>.
=head1 ATTRIBUTES
=head2 host
The hostname of the mpd server. Mandatory, no default.
=head2 port
The port of the mpd server. Mandatory, no default.
=head2 id
The POE session id of the peer to dialog with. Mandatory, no default.
=head2 max_retries
How much time to attempt reconnection before giving up. Defaults to 5.
=head2 retry_wait
How much time to wait (in seconds) before attempting socket
reconnection. Defaults to 2.
=head1 METHODS
=head2 my $id = POE::Component::Client::MPD::Connection->spawn( \%params );
This method will create a L<POE::Component::Client::TCP> session
responsible for low-level communication with mpd.
It will return the poe id of the session newly created.
=head1 PUBLIC EVENTS ACCEPTED
=head2 disconnect( )
Request the pococm-connection to be shutdown. This does B<not> shut down
the MPD server. No argument.
=head2 send( $message )
Request pococm-conn to send the C<$message> over the wires. Note that
this request is a L<POE::Component::Client::MPD::Message> object
properly filled up, and that the C<_commands()> attribute should B<not>
be newline terminated.
=head1 PUBLIC EVENTS FIRED
The following events are fired from the spawned session.
=head2 mpd_connected( $version )
Fired when the session is connected to a mpd server. This event isn't
fired when the socket connection takes place, but when the session has
checked that remote peer is a real mpd server. C<$version> is the
advertised mpd server version.
=head2 mpd_connect_error_fatal( $errstr )
Fired when the session encounters a fatal error. This happens either
when the session is connected to a server which happens to be something
else than a mpd server, or if there was more than C<max_retries> (see
C<spawn()> params) connection retries in a row. C<$errstr> will contain
the problem encountered. No retries will be done.
=head2 mpd_connect_error_retriable( $errstr )
Fired when the session has troubles connecting to the server. C<$errstr>
will point the faulty syscall that failed. Re-connection will be tried
after C<$retry_wait> seconds (see C<spawn()> params).
=head2 mpd_data( $msg )
Fired when C<$msg> has been sent over the wires, and mpd server has
answered with success. The actual output should be looked up in
C<$msg->_data>.
=head2 mpd_disconnected( )
Fired when the socket has been disconnected for whatever reason. Note
that this event is B<not> fired in the case of a programmed shutdown
(see C<disconnect()> event above). A reconnection will be automatically
re-tried after C<$retry_wait> (see C<spawn()> params).
=head2 mpd_error( $msg, $errstr )
Fired when C<$msg> has been sent over the wires, and mpd server has
answered with the error message C<$errstr>.
=head1 AUTHOR
Jerome Quelin
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2007 by Jerome Quelin.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
__END__
|