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 # do some validation of the input
27 die "The directory $root doesn't exist, please RTFM" unless -d $root;
28 die "$root/local doesn't exist, please RTFM" unless -d "$root/local";
29 die "$root/local/DXVars.pm doesn't exist, please RTFM" unless -e "$root/local/DXVars.pm";
31 mkdir "$root/local_cmd", 0777 unless -d "$root/local_cmd";
34 # try to create and lock a lockfile (this isn't atomic but
36 $lockfn = "$root/perl/cluster.lck"; # lock file name
38 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
41 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
44 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
48 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
105 use POSIX ":sys_wait_h";
112 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
113 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
114 $clusterport $mycall $decease $is_win $routeroot $me $reqreg
117 @inqueue = (); # the main input queue, an array of hashes
118 $systime = 0; # the time now (in seconds)
119 $version = "1.51"; # the version no of the software
120 $starttime = 0; # the starting time of the cluster
121 #@outstanding_connects = (); # list of outstanding connects
122 @listeners = (); # list of listeners
123 $reqreg = 0; # 1 = registration required, 2 = deregister people
125 use vars qw($VERSION $BRANCH $build $branch);
126 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
127 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
128 $main::build += 6; # add an offset to make it bigger than last system
129 $main::build += $VERSION;
130 $main::branch += $BRANCH;
133 # send a message to call on conn and disconnect
136 my ($conn, $call, $mess) = @_;
138 $conn->disable_read(1);
139 dbg("-> D $call $mess\n") if isdbg('chan');
140 $conn->send_now("D$call|$mess");
148 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
149 $dxchan->disconnect(1);
152 # handle incoming messages
155 my ($conn, $msg) = @_;
156 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
157 return unless defined $sort;
159 unless (is_callsign($call)) {
160 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
164 # set up the basic channel info
165 # is there one already connected to me - locally?
166 my $user = DXUser->get_current($call);
167 my $dxchan = DXChannel->get($call);
169 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
170 already_conn($conn, $call, $mess);
175 my $basecall = $call;
176 $basecall =~ s/-\d+$//;
177 my $baseuser = DXUser->get_current($basecall);
178 my $lock = $user->lockout if $user;
179 if ($baseuser && $baseuser->lockout || $lock) {
180 if (!$user || !defined $lock || $lock) {
181 my $host = $conn->{peerhost} || "unknown";
182 Log('DXCommand', "$call on $host is locked out, disconnected");
189 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
191 $user = DXUser->new($call);
197 if ($user->passphrase && $main::me->user->passphrase) {
198 $dxchan = QXProt->new($call, $conn, $user);
200 unless ($user->passphrase) {
201 Log('DXCommand', "$call using NP but has no passphrase");
202 dbg("$call using NP but has no passphrase");
204 unless ($main::me->user->passphrase) {
205 Log('DXCommand', "$main::mycall using NP but has no passphrase");
206 dbg("$main::mycall using NP but has no passphrase");
208 already_conn($conn, $call, "Need to exchange passphrases");
211 } elsif ($user->is_node) {
212 $dxchan = DXProt->new($call, $conn, $user);
213 } elsif ($user->is_user) {
214 $dxchan = DXCommandmode->new($call, $conn, $user);
215 } elsif ($user->is_bbs) {
216 $dxchan = BBS->new($call, $conn, $user);
218 die "Invalid sort of user on $call = $sort";
221 # check that the conn has a callsign
222 $conn->conns($call) if $conn->isa('IntMsg');
225 $conn->set_error(sub {error_handler($dxchan)});
226 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
227 rec($dxchan, $conn, $msg);
232 my ($dxchan, $conn, $msg) = @_;
234 # queue the message and the channel object for later processing
236 my $self = bless {}, "inqueue";
237 $self->{dxchan} = $dxchan;
238 $self->{data} = $msg;
239 push @inqueue, $self;
243 # remove any outstanding entries on the inqueue after a disconnection (usually)
247 @inqueue = grep {$_->{dxchan} != $dxchan} @inqueue;
252 return \&new_channel;
255 # cease running this program, close down all the connections nicely
261 $SIG{'TERM'} = 'IGNORE';
262 $SIG{'INT'} = 'IGNORE';
268 Local::finish(); # end local processing
270 dbg("Local::finish error $@") if $@;
273 foreach $dxchan (DXChannel->get_all_nodes) {
274 $dxchan->disconnect(2) unless $dxchan == $main::me;
276 Msg->event_loop(100, 0.01);
279 foreach $dxchan (DXChannel->get_all_users) {
286 # disconnect UDP customers
289 # end everything else
290 Msg->event_loop(100, 0.01);
294 # close all databases
297 # close all listeners
298 foreach my $l (@listeners) {
302 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
303 Log('cluster', "DXSpider V$version, build $build ended");
307 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
311 # the reaper of children
315 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
316 dbg("cpid: $cpid") if isdbg('reap');
317 # Msg->pid_gone($cpid);
318 $zombies-- if $zombies > 0;
320 dbg("cpid: $cpid") if isdbg('reap');
323 # this is where the input queue is dealt with and things are dispatched off to other parts of
328 my $self = shift @inqueue;
331 my $data = $self->{data};
332 my $dxchan = $self->{dxchan};
334 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
335 return unless defined $sort;
337 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
338 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
339 if ($self->{disconnecting}) {
340 dbg('In disconnection, ignored');
345 my $user = $dxchan->user;
346 if ($sort eq 'A' || $sort eq 'O') {
347 $dxchan->start($line, $sort);
348 } elsif ($sort eq 'I') {
349 die "\$user not defined for $call" if !defined $user;
352 $dxchan->normal($line);
353 } elsif ($sort eq 'Z') {
355 } elsif ($sort eq 'D') {
356 ; # ignored (an echo)
357 } elsif ($sort eq 'G') {
358 $dxchan->enhanced($line);
360 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
367 my $t = $systime - $starttime;
368 my $days = int $t / 86400;
370 my $hours = int $t / 3600;
372 my $mins = int $t / 60;
373 return sprintf "%d %02d:%02d", $days, $hours, $mins;
378 AGWMsg::init(\&new_channel);
381 #############################################################
383 # The start of the main line of code
385 #############################################################
387 $starttime = $systime = time;
388 $lang = 'en' unless $lang;
390 # open the debug file, set various FHs to be unbuffered
391 dbginit(\&DXCommandmode::broadcast_debug);
395 STDOUT->autoflush(1);
397 # calculate build number
398 $build += $main::version;
399 $build = "$build.$branch" if $branch;
401 Log('cluster', "DXSpider V$version, build $build started");
404 dbg("Copyright (c) 1998-2002 Dirk Koopman G1TLH");
405 dbg("DXSpider Version $version, build $build started");
408 dbg("loading prefixes ...");
409 my $r = Prefix::init();
414 dbg("loading band data ...");
417 # initialise User file system
418 dbg("loading user file system ...");
419 DXUser->init($userfn, 1);
421 # look for the sysop and the alias user and complain if they aren't there
423 my $ref = DXUser->get($mycall);
424 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
425 $ref = DXUser->get($myalias);
426 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
429 # start listening for incoming messages/connects
430 dbg("starting listeners ...");
431 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
432 $conn->conns("Server $clusteraddr/$clusterport");
433 push @listeners, $conn;
434 dbg("Internal port: $clusteraddr $clusterport");
435 foreach my $l (@main::listen) {
436 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
437 $conn->conns("Server $l->[0]/$l->[1]");
438 push @listeners, $conn;
439 dbg("External Port: $l->[0] $l->[1]");
442 dbg("AGW Listener") if $AGWMsg::enable;
445 dbg("UDP Listener") if $UDPMsg::enable;
446 UDPMsg::init(\&new_channel);
449 dbg("load badwords: " . (BadWords::load or "Ok"));
452 unless ($DB::VERSION) {
453 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
457 $SIG{HUP} = 'IGNORE';
458 $SIG{CHLD} = sub { $zombies++ };
460 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
461 $SIG{IO} = sub { dbg("SIGIO received"); };
462 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
463 $SIG{KILL} = 'DEFAULT'; # as if it matters....
465 # catch the rest with a hopeful message
468 # dbg("Catching SIG $_") if isdbg('chan');
469 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
475 dbg("Starting Dupe system");
478 # read in system messages
479 dbg("Read in Messages");
482 # read in command aliases
483 dbg("Read in Aliases");
486 # initialise the Geomagnetic data engine
492 # initial the Spot stuff
493 dbg("Starting DX Spot system");
496 # initialise the protocol engine
497 dbg("Start Protocol Engines ...");
501 # put in a DXCluster node for us here so we can add users and take them away
502 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
504 # make sure that there is a routing OUTPUT node default file
505 #unless (Filter::read_in('route', 'node_default', 0)) {
506 # my $dxcc = $main::me->dxcc;
507 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
510 # read in any existing message headers and clean out old crap
511 dbg("reading existing message headers ...");
515 # read in any cron jobs
516 dbg("reading cron jobs ...");
519 # read in database descriptors
520 dbg("reading database descriptors ...");
523 # starting local stuff
524 dbg("doing local initialisation ...");
528 dbg("Local::init error $@") if $@;
530 # this, such as it is, is the main loop!
531 dbg("orft we jolly well go ...");
532 my $script = new Script "startup";
533 $script->run($main::me) if $script;
535 #open(DB::OUT, "|tee /tmp/aa");
540 Msg->event_loop(10, 0.010);
542 process_inqueue(); # read in lines from the input queue and despatch them
545 # do timed stuff, ongoing processing happens one a second
546 if ($timenow != $systime) {
549 DXCron::process(); # do cron jobs
550 DXCommandmode::process(); # process ongoing command mode stuff
551 DXProt::process(); # process ongoing ak1a pcxx stuff
553 DXConnect::process();
561 Local::process(); # do any localised processing
563 dbg("Local::process error $@") if $@;
566 last if --$decease <= 0;