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");
107 # handle incoming messages
110 my ($conn, $msg) = @_;
111 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
112 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
113 return unless defined $sort;
115 # set up the basic channel info
116 if (!defined $dxchan) {
118 # is there one already connected to me - locally?
119 my $user = DXUser->get($call);
120 if ($sort ne 'O' && Msg->conns($call)) {
121 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
122 already_conn($conn, $call, $mess);
126 # is there one already connected elsewhere in the cluster?
128 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
131 if (my $ref = DXCluster->get_exact($call)) {
132 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
133 already_conn($conn, $call, $mess);
137 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
139 if (my $ref = DXCluster->get_exact($call)) {
140 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
141 already_conn($conn, $call, $mess);
144 $user = DXUser->new($call);
148 if ($user->lockout) {
149 Log('DXCommand', "$call is locked out, disconnected");
155 $conn->conns($call) unless $sort eq 'O';
156 $conn->set_error(sub {error_handler($dxchan)});
159 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
160 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
161 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
162 die "Invalid sort of user on $call = $sort" if !$dxchan;
165 # queue the message and the channel object for later processing
167 my $self = bless {}, "inqueue";
168 $self->{dxchan} = $dxchan;
169 $self->{data} = $msg;
170 push @inqueue, $self;
179 # cease running this program, close down all the connections nicely
184 $SIG{'TERM'} = 'IGNORE';
185 $SIG{'INT'} = 'IGNORE';
190 Local::finish(); # end local processing
192 dbg('local', "Local::finish error $@") if $@;
195 foreach $dxchan (DXChannel->get_all()) {
196 next unless $dxchan->is_node;
197 $dxchan->disconnect unless $dxchan == $DXProt::me;
199 Msg->event_loop(1, 0.05);
200 Msg->event_loop(1, 0.05);
203 foreach $dxchan (DXChannel->get_all()) {
204 next if $dxchan->is_node;
205 $dxchan->disconnect unless $dxchan == $DXProt::me;
207 Msg->event_loop(1, 0.05);
208 Msg->event_loop(1, 0.05);
209 Msg->event_loop(1, 0.05);
210 Msg->event_loop(1, 0.05);
211 Msg->event_loop(1, 0.05);
212 Msg->event_loop(1, 0.05);
216 # close all databases
219 # close all listeners
224 dbg('chan', "DXSpider version $version ended");
225 Log('cluster', "DXSpider V$version stopped");
229 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
233 # the reaper of children
237 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
238 dbg('reap', "cpid: $cpid");
239 # Msg->pid_gone($cpid);
240 $zombies-- if $zombies > 0;
242 dbg('reap', "cpid: $cpid");
245 # this is where the input queue is dealt with and things are dispatched off to other parts of
249 my $self = shift @inqueue;
252 my $data = $self->{data};
253 my $dxchan = $self->{dxchan};
255 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
256 return unless defined $sort;
258 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
259 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
262 my $user = $dxchan->user;
263 if ($sort eq 'A' || $sort eq 'O') {
264 $dxchan->start($line, $sort);
265 } elsif ($sort eq 'I') {
266 die "\$user not defined for $call" if !defined $user;
268 $dxchan->normal($line);
269 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
270 } elsif ($sort eq 'Z') {
271 $dxchan->conn(undef);
273 } elsif ($sort eq 'D') {
274 ; # ignored (an echo)
276 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
282 my $t = $systime - $starttime;
283 my $days = int $t / 86400;
285 my $hours = int $t / 3600;
287 my $mins = int $t / 60;
288 return sprintf "%d %02d:%02d", $days, $hours, $mins;
290 #############################################################
292 # The start of the main line of code
294 #############################################################
296 $starttime = $systime = time;
297 $lang = 'en' unless $lang;
299 # open the debug file, set various FHs to be unbuffered
304 STDOUT->autoflush(1);
306 Log('cluster', "DXSpider V$version started");
309 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
312 dbg('err', "loading prefixes ...");
316 dbg('err', "loading band data ...");
319 # initialise User file system
320 dbg('err', "loading user file system ...");
321 DXUser->init($userfn, 1);
323 # start listening for incoming messages/connects
326 dbg('err', "starting listeners ...");
327 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
328 $conn->conns("Server $clusteraddr/$clusterport");
329 push @listeners, $conn;
330 dbg('err', "Internal port: $clusteraddr $clusterport");
331 for (@main::listen) {
332 $conn = ExtMsg->new_server($_->[0], $_->[1], \&login);
333 $conn->conns("Server $_->[0]/$_->[1]");
334 push @listeners, $conn;
335 dbg('err', "External Port: $_->[0] $_->[1]");
339 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
342 unless ($^O =~ /^MS/) {
343 unless ($DB::VERSION) {
345 $SIG{TERM} = \&cease;
347 $SIG{HUP} = 'IGNORE';
348 $SIG{CHLD} = sub { $zombies++ };
350 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
351 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
352 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
353 $SIG{KILL} = 'DEFAULT'; # as if it matters....
355 # catch the rest with a hopeful message
358 # dbg('chan', "Catching SIG $_");
359 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
367 # read in system messages
370 # read in command aliases
373 # initialise the Geomagnetic data engine
377 # initial the Spot stuff
380 # initialise the protocol engine
381 dbg('err', "reading in duplicate spot and WWV info ...");
384 # put in a DXCluster node for us here so we can add users and take them away
385 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
387 # read in any existing message headers and clean out old crap
388 dbg('err', "reading existing message headers ...");
392 # read in any cron jobs
393 dbg('err', "reading cron jobs ...");
396 # read in database descriptors
397 dbg('err', "reading database descriptors ...");
400 # starting local stuff
401 dbg('err', "doing local initialisation ...");
405 dbg('local', "Local::init error $@") if $@;
407 # print various flags
408 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
410 # this, such as it is, is the main loop!
411 dbg('err', "orft we jolly well go ...");
413 #open(DB::OUT, "|tee /tmp/aa");
418 Msg->event_loop(10, 0.001);
420 process_inqueue(); # read in lines from the input queue and despatch them
423 # do timed stuff, ongoing processing happens one a second
424 if ($timenow != $systime) {
427 DXCron::process(); # do cron jobs
428 DXCommandmode::process(); # process ongoing command mode stuff
429 DXProt::process(); # process ongoing ak1a pcxx stuff
430 DXConnect::process();
437 Local::process(); # do any localised processing
439 dbg('local', "Local::process error $@") if $@;
442 last if --$decease <= 0;