<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Max says... &#187; Perl</title>
	<atom:link href="http://maxmanders.co.uk/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxmanders.co.uk</link>
	<description>thoughts of a web developer</description>
	<lastBuildDate>Tue, 31 Aug 2010 07:25:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Playing With Perl Sockets</title>
		<link>http://maxmanders.co.uk/general/playing-with-perl-sockets/</link>
		<comments>http://maxmanders.co.uk/general/playing-with-perl-sockets/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 17:47:08 +0000</pubDate>
		<dc:creator>maxmanders</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://maxmanders.co.uk/?p=99</guid>
		<description><![CDATA[At work, I have a Snom 360 IP phone that is hooked up to our internal Asterisk PBX.  If I&#8217;m listening to music with my headphones in, I&#8217;m not always aware that my phone is ringing &#8211; some Perl and PHP hacking later, I&#8217;ve got a crude (and work-in-progress) solution. The phone has an internal [...]]]></description>
			<content:encoded><![CDATA[<p>At work, I have a <a href="http://www.snom.com/en/products/snom-360-voip-phone/">Snom 360</a> IP phone that is hooked up to our internal <a href="http://www.asterisk.org/">Asterisk</a> PBX.  If I&#8217;m listening to music with my headphones in, I&#8217;m not always aware that my phone is ringing &#8211; some Perl and PHP hacking later, I&#8217;ve got a crude (and work-in-progress) solution.<span id="more-99"></span></p>
<p>The phone has an internal web server that lets the user customise a number of options.  The one I&#8217;m interested in is &#8216;Action URI&#8217;.  When a call comes through to my phone, I can enter a URI that my phone will request.  I can send arbitrary parameters in the query string, e.g. a message or the phone number of the caller.  My solution involves a PHP script that the phone can request, a simple Perl socket server sitting listening on my laptop, and a simpel Perl Tk script that will open a window to alert me to the call.</p>
<p>The PHP script that my phone requests is quite simple:</p>
<pre class="brush:perl">
// Hostname of machine
$host = 'somehost';
// Port to connect to
$port = '7890';
$timeout = 30;
$message = $_GET['message'];

$socket = fsockopen($host, $port, $errnum, $errstr, $timeout);
if (!is_resource($socket))
{
exit("Cannot connect: " . $errnum . " " . $errstr);
}
else
{
fputs($socket, $message);
}
fclose($socket);
</pre>
<p>This script is called from my phone and connects to the socket server listening on my laptop.  The code for the corresponding socket server follows:</p>
<pre class="brush:perl">
use strict;
use IO::Socket;
use Net::hostent;

my $PORT = 7890;
my $IP = '192.168.0.1';

my $serverSocket = IO::Socket::INET-&gt;new(
    Proto =&gt; 'tcp',
    LocalHost =&gt; $IP,
    LocalPort =&gt; $PORT,
    Listen =&gt; SOMAXCONN,
    Reuse =&gt; 1,
) or die("Cannot create socket: $!\n");

while (my $clientSocket = $serverSocket-&gt;accept())
{
    $clientSocket-&gt;autoflush(1);
    while (&lt;$clientSocket&gt;)
    {
        `/path/to/tk/script/showMessage.pl "$_"`;
    }
    close($clientSocket);
}
</pre>
<p>This Perl script listens in the background for incoming connections and passes any data it receives to a Perl TK script that displays this data in the form of a simple window on display 0:0.  Here&#8217;s the code for th Perl Tk srcript:</p>
<pre class="brush:perl">
use strict;
use Tk;
use Tk::Font;

my $message = $ARGV[0];

my $mainWindow = MainWindow-&gt;new();
$mainWindow-&gt;minsize(qw(500 200));
$mainWindow-&gt;title("Incoming Call");
$mainWindow-&gt;configure(-background =&gt; 'white');

my $font = $mainWindow-&gt;Font(
    -family =&gt; 'Arial',
    -size =&gt; '24',
);

my $acceptButton = $mainWindow-&gt;Button(
    -text =&gt; $message,
    -background =&gt; 'red',
    -command =&gt; \&amp;exit,
    -foreground =&gt; 'white',
    -font =&gt; $font,
);

$acceptButton-&gt;pack(
    -side =&gt; 'bottom',
    -expand =&gt; 1,
    -fill =&gt; 'both'
);

$mainWindow-&gt;withdraw();
$mainWindow-&gt;update();
my $winXPos = int(($mainWindow-&gt;screenwidth - $mainWindow-&gt;width) / 2);
my $winYPos = int(($mainWindow-&gt;screenheight - $mainWindow-&gt;height) / 2);
$mainWindow-&gt;geometry("+$winXPos+$winYPos");
$mainWindow-&gt;deiconify();

MainLoop();
</pre>
<p>There&#8217;s no doubt about it, this combination of scripts is crude, insecure, and really more of a proof of concept.  Nonetheless, it serves my purposes and hopefully can tidy it up as time allows.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxmanders.co.uk/general/playing-with-perl-sockets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
