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 # set up the basic channel info
146 # is there one already connected to me - locally?
147 my $user = DXUser->get($call);
148 my $dxchan = DXChannel->get($call);
150 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
151 already_conn($conn, $call, $mess);
156 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
158 $user = DXUser->new($call);
162 if ($user->lockout) {
163 my $host = $conn->{peerhost} || "unknown";
164 Log('DXCommand', "$call on $host is locked out, disconnected");
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;
175 # check that the conn has a callsign
176 $conn->conns($call) if $conn->isa('IntMsg');
179 $conn->set_error(sub {error_handler($dxchan)});
180 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
181 rec($dxchan, $conn, $msg);
186 my ($dxchan, $conn, $msg) = @_;
188 # queue the message and the channel object for later processing
190 my $self = bless {}, "inqueue";
191 $self->{dxchan} = $dxchan;
192 $self->{data} = $msg;
193 push @inqueue, $self;
199 return \&new_channel;
202 # cease running this program, close down all the connections nicely
208 $SIG{'TERM'} = 'IGNORE';
209 $SIG{'INT'} = 'IGNORE';
215 Local::finish(); # end local processing
217 dbg("Local::finish error $@") if $@;
220 foreach $dxchan (DXChannel->get_all_nodes) {
221 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
223 Msg->event_loop(100, 0.01);
226 foreach $dxchan (DXChannel->get_all_users) {
233 # end everything else
234 Msg->event_loop(100, 0.01);
238 # close all databases
241 # close all listeners
242 foreach my $l (@listeners) {
246 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
247 Log('cluster', "DXSpider V$version, build $build ended");
251 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
255 # the reaper of children
259 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
260 dbg("cpid: $cpid") if isdbg('reap');
261 # Msg->pid_gone($cpid);
262 $zombies-- if $zombies > 0;
264 dbg("cpid: $cpid") if isdbg('reap');
267 # this is where the input queue is dealt with and things are dispatched off to other parts of
272 my $self = shift @inqueue;
275 my $data = $self->{data};
276 my $dxchan = $self->{dxchan};
278 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
279 return unless defined $sort;
281 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
282 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
285 my $user = $dxchan->user;
286 if ($sort eq 'A' || $sort eq 'O') {
287 $dxchan->start($line, $sort);
288 } elsif ($sort eq 'I') {
289 die "\$user not defined for $call" if !defined $user;
291 $dxchan->normal($line);
292 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
293 } elsif ($sort eq 'Z') {
295 } elsif ($sort eq 'D') {
296 ; # ignored (an echo)
297 } elsif ($sort eq 'G') {
298 $dxchan->enhanced($line);
300 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
307 my $t = $systime - $starttime;
308 my $days = int $t / 86400;
310 my $hours = int $t / 3600;
312 my $mins = int $t / 60;
313 return sprintf "%d %02d:%02d", $days, $hours, $mins;
318 AGWMsg::init(\&new_channel);
321 #############################################################
323 # The start of the main line of code
325 #############################################################
327 $starttime = $systime = time;
328 $lang = 'en' unless $lang;
330 # open the debug file, set various FHs to be unbuffered
331 dbginit(\&DXCommandmode::broadcast_debug);
335 STDOUT->autoflush(1);
337 # calculate build number
338 $build += $main::version;
339 $build = "$build.$branch" if $branch;
341 Log('cluster', "DXSpider V$version, build $build started");
344 dbg("Copyright (c) 1998-2001 Dirk Koopman G1TLH");
345 dbg("DXSpider Version $version, build $build started");
348 dbg("loading prefixes ...");
352 dbg("loading band data ...");
355 # initialise User file system
356 dbg("loading user file system ...");
357 DXUser->init($userfn, 1);
359 # start listening for incoming messages/connects
360 dbg("starting listeners ...");
361 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
362 $conn->conns("Server $clusteraddr/$clusterport");
363 push @listeners, $conn;
364 dbg("Internal port: $clusteraddr $clusterport");
365 foreach my $l (@main::listen) {
366 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
367 $conn->conns("Server $l->[0]/$l->[1]");
368 push @listeners, $conn;
369 dbg("External Port: $l->[0] $l->[1]");
374 dbg("load badwords: " . (BadWords::load or "Ok"));
377 unless ($DB::VERSION) {
378 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
382 $SIG{HUP} = 'IGNORE';
383 $SIG{CHLD} = sub { $zombies++ };
385 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
386 $SIG{IO} = sub { dbg("SIGIO received"); };
387 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
388 $SIG{KILL} = 'DEFAULT'; # as if it matters....
390 # catch the rest with a hopeful message
393 # dbg("Catching SIG $_") if isdbg('chan');
394 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
402 # read in system messages
405 # read in command aliases
408 # initialise the Geomagnetic data engine
412 # initial the Spot stuff
415 # initialise the protocol engine
416 dbg("reading in duplicate spot and WWV info ...");
419 # put in a DXCluster node for us here so we can add users and take them away
420 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($DXProt::me->here)|Route::conf($DXProt::me->conf));
422 # make sure that there is a routing OUTPUT node default file
423 #unless (Filter::read_in('route', 'node_default', 0)) {
424 # my $dxcc = $DXProt::me->dxcc;
425 # $Route::filterdef->cmd($DXProt::me, 'route', 'accept', "node_default call $mycall" );
428 # read in any existing message headers and clean out old crap
429 dbg("reading existing message headers ...");
433 # read in any cron jobs
434 dbg("reading cron jobs ...");
437 # read in database descriptors
438 dbg("reading database descriptors ...");
441 # starting local stuff
442 dbg("doing local initialisation ...");
446 dbg("Local::init error $@") if $@;
448 dbg("cleaning out old debug files");
451 # print various flags
452 #dbg("seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
454 # this, such as it is, is the main loop!
455 dbg("orft we jolly well go ...");
456 my $script = new Script "startup";
457 $script->run($DXProt::me) if $script;
459 #open(DB::OUT, "|tee /tmp/aa");
464 Msg->event_loop(10, 0.010);
466 process_inqueue(); # read in lines from the input queue and despatch them
469 # do timed stuff, ongoing processing happens one a second
470 if ($timenow != $systime) {
473 DXCron::process(); # do cron jobs
474 DXCommandmode::process(); # process ongoing command mode stuff
475 DXProt::process(); # process ongoing ak1a pcxx stuff
476 DXConnect::process();
484 Local::process(); # do any localised processing
486 dbg("Local::process error $@") if $@;
489 last if --$decease <= 0;