blib/lib/POE/Component/Client/MPD/Connection.pm

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