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?
76 use POSIX ":sys_wait_h";
83 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
84 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
85 $clusterport $mycall $decease $build $is_win
88 @inqueue = (); # the main input queue, an array of hashes
89 $systime = 0; # the time now (in seconds)
90 $version = "1.47"; # the version no of the software
91 $starttime = 0; # the starting time of the cluster
92 #@outstanding_connects = (); # list of outstanding connects
93 @listeners = (); # list of listeners
96 # send a message to call on conn and disconnect
99 my ($conn, $call, $mess) = @_;
101 dbg('chan', "-> D $call $mess\n");
102 $conn->send_now("D$call|$mess");
110 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
111 $dxchan->disconnect(1);
114 # handle incoming messages
117 my ($conn, $msg) = @_;
118 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
119 return unless defined $sort;
121 # set up the basic channel info
122 # is there one already connected to me - locally?
123 my $user = DXUser->get($call);
124 my $dxchan = DXChannel->get($call);
126 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
127 already_conn($conn, $call, $mess);
131 # is there one already connected elsewhere in the cluster?
133 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
136 if (my $ref = DXCluster->get_exact($call)) {
137 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
138 already_conn($conn, $call, $mess);
142 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
144 if (my $ref = DXCluster->get_exact($call)) {
145 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
146 already_conn($conn, $call, $mess);
149 $user = DXUser->new($call);
153 if ($user->lockout) {
154 Log('DXCommand', "$call is locked out, disconnected");
160 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
161 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
162 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
163 die "Invalid sort of user on $call = $sort" if !$dxchan;
165 # check that the conn has a callsign
166 $conn->conns($call) if $conn->isa('IntMsg');
169 $conn->set_error(sub {error_handler($dxchan)});
170 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
171 rec($dxchan, $conn, $msg);
176 my ($dxchan, $conn, $msg) = @_;
178 # queue the message and the channel object for later processing
180 my $self = bless {}, "inqueue";
181 $self->{dxchan} = $dxchan;
182 $self->{data} = $msg;
183 push @inqueue, $self;
189 return \&new_channel;
192 # cease running this program, close down all the connections nicely
197 $SIG{'TERM'} = 'IGNORE';
198 $SIG{'INT'} = 'IGNORE';
203 Local::finish(); # end local processing
205 dbg('local', "Local::finish error $@") if $@;
208 foreach $dxchan (DXChannel->get_all()) {
209 next unless $dxchan->is_node;
210 $dxchan->disconnect unless $dxchan == $DXProt::me;
212 Msg->event_loop(1, 0.05);
213 Msg->event_loop(1, 0.05);
216 foreach $dxchan (DXChannel->get_all()) {
217 next if $dxchan->is_node;
218 $dxchan->disconnect unless $dxchan == $DXProt::me;
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);
228 Msg->event_loop(1, 0.05);
229 Msg->event_loop(1, 0.05);
233 # close all databases
236 # close all listeners
241 dbg('chan', "DXSpider version $version, build $build ended");
242 Log('cluster', "DXSpider V$version, build $build ended");
246 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
250 # the reaper of children
254 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
255 dbg('reap', "cpid: $cpid");
256 # Msg->pid_gone($cpid);
257 $zombies-- if $zombies > 0;
259 dbg('reap', "cpid: $cpid");
262 # this is where the input queue is dealt with and things are dispatched off to other parts of
266 my $self = shift @inqueue;
269 my $data = $self->{data};
270 my $dxchan = $self->{dxchan};
272 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
273 return unless defined $sort;
275 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
276 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
279 my $user = $dxchan->user;
280 if ($sort eq 'A' || $sort eq 'O') {
281 $dxchan->start($line, $sort);
282 } elsif ($sort eq 'I') {
283 die "\$user not defined for $call" if !defined $user;
285 $dxchan->normal($line);
286 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
287 } elsif ($sort eq 'Z') {
289 } elsif ($sort eq 'D') {
290 ; # ignored (an echo)
292 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
298 my $t = $systime - $starttime;
299 my $days = int $t / 86400;
301 my $hours = int $t / 3600;
303 my $mins = int $t / 60;
304 return sprintf "%d %02d:%02d", $days, $hours, $mins;
309 AGWMsg::init(\&new_channel);
312 #############################################################
314 # The start of the main line of code
316 #############################################################
318 $starttime = $systime = time;
319 $lang = 'en' unless $lang;
321 # open the debug file, set various FHs to be unbuffered
326 STDOUT->autoflush(1);
328 # calculate build number
329 $build = $main::version;
331 if (opendir(DIR, "$main::root/perl")) {
332 my @d = readdir(DIR);
334 foreach my $fn (@d) {
335 if ($fn =~ /^cluster\.pl$/ || $fn =~ /\.pm$/) {
336 my $f = new IO::File "$main::root/perl/$fn" or next;
338 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
348 Log('cluster', "DXSpider V$version, build $build started");
351 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
354 dbg('err', "loading prefixes ...");
358 dbg('err', "loading band data ...");
361 # initialise User file system
362 dbg('err', "loading user file system ...");
363 DXUser->init($userfn, 1);
365 # start listening for incoming messages/connects
368 dbg('err', "starting listeners ...");
369 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
370 $conn->conns("Server $clusteraddr/$clusterport");
371 push @listeners, $conn;
372 dbg('err', "Internal port: $clusteraddr $clusterport");
373 for (@main::listen) {
374 $conn = ExtMsg->new_server($_->[0], $_->[1], \&login);
375 $conn->conns("Server $_->[0]/$_->[1]");
376 push @listeners, $conn;
377 dbg('err', "External Port: $_->[0] $_->[1]");
382 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
386 unless ($DB::VERSION) {
388 $SIG{TERM} = \&cease;
390 $SIG{HUP} = 'IGNORE';
391 $SIG{CHLD} = sub { $zombies++ };
393 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
394 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
395 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
396 $SIG{KILL} = 'DEFAULT'; # as if it matters....
398 # catch the rest with a hopeful message
401 # dbg('chan', "Catching SIG $_");
402 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
410 # read in system messages
413 # read in command aliases
416 # initialise the Geomagnetic data engine
420 # initial the Spot stuff
423 # initialise the protocol engine
424 dbg('err', "reading in duplicate spot and WWV info ...");
427 # put in a DXCluster node for us here so we can add users and take them away
428 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
430 # read in any existing message headers and clean out old crap
431 dbg('err', "reading existing message headers ...");
435 # read in any cron jobs
436 dbg('err', "reading cron jobs ...");
439 # read in database descriptors
440 dbg('err', "reading database descriptors ...");
443 # starting local stuff
444 dbg('err', "doing local initialisation ...");
448 dbg('local', "Local::init error $@") if $@;
450 # print various flags
451 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
453 # this, such as it is, is the main loop!
454 dbg('err', "orft we jolly well go ...");
456 #open(DB::OUT, "|tee /tmp/aa");
461 Msg->event_loop(10, 0.010);
463 process_inqueue(); # read in lines from the input queue and despatch them
466 # do timed stuff, ongoing processing happens one a second
467 if ($timenow != $systime) {
470 DXCron::process(); # do cron jobs
471 DXCommandmode::process(); # process ongoing command mode stuff
472 DXProt::process(); # process ongoing ak1a pcxx stuff
473 DXConnect::process();
481 Local::process(); # do any localised processing
483 dbg('local', "Local::process error $@") if $@;
486 last if --$decease <= 0;