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 $lockfn = "$root/perl/cluster.lck"; # 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) $!";
40 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
89 use POSIX ":sys_wait_h";
96 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
97 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
98 $clusterport $mycall $decease $build $is_win
101 @inqueue = (); # the main input queue, an array of hashes
102 $systime = 0; # the time now (in seconds)
103 $version = "1.47"; # the version no of the software
104 $starttime = 0; # the starting time of the cluster
105 #@outstanding_connects = (); # list of outstanding connects
106 @listeners = (); # list of listeners
109 # send a message to call on conn and disconnect
112 my ($conn, $call, $mess) = @_;
114 dbg('chan', "-> D $call $mess\n");
115 $conn->send_now("D$call|$mess");
123 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
124 $dxchan->disconnect(1);
127 # handle incoming messages
130 my ($conn, $msg) = @_;
131 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
132 return unless defined $sort;
134 # set up the basic channel info
135 # is there one already connected to me - locally?
136 my $user = DXUser->get($call);
137 my $dxchan = DXChannel->get($call);
139 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
140 already_conn($conn, $call, $mess);
144 # is there one already connected elsewhere in the cluster?
146 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
149 if (my $ref = DXCluster->get_exact($call)) {
150 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->dxchancall);
151 already_conn($conn, $call, $mess);
155 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
157 if (my $ref = DXCluster->get_exact($call)) {
158 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->dxchancall);
159 already_conn($conn, $call, $mess);
162 $user = DXUser->new($call);
166 if ($user->lockout) {
167 Log('DXCommand', "$call is locked out, disconnected");
173 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
174 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
175 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
176 die "Invalid sort of user on $call = $sort" if !$dxchan;
178 # check that the conn has a callsign
179 $conn->conns($call) if $conn->isa('IntMsg');
182 $conn->set_error(sub {error_handler($dxchan)});
183 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
184 rec($dxchan, $conn, $msg);
189 my ($dxchan, $conn, $msg) = @_;
191 # queue the message and the channel object for later processing
193 my $self = bless {}, "inqueue";
194 $self->{dxchan} = $dxchan;
195 $self->{data} = $msg;
196 push @inqueue, $self;
202 return \&new_channel;
205 # cease running this program, close down all the connections nicely
211 $SIG{'TERM'} = 'IGNORE';
212 $SIG{'INT'} = 'IGNORE';
218 Local::finish(); # end local processing
220 dbg('local', "Local::finish error $@") if $@;
223 foreach $dxchan (DXChannel->get_all_nodes) {
224 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
226 Msg->event_loop(100, 0.01);
229 foreach $dxchan (DXChannel->get_all_users) {
236 # end everything else
237 Msg->event_loop(100, 0.01);
241 # close all databases
244 # close all listeners
245 foreach my $l (@listeners) {
249 dbg('chan', "DXSpider version $version, build $build ended");
250 Log('cluster', "DXSpider V$version, build $build ended");
254 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
258 # the reaper of children
262 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
263 dbg('reap', "cpid: $cpid");
264 # Msg->pid_gone($cpid);
265 $zombies-- if $zombies > 0;
267 dbg('reap', "cpid: $cpid");
270 # this is where the input queue is dealt with and things are dispatched off to other parts of
274 my $self = shift @inqueue;
277 my $data = $self->{data};
278 my $dxchan = $self->{dxchan};
280 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
281 return unless defined $sort;
283 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
284 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
287 my $user = $dxchan->user;
288 if ($sort eq 'A' || $sort eq 'O') {
289 $dxchan->start($line, $sort);
290 } elsif ($sort eq 'I') {
291 die "\$user not defined for $call" if !defined $user;
293 $dxchan->normal($line);
294 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
295 } elsif ($sort eq 'Z') {
297 } elsif ($sort eq 'D') {
298 ; # ignored (an echo)
300 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
306 my $t = $systime - $starttime;
307 my $days = int $t / 86400;
309 my $hours = int $t / 3600;
311 my $mins = int $t / 60;
312 return sprintf "%d %02d:%02d", $days, $hours, $mins;
317 AGWMsg::init(\&new_channel);
320 #############################################################
322 # The start of the main line of code
324 #############################################################
326 $starttime = $systime = time;
327 $lang = 'en' unless $lang;
329 # open the debug file, set various FHs to be unbuffered
334 STDOUT->autoflush(1);
336 # calculate build number
337 $build = $main::version;
340 open(CL, "$main::root/perl/cluster.pl") or die "Cannot open cluster.pl $!";
342 next unless /^use\s+([\w:_]+)/;
346 foreach my $fn (@fn) {
347 open(CL, "$main::root/perl/${fn}.pm") or next;
349 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
357 Log('cluster', "DXSpider V$version, build $build started");
360 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
363 dbg('err', "loading prefixes ...");
367 dbg('err', "loading band data ...");
370 # initialise User file system
371 dbg('err', "loading user file system ...");
372 DXUser->init($userfn, 1);
374 # start listening for incoming messages/connects
375 dbg('err', "starting listeners ...");
376 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
377 $conn->conns("Server $clusteraddr/$clusterport");
378 push @listeners, $conn;
379 dbg('err', "Internal port: $clusteraddr $clusterport");
380 foreach my $l (@main::listen) {
381 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
382 $conn->conns("Server $l->[0]/$l->[1]");
383 push @listeners, $conn;
384 dbg('err', "External Port: $l->[0] $l->[1]");
389 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
392 unless ($DB::VERSION) {
393 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
397 $SIG{HUP} = 'IGNORE';
398 $SIG{CHLD} = sub { $zombies++ };
400 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
401 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
402 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
403 $SIG{KILL} = 'DEFAULT'; # as if it matters....
405 # catch the rest with a hopeful message
408 # dbg('chan', "Catching SIG $_");
409 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
417 # read in system messages
420 # read in command aliases
423 # initialise the Geomagnetic data engine
427 # initial the Spot stuff
430 # initialise the protocol engine
431 dbg('err', "reading in duplicate spot and WWV info ...");
434 # put in a DXCluster node for us here so we can add users and take them away
435 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
437 # read in any existing message headers and clean out old crap
438 dbg('err', "reading existing message headers ...");
442 # read in any cron jobs
443 dbg('err', "reading cron jobs ...");
446 # read in database descriptors
447 dbg('err', "reading database descriptors ...");
450 # starting local stuff
451 dbg('err', "doing local initialisation ...");
455 dbg('local', "Local::init error $@") if $@;
457 # print various flags
458 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
460 # this, such as it is, is the main loop!
461 dbg('err', "orft we jolly well go ...");
463 #open(DB::OUT, "|tee /tmp/aa");
468 Msg->event_loop(10, 0.010);
470 process_inqueue(); # read in lines from the input queue and despatch them
473 # do timed stuff, ongoing processing happens one a second
474 if ($timenow != $systime) {
477 DXCron::process(); # do cron jobs
478 DXCommandmode::process(); # process ongoing command mode stuff
479 DXProt::process(); # process ongoing ak1a pcxx stuff
480 DXConnect::process();
488 Local::process(); # do any localised processing
490 dbg('local', "Local::process error $@") if $@;
493 last if --$decease <= 0;