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 $build $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
112 # send a message to call on conn and disconnect
115 my ($conn, $call, $mess) = @_;
117 $conn->disable_read(1);
118 dbg('chan', "-> D $call $mess\n");
119 $conn->send_now("D$call|$mess");
127 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
128 $dxchan->disconnect(1);
131 # handle incoming messages
134 my ($conn, $msg) = @_;
135 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
136 return unless defined $sort;
138 # set up the basic channel info
139 # is there one already connected to me - locally?
140 my $user = DXUser->get($call);
141 my $dxchan = DXChannel->get($call);
143 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
144 already_conn($conn, $call, $mess);
148 # is there one already connected elsewhere in the cluster?
150 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
153 if (my $ref = DXCluster->get_exact($call)) {
154 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->dxchancall);
155 already_conn($conn, $call, $mess);
159 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
161 if (my $ref = DXCluster->get_exact($call)) {
162 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->dxchancall);
163 already_conn($conn, $call, $mess);
166 $user = DXUser->new($call);
170 if ($user->lockout) {
171 Log('DXCommand', "$call is locked out, disconnected");
177 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
178 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
179 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
180 die "Invalid sort of user on $call = $sort" if !$dxchan;
182 # check that the conn has a callsign
183 $conn->conns($call) if $conn->isa('IntMsg');
186 $conn->set_error(sub {error_handler($dxchan)});
187 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
188 rec($dxchan, $conn, $msg);
193 my ($dxchan, $conn, $msg) = @_;
195 # queue the message and the channel object for later processing
197 my $self = bless {}, "inqueue";
198 $self->{dxchan} = $dxchan;
199 $self->{data} = $msg;
200 push @inqueue, $self;
206 return \&new_channel;
209 # cease running this program, close down all the connections nicely
215 $SIG{'TERM'} = 'IGNORE';
216 $SIG{'INT'} = 'IGNORE';
222 Local::finish(); # end local processing
224 dbg('local', "Local::finish error $@") if $@;
227 foreach $dxchan (DXChannel->get_all_nodes) {
228 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
230 Msg->event_loop(100, 0.01);
233 foreach $dxchan (DXChannel->get_all_users) {
240 # end everything else
241 Msg->event_loop(100, 0.01);
245 # close all databases
248 # close all listeners
249 foreach my $l (@listeners) {
253 dbg('chan', "DXSpider version $version, build $build ended");
254 Log('cluster', "DXSpider V$version, build $build ended");
258 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
262 # the reaper of children
266 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
267 dbg('reap', "cpid: $cpid");
268 # Msg->pid_gone($cpid);
269 $zombies-- if $zombies > 0;
271 dbg('reap', "cpid: $cpid");
274 # this is where the input queue is dealt with and things are dispatched off to other parts of
278 my $self = shift @inqueue;
281 my $data = $self->{data};
282 my $dxchan = $self->{dxchan};
284 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
285 return unless defined $sort;
287 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
288 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
291 my $user = $dxchan->user;
292 if ($sort eq 'A' || $sort eq 'O') {
293 $dxchan->start($line, $sort);
294 } elsif ($sort eq 'I') {
295 die "\$user not defined for $call" if !defined $user;
297 $dxchan->normal($line);
298 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
299 } elsif ($sort eq 'Z') {
301 } elsif ($sort eq 'D') {
302 ; # ignored (an echo)
304 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
310 my $t = $systime - $starttime;
311 my $days = int $t / 86400;
313 my $hours = int $t / 3600;
315 my $mins = int $t / 60;
316 return sprintf "%d %02d:%02d", $days, $hours, $mins;
321 AGWMsg::init(\&new_channel);
324 #############################################################
326 # The start of the main line of code
328 #############################################################
330 $starttime = $systime = time;
331 $lang = 'en' unless $lang;
333 # open the debug file, set various FHs to be unbuffered
338 STDOUT->autoflush(1);
340 # calculate build number
341 $build = $main::version;
344 open(CL, "$main::root/perl/cluster.pl") or die "Cannot open cluster.pl $!";
346 next unless /^use\s+([\w:_]+)/;
350 foreach my $fn (@fn) {
351 open(CL, "$main::root/perl/${fn}.pm") or next;
353 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
361 Log('cluster', "DXSpider V$version, build $build started");
364 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
367 dbg('err', "loading prefixes ...");
371 dbg('err', "loading band data ...");
374 # initialise User file system
375 dbg('err', "loading user file system ...");
376 DXUser->init($userfn, 1);
378 # start listening for incoming messages/connects
379 dbg('err', "starting listeners ...");
380 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
381 $conn->conns("Server $clusteraddr/$clusterport");
382 push @listeners, $conn;
383 dbg('err', "Internal port: $clusteraddr $clusterport");
384 foreach my $l (@main::listen) {
385 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
386 $conn->conns("Server $l->[0]/$l->[1]");
387 push @listeners, $conn;
388 dbg('err', "External Port: $l->[0] $l->[1]");
393 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
396 unless ($DB::VERSION) {
397 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
401 $SIG{HUP} = 'IGNORE';
402 $SIG{CHLD} = sub { $zombies++ };
404 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
405 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
406 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
407 $SIG{KILL} = 'DEFAULT'; # as if it matters....
409 # catch the rest with a hopeful message
412 # dbg('chan', "Catching SIG $_");
413 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
421 # read in system messages
424 # read in command aliases
427 # initialise the Geomagnetic data engine
431 # initial the Spot stuff
434 # initialise the protocol engine
435 dbg('err', "reading in duplicate spot and WWV info ...");
438 # put in a DXCluster node for us here so we can add users and take them away
439 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
440 $routeroot = Route::Node->new($mycall, $version, Route::here($DXProt::me->here)|Route::conf($DXProt::me->confmode));
442 # read in any existing message headers and clean out old crap
443 dbg('err', "reading existing message headers ...");
447 # read in any cron jobs
448 dbg('err', "reading cron jobs ...");
451 # read in database descriptors
452 dbg('err', "reading database descriptors ...");
455 # starting local stuff
456 dbg('err', "doing local initialisation ...");
460 dbg('local', "Local::init error $@") if $@;
462 # print various flags
463 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
465 # this, such as it is, is the main loop!
466 dbg('err', "orft we jolly well go ...");
468 #open(DB::OUT, "|tee /tmp/aa");
473 Msg->event_loop(10, 0.010);
475 process_inqueue(); # read in lines from the input queue and despatch them
478 # do timed stuff, ongoing processing happens one a second
479 if ($timenow != $systime) {
482 DXCron::process(); # do cron jobs
483 DXCommandmode::process(); # process ongoing command mode stuff
484 DXProt::process(); # process ongoing ak1a pcxx stuff
485 DXConnect::process();
493 Local::process(); # do any localised processing
495 dbg('local', "Local::process error $@") if $@;
498 last if --$decease <= 0;