Apparently, I don't seem to have much of a life, and decided to take a few minutes to write a standalone web server app that would (more or less) simulate playing an MP3. The comments pretty much say it all.
#!/usr/local/bin/perl -w
#
# Danger! Danger! Running with scissors! Carry handgrenades withou
+t pins! Driving without brakes!
#
# This code is a super simple super stupid web-based MP3 player conce
+pt program. It has *no* error
# checking. It probably has security holes out the whazoo. It uses
+a template system so lame, the
# person who wrote it should be shot (wait a minute!). If two users
+try to use it at the same time,
# bad things will happen (because of the stupid HTTP::Daemon not supp
+orting printing a string as a
# response, not so much anything else). And worst of all, I wrote it
+ using Windows(tm) based tools
# (VisualSlickEdit and HotMetalPro ROCK!)
#
# On the other hand, it does demonstrate a standalone web-based appli
+cation, using all those great
# little CPAN modules. I'm sure [merlyn] and [tilly] will pump dozen
+s of rounds into it, saying
# how there's a better way to do it. On the third hand, I wrote it h
+alf an hour, it passes use strict
# and -w, and I did it first. Nyah! (I also don't care that stuff go
+es past column 80)
#
# Last, and leastly, sometimes if you control-c out of the app (or Do
+g forbid, it should crash), you
# may have to wait a few seconds for the socket to clear before resta
+rting it. I don't know why the
# socket stays allocated for a brief time after the app has exited.
#
use strict;
use HTTP::Daemon;
use HTTP::Status;
use CGI;
my $serverport = 2000;
my %actions = ('Next' => \&mp3_next,
'Last' => \&mp3_last,
'Stop' => \&mp3_stop,
'Play' => \&mp3_play,
'Vol +10%' => \&mp3_volumeup,
'Vol -10%' => \&mp3_volumedown,
'Left 10%' => \&mp3_balanceleft,
'Right 10%' => \&mp3_balanceright,
);
my @tracklist = ('Pink Floyd - Dark Side Of The Moon',
'Widespread Panic - Bombs & Butterflies',
'Allgood - Trilogy',
'Mox - Dr. Bombay',
'Soundtrack - Debbie Does Dallas',
'The Orb - Perpetual Dawn',
'Tangerine Dream - Underwater Sunlight'
);
my $mode = 'Stopped';
my $curtrack = 0;
my $volume = 50;
my $balance = 50;
#
#
#
{
my $htmltext = 0;
#
# Stupid HTTP::Daemon doesn't support sending a string as a respon
+se, only a file. So
# we generate index.html, and keep the page in $htmltext for when
+we change parameters.
#
{
local $/ = undef;
$htmltext = <DATA>;
}
my $d = new HTTP::Daemon (LocalPort => $serverport) || die "Can't b
+ind to port! ($serverport already in use?)";
print "Please contact me at: <URL:", $d->url, ">\n";
while (my $c = $d->accept)
{
while (my $r = $c->get_request)
{
if ($r->method eq 'GET')
{
ultralame_template_system ($htmltext);
$c->send_file_response ("index.html");
}
else
{
my $q = new CGI ($r->content);
&{$actions {$q->param ('Action')}} if ($q->param ('Action'
+));
ultralame_template_system ($htmltext);
$c->send_file_response ("index.html");
}
}
$c->close;
undef ($c);
}
}
sub ultralame_template_system
{
my $temptext = shift;
$temptext =~ s/<!--##rewrite trackname-->/@tracklist [$curtrack]/ei
+;
$temptext =~ s/<!--##rewrite mode-->/$mode/ei;
$temptext =~ s/<!--##rewrite volume-->/$volume/ei;
$temptext =~ s/<!--##rewrite balance-->/$balance/ei;
open (FH, ">index.html") || die "Can't open file: $!";
print FH $temptext;
close FH;
}
#
# Normally, I don't condone oneliners like this, but for sample code,
+ it'll work
#
sub mp3_next { $curtrack = ++$curtrack % scalar @tracklist; }
sub mp3_last { $curtrack = --$curtrack % scalar @tracklist; }
sub mp3_stop { $mode = 'Stopped'; }
sub mp3_play { $mode = 'Playing'; }
sub mp3_volumeup { $volume = min (100, $volume + 10); }
sub mp3_volumedown { $volume = max (0, $volume - 10); }
sub mp3_balanceleft { $balance = max (10, $balance - 10); }
sub mp3_balanceright { $balance = min (100, $balance + 10); }
#
#
#
sub min { return ($_[0] < $_[1] ? $_[0] : $_[1]); }
sub max { return ($_[0] > $_[1] ? $_[0] : $_[1]); }
__DATA__
<!DOCTYPE HTML PUBLIC "-//SoftQuad Software//DTD HoTMetaL PRO 5.0::199
+81217::extensions to HTML 4.0//EN" "hmpro5.dtd">
<HTML>
<HEAD>
<TITLE>Stupid Sample Code For a Pico MP3 Server</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST">
<TABLE BORDER="0\" ALIGN="CENTER">
<TR>
<TD><FONT SIZE="3" FACE="Verdana">Track Now Playing:</FONT></T
+D>
<TD><FONT SIZE="3" FACE="Verdana"><B><!--##rewrite trackname--
+></B></FONT></TD>
</TR>
</TABLE>
<BR>
<TABLE BORDER="0" ALIGN="CENTER">
<TR>
<TD><FONT SIZE="3" FACE="Verdana">Mode:</FONT></TD>
<TD><FONT SIZE="3" FACE="Verdana"><B><!--##rewrite mode--></B>
+</FONT></TD>
</TR>
<TR>
<TD><FONT SIZE="3" FACE="Verdana">Volume:</FONT></TD>
<TD><FONT SIZE="3" FACE="Verdana"><B><!--##rewrite volume-->%<
+/B></FONT></TD>
</TR>
<TR>
<TD><FONT SIZE="3" FACE="Verdana">Balance:</FONT></TD>
<TD><FONT SIZE="3" FACE="Verdana"><B><!--##rewrite balance-->%
+</B></FONT></TD>
</TR>
</TABLE>
<BR>
<TABLE BORDER="0" ALIGN="CENTER">
<TR>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Next"></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Last"></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Stop"></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Play"></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Vol +10%"></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Vol -10%"></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Left 10%"></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Action" VALUE="Right 10%"></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
--Chris
e-mail jcwren |