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?
92 use POSIX ":sys_wait_h";
99 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
100 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
101 $clusterport $mycall $decease $is_win $routeroot
104 @inqueue = (); # the main input queue, an array of hashes
105 $systime = 0; # the time now (in seconds)
106 $version = "1.48"; # the version no of the software
107 $starttime = 0; # the starting time of the cluster
108 #@outstanding_connects = (); # list of outstanding connects
109 @listeners = (); # list of listeners
111 use vars qw($VERSION $BRANCH $build $branch);
112 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
113 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
114 $main::build += 14; # add an offset to make it bigger than last system
115 $main::build += $VERSION;
116 $main::branch += $BRANCH;
119 # send a message to call on conn and disconnect
122 my ($conn, $call, $mess) = @_;
124 $conn->disable_read(1);
125 dbg("-> D $call $mess\n") if isdbg('chan');
126 $conn->send_now("D$call|$mess");
134 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
135 $dxchan->disconnect(1);
138 # handle incoming messages
141 my ($conn, $msg) = @_;
142 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
143 return unless defined $sort;
145 unless (is_callsign($call)) {
146 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
150 # set up the basic channel info
151 # is there one already connected to me - locally?
152 my $user = DXUser->get($call);
153 my $dxchan = DXChannel->get($call);
155 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
156 already_conn($conn, $call, $mess);
161 my $basecall = $call;
162 $basecall =~ s/-\d+$//;
163 my $baseuser = DXUser->get($basecall);
164 if ($baseuser && $baseuser->lockout) {
165 my $lock = $user->lockout if $user;
166 if (!$user || !defined $lock || $lock) {
167 my $host = $conn->{peerhost} || "unknown";
168 Log('DXCommand', "$call on $host is locked out, disconnected");
175 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
177 $user = DXUser->new($call);
182 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
183 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
184 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
185 die "Invalid sort of user on $call = $sort" if !$dxchan;
187 # check that the conn has a callsign
188 $conn->conns($call) if $conn->isa('IntMsg');
191 $conn->set_error(sub {error_handler($dxchan)});
192 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
193 rec($dxchan, $conn, $msg);
198 my ($dxchan, $conn, $msg) = @_;
200 # queue the message and the channel object for later processing
202 my $self = bless {}, "inqueue";
203 $self->{dxchan} = $dxchan;
204 $self->{data} = $msg;
205 push @inqueue, $self;
211 return \&new_channel;
214 # cease running this program, close down all the connections nicely
220 $SIG{'TERM'} = 'IGNORE';
221 $SIG{'INT'} = 'IGNORE';
227 Local::finish(); # end local processing
229 dbg("Local::finish error $@") if $@;
232 foreach $dxchan (DXChannel->get_all_nodes) {
233 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
235 Msg->event_loop(100, 0.01);
238 foreach $dxchan (DXChannel->get_all_users) {
245 # end everything else
246 Msg->event_loop(100, 0.01);
250 # close all databases
253 # close all listeners
254 foreach my $l (@listeners) {
258 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
259 Log('cluster', "DXSpider V$version, build $build ended");
263 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
267 # the reaper of children
271 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
272 dbg("cpid: $cpid") if isdbg('reap');
273 # Msg->pid_gone($cpid);
274 $zombies-- if $zombies > 0;
276 dbg("cpid: $cpid") if isdbg('reap');
279 # this is where the input queue is dealt with and things are dispatched off to other parts of
284 my $self = shift @inqueue;
287 my $data = $self->{data};
288 my $dxchan = $self->{dxchan};
290 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
291 return unless defined $sort;
293 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
294 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
297 my $user = $dxchan->user;
298 if ($sort eq 'A' || $sort eq 'O') {
299 $dxchan->start($line, $sort);
300 } elsif ($sort eq 'I') {
301 die "\$user not defined for $call" if !defined $user;
303 $dxchan->normal($line);
304 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
305 } elsif ($sort eq 'Z') {
307 } elsif ($sort eq 'D') {
308 ; # ignored (an echo)
309 } elsif ($sort eq 'G') {
310 $dxchan->enhanced($line);
312 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
319 my $t = $systime - $starttime;
320 my $days = int $t / 86400;
322 my $hours = int $t / 3600;
324 my $mins = int $t / 60;
325 return sprintf "%d %02d:%02d", $days, $hours, $mins;
330 AGWMsg::init(\&new_channel);
333 #############################################################
335 # The start of the main line of code
337 #############################################################
339 $starttime = $systime = time;
340 $lang = 'en' unless $lang;
342 # open the debug file, set various FHs to be unbuffered
343 dbginit(\&DXCommandmode::broadcast_debug);
347 STDOUT->autoflush(1);
349 # calculate build number
350 $build += $main::version;
351 $build = "$build.$branch" if $branch;
353 Log('cluster', "DXSpider V$version, build $build started");
356 dbg("Copyright (c) 1998-2001 Dirk Koopman G1TLH");
357 dbg("DXSpider Version $version, build $build started");
360 dbg("loading prefixes ...");
364 dbg("loading band data ...");
367 # initialise User file system
368 dbg("loading user file system ...");
369 DXUser->init($userfn, 1);
371 # start listening for incoming messages/connects
372 dbg("starting listeners ...");
373 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
374 $conn->conns("Server $clusteraddr/$clusterport");
375 push @listeners, $conn;
376 dbg("Internal port: $clusteraddr $clusterport");
377 foreach my $l (@main::listen) {
378 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
379 $conn->conns("Server $l->[0]/$l->[1]");
380 push @listeners, $conn;
381 dbg("External Port: $l->[0] $l->[1]");
386 dbg("load badwords: " . (BadWords::load or "Ok"));
389 unless ($DB::VERSION) {
390 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
394 $SIG{HUP} = 'IGNORE';
395 $SIG{CHLD} = sub { $zombies++ };
397 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
398 $SIG{IO} = sub { dbg("SIGIO received"); };
399 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
400 $SIG{KILL} = 'DEFAULT'; # as if it matters....
402 # catch the rest with a hopeful message
405 # dbg("Catching SIG $_") if isdbg('chan');
406 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
414 # read in system messages
417 # read in command aliases
420 # initialise the Geomagnetic data engine
424 # initial the Spot stuff
427 # initialise the protocol engine
428 dbg("reading in duplicate spot and WWV info ...");
431 # put in a DXCluster node for us here so we can add users and take them away
432 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($DXProt::me->here)|Route::conf($DXProt::me->conf));
434 # make sure that there is a routing OUTPUT node default file
435 #unless (Filter::read_in('route', 'node_default', 0)) {
436 # my $dxcc = $DXProt::me->dxcc;
437 # $Route::filterdef->cmd($DXProt::me, 'route', 'accept', "node_default call $mycall" );
440 # read in any existing message headers and clean out old crap
441 dbg("reading existing message headers ...");
445 # read in any cron jobs
446 dbg("reading cron jobs ...");
449 # read in database descriptors
450 dbg("reading database descriptors ...");
453 # starting local stuff
454 dbg("doing local initialisation ...");
458 dbg("Local::init error $@") if $@;
460 dbg("cleaning out old debug files");
463 # print various flags
464 #dbg("seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
466 # this, such as it is, is the main loop!
467 dbg("orft we jolly well go ...");
468 my $script = new Script "startup";
469 $script->run($DXProt::me) if $script;
471 #open(DB::OUT, "|tee /tmp/aa");
476 Msg->event_loop(10, 0.010);
478 process_inqueue(); # read in lines from the input queue and despatch them
481 # do timed stuff, ongoing processing happens one a second
482 if ($timenow != $systime) {
485 DXCron::process(); # do cron jobs
486 DXCommandmode::process(); # process ongoing command mode stuff
487 DXProt::process(); # process ongoing ak1a pcxx stuff
488 DXConnect::process();
496 Local::process(); # do any localised processing
498 dbg("Local::process error $@") if $@;
501 last if --$decease <= 0;