#!/usr/bin/perl -w

use strict;
use warnings;
use IO::Socket::UNIX;

my $Addr = $ARGV[0] or die("usage: $0 <UDS socket name>\n");
my $Gap = 3.0; # seconds

unlink($Addr);
my $Server = new IO::Socket::UNIX(Local => $Addr,
	Listen => SOMAXCONN, Type => SOCK_DGRAM)
	or die("cannot listen: $!, stopped");

my $lastTime = time();
printf("%d waiting for messages\n", $lastTime);

my $msg;
while (defined(my $peer = $Server->recv($msg, 1024))) {
	my $now = time();
	printf("%d got msg #%02d after %.2f seconds ... sleeping for %.2f seconds\n",
		$now, $msg, $now - $lastTime, $Gap);
	$lastTime = $now;
	select(undef, undef, undef, $Gap);
}

print("Done: $!.\n");

exit 0;

