#!/usr/bin/env perl use threads; use threads::shared; use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr sub read_wxnow { my ($pathname,$lastwxref,$wxchangedref,$POLLTIME) = @_; my $slash="/"; while ( -e $pathname) { open (WXNOWFILE, "<$pathname") || die "Cannot open wxnow file $pathname\n"; my $firstline=1; while () { if ($firstline==1) { $firstline=0; next; } else { $newwx=$_; chomp($newwx); $newwx =~ s/^([0-9]{3})$slash([0-9]{3})g/c$1s$2g/; $newwx = $newwx . "xDvs"; if ($newwx ne $$lastwxref) { $$lastwxref=$newwx; $$wxchangedref =1; } else { $$wxchangedref=0; } } } close (WXNOWFILE); sleep $POLLTIME; } } sub handle_connection { my ($socket,$lastwxref,$wxchangedref,$POLLTIME) = @_; my $disconn = 0; my $firsttime=1; $socket->autoflush(1); my $hostinfo = gethostbyaddr($socket->peeraddr); while (1) { $foobar=getpeername($socket) or $disconn=1; if ($socket->connected()) { $disconn=0; } else { $disconn=1; } if ($disconn==0) { if ($$wxchangedref==1 || $firsttime==1) { send($socket,"$$lastwxref\n",0); $firsttime=0; } sleep $POLLTIME/2; } last if $disconn==1; } } $SERVER_PORT=5500; my $listen = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $SERVER_PORT, ReuseAddr => 1, Listen => SOMAXCONN ); my $lastwx : shared =""; my $wxchanged : shared =0; my $POLLTIME =10; $pathname="/users/russo/Wxnow.txt"; async(\&read_wxnow, $pathname,\$lastwx,\$wxchanged,$POLLTIME)->detach; while (my $socket = $listen->accept) { async(\&handle_connection, $socket,\$lastwx,\$wxchanged,$POLLTIME)->detach; }