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?
91 use POSIX ":sys_wait_h";
98 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
99 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
100 $clusterport $mycall $decease $is_win $routeroot
103 @inqueue = (); # the main input queue, an array of hashes
104 $systime = 0; # the time now (in seconds)
105 $version = "1.48"; # the version no of the software
106 $starttime = 0; # the starting time of the cluster
107 #@outstanding_connects = (); # list of outstanding connects
108 @listeners = (); # list of listeners
110 use vars qw($VERSION $BRANCH $build $branch);
111 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
112 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
113 $main::build += 15; # add an offset to make it bigger than last system
114 $main::build += $VERSION;
115 $main::branch += $BRANCH;
118 # send a message to call on conn and disconnect
121 my ($conn, $call, $mess) = @_;
123 $conn->disable_read(1);
124 dbg("-> D $call $mess\n") if isdbg('chan');
125 $conn->send_now("D$call|$mess");
133 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
134 $dxchan->disconnect(1);
137 # handle incoming messages
140 my ($conn, $msg) = @_;
141 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
142 return unless defined $sort;
144 # set up the basic channel info
145 # is there one already connected to me - locally?
146 my $user = DXUser->get($call);
147 my $dxchan = DXChannel->get($call);
149 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
150 already_conn($conn, $call, $mess);
155 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
157 $user = DXUser->new($call);
161 if ($user->lockout) {
162 my $host = $conn->{peerhost} || "unknown";
163 Log('DXCommand', "$call on $host is locked out, disconnected");
169 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
170 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
171 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
172 die "Invalid sort of user on $call = $sort" if !$dxchan;
174 # check that the conn has a callsign
175 $conn->conns($call) if $conn->isa('IntMsg');
178 $conn->set_error(sub {error_handler($dxchan)});
179 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
180 rec($dxchan, $conn, $msg);
185 my ($dxchan, $conn, $msg) = @_;
187 # queue the message and the channel object for later processing
189 my $self = bless {}, "inqueue";
190 $self->{dxchan} = $dxchan;
191 $self->{data} = $msg;
192 push @inqueue, $self;
198 return \&new_channel;
201 # cease running this program, close down all the connections nicely
207 $SIG{'TERM'} = 'IGNORE';
208 $SIG{'INT'} = 'IGNORE';
214 Local::finish(); # end local processing
216 dbg("Local::finish error $@") if $@;
219 foreach $dxchan (DXChannel->get_all_nodes) {
220 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
222 Msg->event_loop(100, 0.01);
225 foreach $dxchan (DXChannel->get_all_users) {
232 # end everything else
233 Msg->event_loop(100, 0.01);
237 # close all databases
240 # close all listeners
241 foreach my $l (@listeners) {
245 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
246 Log('cluster', "DXSpider V$version, build $build ended");
250 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
254 # the reaper of children
258 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
259 dbg("cpid: $cpid") if isdbg('reap');
260 # Msg->pid_gone($cpid);
261 $zombies-- if $zombies > 0;
263 dbg("cpid: $cpid") if isdbg('reap');
266 # this is where the input queue is dealt with and things are dispatched off to other parts of
271 my $self = shift @inqueue;
274 my $data = $self->{data};
275 my $dxchan = $self->{dxchan};
277 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
278 return unless defined $sort;
280 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
281 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
284 my $user = $dxchan->user;
285 if ($sort eq 'A' || $sort eq 'O') {
286 $dxchan->start($line, $sort);
287 } elsif ($sort eq 'I') {
288 die "\$user not defined for $call" if !defined $user;
290 $dxchan->normal($line);
291 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
292 } elsif ($sort eq 'Z') {
294 } elsif ($sort eq 'D') {
295 ; # ignored (an echo)
296 } elsif ($sort eq 'G') {
297 $dxchan->enhanced($line);
299 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
306 my $t = $systime - $starttime;
307 my $days = int $t / 86400;
309 my $hours = int $t / 3600;
311 my $mins = int $t / 60;
312 return sprintf "%d %02d:%02d", $days, $hours, $mins;
317 AGWMsg::init(\&new_channel);
320 #############################################################
322 # The start of the main line of code
324 #############################################################
326 $starttime = $systime = time;
327 $lang = 'en' unless $lang;
329 # open the debug file, set various FHs to be unbuffered
330 dbginit(\&DXCommandmode::broadcast_debug);
334 STDOUT->autoflush(1);
336 # calculate build number
337 $build += $main::version;
338 $build = "$build.$branch" if $branch;
340 Log('cluster', "DXSpider V$version, build $build started");
343 dbg("Copyright (c) 1998-2001 Dirk Koopman G1TLH");
344 dbg("DXSpider Version $version, build $build started");
347 dbg("loading prefixes ...");
351 dbg("loading band data ...");
354 # initialise User file system
355 dbg("loading user file system ...");
356 DXUser->init($userfn, 1);
358 # start listening for incoming messages/connects
359 dbg("starting listeners ...");
360 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
361 $conn->conns("Server $clusteraddr/$clusterport");
362 push @listeners, $conn;
363 dbg("Internal port: $clusteraddr $clusterport");
364 foreach my $l (@main::listen) {
365 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
366 $conn->conns("Server $l->[0]/$l->[1]");
367 push @listeners, $conn;
368 dbg("External Port: $l->[0] $l->[1]");
373 dbg("load badwords: " . (BadWords::load or "Ok"));
376 unless ($DB::VERSION) {
377 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
381 $SIG{HUP} = 'IGNORE';
382 $SIG{CHLD} = sub { $zombies++ };
384 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
385 $SIG{IO} = sub { dbg("SIGIO received"); };
386 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
387 $SIG{KILL} = 'DEFAULT'; # as if it matters....
389 # catch the rest with a hopeful message
392 # dbg("Catching SIG $_") if isdbg('chan');
393 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
401 # read in system messages
404 # read in command aliases
407 # initialise the Geomagnetic data engine
411 # initial the Spot stuff
414 # initialise the protocol engine
415 dbg("reading in duplicate spot and WWV info ...");
418 # put in a DXCluster node for us here so we can add users and take them away
419 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($DXProt::me->here)|Route::conf($DXProt::me->conf));
421 # make sure that there is a routing OUTPUT node default file
422 #unless (Filter::read_in('route', 'node_default', 0)) {
423 # my $dxcc = $DXProt::me->dxcc;
424 # $Route::filterdef->cmd($DXProt::me, 'route', 'accept', "node_default call $mycall" );
427 # read in any existing message headers and clean out old crap
428 dbg("reading existing message headers ...");
432 # read in any cron jobs
433 dbg("reading cron jobs ...");
436 # read in database descriptors
437 dbg("reading database descriptors ...");
440 # starting local stuff
441 dbg("doing local initialisation ...");
445 dbg("Local::init error $@") if $@;
447 dbg("cleaning out old debug files");
450 # print various flags
451 #dbg("seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
453 # this, such as it is, is the main loop!
454 dbg("orft we jolly well go ...");
456 #open(DB::OUT, "|tee /tmp/aa");
461 Msg->event_loop(10, 0.010);
463 process_inqueue(); # read in lines from the input queue and despatch them
466 # do timed stuff, ongoing processing happens one a second
467 if ($timenow != $systime) {
470 DXCron::process(); # do cron jobs
471 DXCommandmode::process(); # process ongoing command mode stuff
472 DXProt::process(); # process ongoing ak1a pcxx stuff
473 DXConnect::process();
481 Local::process(); # do any localised processing
483 dbg("Local::process error $@") if $@;
486 last if --$decease <= 0;