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