all dxdebug to have other directories, add wsjtl.pl
[spider.git] / perl / wsjtl.pl
1 #!/usr/binenv perl
2 #
3 # A basic listener and decoder of wsjtx packets
4 #
5 #
6
7 our ($systime, $root, $local_data);
8
9 BEGIN {
10         umask 002;
11         $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN };
12                         
13         # take into account any local::lib that might be present
14         eval {
15                 require local::lib;
16         };
17         unless ($@) {
18 #               import local::lib;
19                 import local::lib qw(/spider/perl5lib);
20         } 
21
22         # root of directory tree for this system
23         $root = "/spider";
24         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
25
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";
29
30         # do some validation of the input
31         die "The directory $root doesn't exist, please RTFM" unless -d $root;
32
33         # locally stored data lives here
34         $local_data = "$root/local_data";
35         mkdir $local_data, 02774 unless -d $local_data;
36
37         # try to create and lock a lockfile (this isn't atomic but
38         # should do for now
39         $lockfn = "$root/local_data/wsjtxl.lck";       # lock file name
40         if (-w $lockfn) {
41                 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
42                 my $pid = <CLLOCK>;
43                 if ($pid) {
44                         chomp $pid;
45                         if (kill 0, $pid) {
46                                 warn "Lockfile ($lockfn) and process $pid exist, another cluster running?\n";
47                                 exit 1;
48                         }
49                 }
50                 unlink $lockfn;
51                 close CLLOCK;
52         }
53         open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
54         print CLLOCK "$$\n";
55         close CLLOCK;
56
57         $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
58         $systime = time;
59 }
60
61 use strict;
62 use warnings;
63 use 5.22.0;
64
65 use Mojolicious 8.1;
66 use Mojo::IOLoop;
67 use Mojo::IOLoop::Server;
68 use DXDebug;
69 use DXUDP;
70
71 use WSJTX;
72
73 our $udp_host = '0.0.0.0';
74 our $udp_port = 2237;
75 our $tcp_host = '::';
76 our $tcp_port = 2238;
77
78 my $uh;                                                 # the mojo handle for the UDP listener
79 my $th;                                                 #  ditto TCP
80 my $wsjtx;                                              # the wsjtx decoder
81
82
83 our %slot;                        # where the connected TCP client structures live
84
85
86 dbginit('wsjtl');
87 dbgadd('udp');
88
89 $uh = DXUDP->new;
90 $uh->start(host => $udp_host, port => $udp_port) or die "Cannot listen on $udp_host:$udp_port $!\n";
91
92 $wsjtx = WSJTX->new();
93 $uh->on(read => sub {wstjx->handle(@_)});
94
95 Mojo::IOLoop->start() unless Mojo::IOLoop->is_running;
96
97 exit;
98
99