fixed silly typo
[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 require 5.004;
14
15 # make sure that modules are searched in the order local then perl
16 BEGIN {
17         # root of directory tree for this system
18         $root = "/spider"; 
19         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
20         
21         unshift @INC, "$root/perl";     # this IS the right way round!
22         unshift @INC, "$root/local";
23 }
24
25 use Msg;
26 use DXVars;
27 use DXDebug;
28 use DXLog;
29 use DXLogPrint;
30 use DXUtil;
31 use DXChannel;
32 use DXUser;
33 use DXM;
34 use DXCommandmode;
35 use DXProt;
36 use DXMsg;
37 use DXCluster;
38 use DXCron;
39 use DXConnect;
40 use Prefix;
41 use Bands;
42 use Geomag;
43 use CmdAlias;
44 use Local;
45
46 use Carp;
47
48 package main;
49
50 @inqueue = ();                                  # the main input queue, an array of hashes
51 $systime = 0;                                   # the time now (in seconds)
52 $version = "1.22";                              # the version no of the software
53 $starttime = 0;                 # the starting time of the cluster   
54  
55 # handle disconnections
56 sub disconnect
57 {
58         my $dxchan = shift;
59         return if !defined $dxchan;
60         $dxchan->disconnect();
61 }
62
63 # send a message to call on conn and disconnect
64 sub already_conn
65 {
66         my ($conn, $call, $mess) = @_;
67         
68         dbg('chan', "-> D $call $mess\n"); 
69         $conn->send_now("D$call|$mess");
70         sleep(1);
71         dbg('chan', "-> Z $call bye\n");
72         $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
73 }
74
75 # handle incoming messages
76 sub rec
77 {
78         my ($conn, $msg, $err) = @_;
79         my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
80         
81         if (defined $err && $err) {
82                 disconnect($dxchan) if defined $dxchan;
83                 return;
84         }
85         
86         # set up the basic channel info - this needs a bit more thought - there is duplication here
87         if (!defined $dxchan) {
88                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
89                 
90                 # is there one already connected elsewhere in the cluster (and not a cluster)
91                 my $user = DXUser->get($call);
92                 if ($user) {
93                         if (($user->sort eq 'A' || $call eq $myalias) && !DXCluster->get_exact($call)) {
94                                 ;
95                         } else {
96                                 if (DXCluster->get($call) || DXChannel->get($call)) {
97                                         my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
98                                         already_conn($conn, $call, $mess);
99                                         return;
100                                 }
101                         }
102                         $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
103                 } else {
104                         if (DXCluster->get($call)) {
105                                 my $mess = DXM::msg($lang, 'conother', $call);
106                                 already_conn($conn, $call, $mess);
107                                 return;
108                         }
109                         $user = DXUser->new($call);
110                 }
111
112                 # is he locked out ?
113                 if ($user->lockout) {
114                         Log('DXCommand', "$call is locked out, disconnected");
115                         $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
116                         return;
117                 }
118
119                 # create the channel
120                 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
121                 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
122                 die "Invalid sort of user on $call = $sort" if !$dxchan;
123         }
124         
125         # queue the message and the channel object for later processing
126         if (defined $msg) {
127                 my $self = bless {}, "inqueue";
128                 $self->{dxchan} = $dxchan;
129                 $self->{data} = $msg;
130                 push @inqueue, $self;
131         }
132 }
133
134 sub login
135 {
136         return \&rec;
137 }
138
139 # cease running this program, close down all the connections nicely
140 sub cease
141 {
142         my $dxchan;
143         
144         eval {
145                 Local::finish();   # end local processing
146         };
147         dbg('local', "Local::finish error $@") if $@;
148         
149         foreach $dxchan (DXChannel->get_all()) {
150                 disconnect($dxchan) unless $dxchan == $DXProt::me;
151         }
152         Log('cluster', "DXSpider V$version stopped");
153         exit(0);
154 }
155
156 # the reaper of children
157 sub reap
158 {
159         $SIG{'CHLD'} = \&reap;
160         my $cpid = wait;
161 }
162
163 # this is where the input queue is dealt with and things are dispatched off to other parts of
164 # the cluster
165 sub process_inqueue
166 {
167         my $self = shift @inqueue;
168         return if !$self;
169         
170         my $data = $self->{data};
171         my $dxchan = $self->{dxchan};
172         my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
173         
174         # do the really sexy console interface bit! (Who is going to do the TK interface then?)
175         dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
176         
177         # handle A records
178         my $user = $dxchan->user;
179         if ($sort eq 'A' || $sort eq 'O') {
180                 $dxchan->start($line, $sort);  
181         } elsif ($sort eq 'I') {
182                 die "\$user not defined for $call" if !defined $user;
183                 
184                 # normal input
185                 $dxchan->normal($line);
186                 
187                 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
188         } elsif ($sort eq 'Z') {
189                 disconnect($dxchan);
190         } elsif ($sort eq 'D') {
191                 ;                       # ignored (an echo)
192         } else {
193                 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
194         }
195 }
196
197 sub uptime
198 {
199         my $t = $systime - $starttime;
200         my $days = int $t / 86400;
201         $t -= $days * 86400;
202         my $hours = int $t / 3600;
203         $t -= $hours * 3600;
204         my $mins = int $t / 60;
205         return sprintf "%d %02d:%02d", $days, $hours, $mins;
206 }
207 #############################################################
208 #
209 # The start of the main line of code 
210 #
211 #############################################################
212
213 $starttime = $systime = time;
214
215 # open the debug file, set various FHs to be unbuffered
216 foreach (@debug) {
217         dbgadd($_);
218 }
219 STDOUT->autoflush(1);
220
221 Log('cluster', "DXSpider V$version started");
222
223 # banner
224 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
225
226 # load Prefixes
227 print "loading prefixes ...\n";
228 Prefix::load();
229
230 # load band data
231 print "loading band data ...\n";
232 Bands::load();
233
234 # initialise User file system
235 print "loading user file system ...\n"; 
236 DXUser->init($userfn);
237
238 # start listening for incoming messages/connects
239 print "starting listener ...\n";
240 Msg->new_server("$clusteraddr", $clusterport, \&login);
241
242 # prime some signals
243 $SIG{'INT'} = \&cease;
244 $SIG{'TERM'} = \&cease;
245 $SIG{'HUP'} = 'IGNORE';
246 $SIG{'CHLD'} = \&reap;
247
248 # read in system messages
249 DXM->init();
250
251 # read in command aliases
252 CmdAlias->init();
253
254 # initialise the Geomagnetic data engine
255 Geomag->init();
256
257 # initial the Spot stuff
258 Spot->init();
259
260 # initialise the protocol engine
261 print "reading in duplicate spot and WWV info ...\n";
262 DXProt->init();
263
264
265 # put in a DXCluster node for us here so we can add users and take them away
266 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version); 
267
268 # read in any existing message headers and clean out old crap
269 print "reading existing message headers ...\n";
270 DXMsg->init();
271 DXMsg::clean_old();
272
273 # read in any cron jobs
274 print "reading cron jobs ...\n";
275 DXCron->init();
276
277 # starting local stuff
278 print "doing local initialisation ...\n";
279 eval {
280         Local::init();
281 };
282 dbg('local', "Local::init error $@") if $@;
283
284
285
286 # print various flags
287 #print "useful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P\n";
288
289 # this, such as it is, is the main loop!
290 print "orft we jolly well go ...\n";
291 for (;;) {
292         my $timenow;
293         Msg->event_loop(1, 0.001);
294         $timenow = time;
295         process_inqueue();                      # read in lines from the input queue and despatch them
296         
297         # do timed stuff, ongoing processing happens one a second
298         if ($timenow != $systime) {
299                 $systime = $timenow;
300                 $cldate = &cldate();
301                 $ztime = &ztime();
302                 DXCron::process();      # do cron jobs
303                 DXCommandmode::process(); # process ongoing command mode stuff
304                 DXProt::process();              # process ongoing ak1a pcxx stuff
305                 DXConnect::process();
306                 eval { 
307                         Local::process();       # do any localised processing
308                 };
309                 dbg('local', "Local::process error $@") if $@;
310         }
311         if ($decease) {
312                 last if --$decease <= 0;
313         }
314 }
315
316