A Perl script implements a singing, musical Internet

Catchy Logs

© Sebastian Duda, Fotolia

© Sebastian Duda, Fotolia

Article from Issue 98/2009

Instead of just monitoring incoming requests in your web server's logfile, a sound server makes them audible and lets you listen to the tune of users surfing the site.

Whenever I upload a new version of our blog-like newsletter [1], send an email announcement, or update the RSS feed, I tend to check the web server access log to watch the first information-hungry visitors read the latest news and click on the high-res images.

Of course, deciphering the server log entries scrolling by is fairly tedious. It would be much better to monitor the requests in the background and start working on something else in the meantime. One way to do this would be to transform web hits into sound. Many moons ago, I read in Netscape Time, by Jim Clark, that the early Netscapers used to output incoming hits via PC speaker after creating a new release [2]. A Netscape browser download for Windows croaked like a frog, the sound of breaking glass played for Macs, and Unix downloads were announced with a cannon shot. This meant that the Internet pioneers could share the sound of success in their cubicles after the long coding stretch that preceded the launch.

Implementing something like this in Perl is fairly easy. In my case, however, things are not quite as simple because the web server is somewhere in a hosting provider's server farm. Although the hoster allows ssh-based shell access, it can't transmit sound.

Firewall Tunnel

The boom-sender script on the hosting provider's shared server monitors the web server's access.log file and sends messages for specific URLs through an SSH tunnel to the boom-receiver sound server script running on my home PC.

Figure 1 shows the setup: The sound server is a script implemented by the CPAN POE module that listens for sound commands on port 8080 of the local machine. Another POE script runs provider-side, reacting to changes in the access log and sending messages home as a TCP client. Because my home machine resides behind a firewall, the boom-sender log checker can't talk to it directly. Instead, a tunnel setup that uses the command

home$ ssh -R 8080:localhost:8080 host.xyz-hosting.com

on my home PC connects the two dialog partners. The log script on the hosted server just has to send its messages to its local port 8080, and – hey presto – they are whisked away through the tunnel to port 8080 on the home PC as if the firewall never existed.

Sound on Demand

The local sound machine receives names of sound files in this way and proceeds to play them on Linux with the play utility from the Sox package treasure trove.

By default, the Play program is included with Ubuntu and can handle both WAV files and MP3s, assuming you configure Ubuntu to support this.

Figure 2 shows the interaction of a test client with the sound server running. The telnet command is launched to connect to localhost's port 8080 and receives a greeting from the server and a list of the sound files it has. When the client sends the name of one of these WAV files to the server, the server plays the file. For security reasons, only file names are allowed, rather than paths.

The default location in which boom-receiver looks for these sound files is the current working directory (.), specified as $SOUND_DIR in line 9 of Listing 1.

Because it offers a plethora of server and client components that just need to be put together in creative ways, POE is a good choice of server and client technology. The poe.perl.org website and the POE chapter in Advanced Perl Programming [3] both offer useful introductions to POE, which requires a non-traditional, event-based programming model that takes some time getting used to. The server in Listing 1 defines callbacks for the states ClientConnected (client has opened a connection), ClientInput (client has sent a line of text), and sound_ended, the state that handles the clean up work (described below) after playing a sound.

The sound server handles multiple client connections quasi-simultaneously. The POE component logic takes care of the low-level implementation details and ensures smooth request and error handling behind the scenes. Just like any other POE script, the program code first defines the behavior for any possible events and then calls POE::Kernel->run() to launch the POE kernel. The kernel runs until the program ends, until a fatal error occurs, or until the user terminates the script.

Listing 1



Don't Dawdle

The sound_play() function in line 60 of Listing 1 plays a sound file passed to it by name. It creates POE::Wheel, a cogwheel in the POE system's works that allows the POE kernel to talk to the world outside.

To allow the system to process multiple tasks quasi-simultaneously, Perl code in POE should only run uninterrupted as long as it proceeds at full speed. Any interactions with files, sockets, or other processes obviously cause delays because disk or network access is far slower than processing CPU instructions or accessing RAM, and it would be extremely inefficient to let the CPU sit idle while waiting for these tasks to complete. Instead, they are handed off to wheels, which accomplish them one slice at a time and report results back asynchronously to the POE kernel.

Applying the play command to start a new Unix process, passing a short sound file to it, and waiting for it to play takes more than a second. If the script was blocked for this time, it would delay the client response and not be available for new requests.

Instead, a wheel is spun off with the process task, the callback returns immediately, and the POE kernel reassumes control, leaving everything else to run in the background.

The wheel – POE::Wheel::Run – expects as parameters an external program to launch, its arguments, and a StderrEvent callback, triggered if the process writes anything to its STDERR channel. Of course, this is not relevant for the Play program, which does not normally output error messages and simply terminates after playing a sound file. Boom-receiver simply defines a non-existing state for this event, which POE later ignores.

When the wheel notices that the play process has terminated, it triggers CloseEvent in line 75, assigned to a subroutine in line 48. Then it removes the remaining reference to the wheel from the system, which unleashes the POE kernel's garbage collector to clean up its remains.

Ideally, the wheel would just launch a process such as xmms, which would run permanently, and then occasionally pass sound files to it. However, the POE component for this on CPAN is badly out of date and won't compile with the current version of XMMS. Pity!

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Twisted

    The Twisted framework makes it so easy to create network-aware applications in Python. Twisted speaks all the major Internet protocols, from mail through chat, and it can handle encryption. We’ll show you how to set up a personal web server with Twisted.

  • MPD and Cantata

    The MPD server helps you manage your music collection flexibly and remotely and integrates external content.

  • PulseEffects

    A wildly flashing equalizer once was part of the basic equipment of every decent stereo system. PulseEffects upgrades the PulseAudio server to include these slide controls – and offers even more.

  • LTSP

    The Linux Terminal Server Project offers a comprehensive approach to terminal services in Linux, including easy access to local sound cards, printers, and USB sticks.

  • MIDI with Linux

    A MIDI keyboard is a useful extension to any audio workstation. Learn how to connect a MIDI instrument to your Linux sound studio through a MIDI interface device.

comments powered by Disqus