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?
89 use POSIX ":sys_wait_h";
96 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
97 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
98 $clusterport $mycall $decease $build $is_win
101 @inqueue = (); # the main input queue, an array of hashes
102 $systime = 0; # the time now (in seconds)
103 $version = "1.48"; # the version no of the software
104 $starttime = 0; # the starting time of the cluster
105 #@outstanding_connects = (); # list of outstanding connects
106 @listeners = (); # list of listeners
109 # send a message to call on conn and disconnect
112 my ($conn, $call, $mess) = @_;
114 $conn->disable_read(1);
115 dbg('chan', "-> D $call $mess\n");
116 $conn->send_now("D$call|$mess");
124 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
125 $dxchan->disconnect(1);
128 # handle incoming messages
131 my ($conn, $msg) = @_;
132 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
133 return unless defined $sort;
135 # set up the basic channel info
136 # is there one already connected to me - locally?
137 my $user = DXUser->get($call);
138 my $dxchan = DXChannel->get($call);
140 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
141 already_conn($conn, $call, $mess);
145 # is there one already connected elsewhere in the cluster?
147 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
150 if (my $ref = DXCluster->get_exact($call)) {
151 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->dxchancall);
152 already_conn($conn, $call, $mess);
156 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
158 if (my $ref = DXCluster->get_exact($call)) {
159 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->dxchancall);
160 already_conn($conn, $call, $mess);
163 $user = DXUser->new($call);
167 if ($user->lockout) {
168 Log('DXCommand', "$call is locked out, disconnected");
174 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
175 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
176 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
177 die "Invalid sort of user on $call = $sort" if !$dxchan;
179 # check that the conn has a callsign
180 $conn->conns($call) if $conn->isa('IntMsg');
183 $conn->set_error(sub {error_handler($dxchan)});
184 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
185 rec($dxchan, $conn, $msg);
190 my ($dxchan, $conn, $msg) = @_;
192 # queue the message and the channel object for later processing
194 my $self = bless {}, "inqueue";
195 $self->{dxchan} = $dxchan;
196 $self->{data} = $msg;
197 push @inqueue, $self;
203 return \&new_channel;
206 # cease running this program, close down all the connections nicely
212 $SIG{'TERM'} = 'IGNORE';
213 $SIG{'INT'} = 'IGNORE';
219 Local::finish(); # end local processing
221 dbg('local', "Local::finish error $@") if $@;
224 foreach $dxchan (DXChannel->get_all_nodes) {
225 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
227 Msg->event_loop(100, 0.01);
230 foreach $dxchan (DXChannel->get_all_users) {
237 # end everything else
238 Msg->event_loop(100, 0.01);
242 # close all databases
245 # close all listeners
246 foreach my $l (@listeners) {
250 dbg('chan', "DXSpider version $version, build $build ended");
251 Log('cluster', "DXSpider V$version, build $build ended");
255 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
259 # the reaper of children
263 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
264 dbg('reap', "cpid: $cpid");
265 # Msg->pid_gone($cpid);
266 $zombies-- if $zombies > 0;
268 dbg('reap', "cpid: $cpid");
271 # this is where the input queue is dealt with and things are dispatched off to other parts of
275 my $self = shift @inqueue;
278 my $data = $self->{data};
279 my $dxchan = $self->{dxchan};
281 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
282 return unless defined $sort;
284 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
285 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
288 my $user = $dxchan->user;
289 if ($sort eq 'A' || $sort eq 'O') {
290 $dxchan->start($line, $sort);
291 } elsif ($sort eq 'I') {
292 die "\$user not defined for $call" if !defined $user;
294 $dxchan->normal($line);
295 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
296 } elsif ($sort eq 'Z') {
298 } elsif ($sort eq 'D') {
299 ; # ignored (an echo)
301 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
307 my $t = $systime - $starttime;
308 my $days = int $t / 86400;
310 my $hours = int $t / 3600;
312 my $mins = int $t / 60;
313 return sprintf "%d %02d:%02d", $days, $hours, $mins;
318 AGWMsg::init(\&new_channel);
321 #############################################################
323 # The start of the main line of code
325 #############################################################
327 $starttime = $systime = time;
328 $lang = 'en' unless $lang;
330 # open the debug file, set various FHs to be unbuffered
335 STDOUT->autoflush(1);
337 # calculate build number
338 $build = $main::version;
341 open(CL, "$main::root/perl/cluster.pl") or die "Cannot open cluster.pl $!";
343 next unless /^use\s+([\w:_]+)/;
347 foreach my $fn (@fn) {
348 open(CL, "$main::root/perl/${fn}.pm") or next;
350 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
358 Log('cluster', "DXSpider V$version, build $build started");
361 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
364 dbg('err', "loading prefixes ...");
368 dbg('err', "loading band data ...");
371 # initialise User file system
372 dbg('err', "loading user file system ...");
373 DXUser->init($userfn, 1);
375 # start listening for incoming messages/connects
376 dbg('err', "starting listeners ...");
377 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
378 $conn->conns("Server $clusteraddr/$clusterport");
379 push @listeners, $conn;
380 dbg('err', "Internal port: $clusteraddr $clusterport");
381 foreach my $l (@main::listen) {
382 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
383 $conn->conns("Server $l->[0]/$l->[1]");
384 push @listeners, $conn;
385 dbg('err', "External Port: $l->[0] $l->[1]");
390 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
393 unless ($DB::VERSION) {
394 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
398 $SIG{HUP} = 'IGNORE';
399 $SIG{CHLD} = sub { $zombies++ };
401 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
402 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
403 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
404 $SIG{KILL} = 'DEFAULT'; # as if it matters....
406 # catch the rest with a hopeful message
409 # dbg('chan', "Catching SIG $_");
410 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
418 # read in system messages
421 # read in command aliases
424 # initialise the Geomagnetic data engine
428 # initial the Spot stuff
431 # initialise the protocol engine
432 dbg('err', "reading in duplicate spot and WWV info ...");
435 # put in a DXCluster node for us here so we can add users and take them away
436 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
438 # read in any existing message headers and clean out old crap
439 dbg('err', "reading existing message headers ...");
443 # read in any cron jobs
444 dbg('err', "reading cron jobs ...");
447 # read in database descriptors
448 dbg('err', "reading database descriptors ...");
451 # starting local stuff
452 dbg('err', "doing local initialisation ...");
456 dbg('local', "Local::init error $@") if $@;
458 # print various flags
459 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
461 # this, such as it is, is the main loop!
462 dbg('err', "orft we jolly well go ...");
464 #open(DB::OUT, "|tee /tmp/aa");
469 Msg->event_loop(10, 0.010);
471 process_inqueue(); # read in lines from the input queue and despatch them
474 # do timed stuff, ongoing processing happens one a second
475 if ($timenow != $systime) {
478 DXCron::process(); # do cron jobs
479 DXCommandmode::process(); # process ongoing command mode stuff
480 DXProt::process(); # process ongoing ak1a pcxx stuff
481 DXConnect::process();
489 Local::process(); # do any localised processing
491 dbg('local', "Local::process error $@") if $@;
494 last if --$decease <= 0;