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) {
352 open(CL, "$main::root/perl/${fn}.pm") or next;
354 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
362 Log('cluster', "DXSpider V$version, build $build started");
365 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
368 dbg('err', "loading prefixes ...");
372 dbg('err', "loading band data ...");
375 # initialise User file system
376 dbg('err', "loading user file system ...");
377 DXUser->init($userfn, 1);
379 # start listening for incoming messages/connects
380 dbg('err', "starting listeners ...");
381 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
382 $conn->conns("Server $clusteraddr/$clusterport");
383 push @listeners, $conn;
384 dbg('err', "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('err', "External Port: $l->[0] $l->[1]");
394 dbg('err', "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('err', "Broken PIPE signal received"); };
406 $SIG{IO} = sub { dbg('err', "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('chan', "Catching SIG $_");
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('err', "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 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
441 $routeroot = Route::Node->new($mycall, $version, Route::here($DXProt::me->here)|Route::conf($DXProt::me->confmode));
443 # read in any existing message headers and clean out old crap
444 dbg('err', "reading existing message headers ...");
448 # read in any cron jobs
449 dbg('err', "reading cron jobs ...");
452 # read in database descriptors
453 dbg('err', "reading database descriptors ...");
456 # starting local stuff
457 dbg('err', "doing local initialisation ...");
461 dbg('local', "Local::init error $@") if $@;
463 # print various flags
464 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
466 # this, such as it is, is the main loop!
467 dbg('err', "orft we jolly well go ...");
469 #open(DB::OUT, "|tee /tmp/aa");
474 Msg->event_loop(10, 0.010);
476 process_inqueue(); # read in lines from the input queue and despatch them
479 # do timed stuff, ongoing processing happens one a second
480 if ($timenow != $systime) {
483 DXCron::process(); # do cron jobs
484 DXCommandmode::process(); # process ongoing command mode stuff
485 DXProt::process(); # process ongoing ak1a pcxx stuff
486 DXConnect::process();
494 Local::process(); # do any localised processing
496 dbg('local', "Local::process error $@") if $@;
499 last if --$decease <= 0;