PDA

View Full Version : Using the slimserver cli from perl



max.spicer
2005-07-17, 09:34
As part of an attempt to refresh my rusty Perl knowledge, I'm trying to do some scripting using the slimserver cli. For now, I'm just trying to issue some queries. I'm currently using Net::Telnet, but aren't sure if this is the best thing to be doing. My current attempts aren't going well as I don't seem able to set the prompt properly. As a result, my script never gets the responses from the cli. Here's my first attempt. This successfully pauses the player but never prints anything. Could anyone offer any pointers?

Thanks,

Max

#!/usr/bin/perl -w
use strict;
use Net::Telnet;

my $t = new Net::Telnet (Host => 'localhost',
Port => 9023,
Prompt => '/^$/',
Cmd_remove_mode => 0);
print $t->cmd('00%3A04%3A20%3A05%3Aa4%3A21 pause');
print $t->cmd('player count %3F');

Dan Sully
2005-07-17, 09:39
* max.spicer shaped the electrons to say...

>As part of an attempt to refresh my rusty Perl knowledge, I'm trying to
>do some scripting using the slimserver cli. For now, I'm just trying
>to issue some queries. I'm currently using Net::Telnet, but aren't
>sure if this is the best thing to be doing. My current attempts aren't
>going well as I don't seem able to set the prompt properly. As a
>result, my script never gets the responses from the cli. Here's my
>first attempt. This successfully pauses the player but never prints
>anything. Could anyone offer any pointers?

Just use IO::Socket instead of Net::Telnet.

-D
--
It does not do to leave a live Dragon out of your calculations..

max.spicer
2005-07-17, 10:02
Okay, I've now got the following, but am still struggling to read responses. The current <$s> is an optimistic attempt that occasionally works, but generally just makes the script hang after doing the pause.

#!/usr/bin/perl -w
use strict;
use IO::Socket;

my $player = '00%3A04%3A20%3A05%3Aa9%3A87';

my $s = new IO::Socket::INET (PeerAddr => 'localhost',
PeerPort => 9023,
Proto => 'tcp',
Type => SOCK_STREAM)
or die 'Couldn\'t connect to server';

$s->autoflush(1);

print $s "$player pause\n";
print $s "exit\n";
print <$s>;

close $s;


Thanks,

Max


Just use IO::Socket instead of Net::Telnet.

mherger
2005-07-17, 13:56
Max

> Okay, I've now got the following, but am still struggling to read
> responses. The current <$s> is an optimistic attempt that occasionally
> works, but generally just makes the script hang after doing the pause.

Have a look at some of Felix Müller's scripts (eg. the FollowMe.pl
http://www.gwendesign.ch/slimserver/src/followme01.pl) - I've always used
one of these to copy the communication part from. And it has worked for
me, too.

--

Michael

-----------------------------------------------------------
Help translate SlimServer by using the
StringEditor Plugin (http://www.herger.net/slim/)

jth
2005-07-17, 15:13
Shameless plug: see also the slim_cli.pl script here -

http://ultratrendy.com:31888/slim/

max.spicer
2005-07-18, 04:41
Thanks, I'll have a play with them tomorrow. They look as if they're doing exactly what I'm trying to do at one stage, and in the same way too. Hmm...

Max


Max

> Okay, I've now got the following, but am still struggling to read
> responses. The current <$s> is an optimistic attempt that occasionally
> works, but generally just makes the script hang after doing the pause.

Have a look at some of Felix Müller's scripts (eg. the FollowMe.pl
http://www.gwendesign.ch/slimserver/src/followme01.pl) - I've always used
one of these to copy the communication part from. And it has worked for
me, too.

--

Michael

-----------------------------------------------------------
Help translate SlimServer by using the
StringEditor Plugin (http://www.herger.net/slim/)

max.spicer
2005-07-18, 04:44
Actually, I already had done. My last posted attempt is a reworking of that very script. :-) Have you ever tried printing $response? It generally doesn't work for me. Don't waste any time on it though - I'll have a closer look at things tomorrow.

You don't need $socket->autoflush (1); any more, by the way. From cpan: As of VERSION 1.18 all IO::Socket objects have autoflush turned on by default. This was not the case with earlier releases.

Max


Shameless plug: see also the slim_cli.pl script here -

http://ultratrendy.com:31888/slim/

max.spicer
2005-07-21, 10:31
It turns out that saying
print <$s>;
generally causes the script to hang, whereas saying
my $answer = <$s>;
print $answer;
works every time. Could anyone enlighten me as to why this is?

Thanks for the help anyway!

Max


Thanks, I'll have a play with them tomorrow. They look as if they're doing exactly what I'm trying to do at one stage, and in the same way too. Hmm...

Max

Dan Sully
2005-07-21, 10:52
* max.spicer shaped the electrons to say...

>It turns out that saying
>print <$s>;
>generally causes the script to hang, whereas saying
>my $answer = <$s>;
>print $answer;
>works every time. Could anyone enlighten me as to why this is?

print operates in list context - and reading from a filehandle, like <$s> is
list context. So it blocks, expecting to read more.

Retrieving data in scalar context, such as my $answer = <$s> only pulls one line.*

Hope that helps.

* - Really the input record separator: $/ which by default is "\n"

-D
--
<noah> the auto mechanic told me there was something wrong with my rear differential.
I told him I never took calculus.