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