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 @listeners $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");
104 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
108 # handle incoming messages
111 my ($conn, $msg) = @_;
112 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
113 return unless defined $sort;
115 # set up the basic channel info
116 # is there one already connected to me - locally?
117 my $user = DXUser->get($call);
118 my $dxchan = DXChannel->get($call);
120 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
121 already_conn($conn, $call, $mess);
125 # is there one already connected elsewhere in the cluster?
127 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
130 if (my $ref = DXCluster->get_exact($call)) {
131 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
132 already_conn($conn, $call, $mess);
136 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
138 if (my $ref = DXCluster->get_exact($call)) {
139 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
140 already_conn($conn, $call, $mess);
143 $user = DXUser->new($call);
147 if ($user->lockout) {
148 Log('DXCommand', "$call is locked out, disconnected");
154 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
155 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
156 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
157 die "Invalid sort of user on $call = $sort" if !$dxchan;
159 # check that the conn has a callsign
160 $conn->conns($call) if $conn->isa('IntMsg');
163 $conn->set_error(sub {error_handler($dxchan)});
164 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
165 rec($dxchan, $conn, $msg);
170 my ($dxchan, $conn, $msg) = @_;
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;
183 return \&new_channel;
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 # Msg->pid_gone($cpid);
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') {
279 } elsif ($sort eq 'D') {
280 ; # ignored (an echo)
282 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
288 my $t = $systime - $starttime;
289 my $days = int $t / 86400;
291 my $hours = int $t / 3600;
293 my $mins = int $t / 60;
294 return sprintf "%d %02d:%02d", $days, $hours, $mins;
296 #############################################################
298 # The start of the main line of code
300 #############################################################
302 $starttime = $systime = time;
303 $lang = 'en' unless $lang;
305 # open the debug file, set various FHs to be unbuffered
310 STDOUT->autoflush(1);
312 Log('cluster', "DXSpider V$version started");
315 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
318 dbg('err', "loading prefixes ...");
322 dbg('err', "loading band data ...");
325 # initialise User file system
326 dbg('err', "loading user file system ...");
327 DXUser->init($userfn, 1);
329 # start listening for incoming messages/connects
332 dbg('err', "starting listeners ...");
333 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
334 $conn->conns("Server $clusteraddr/$clusterport");
335 push @listeners, $conn;
336 dbg('err', "Internal port: $clusteraddr $clusterport");
337 for (@main::listen) {
338 $conn = ExtMsg->new_server($_->[0], $_->[1], \&login);
339 $conn->conns("Server $_->[0]/$_->[1]");
340 push @listeners, $conn;
341 dbg('err', "External Port: $_->[0] $_->[1]");
345 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
348 unless ($^O =~ /^MS/) {
349 unless ($DB::VERSION) {
351 $SIG{TERM} = \&cease;
353 $SIG{HUP} = 'IGNORE';
354 $SIG{CHLD} = sub { $zombies++ };
356 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
357 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
358 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
359 $SIG{KILL} = 'DEFAULT'; # as if it matters....
361 # catch the rest with a hopeful message
364 # dbg('chan', "Catching SIG $_");
365 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
373 # read in system messages
376 # read in command aliases
379 # initialise the Geomagnetic data engine
383 # initial the Spot stuff
386 # initialise the protocol engine
387 dbg('err', "reading in duplicate spot and WWV info ...");
390 # put in a DXCluster node for us here so we can add users and take them away
391 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
393 # read in any existing message headers and clean out old crap
394 dbg('err', "reading existing message headers ...");
398 # read in any cron jobs
399 dbg('err', "reading cron jobs ...");
402 # read in database descriptors
403 dbg('err', "reading database descriptors ...");
406 # starting local stuff
407 dbg('err', "doing local initialisation ...");
411 dbg('local', "Local::init error $@") if $@;
413 # print various flags
414 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
416 # this, such as it is, is the main loop!
417 dbg('err', "orft we jolly well go ...");
419 #open(DB::OUT, "|tee /tmp/aa");
424 Msg->event_loop(25, 0.001);
426 process_inqueue(); # read in lines from the input queue and despatch them
429 # do timed stuff, ongoing processing happens one a second
430 if ($timenow != $systime) {
433 DXCron::process(); # do cron jobs
434 DXCommandmode::process(); # process ongoing command mode stuff
435 DXProt::process(); # process ongoing ak1a pcxx stuff
436 DXConnect::process();
443 Local::process(); # do any localised processing
445 dbg('local', "Local::process error $@") if $@;
448 last if --$decease <= 0;