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);
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 (DXCluster->get_exact($call)) {
143 my $mess = DXM::msg($lang, $user->is_node ? 'concluster' : 'conother', $call);
144 already_conn($conn, $call, $mess);
148 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
150 if (DXCluster->get_exact($call)) {
151 my $mess = DXM::msg($lang, 'conother', $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
166 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
167 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
168 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
169 die "Invalid sort of user on $call = $sort" if !$dxchan;
172 # queue the message and the channel object for later processing
174 my $self = bless {}, "inqueue";
175 $self->{dxchan} = $dxchan;
176 $self->{data} = $msg;
177 push @inqueue, $self;
186 # cease running this program, close down all the connections nicely
191 $SIG{'TERM'} = 'IGNORE';
192 $SIG{'INT'} = 'IGNORE';
197 Local::finish(); # end local processing
199 dbg('local', "Local::finish error $@") if $@;
202 foreach $dxchan (DXChannel->get_all()) {
203 next unless $dxchan->is_node;
204 $dxchan->disconnect unless $dxchan == $DXProt::me;
206 Msg->event_loop(1, 0.05);
207 Msg->event_loop(1, 0.05);
210 foreach $dxchan (DXChannel->get_all()) {
211 next if $dxchan->is_node;
212 $dxchan->disconnect unless $dxchan == $DXProt::me;
214 Msg->event_loop(1, 0.05);
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);
223 # close all databases
226 # close all listeners
231 dbg('chan', "DXSpider version $version ended");
232 Log('cluster', "DXSpider V$version stopped");
236 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
240 # the reaper of children
244 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
245 dbg('reap', "cpid: $cpid");
246 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
247 $zombies-- if $zombies > 0;
249 dbg('reap', "cpid: $cpid");
252 # this is where the input queue is dealt with and things are dispatched off to other parts of
256 my $self = shift @inqueue;
259 my $data = $self->{data};
260 my $dxchan = $self->{dxchan};
262 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
263 return unless defined $sort;
265 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
266 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
269 my $user = $dxchan->user;
270 if ($sort eq 'A' || $sort eq 'O') {
271 $dxchan->start($line, $sort);
272 } elsif ($sort eq 'I') {
273 die "\$user not defined for $call" if !defined $user;
275 $dxchan->normal($line);
276 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
277 } elsif ($sort eq 'Z') {
278 $dxchan->conn(undef);
280 } elsif ($sort eq 'D') {
281 ; # ignored (an echo)
283 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
289 my $t = $systime - $starttime;
290 my $days = int $t / 86400;
292 my $hours = int $t / 3600;
294 my $mins = int $t / 60;
295 return sprintf "%d %02d:%02d", $days, $hours, $mins;
297 #############################################################
299 # The start of the main line of code
301 #############################################################
303 $starttime = $systime = time;
304 $lang = 'en' unless $lang;
306 # open the debug file, set various FHs to be unbuffered
311 STDOUT->autoflush(1);
313 Log('cluster', "DXSpider V$version started");
316 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
319 dbg('err', "loading prefixes ...");
323 dbg('err', "loading band data ...");
326 # initialise User file system
327 dbg('err', "loading user file system ...");
328 DXUser->init($userfn, 1);
330 # start listening for incoming messages/connects
333 dbg('err', "starting listeners ...");
334 push @listeners, IntMsg->new_server("$clusteraddr", $clusterport, \&login);
335 dbg('err', "Internal port: $clusteraddr $clusterport");
336 for (@main::listen) {
337 push @listeners, ExtMsg->new_server($_->[0], $_->[1], \&login);
338 dbg('err', "External Port: $_->[0] $_->[1]");
342 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
345 unless ($^O =~ /^MS/) {
346 unless ($DB::VERSION) {
348 $SIG{TERM} = \&cease;
350 $SIG{HUP} = 'IGNORE';
351 $SIG{CHLD} = sub { $zombies++ };
353 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
354 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
355 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
356 $SIG{KILL} = 'DEFAULT'; # as if it matters....
358 # catch the rest with a hopeful message
361 # dbg('chan', "Catching SIG $_");
362 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
370 # read in system messages
373 # read in command aliases
376 # initialise the Geomagnetic data engine
380 # initial the Spot stuff
383 # initialise the protocol engine
384 dbg('err', "reading in duplicate spot and WWV info ...");
387 # put in a DXCluster node for us here so we can add users and take them away
388 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
390 # read in any existing message headers and clean out old crap
391 dbg('err', "reading existing message headers ...");
395 # read in any cron jobs
396 dbg('err', "reading cron jobs ...");
399 # read in database descriptors
400 dbg('err', "reading database descriptors ...");
403 # starting local stuff
404 dbg('err', "doing local initialisation ...");
408 dbg('local', "Local::init error $@") if $@;
410 # print various flags
411 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
413 # this, such as it is, is the main loop!
414 dbg('err', "orft we jolly well go ...");
416 #open(DB::OUT, "|tee /tmp/aa");
421 Msg->event_loop(1, 0.1);
423 process_inqueue(); # read in lines from the input queue and despatch them
426 # do timed stuff, ongoing processing happens one a second
427 if ($timenow != $systime) {
430 DXCron::process(); # do cron jobs
431 DXCommandmode::process(); # process ongoing command mode stuff
432 DXProt::process(); # process ongoing ak1a pcxx stuff
433 DXConnect::process();
440 Local::process(); # do any localised processing
442 dbg('local', "Local::process error $@") if $@;
445 last if --$decease <= 0;