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) $!";
72 use POSIX ":sys_wait_h";
79 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
80 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
81 $clusterport $mycall $decease $build
84 @inqueue = (); # the main input queue, an array of hashes
85 $systime = 0; # the time now (in seconds)
86 $version = "1.47"; # the version no of the software
87 $starttime = 0; # the starting time of the cluster
88 $lockfn = "cluster.lock"; # lock file name
89 #@outstanding_connects = (); # list of outstanding connects
90 @listeners = (); # list of listeners
93 # send a message to call on conn and disconnect
96 my ($conn, $call, $mess) = @_;
98 dbg('chan', "-> D $call $mess\n");
99 $conn->send_now("D$call|$mess");
107 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
108 $dxchan->disconnect(1);
111 # handle incoming messages
114 my ($conn, $msg) = @_;
115 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
116 return unless defined $sort;
118 # set up the basic channel info
119 # is there one already connected to me - locally?
120 my $user = DXUser->get($call);
121 my $dxchan = DXChannel->get($call);
123 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
124 already_conn($conn, $call, $mess);
128 # is there one already connected elsewhere in the cluster?
130 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
133 if (my $ref = DXCluster->get_exact($call)) {
134 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
135 already_conn($conn, $call, $mess);
139 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
141 if (my $ref = DXCluster->get_exact($call)) {
142 my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
143 already_conn($conn, $call, $mess);
146 $user = DXUser->new($call);
150 if ($user->lockout) {
151 Log('DXCommand', "$call is locked out, disconnected");
157 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
158 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
159 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
160 die "Invalid sort of user on $call = $sort" if !$dxchan;
162 # check that the conn has a callsign
163 $conn->conns($call) if $conn->isa('IntMsg');
166 $conn->set_error(sub {error_handler($dxchan)});
167 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
168 rec($dxchan, $conn, $msg);
173 my ($dxchan, $conn, $msg) = @_;
175 # queue the message and the channel object for later processing
177 my $self = bless {}, "inqueue";
178 $self->{dxchan} = $dxchan;
179 $self->{data} = $msg;
180 push @inqueue, $self;
186 return \&new_channel;
189 # cease running this program, close down all the connections nicely
194 $SIG{'TERM'} = 'IGNORE';
195 $SIG{'INT'} = 'IGNORE';
200 Local::finish(); # end local processing
202 dbg('local', "Local::finish error $@") if $@;
205 foreach $dxchan (DXChannel->get_all()) {
206 next unless $dxchan->is_node;
207 $dxchan->disconnect unless $dxchan == $DXProt::me;
209 Msg->event_loop(1, 0.05);
210 Msg->event_loop(1, 0.05);
213 foreach $dxchan (DXChannel->get_all()) {
214 next if $dxchan->is_node;
215 $dxchan->disconnect unless $dxchan == $DXProt::me;
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);
221 Msg->event_loop(1, 0.05);
222 Msg->event_loop(1, 0.05);
226 # close all databases
229 # close all listeners
234 dbg('chan', "DXSpider version $version, build $build ended");
235 Log('cluster', "DXSpider V$version, build $build ended");
239 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
243 # the reaper of children
247 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
248 dbg('reap', "cpid: $cpid");
249 # Msg->pid_gone($cpid);
250 $zombies-- if $zombies > 0;
252 dbg('reap', "cpid: $cpid");
255 # this is where the input queue is dealt with and things are dispatched off to other parts of
259 my $self = shift @inqueue;
262 my $data = $self->{data};
263 my $dxchan = $self->{dxchan};
265 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
266 return unless defined $sort;
268 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
269 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
272 my $user = $dxchan->user;
273 if ($sort eq 'A' || $sort eq 'O') {
274 $dxchan->start($line, $sort);
275 } elsif ($sort eq 'I') {
276 die "\$user not defined for $call" if !defined $user;
278 $dxchan->normal($line);
279 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
280 } elsif ($sort eq 'Z') {
282 } elsif ($sort eq 'D') {
283 ; # ignored (an echo)
285 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
291 my $t = $systime - $starttime;
292 my $days = int $t / 86400;
294 my $hours = int $t / 3600;
296 my $mins = int $t / 60;
297 return sprintf "%d %02d:%02d", $days, $hours, $mins;
299 #############################################################
301 # The start of the main line of code
303 #############################################################
305 $starttime = $systime = time;
306 $lang = 'en' unless $lang;
308 # open the debug file, set various FHs to be unbuffered
313 STDOUT->autoflush(1);
315 # calculate build number
316 $build = $main::version;
318 if (opendir(DIR, "$main::root/perl")) {
319 my @d = readdir(DIR);
321 foreach my $fn (@d) {
322 if ($fn =~ /^cluster\.pl$/ || $fn =~ /\.pm$/) {
323 my $f = new IO::File "$main::root/perl/$fn" or next;
325 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
335 Log('cluster', "DXSpider V$version, build $build started");
338 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
341 dbg('err', "loading prefixes ...");
345 dbg('err', "loading band data ...");
348 # initialise User file system
349 dbg('err', "loading user file system ...");
350 DXUser->init($userfn, 1);
352 # start listening for incoming messages/connects
355 dbg('err', "starting listeners ...");
356 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
357 $conn->conns("Server $clusteraddr/$clusterport");
358 push @listeners, $conn;
359 dbg('err', "Internal port: $clusteraddr $clusterport");
360 for (@main::listen) {
361 $conn = ExtMsg->new_server($_->[0], $_->[1], \&login);
362 $conn->conns("Server $_->[0]/$_->[1]");
363 push @listeners, $conn;
364 dbg('err', "External Port: $_->[0] $_->[1]");
368 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
371 unless ($^O =~ /^MS/) {
372 unless ($DB::VERSION) {
374 $SIG{TERM} = \&cease;
376 $SIG{HUP} = 'IGNORE';
377 $SIG{CHLD} = sub { $zombies++ };
379 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
380 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
381 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
382 $SIG{KILL} = 'DEFAULT'; # as if it matters....
384 # catch the rest with a hopeful message
387 # dbg('chan', "Catching SIG $_");
388 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
396 # read in system messages
399 # read in command aliases
402 # initialise the Geomagnetic data engine
406 # initial the Spot stuff
409 # initialise the protocol engine
410 dbg('err', "reading in duplicate spot and WWV info ...");
413 # put in a DXCluster node for us here so we can add users and take them away
414 DXNode->new($DXProt::me, $mycall, 0, 1, $DXProt::myprot_version);
416 # read in any existing message headers and clean out old crap
417 dbg('err', "reading existing message headers ...");
421 # read in any cron jobs
422 dbg('err', "reading cron jobs ...");
425 # read in database descriptors
426 dbg('err', "reading database descriptors ...");
429 # starting local stuff
430 dbg('err', "doing local initialisation ...");
434 dbg('local', "Local::init error $@") if $@;
436 # print various flags
437 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
439 # this, such as it is, is the main loop!
440 dbg('err', "orft we jolly well go ...");
442 #open(DB::OUT, "|tee /tmp/aa");
447 Msg->event_loop(10, 0.010);
449 process_inqueue(); # read in lines from the input queue and despatch them
452 # do timed stuff, ongoing processing happens one a second
453 if ($timenow != $systime) {
456 DXCron::process(); # do cron jobs
457 DXCommandmode::process(); # process ongoing command mode stuff
458 DXProt::process(); # process ongoing ak1a pcxx stuff
459 DXConnect::process();
466 Local::process(); # do any localised processing
468 dbg('local', "Local::process error $@") if $@;
471 last if --$decease <= 0;