3 # A basic listener and decoder of wsjtx packets
7 our ($systime, $root, $local_data);
11 $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN };
13 # take into account any local::lib that might be present
19 import local::lib qw(/spider/perl5lib);
22 # root of directory tree for this system
24 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
26 unshift @INC, "$root/perl5lib" unless grep {$_ eq "$root/perl5lib"} @INC;
27 unshift @INC, "$root/perl"; # this IS the right way round!
28 unshift @INC, "$root/local";
30 # do some validation of the input
31 die "The directory $root doesn't exist, please RTFM" unless -d $root;
33 # locally stored data lives here
34 $local_data = "$root/local_data";
35 mkdir $local_data, 02774 unless -d $local_data;
37 # try to create and lock a lockfile (this isn't atomic but
39 $lockfn = "$root/local_data/wsjtxl.lck"; # lock file name
41 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
46 warn "Lockfile ($lockfn) and process $pid exist, another cluster running?\n";
53 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
57 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
67 use Mojo::IOLoop::Server;
78 my $uh; # the mojo handle for the UDP listener
80 my $wsjtx; # the wsjtx decoder
83 our %slot; # where the connected TCP client structures live
91 $uh->start(host => $udp_host, port => $udp_port) or die "Cannot listen on $udp_host:$udp_port $!\n";
93 $wsjtx = WSJTX->new();
94 $uh->on(read => \&_udpread);
96 $th = Mojo::IOLoop::Server->new;
97 $th->on(accept => \&_accept);
98 $th->listen(address => $tcp_host, port => $tcp_port);
101 Mojo::IOLoop->start() unless Mojo::IOLoop->is_running;
107 my ($handle, $data) = @_;
109 my $host = $handle->peerhost;
110 my $port = $handle->peerport;
112 my $in = $wsjtx->handle($handle, $data, "$host:$port");
114 distribute($in) if $in && length $in;
119 my ($id, $handle) = @_;
120 my $host = $handle->peerhost;
121 my $port = $handle->peerport;
124 my $s = $slot{"$host:$port"} = { addr => "$host:$port"};
125 my $stream = $s->{stream} = Mojo::IOLoop::Stream->new($handle);
126 $stream->on(error => sub { $stream->close; delete $s->{addr}});
127 $stream->on(close => sub { delete $s->{addr}});
128 $stream->on(read => sub {_tcpread($s, $_[1])});
138 dbg("incoming: $data");
144 foreach my $c (values %slot) {
145 $c->{stream}->write("$in\r\n");