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_nodes) {
221 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
223 Msg->event_loop(100, 0.01);
226 foreach $dxchan (DXChannel->get_all_users) {
233 # end everything else
234 Msg->event_loop(100, 0.01);
238 # close all databases
241 # close all listeners
242 foreach my $l (@listeners) {
246 dbg('chan', "DXSpider version $version, build $build ended");
247 Log('cluster', "DXSpider V$version, build $build ended");
251 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
255 # the reaper of children
259 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
260 dbg('reap', "cpid: $cpid");
261 # Msg->pid_gone($cpid);
262 $zombies-- if $zombies > 0;
264 dbg('reap', "cpid: $cpid");
267 # 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('chan', "<- $sort $call $line\n") unless $sort eq 'D';
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)
297 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
303 my $t = $systime - $starttime;
304 my $days = int $t / 86400;
306 my $hours = int $t / 3600;
308 my $mins = int $t / 60;
309 return sprintf "%d %02d:%02d", $days, $hours, $mins;
314 AGWMsg::init(\&new_channel);
317 #############################################################
319 # The start of the main line of code
321 #############################################################
323 $starttime = $systime = time;
324 $lang = 'en' unless $lang;
326 # open the debug file, set various FHs to be unbuffered
331 STDOUT->autoflush(1);
333 # calculate build number
334 $build = $main::version;
337 open(CL, "$main::root/perl/cluster.pl") or die "Cannot open cluster.pl $!";
339 next unless /^use\s+([\w:_]+)/;
343 foreach my $fn (@fn) {
344 open(CL, "$main::root/perl/${fn}.pm") or next;
346 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
354 Log('cluster', "DXSpider V$version, build $build started");
357 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
360 dbg('err', "loading prefixes ...");
364 dbg('err', "loading band data ...");
367 # initialise User file system
368 dbg('err', "loading user file system ...");
369 DXUser->init($userfn, 1);
371 # start listening for incoming messages/connects
372 dbg('err', "starting listeners ...");
373 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
374 $conn->conns("Server $clusteraddr/$clusterport");
375 push @listeners, $conn;
376 dbg('err', "Internal port: $clusteraddr $clusterport");
377 foreach my $l (@main::listen) {
378 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
379 $conn->conns("Server $l->[0]/$l->[1]");
380 push @listeners, $conn;
381 dbg('err', "External Port: $l->[0] $l->[1]");
386 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
390 unless ($DB::VERSION) {
392 $SIG{TERM} = \&cease;
394 $SIG{HUP} = 'IGNORE';
395 $SIG{CHLD} = sub { $zombies++ };
397 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
398 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
399 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
400 $SIG{KILL} = 'DEFAULT'; # as if it matters....
402 # catch the rest with a hopeful message
405 # dbg('chan', "Catching SIG $_");
406 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
414 # read in system messages
417 # read in command aliases
420 # initialise the Geomagnetic data engine
424 # initial the Spot stuff
427 # initialise the protocol engine
428 dbg('err', "reading in duplicate spot and WWV info ...");
431 # put in a DXCluster node for us here so we can add users and take them away
432 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
434 # read in any existing message headers and clean out old crap
435 dbg('err', "reading existing message headers ...");
439 # read in any cron jobs
440 dbg('err', "reading cron jobs ...");
443 # read in database descriptors
444 dbg('err', "reading database descriptors ...");
447 # starting local stuff
448 dbg('err', "doing local initialisation ...");
452 dbg('local', "Local::init error $@") if $@;
454 # print various flags
455 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
457 # this, such as it is, is the main loop!
458 dbg('err', "orft we jolly well go ...");
460 #open(DB::OUT, "|tee /tmp/aa");
465 Msg->event_loop(10, 0.010);
467 process_inqueue(); # read in lines from the input queue and despatch them
470 # do timed stuff, ongoing processing happens one a second
471 if ($timenow != $systime) {
474 DXCron::process(); # do cron jobs
475 DXCommandmode::process(); # process ongoing command mode stuff
476 DXProt::process(); # process ongoing ak1a pcxx stuff
477 DXConnect::process();
485 Local::process(); # do any localised processing
487 dbg('local', "Local::process error $@") if $@;
490 last if --$decease <= 0;