7db7ae3c9a9ddfe57e67903bdb3f58f2ec61acdc
[spider.git] / perl / cluster.pl
1 #!/usr/bin/perl
2 #
3 # This is the DX cluster 'daemon'. It sits in the middle of its little
4 # web of client routines sucking and blowing data where it may.
5 #
6 # Hence the name of 'spider' (although it may become 'dxspider')
7 #
8 # Copyright (c) 1998 Dirk Koopman G1TLH
9 #
10 # $Id$
11
12
13 # make sure that modules are searched in the order local then perl
14 BEGIN {
15   # root of directory tree for this system
16   $root = "/spider"; 
17   $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
18
19   unshift @INC, "$root/perl";  # this IS the right way round!
20   unshift @INC, "$root/local";
21 }
22
23 use Msg;
24 use DXVars;
25 use DXUtil;
26 use DXChannel;
27 use DXUser;
28 use DXM;
29 use DXCommandmode;
30 use DXProt;
31 use DXMsg;
32 use DXCluster;
33 use DXDebug;
34 use DXCron;
35 use DXConnect;
36 use Prefix;
37 use Bands;
38 use Geomag;
39 use Carp;
40
41 package main;
42
43 @inqueue = ();                # the main input queue, an array of hashes
44 $systime = 0;                 # the time now (in seconds)
45 $version = 1.2;               # the version no of the software
46
47 # handle disconnections
48 sub disconnect
49 {
50   my $dxchan = shift;
51   return if !defined $dxchan;
52   $dxchan->disconnect();
53 }
54
55 # handle incoming messages
56 sub rec
57 {
58   my ($conn, $msg, $err) = @_;
59   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
60   
61   if (defined $err && $err) {
62     disconnect($dxchan) if defined $dxchan;
63         return;
64   }
65   
66   # set up the basic channel info - this needs a bit more thought - there is duplication here
67   if (!defined $dxchan) {
68      my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
69
70      # is there one already connected?
71          if (DXChannel->get($call)) {
72            my $mess = DXM::msg('conother', $call);
73            dbg('chan', "-> D $call $mess\n"); 
74        $conn->send_now("D$call|$mess");
75            sleep(1);
76            dbg('chan', "-> Z $call bye\n");
77        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
78            return;
79      }
80
81          # is there one already connected elsewhere in the cluster?
82          if (DXCluster->get($call)) {
83            my $mess = DXM::msg('concluster', $call);
84            dbg('chan', "-> D $call $mess\n"); 
85        $conn->send_now("D$call|$mess");
86            sleep(1);
87            dbg('chan', "-> Z $call bye\n");
88        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
89            return;
90      }
91
92      my $user = DXUser->get($call);
93          if (!defined $user) {
94            $user = DXUser->new($call);
95          }
96
97      # create the channel
98      $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
99      $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
100          die "Invalid sort of user on $call = $sort" if !$dxchan;
101   }
102   
103   # queue the message and the channel object for later processing
104   if (defined $msg) {
105     my $self = bless {}, "inqueue";
106     $self->{dxchan} = $dxchan;
107     $self->{data} = $msg;
108         push @inqueue, $self;
109   }
110 }
111
112 sub login
113 {
114   return \&rec;
115 }
116
117 # cease running this program, close down all the connections nicely
118 sub cease
119 {
120   my $dxchan;
121   foreach $dxchan (DXChannel->get_all()) {
122     disconnect($dxchan);
123   }
124   exit(0);
125 }
126
127 # this is where the input queue is dealt with and things are dispatched off to other parts of
128 # the cluster
129 sub process_inqueue
130 {
131   my $self = shift @inqueue;
132   return if !$self;
133   
134   my $data = $self->{data};
135   my $dxchan = $self->{dxchan};
136   my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
137   
138   # do the really sexy console interface bit! (Who is going to do the TK interface then?)
139   dbg('chan', "<- $sort $call $line\n");
140   
141   # handle A records
142   my $user = $dxchan->user;
143   if ($sort eq 'A') {
144     $dxchan->start($line);  
145   } elsif ($sort eq 'D') {
146     die "\$user not defined for $call" if !defined $user;
147         
148         # normal input
149         $dxchan->normal($line);
150
151     disconnect($dxchan) if ($dxchan->{state} eq 'bye');
152   } elsif ($sort eq 'Z') {
153     disconnect($dxchan);
154   } else {
155     print STDERR atime, " Unknown command letter ($sort) received from $call\n";
156   }
157 }
158
159 #############################################################
160 #
161 # The start of the main line of code 
162 #
163 #############################################################
164
165 # open the debug file, set various FHs to be unbuffered
166 dbginit($debugfn);
167 foreach(@debug) {
168   dbgadd($_);
169 }
170 STDOUT->autoflush(1);
171
172 # banner
173 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
174
175 # load Prefixes
176 print "loading prefixes ...\n";
177 Prefix::load();
178
179 # load band data
180 print "loading band data ...\n";
181 Bands::load();
182
183 # initialise User file system
184 print "loading user file system ...\n"; 
185 DXUser->init($userfn);
186
187 # start listening for incoming messages/connects
188 print "starting listener ...\n";
189 Msg->new_server("$clusteraddr", $clusterport, \&login);
190
191 # prime some signals
192 $SIG{'INT'} = \&cease;
193 $SIG{'TERM'} = \&cease;
194 $SIG{'HUP'} = 'IGNORE';
195
196 # initialise the protocol engine
197 DXProt->init();
198
199 # initialise the Geomagnetic data engine
200 Geomag->init();
201
202 # initial the Spot stuff
203 Spot->init();
204
205 # put in a DXCluster node for us here so we can add users and take them away
206 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version); 
207
208 # read in any existing message headers
209 print "reading existing message headers\n";
210 DXMsg->init();
211
212 # read in any cron jobs
213 print "reading cron jobs\n";
214 DXCron->init();
215
216 # this, such as it is, is the main loop!
217 print "orft we jolly well go ...\n";
218 for (;;) {
219   my $timenow;
220   Msg->event_loop(1, 0.001);
221   $timenow = time;
222   process_inqueue();                 # read in lines from the input queue and despatch them
223
224   # do timed stuff, ongoing processing happens one a second
225   if ($timenow != $systime) {
226     $systime = $timenow;
227         $cldate = &cldate();
228         $ztime = &ztime();
229     DXCommandmode::process();     # process ongoing command mode stuff
230     DXProt::process();              # process ongoing ak1a pcxx stuff
231         DXCron::process();
232         DXConnect::process();
233   }
234 }
235