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) $!";
71 use POSIX ":sys_wait_h";
78 #use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects $zombies $root
79 # $lang $myalias @debug $userfn $clusteraddr $clusterport $mycall $decease );
81 @inqueue = (); # the main input queue, an array of hashes
82 $systime = 0; # the time now (in seconds)
83 $version = "1.47"; # the version no of the software
84 $starttime = 0; # the starting time of the cluster
85 $lockfn = "cluster.lock"; # lock file name
86 @outstanding_connects = (); # list of outstanding connects
87 @listeners = (); # list of listeners
90 # send a message to call on conn and disconnect
93 my ($conn, $call, $mess) = @_;
95 dbg('chan', "-> D $call $mess\n");
96 $conn->send_now("D$call|$mess");
98 dbg('chan', "-> Z $call bye\n");
99 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
104 # handle incoming messages
107 my ($conn, $msg, $err) = @_;
108 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
110 if (!defined $msg || (defined $err && $err)) {
115 $dxchan->conn(undef);
124 # set up the basic channel info - this needs a bit more thought - there is duplication here
125 if (!defined $dxchan) {
126 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
127 return unless defined $sort;
129 # is there one already connected to me - locally?
130 my $user = DXUser->get($call);
131 if (DXChannel->get($call)) {
132 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
133 already_conn($conn, $call, $mess);
137 # is there one already connected elsewhere in the cluster?
139 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
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);
148 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
150 if (my $ref = DXCluster->get_exact($call)) {
151 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
152 already_conn($conn, $call, $mess);
155 $user = DXUser->new($call);
159 if ($user->lockout) {
160 Log('DXCommand', "$call is locked out, disconnected");
161 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
167 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
168 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
169 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
170 die "Invalid sort of user on $call = $sort" if !$dxchan;
173 # queue the message and the channel object for later processing
175 my $self = bless {}, "inqueue";
176 $self->{dxchan} = $dxchan;
177 $self->{data} = $msg;
178 push @inqueue, $self;
187 # cease running this program, close down all the connections nicely
192 $SIG{'TERM'} = 'IGNORE';
193 $SIG{'INT'} = 'IGNORE';
198 Local::finish(); # end local processing
200 dbg('local', "Local::finish error $@") if $@;
203 foreach $dxchan (DXChannel->get_all()) {
204 next unless $dxchan->is_node;
205 $dxchan->disconnect unless $dxchan == $DXProt::me;
207 Msg->event_loop(1, 0.05);
208 Msg->event_loop(1, 0.05);
211 foreach $dxchan (DXChannel->get_all()) {
212 next if $dxchan->is_node;
213 $dxchan->disconnect unless $dxchan == $DXProt::me;
215 Msg->event_loop(1, 0.05);
216 Msg->event_loop(1, 0.05);
217 Msg->event_loop(1, 0.05);
218 Msg->event_loop(1, 0.05);
219 Msg->event_loop(1, 0.05);
220 Msg->event_loop(1, 0.05);
224 # close all databases
227 # close all listeners
232 dbg('chan', "DXSpider version $version ended");
233 Log('cluster', "DXSpider V$version stopped");
237 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
241 # the reaper of children
245 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
246 dbg('reap', "cpid: $cpid");
247 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
248 $zombies-- if $zombies > 0;
250 dbg('reap', "cpid: $cpid");
253 # this is where the input queue is dealt with and things are dispatched off to other parts of
257 my $self = shift @inqueue;
260 my $data = $self->{data};
261 my $dxchan = $self->{dxchan};
263 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
264 return unless defined $sort;
266 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
267 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
270 my $user = $dxchan->user;
271 if ($sort eq 'A' || $sort eq 'O') {
272 $dxchan->start($line, $sort);
273 } elsif ($sort eq 'I') {
274 die "\$user not defined for $call" if !defined $user;
276 $dxchan->normal($line);
277 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
278 } elsif ($sort eq 'Z') {
279 $dxchan->conn(undef);
281 } elsif ($sort eq 'D') {
282 ; # ignored (an echo)
284 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
290 my $t = $systime - $starttime;
291 my $days = int $t / 86400;
293 my $hours = int $t / 3600;
295 my $mins = int $t / 60;
296 return sprintf "%d %02d:%02d", $days, $hours, $mins;
298 #############################################################
300 # The start of the main line of code
302 #############################################################
304 $starttime = $systime = time;
305 $lang = 'en' unless $lang;
307 # open the debug file, set various FHs to be unbuffered
312 STDOUT->autoflush(1);
314 Log('cluster', "DXSpider V$version started");
317 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
320 dbg('err', "loading prefixes ...");
324 dbg('err', "loading band data ...");
327 # initialise User file system
328 dbg('err', "loading user file system ...");
329 DXUser->init($userfn, 1);
331 # start listening for incoming messages/connects
334 dbg('err', "starting listeners ...");
335 push @listeners, IntMsg->new_server("$clusteraddr", $clusterport, \&login);
336 dbg('err', "Internal port: $clusteraddr $clusterport");
337 for (@main::listen) {
338 push @listeners, ExtMsg->new_server($_->[0], $_->[1], \&login);
339 dbg('err', "External Port: $_->[0] $_->[1]");
343 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
346 unless ($^O =~ /^MS/) {
347 unless ($DB::VERSION) {
349 $SIG{TERM} = \&cease;
351 $SIG{HUP} = 'IGNORE';
352 $SIG{CHLD} = sub { $zombies++ };
354 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
355 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
356 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
357 $SIG{KILL} = 'DEFAULT'; # as if it matters....
359 # catch the rest with a hopeful message
362 # dbg('chan', "Catching SIG $_");
363 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
371 # read in system messages
374 # read in command aliases
377 # initialise the Geomagnetic data engine
381 # initial the Spot stuff
384 # initialise the protocol engine
385 dbg('err', "reading in duplicate spot and WWV info ...");
388 # put in a DXCluster node for us here so we can add users and take them away
389 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
391 # read in any existing message headers and clean out old crap
392 dbg('err', "reading existing message headers ...");
396 # read in any cron jobs
397 dbg('err', "reading cron jobs ...");
400 # read in database descriptors
401 dbg('err', "reading database descriptors ...");
404 # starting local stuff
405 dbg('err', "doing local initialisation ...");
409 dbg('local', "Local::init error $@") if $@;
411 # print various flags
412 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
414 # this, such as it is, is the main loop!
415 dbg('err', "orft we jolly well go ...");
417 #open(DB::OUT, "|tee /tmp/aa");
422 Msg->event_loop(1, 0.1);
424 process_inqueue(); # read in lines from the input queue and despatch them
427 # do timed stuff, ongoing processing happens one a second
428 if ($timenow != $systime) {
431 DXCron::process(); # do cron jobs
432 DXCommandmode::process(); # process ongoing command mode stuff
433 DXProt::process(); # process ongoing ak1a pcxx stuff
434 DXConnect::process();
441 Local::process(); # do any localised processing
443 dbg('local', "Local::process error $@") if $@;
446 last if --$decease <= 0;