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?
88 use POSIX ":sys_wait_h";
95 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
96 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
97 $clusterport $mycall $decease $build $is_win
100 @inqueue = (); # the main input queue, an array of hashes
101 $systime = 0; # the time now (in seconds)
102 $version = "1.47"; # the version no of the software
103 $starttime = 0; # the starting time of the cluster
104 #@outstanding_connects = (); # list of outstanding connects
105 @listeners = (); # list of listeners
108 # send a message to call on conn and disconnect
111 my ($conn, $call, $mess) = @_;
113 dbg('chan', "-> D $call $mess\n");
114 $conn->send_now("D$call|$mess");
122 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
123 $dxchan->disconnect(1);
126 # handle incoming messages
129 my ($conn, $msg) = @_;
130 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
131 return unless defined $sort;
133 # set up the basic channel info
134 # is there one already connected to me - locally?
135 my $user = DXUser->get($call);
136 my $dxchan = DXChannel->get($call);
138 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
139 already_conn($conn, $call, $mess);
143 # is there one already connected elsewhere in the cluster?
145 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
148 if (my $ref = DXCluster->get_exact($call)) {
149 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
150 already_conn($conn, $call, $mess);
154 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
156 if (my $ref = DXCluster->get_exact($call)) {
157 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
158 already_conn($conn, $call, $mess);
161 $user = DXUser->new($call);
165 if ($user->lockout) {
166 Log('DXCommand', "$call is locked out, disconnected");
172 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
173 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
174 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
175 die "Invalid sort of user on $call = $sort" if !$dxchan;
177 # check that the conn has a callsign
178 $conn->conns($call) if $conn->isa('IntMsg');
181 $conn->set_error(sub {error_handler($dxchan)});
182 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
183 rec($dxchan, $conn, $msg);
188 my ($dxchan, $conn, $msg) = @_;
190 # queue the message and the channel object for later processing
192 my $self = bless {}, "inqueue";
193 $self->{dxchan} = $dxchan;
194 $self->{data} = $msg;
195 push @inqueue, $self;
201 return \&new_channel;
204 # cease running this program, close down all the connections nicely
209 $SIG{'TERM'} = 'IGNORE';
210 $SIG{'INT'} = 'IGNORE';
215 Local::finish(); # end local processing
217 dbg('local', "Local::finish error $@") if $@;
220 foreach $dxchan (DXChannel->get_all()) {
221 next unless $dxchan->is_node;
222 $dxchan->disconnect unless $dxchan == $DXProt::me;
224 Msg->event_loop(1, 0.05);
225 Msg->event_loop(1, 0.05);
228 foreach $dxchan (DXChannel->get_all()) {
229 next if $dxchan->is_node;
230 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
236 Msg->event_loop(1, 0.05);
237 Msg->event_loop(1, 0.05);
238 Msg->event_loop(1, 0.05);
239 Msg->event_loop(1, 0.05);
240 Msg->event_loop(1, 0.05);
241 Msg->event_loop(1, 0.05);
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"));
397 unless ($DB::VERSION) {
399 $SIG{TERM} = \&cease;
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);
441 # read in any existing message headers and clean out old crap
442 dbg('err', "reading existing message headers ...");
446 # read in any cron jobs
447 dbg('err', "reading cron jobs ...");
450 # read in database descriptors
451 dbg('err', "reading database descriptors ...");
454 # starting local stuff
455 dbg('err', "doing local initialisation ...");
459 dbg('local', "Local::init error $@") if $@;
461 # print various flags
462 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
464 # this, such as it is, is the main loop!
465 dbg('err', "orft we jolly well go ...");
467 #open(DB::OUT, "|tee /tmp/aa");
472 Msg->event_loop(10, 0.010);
474 process_inqueue(); # read in lines from the input queue and despatch them
477 # do timed stuff, ongoing processing happens one a second
478 if ($timenow != $systime) {
481 DXCron::process(); # do cron jobs
482 DXCommandmode::process(); # process ongoing command mode stuff
483 DXProt::process(); # process ongoing ak1a pcxx stuff
484 DXConnect::process();
492 Local::process(); # do any localised processing
494 dbg('local', "Local::process error $@") if $@;
497 last if --$decease <= 0;