A Perl script implements a singing, musical Internet
Catchy Logs
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
boom-receiver
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
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.