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.
6 # Hence the name of 'spider' (although it may become 'dxspider')
8 # Copyright (c) 1998 Dirk Koopman G1TLH
15 # make sure that modules are searched in the order local then perl
19 # root of directory tree for this system
21 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
23 unshift @INC, "$root/perl"; # this IS the right way round!
24 unshift @INC, "$root/local";
26 # try to create and lock a lockfile (this isn't atomic but
28 my $lockfn = "$root/perl/cluster.lock"; # lock file name
30 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
33 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
36 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
69 use POSIX ":sys_wait_h";
76 #use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects $zombies $root
77 # $lang $myalias @debug $userfn $clusteraddr $clusterport $mycall $decease );
79 @inqueue = (); # the main input queue, an array of hashes
80 $systime = 0; # the time now (in seconds)
81 $version = "1.45"; # the version no of the software
82 $starttime = 0; # the starting time of the cluster
83 $lockfn = "cluster.lock"; # lock file name
84 @outstanding_connects = (); # list of outstanding connects
86 # handle disconnections
90 return if !defined $dxchan;
91 $dxchan->disconnect();
94 # send a message to call on conn and disconnect
97 my ($conn, $call, $mess) = @_;
99 dbg('chan', "-> D $call $mess\n");
100 $conn->send_now("D$call|$mess");
102 dbg('chan', "-> Z $call bye\n");
103 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
108 # handle incoming messages
111 my ($conn, $msg, $err) = @_;
112 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
114 if (!defined $msg || (defined $err && $err)) {
119 $dxchan->conn(undef);
128 # set up the basic channel info - this needs a bit more thought - there is duplication here
129 if (!defined $dxchan) {
130 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
131 return unless defined $sort;
133 # is there one already connected to me - locally?
134 my $user = DXUser->get($call);
135 if (DXChannel->get($call)) {
136 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call);
137 already_conn($conn, $call, $mess);
141 # is there one already connected elsewhere in the cluster?
143 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
146 if (DXCluster->get_exact($call)) {
147 my $mess = DXM::msg($lang, $user->is_node ? 'concluster' : 'conother', $call);
148 already_conn($conn, $call, $mess);
152 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
154 if (DXCluster->get_exact($call)) {
155 my $mess = DXM::msg($lang, 'conother', $call);
156 already_conn($conn, $call, $mess);
159 $user = DXUser->new($call);
163 if ($user->lockout) {
164 Log('DXCommand', "$call is locked out, disconnected");
165 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
170 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
171 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
172 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
173 die "Invalid sort of user on $call = $sort" if !$dxchan;
176 # queue the message and the channel object for later processing
178 my $self = bless {}, "inqueue";
179 $self->{dxchan} = $dxchan;
180 $self->{data} = $msg;
181 push @inqueue, $self;
190 # cease running this program, close down all the connections nicely
195 $SIG{'TERM'} = 'IGNORE';
196 $SIG{'INT'} = 'IGNORE';
201 Local::finish(); # end local processing
203 dbg('local', "Local::finish error $@") if $@;
206 foreach $dxchan (DXChannel->get_all()) {
207 next unless $dxchan->is_node;
208 disconnect($dxchan) unless $dxchan == $DXProt::me;
210 Msg->event_loop(1, 0.05);
211 Msg->event_loop(1, 0.05);
212 Msg->event_loop(1, 0.05);
213 Msg->event_loop(1, 0.05);
214 Msg->event_loop(1, 0.05);
215 Msg->event_loop(1, 0.05);
218 foreach $dxchan (DXChannel->get_all()) {
219 next if $dxchan->is_node;
220 disconnect($dxchan) unless $dxchan == $DXProt::me;
222 Msg->event_loop(1, 0.05);
223 Msg->event_loop(1, 0.05);
224 Msg->event_loop(1, 0.05);
225 Msg->event_loop(1, 0.05);
226 Msg->event_loop(1, 0.05);
227 Msg->event_loop(1, 0.05);
231 # close all databases
234 dbg('chan', "DXSpider version $version ended");
235 Log('cluster', "DXSpider V$version stopped");
239 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
243 # the reaper of children
247 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
248 dbg('reap', "cpid: $cpid");
249 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
250 $zombies-- if $zombies > 0;
252 dbg('reap', "cpid: $cpid");
255 # this is where the input queue is dealt with and things are dispatched off to other parts of
259 my $self = shift @inqueue;
262 my $data = $self->{data};
263 my $dxchan = $self->{dxchan};
265 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
266 return unless defined $sort;
268 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
269 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
272 my $user = $dxchan->user;
273 if ($sort eq 'A' || $sort eq 'O') {
274 $dxchan->start($line, $sort);
275 } elsif ($sort eq 'I') {
276 die "\$user not defined for $call" if !defined $user;
278 $dxchan->normal($line);
279 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
280 } elsif ($sort eq 'Z') {
281 $dxchan->conn(undef);
283 } elsif ($sort eq 'D') {
284 ; # ignored (an echo)
286 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
292 my $t = $systime - $starttime;
293 my $days = int $t / 86400;
295 my $hours = int $t / 3600;
297 my $mins = int $t / 60;
298 return sprintf "%d %02d:%02d", $days, $hours, $mins;
300 #############################################################
302 # The start of the main line of code
304 #############################################################
306 $starttime = $systime = time;
307 $lang = 'en' unless $lang;
309 # open the debug file, set various FHs to be unbuffered
314 STDOUT->autoflush(1);
316 Log('cluster', "DXSpider V$version started");
319 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2000 Dirk Koopman G1TLH");
322 dbg('err', "loading prefixes ...");
326 dbg('err', "loading band data ...");
329 # initialise User file system
330 dbg('err', "loading user file system ...");
331 DXUser->init($userfn, 1);
333 # start listening for incoming messages/connects
334 dbg('err', "starting listener ...");
335 Msg->new_server("$clusteraddr", $clusterport, \&login);
338 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
342 $SIG{TERM} = \&cease;
343 $SIG{HUP} = 'IGNORE';
344 $SIG{CHLD} = sub { $zombies++ };
346 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
347 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
348 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
349 $SIG{KILL} = 'DEFAULT'; # as if it matters....
351 # catch the rest with a hopeful message
354 # dbg('chan', "Catching SIG $_");
355 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
362 # read in system messages
365 # read in command aliases
368 # initialise the Geomagnetic data engine
372 # initial the Spot stuff
375 # initialise the protocol engine
376 dbg('err', "reading in duplicate spot and WWV info ...");
379 # put in a DXCluster node for us here so we can add users and take them away
380 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
382 # read in any existing message headers and clean out old crap
383 dbg('err', "reading existing message headers ...");
387 # read in any cron jobs
388 dbg('err', "reading cron jobs ...");
391 # read in database descriptors
392 dbg('err', "reading database descriptors ...");
395 # starting local stuff
396 dbg('err', "doing local initialisation ...");
400 dbg('local', "Local::init error $@") if $@;
402 # print various flags
403 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
405 # this, such as it is, is the main loop!
406 dbg('err', "orft we jolly well go ...");
408 #open(DB::OUT, "|tee /tmp/aa");
413 Msg->event_loop(1, 0.1);
415 process_inqueue(); # read in lines from the input queue and despatch them
418 # do timed stuff, ongoing processing happens one a second
419 if ($timenow != $systime) {
422 DXCron::process(); # do cron jobs
423 DXCommandmode::process(); # process ongoing command mode stuff
424 DXProt::process(); # process ongoing ak1a pcxx stuff
425 DXConnect::process();
432 Local::process(); # do any localised processing
434 dbg('local', "Local::process error $@") if $@;
437 last if --$decease <= 0;