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 my $lockfn = "$root/perl/cluster.lock"; # 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) $!";
73 use POSIX ":sys_wait_h";
80 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
81 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
82 $clusterport $mycall $decease $build
85 @inqueue = (); # the main input queue, an array of hashes
86 $systime = 0; # the time now (in seconds)
87 $version = "1.47"; # the version no of the software
88 $starttime = 0; # the starting time of the cluster
89 $lockfn = "cluster.lock"; # lock file name
90 #@outstanding_connects = (); # list of outstanding connects
91 @listeners = (); # list of listeners
94 # send a message to call on conn and disconnect
97 my ($conn, $call, $mess) = @_;
99 dbg('chan', "-> D $call $mess\n");
100 $conn->send_now("D$call|$mess");
108 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
109 $dxchan->disconnect(1);
112 # handle incoming messages
115 my ($conn, $msg) = @_;
116 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
117 return unless defined $sort;
119 # set up the basic channel info
120 # is there one already connected to me - locally?
121 my $user = DXUser->get($call);
122 my $dxchan = DXChannel->get($call);
124 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
125 already_conn($conn, $call, $mess);
129 # is there one already connected elsewhere in the cluster?
131 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
134 if (my $ref = DXCluster->get_exact($call)) {
135 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
136 already_conn($conn, $call, $mess);
140 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
142 if (my $ref = DXCluster->get_exact($call)) {
143 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
144 already_conn($conn, $call, $mess);
147 $user = DXUser->new($call);
151 if ($user->lockout) {
152 Log('DXCommand', "$call is locked out, disconnected");
158 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
159 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
160 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
161 die "Invalid sort of user on $call = $sort" if !$dxchan;
163 # check that the conn has a callsign
164 $conn->conns($call) if $conn->isa('IntMsg');
167 $conn->set_error(sub {error_handler($dxchan)});
168 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
169 rec($dxchan, $conn, $msg);
174 my ($dxchan, $conn, $msg) = @_;
176 # queue the message and the channel object for later processing
178 my $self = bless {}, "inqueue";
179 $self->{dxchan} = $dxchan;
180 $self->{data} = $msg;
181 push @inqueue, $self;
187 return \&new_channel;
190 # cease running this program, close down all the connections nicely
195 $SIG{'TERM'} = 'IGNORE';
196 $SIG{'INT'} = 'IGNORE';
201 Local::finish(); # end local processing
203 dbg('local', "Local::finish error $@") if $@;
206 foreach $dxchan (DXChannel->get_all()) {
207 next unless $dxchan->is_node;
208 $dxchan->disconnect unless $dxchan == $DXProt::me;
210 Msg->event_loop(1, 0.05);
211 Msg->event_loop(1, 0.05);
214 foreach $dxchan (DXChannel->get_all()) {
215 next if $dxchan->is_node;
216 $dxchan->disconnect unless $dxchan == $DXProt::me;
222 Msg->event_loop(1, 0.05);
223 Msg->event_loop(1, 0.05);
224 Msg->event_loop(1, 0.05);
225 Msg->event_loop(1, 0.05);
226 Msg->event_loop(1, 0.05);
227 Msg->event_loop(1, 0.05);
231 # close all databases
234 # close all listeners
239 dbg('chan', "DXSpider version $version, build $build ended");
240 Log('cluster', "DXSpider V$version, build $build ended");
244 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
248 # the reaper of children
252 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
253 dbg('reap', "cpid: $cpid");
254 # Msg->pid_gone($cpid);
255 $zombies-- if $zombies > 0;
257 dbg('reap', "cpid: $cpid");
260 # this is where the input queue is dealt with and things are dispatched off to other parts of
264 my $self = shift @inqueue;
267 my $data = $self->{data};
268 my $dxchan = $self->{dxchan};
270 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
271 return unless defined $sort;
273 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
274 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
277 my $user = $dxchan->user;
278 if ($sort eq 'A' || $sort eq 'O') {
279 $dxchan->start($line, $sort);
280 } elsif ($sort eq 'I') {
281 die "\$user not defined for $call" if !defined $user;
283 $dxchan->normal($line);
284 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
285 } elsif ($sort eq 'Z') {
287 } elsif ($sort eq 'D') {
288 ; # ignored (an echo)
290 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
296 my $t = $systime - $starttime;
297 my $days = int $t / 86400;
299 my $hours = int $t / 3600;
301 my $mins = int $t / 60;
302 return sprintf "%d %02d:%02d", $days, $hours, $mins;
304 #############################################################
306 # The start of the main line of code
308 #############################################################
310 $starttime = $systime = time;
311 $lang = 'en' unless $lang;
313 # open the debug file, set various FHs to be unbuffered
318 STDOUT->autoflush(1);
320 # calculate build number
321 $build = $main::version;
323 if (opendir(DIR, "$main::root/perl")) {
324 my @d = readdir(DIR);
326 foreach my $fn (@d) {
327 if ($fn =~ /^cluster\.pl$/ || $fn =~ /\.pm$/) {
328 my $f = new IO::File "$main::root/perl/$fn" or next;
330 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
340 Log('cluster', "DXSpider V$version, build $build started");
343 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
346 dbg('err', "loading prefixes ...");
350 dbg('err', "loading band data ...");
353 # initialise User file system
354 dbg('err', "loading user file system ...");
355 DXUser->init($userfn, 1);
357 # start listening for incoming messages/connects
360 dbg('err', "starting listeners ...");
361 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
362 $conn->conns("Server $clusteraddr/$clusterport");
363 push @listeners, $conn;
364 dbg('err', "Internal port: $clusteraddr $clusterport");
365 for (@main::listen) {
366 $conn = ExtMsg->new_server($_->[0], $_->[1], \&login);
367 $conn->conns("Server $_->[0]/$_->[1]");
368 push @listeners, $conn;
369 dbg('err', "External Port: $_->[0] $_->[1]");
371 AGWMsg::init(\&new_channel);
374 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
377 unless ($^O =~ /^MS/) {
378 unless ($DB::VERSION) {
380 $SIG{TERM} = \&cease;
382 $SIG{HUP} = 'IGNORE';
383 $SIG{CHLD} = sub { $zombies++ };
385 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
386 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
387 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
388 $SIG{KILL} = 'DEFAULT'; # as if it matters....
390 # catch the rest with a hopeful message
393 # dbg('chan', "Catching SIG $_");
394 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
402 # read in system messages
405 # read in command aliases
408 # initialise the Geomagnetic data engine
412 # initial the Spot stuff
415 # initialise the protocol engine
416 dbg('err', "reading in duplicate spot and WWV info ...");
419 # put in a DXCluster node for us here so we can add users and take them away
420 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
422 # read in any existing message headers and clean out old crap
423 dbg('err', "reading existing message headers ...");
427 # read in any cron jobs
428 dbg('err', "reading cron jobs ...");
431 # read in database descriptors
432 dbg('err', "reading database descriptors ...");
435 # starting local stuff
436 dbg('err', "doing local initialisation ...");
440 dbg('local', "Local::init error $@") if $@;
442 # print various flags
443 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
445 # this, such as it is, is the main loop!
446 dbg('err', "orft we jolly well go ...");
448 #open(DB::OUT, "|tee /tmp/aa");
453 Msg->event_loop(10, 0.010);
455 process_inqueue(); # read in lines from the input queue and despatch them
458 # do timed stuff, ongoing processing happens one a second
459 if ($timenow != $systime) {
462 DXCron::process(); # do cron jobs
463 DXCommandmode::process(); # process ongoing command mode stuff
464 DXProt::process(); # process ongoing ak1a pcxx stuff
465 DXConnect::process();
473 Local::process(); # do any localised processing
475 dbg('local', "Local::process error $@") if $@;
478 last if --$decease <= 0;