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 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
163 $user = DXUser->new($call);
167 if ($user->lockout) {
168 my $host = $conn->{peerhost} || "unknown";
169 Log('DXCommand', "$call on $host is locked out, disconnected");
175 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
176 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
177 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
178 die "Invalid sort of user on $call = $sort" if !$dxchan;
180 # check that the conn has a callsign
181 $conn->conns($call) if $conn->isa('IntMsg');
184 $conn->set_error(sub {error_handler($dxchan)});
185 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
186 rec($dxchan, $conn, $msg);
191 my ($dxchan, $conn, $msg) = @_;
193 # queue the message and the channel object for later processing
195 my $self = bless {}, "inqueue";
196 $self->{dxchan} = $dxchan;
197 $self->{data} = $msg;
198 push @inqueue, $self;
204 return \&new_channel;
207 # cease running this program, close down all the connections nicely
213 $SIG{'TERM'} = 'IGNORE';
214 $SIG{'INT'} = 'IGNORE';
220 Local::finish(); # end local processing
222 dbg("Local::finish error $@") if $@;
225 foreach $dxchan (DXChannel->get_all_nodes) {
226 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
228 Msg->event_loop(100, 0.01);
231 foreach $dxchan (DXChannel->get_all_users) {
238 # end everything else
239 Msg->event_loop(100, 0.01);
243 # close all databases
246 # close all listeners
247 foreach my $l (@listeners) {
251 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
252 Log('cluster', "DXSpider V$version, build $build ended");
256 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
260 # the reaper of children
264 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
265 dbg("cpid: $cpid") if isdbg('reap');
266 # Msg->pid_gone($cpid);
267 $zombies-- if $zombies > 0;
269 dbg("cpid: $cpid") if isdbg('reap');
272 # this is where the input queue is dealt with and things are dispatched off to other parts of
277 my $self = shift @inqueue;
280 my $data = $self->{data};
281 my $dxchan = $self->{dxchan};
283 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
284 return unless defined $sort;
286 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
287 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
290 my $user = $dxchan->user;
291 if ($sort eq 'A' || $sort eq 'O') {
292 $dxchan->start($line, $sort);
293 } elsif ($sort eq 'I') {
294 die "\$user not defined for $call" if !defined $user;
296 $dxchan->normal($line);
297 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
298 } elsif ($sort eq 'Z') {
300 } elsif ($sort eq 'D') {
301 ; # ignored (an echo)
302 } elsif ($sort eq 'G') {
303 $dxchan->enhanced($line);
305 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
312 my $t = $systime - $starttime;
313 my $days = int $t / 86400;
315 my $hours = int $t / 3600;
317 my $mins = int $t / 60;
318 return sprintf "%d %02d:%02d", $days, $hours, $mins;
323 AGWMsg::init(\&new_channel);
326 #############################################################
328 # The start of the main line of code
330 #############################################################
332 $starttime = $systime = time;
333 $lang = 'en' unless $lang;
335 # open the debug file, set various FHs to be unbuffered
336 dbginit(\&DXCommandmode::broadcast_debug);
340 STDOUT->autoflush(1);
342 # calculate build number
343 $build += $main::version;
344 $build = "$build.$branch" if $branch;
346 Log('cluster', "DXSpider V$version, build $build started");
349 dbg("Copyright (c) 1998-2001 Dirk Koopman G1TLH");
350 dbg("DXSpider Version $version, build $build started");
353 dbg("loading prefixes ...");
357 dbg("loading band data ...");
360 # initialise User file system
361 dbg("loading user file system ...");
362 DXUser->init($userfn, 1);
364 # start listening for incoming messages/connects
365 dbg("starting listeners ...");
366 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
367 $conn->conns("Server $clusteraddr/$clusterport");
368 push @listeners, $conn;
369 dbg("Internal port: $clusteraddr $clusterport");
370 foreach my $l (@main::listen) {
371 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
372 $conn->conns("Server $l->[0]/$l->[1]");
373 push @listeners, $conn;
374 dbg("External Port: $l->[0] $l->[1]");
379 dbg("load badwords: " . (BadWords::load or "Ok"));
382 unless ($DB::VERSION) {
383 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
387 $SIG{HUP} = 'IGNORE';
388 $SIG{CHLD} = sub { $zombies++ };
390 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
391 $SIG{IO} = sub { dbg("SIGIO received"); };
392 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
393 $SIG{KILL} = 'DEFAULT'; # as if it matters....
395 # catch the rest with a hopeful message
398 # dbg("Catching SIG $_") if isdbg('chan');
399 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
407 # read in system messages
410 # read in command aliases
413 # initialise the Geomagnetic data engine
417 # initial the Spot stuff
420 # initialise the protocol engine
421 dbg("reading in duplicate spot and WWV info ...");
424 # put in a DXCluster node for us here so we can add users and take them away
425 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($DXProt::me->here)|Route::conf($DXProt::me->conf));
427 # make sure that there is a routing OUTPUT node default file
428 #unless (Filter::read_in('route', 'node_default', 0)) {
429 # my $dxcc = $DXProt::me->dxcc;
430 # $Route::filterdef->cmd($DXProt::me, 'route', 'accept', "node_default call $mycall" );
433 # read in any existing message headers and clean out old crap
434 dbg("reading existing message headers ...");
438 # read in any cron jobs
439 dbg("reading cron jobs ...");
442 # read in database descriptors
443 dbg("reading database descriptors ...");
446 # starting local stuff
447 dbg("doing local initialisation ...");
451 dbg("Local::init error $@") if $@;
453 dbg("cleaning out old debug files");
456 # print various flags
457 #dbg("seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
459 # this, such as it is, is the main loop!
460 dbg("orft we jolly well go ...");
461 my $script = new Script "startup";
462 $script->run($DXProt::me) if $script;
464 #open(DB::OUT, "|tee /tmp/aa");
469 Msg->event_loop(10, 0.010);
471 process_inqueue(); # read in lines from the input queue and despatch them
474 # do timed stuff, ongoing processing happens one a second
475 if ($timenow != $systime) {
478 DXCron::process(); # do cron jobs
479 DXCommandmode::process(); # process ongoing command mode stuff
480 DXProt::process(); # process ongoing ak1a pcxx stuff
481 DXConnect::process();
489 Local::process(); # do any localised processing
491 dbg("Local::process error $@") if $@;
494 last if --$decease <= 0;