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 $lockfn = "$root/perl/cluster.lck"; # 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) $!";
40 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
91 use POSIX ":sys_wait_h";
98 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
99 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
100 $clusterport $mycall $decease $build $is_win $routeroot
103 @inqueue = (); # the main input queue, an array of hashes
104 $systime = 0; # the time now (in seconds)
105 $version = "1.48"; # the version no of the software
106 $starttime = 0; # the starting time of the cluster
107 #@outstanding_connects = (); # list of outstanding connects
108 @listeners = (); # list of listeners
111 # send a message to call on conn and disconnect
114 my ($conn, $call, $mess) = @_;
116 $conn->disable_read(1);
117 dbg('chan', "-> D $call $mess\n");
118 $conn->send_now("D$call|$mess");
126 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
127 $dxchan->disconnect(1);
130 # handle incoming messages
133 my ($conn, $msg) = @_;
134 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
135 return unless defined $sort;
137 # set up the basic channel info
138 # is there one already connected to me - locally?
139 my $user = DXUser->get($call);
140 my $dxchan = DXChannel->get($call);
142 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
143 already_conn($conn, $call, $mess);
148 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
150 $user = DXUser->new($call);
154 if ($user->lockout) {
155 Log('DXCommand', "$call is locked out, disconnected");
161 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
162 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
163 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
164 die "Invalid sort of user on $call = $sort" if !$dxchan;
166 # check that the conn has a callsign
167 $conn->conns($call) if $conn->isa('IntMsg');
170 $conn->set_error(sub {error_handler($dxchan)});
171 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
172 rec($dxchan, $conn, $msg);
177 my ($dxchan, $conn, $msg) = @_;
179 # queue the message and the channel object for later processing
181 my $self = bless {}, "inqueue";
182 $self->{dxchan} = $dxchan;
183 $self->{data} = $msg;
184 push @inqueue, $self;
190 return \&new_channel;
193 # cease running this program, close down all the connections nicely
199 $SIG{'TERM'} = 'IGNORE';
200 $SIG{'INT'} = 'IGNORE';
206 Local::finish(); # end local processing
208 dbg('local', "Local::finish error $@") if $@;
211 foreach $dxchan (DXChannel->get_all_nodes) {
212 $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
214 Msg->event_loop(100, 0.01);
217 foreach $dxchan (DXChannel->get_all_users) {
224 # end everything else
225 Msg->event_loop(100, 0.01);
229 # close all databases
232 # close all listeners
233 foreach my $l (@listeners) {
237 dbg('chan', "DXSpider version $version, build $build ended");
238 Log('cluster', "DXSpider V$version, build $build ended");
242 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
246 # the reaper of children
250 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
251 dbg('reap', "cpid: $cpid");
252 # Msg->pid_gone($cpid);
253 $zombies-- if $zombies > 0;
255 dbg('reap', "cpid: $cpid");
258 # this is where the input queue is dealt with and things are dispatched off to other parts of
262 my $self = shift @inqueue;
265 my $data = $self->{data};
266 my $dxchan = $self->{dxchan};
268 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
269 return unless defined $sort;
271 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
272 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
275 my $user = $dxchan->user;
276 if ($sort eq 'A' || $sort eq 'O') {
277 $dxchan->start($line, $sort);
278 } elsif ($sort eq 'I') {
279 die "\$user not defined for $call" if !defined $user;
281 $dxchan->normal($line);
282 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
283 } elsif ($sort eq 'Z') {
285 } elsif ($sort eq 'D') {
286 ; # ignored (an echo)
288 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
294 my $t = $systime - $starttime;
295 my $days = int $t / 86400;
297 my $hours = int $t / 3600;
299 my $mins = int $t / 60;
300 return sprintf "%d %02d:%02d", $days, $hours, $mins;
305 AGWMsg::init(\&new_channel);
308 #############################################################
310 # The start of the main line of code
312 #############################################################
314 $starttime = $systime = time;
315 $lang = 'en' unless $lang;
317 # open the debug file, set various FHs to be unbuffered
322 STDOUT->autoflush(1);
324 # calculate build number
325 $build = $main::version;
328 open(CL, "$main::root/perl/cluster.pl") or die "Cannot open cluster.pl $!";
330 next unless /^use\s+([\w:_]+)/;
335 foreach my $fn (@fn) {
337 open(CL, "$main::root/perl/${fn}.pm") or next;
339 if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)\.?(\d+.\d+)?/ ) {
341 $subbuild += $2 if $2;
347 $build = "$build.$subbuild" if $subbuild;
349 Log('cluster', "DXSpider V$version, build $build started");
352 dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
355 dbg('err', "loading prefixes ...");
359 dbg('err', "loading band data ...");
362 # initialise User file system
363 dbg('err', "loading user file system ...");
364 DXUser->init($userfn, 1);
366 # start listening for incoming messages/connects
367 dbg('err', "starting listeners ...");
368 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
369 $conn->conns("Server $clusteraddr/$clusterport");
370 push @listeners, $conn;
371 dbg('err', "Internal port: $clusteraddr $clusterport");
372 foreach my $l (@main::listen) {
373 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
374 $conn->conns("Server $l->[0]/$l->[1]");
375 push @listeners, $conn;
376 dbg('err', "External Port: $l->[0] $l->[1]");
381 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
384 unless ($DB::VERSION) {
385 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
389 $SIG{HUP} = 'IGNORE';
390 $SIG{CHLD} = sub { $zombies++ };
392 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
393 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
394 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
395 $SIG{KILL} = 'DEFAULT'; # as if it matters....
397 # catch the rest with a hopeful message
400 # dbg('chan', "Catching SIG $_");
401 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
409 # read in system messages
412 # read in command aliases
415 # initialise the Geomagnetic data engine
419 # initial the Spot stuff
422 # initialise the protocol engine
423 dbg('err', "reading in duplicate spot and WWV info ...");
426 # put in a DXCluster node for us here so we can add users and take them away
427 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($DXProt::me->here)|Route::conf($DXProt::me->conf));
429 # make sure that there is a routing OUTPUT node default file
430 unless (Filter::read_in('route', 'node_default', 0)) {
431 my $dxcc = $DXProt::me->dxcc;
432 $Route::filterdef->cmd($DXProt::me, 'route', 'accept', "node_default call_dxcc $dxcc" );
435 # read in any existing message headers and clean out old crap
436 dbg('err', "reading existing message headers ...");
440 # read in any cron jobs
441 dbg('err', "reading cron jobs ...");
444 # read in database descriptors
445 dbg('err', "reading database descriptors ...");
448 # starting local stuff
449 dbg('err', "doing local initialisation ...");
453 dbg('local', "Local::init error $@") if $@;
455 # print various flags
456 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
458 # this, such as it is, is the main loop!
459 dbg('err', "orft we jolly well go ...");
461 #open(DB::OUT, "|tee /tmp/aa");
466 Msg->event_loop(10, 0.010);
468 process_inqueue(); # read in lines from the input queue and despatch them
471 # do timed stuff, ongoing processing happens one a second
472 if ($timenow != $systime) {
475 DXCron::process(); # do cron jobs
476 DXCommandmode::process(); # process ongoing command mode stuff
477 DXProt::process(); # process ongoing ak1a pcxx stuff
478 DXConnect::process();
486 Local::process(); # do any localised processing
488 dbg('local', "Local::process error $@") if $@;
491 last if --$decease <= 0;