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?
93 use POSIX ":sys_wait_h";
100 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
101 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
102 $clusterport $mycall $decease $is_win $routeroot $me
105 @inqueue = (); # the main input queue, an array of hashes
106 $systime = 0; # the time now (in seconds)
107 $version = "1.48"; # the version no of the software
108 $starttime = 0; # the starting time of the cluster
109 #@outstanding_connects = (); # list of outstanding connects
110 @listeners = (); # list of listeners
112 use vars qw($VERSION $BRANCH $build $branch);
113 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
114 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
115 $main::build += 13; # add an offset to make it bigger than last system
116 $main::build += $VERSION;
117 $main::branch += $BRANCH;
120 # send a message to call on conn and disconnect
123 my ($conn, $call, $mess) = @_;
125 $conn->disable_read(1);
126 dbg("-> D $call $mess\n") if isdbg('chan');
127 $conn->send_now("D$call|$mess");
135 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
136 $dxchan->disconnect(1);
139 # handle incoming messages
142 my ($conn, $msg) = @_;
143 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
144 return unless defined $sort;
146 unless (is_callsign($call)) {
147 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
151 # set up the basic channel info
152 # is there one already connected to me - locally?
153 my $user = DXUser->get($call);
154 my $dxchan = DXChannel->get($call);
156 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
157 already_conn($conn, $call, $mess);
162 my $basecall = $call;
163 $basecall =~ s/-\d+$//;
164 my $baseuser = DXUser->get($basecall);
165 my $lock = $user->lockout if $user;
166 if ($baseuser && $baseuser->lockout || $lock) {
167 if (!$user || !defined $lock || $lock) {
168 my $host = $conn->{peerhost} || "unknown";
169 Log('DXCommand', "$call on $host is locked out, disconnected");
176 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
178 $user = DXUser->new($call);
183 if ($user->is_spider) {
184 $dxchan = QXProt->new($call, $conn, $user);
185 } elsif ($user->is_node) {
186 $dxchan = DXProt->new($call, $conn, $user);
187 } elsif ($user->is_user) {
188 $dxchan = DXCommandmode->new($call, $conn, $user);
189 } elsif ($user->is_bbs) {
190 $dxchan = BBS->new($call, $conn, $user);
192 die "Invalid sort of user on $call = $sort";
195 # check that the conn has a callsign
196 $conn->conns($call) if $conn->isa('IntMsg');
199 $conn->set_error(sub {error_handler($dxchan)});
200 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
201 rec($dxchan, $conn, $msg);
206 my ($dxchan, $conn, $msg) = @_;
208 # queue the message and the channel object for later processing
210 my $self = bless {}, "inqueue";
211 $self->{dxchan} = $dxchan;
212 $self->{data} = $msg;
213 push @inqueue, $self;
219 return \&new_channel;
222 # cease running this program, close down all the connections nicely
228 $SIG{'TERM'} = 'IGNORE';
229 $SIG{'INT'} = 'IGNORE';
235 Local::finish(); # end local processing
237 dbg("Local::finish error $@") if $@;
240 foreach $dxchan (DXChannel->get_all_nodes) {
241 $dxchan->disconnect(2) unless $dxchan == $main::me;
243 Msg->event_loop(100, 0.01);
246 foreach $dxchan (DXChannel->get_all_users) {
253 # end everything else
254 Msg->event_loop(100, 0.01);
258 # close all databases
261 # close all listeners
262 foreach my $l (@listeners) {
266 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
267 Log('cluster', "DXSpider V$version, build $build ended");
271 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
275 # the reaper of children
279 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
280 dbg("cpid: $cpid") if isdbg('reap');
281 # Msg->pid_gone($cpid);
282 $zombies-- if $zombies > 0;
284 dbg("cpid: $cpid") if isdbg('reap');
287 # this is where the input queue is dealt with and things are dispatched off to other parts of
292 my $self = shift @inqueue;
295 my $data = $self->{data};
296 my $dxchan = $self->{dxchan};
298 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
299 return unless defined $sort;
301 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
302 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
305 my $user = $dxchan->user;
306 if ($sort eq 'A' || $sort eq 'O') {
307 $dxchan->start($line, $sort);
308 } elsif ($sort eq 'I') {
309 die "\$user not defined for $call" if !defined $user;
311 $dxchan->normal($line);
312 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
313 } elsif ($sort eq 'Z') {
315 } elsif ($sort eq 'D') {
316 ; # ignored (an echo)
317 } elsif ($sort eq 'G') {
318 $dxchan->enhanced($line);
320 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
327 my $t = $systime - $starttime;
328 my $days = int $t / 86400;
330 my $hours = int $t / 3600;
332 my $mins = int $t / 60;
333 return sprintf "%d %02d:%02d", $days, $hours, $mins;
338 AGWMsg::init(\&new_channel);
341 #############################################################
343 # The start of the main line of code
345 #############################################################
347 $starttime = $systime = time;
348 $lang = 'en' unless $lang;
350 # open the debug file, set various FHs to be unbuffered
351 dbginit(\&DXCommandmode::broadcast_debug);
355 STDOUT->autoflush(1);
357 # calculate build number
358 $build += $main::version;
359 $build = "$build.$branch" if $branch;
361 Log('cluster', "DXSpider V$version, build $build started");
364 dbg("Copyright (c) 1998-2001 Dirk Koopman G1TLH");
365 dbg("DXSpider Version $version, build $build started");
368 dbg("loading prefixes ...");
372 dbg("loading band data ...");
375 # initialise User file system
376 dbg("loading user file system ...");
377 DXUser->init($userfn, 1);
379 # start listening for incoming messages/connects
380 dbg("starting listeners ...");
381 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
382 $conn->conns("Server $clusteraddr/$clusterport");
383 push @listeners, $conn;
384 dbg("Internal port: $clusteraddr $clusterport");
385 foreach my $l (@main::listen) {
386 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
387 $conn->conns("Server $l->[0]/$l->[1]");
388 push @listeners, $conn;
389 dbg("External Port: $l->[0] $l->[1]");
394 dbg("load badwords: " . (BadWords::load or "Ok"));
397 unless ($DB::VERSION) {
398 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
402 $SIG{HUP} = 'IGNORE';
403 $SIG{CHLD} = sub { $zombies++ };
405 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
406 $SIG{IO} = sub { dbg("SIGIO received"); };
407 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
408 $SIG{KILL} = 'DEFAULT'; # as if it matters....
410 # catch the rest with a hopeful message
413 # dbg("Catching SIG $_") if isdbg('chan');
414 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
422 # read in system messages
425 # read in command aliases
428 # initialise the Geomagnetic data engine
432 # initial the Spot stuff
435 # initialise the protocol engine
436 dbg("reading in duplicate spot and WWV info ...");
439 # put in a DXCluster node for us here so we can add users and take them away
440 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
442 # make sure that there is a routing OUTPUT node default file
443 #unless (Filter::read_in('route', 'node_default', 0)) {
444 # my $dxcc = $main::me->dxcc;
445 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
448 # read in any existing message headers and clean out old crap
449 dbg("reading existing message headers ...");
453 # read in any cron jobs
454 dbg("reading cron jobs ...");
457 # read in database descriptors
458 dbg("reading database descriptors ...");
461 # starting local stuff
462 dbg("doing local initialisation ...");
466 dbg("Local::init error $@") if $@;
468 dbg("cleaning out old debug files");
471 # print various flags
472 #dbg("seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
474 # this, such as it is, is the main loop!
475 dbg("orft we jolly well go ...");
476 my $script = new Script "startup";
477 $script->run($main::me) if $script;
479 #open(DB::OUT, "|tee /tmp/aa");
484 Msg->event_loop(10, 0.010);
486 process_inqueue(); # read in lines from the input queue and despatch them
489 # do timed stuff, ongoing processing happens one a second
490 if ($timenow != $systime) {
493 DXCron::process(); # do cron jobs
494 DXCommandmode::process(); # process ongoing command mode stuff
495 DXProt::process(); # process ongoing ak1a pcxx stuff
497 DXConnect::process();
505 Local::process(); # do any localised processing
507 dbg("Local::process error $@") if $@;
510 last if --$decease <= 0;