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?
94 use POSIX ":sys_wait_h";
101 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
102 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
103 $clusterport $mycall $decease $is_win $routeroot $me $reqreg
106 @inqueue = (); # the main input queue, an array of hashes
107 $systime = 0; # the time now (in seconds)
108 $version = "1.49"; # the version no of the software
109 $starttime = 0; # the starting time of the cluster
110 #@outstanding_connects = (); # list of outstanding connects
111 @listeners = (); # list of listeners
112 $reqreg = 0; # 1 = registration required, 2 = deregister people
114 use vars qw($VERSION $BRANCH $build $branch);
115 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
116 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
117 $main::build += 12; # add an offset to make it bigger than last system
118 $main::build += $VERSION;
119 $main::branch += $BRANCH;
122 # send a message to call on conn and disconnect
125 my ($conn, $call, $mess) = @_;
127 $conn->disable_read(1);
128 dbg("-> D $call $mess\n") if isdbg('chan');
129 $conn->send_now("D$call|$mess");
137 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
138 $dxchan->disconnect(1);
141 # handle incoming messages
144 my ($conn, $msg) = @_;
145 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
146 return unless defined $sort;
148 unless (is_callsign($call)) {
149 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
153 # set up the basic channel info
154 # is there one already connected to me - locally?
155 my $user = DXUser->get($call);
156 my $dxchan = DXChannel->get($call);
158 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
159 already_conn($conn, $call, $mess);
164 my $basecall = $call;
165 $basecall =~ s/-\d+$//;
166 my $baseuser = DXUser->get($basecall);
167 my $lock = $user->lockout if $user;
168 if ($baseuser && $baseuser->lockout || $lock) {
169 if (!$user || !defined $lock || $lock) {
170 my $host = $conn->{peerhost} || "unknown";
171 Log('DXCommand', "$call on $host is locked out, disconnected");
178 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
180 $user = DXUser->new($call);
185 if ($user->is_spider) {
186 $dxchan = QXProt->new($call, $conn, $user);
187 } elsif ($user->is_node) {
188 $dxchan = DXProt->new($call, $conn, $user);
189 } elsif ($user->is_user) {
190 $dxchan = DXCommandmode->new($call, $conn, $user);
191 } elsif ($user->is_bbs) {
192 $dxchan = BBS->new($call, $conn, $user);
194 die "Invalid sort of user on $call = $sort";
197 # check that the conn has a callsign
198 $conn->conns($call) if $conn->isa('IntMsg');
201 $conn->set_error(sub {error_handler($dxchan)});
202 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
203 rec($dxchan, $conn, $msg);
208 my ($dxchan, $conn, $msg) = @_;
210 # queue the message and the channel object for later processing
212 my $self = bless {}, "inqueue";
213 $self->{dxchan} = $dxchan;
214 $self->{data} = $msg;
215 push @inqueue, $self;
221 return \&new_channel;
224 # cease running this program, close down all the connections nicely
230 $SIG{'TERM'} = 'IGNORE';
231 $SIG{'INT'} = 'IGNORE';
237 Local::finish(); # end local processing
239 dbg("Local::finish error $@") if $@;
242 foreach $dxchan (DXChannel->get_all_nodes) {
243 $dxchan->disconnect(2) unless $dxchan == $main::me;
245 Msg->event_loop(100, 0.01);
248 foreach $dxchan (DXChannel->get_all_users) {
255 # end everything else
256 Msg->event_loop(100, 0.01);
260 # close all databases
263 # close all listeners
264 foreach my $l (@listeners) {
268 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
269 Log('cluster', "DXSpider V$version, build $build ended");
273 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
277 # the reaper of children
281 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
282 dbg("cpid: $cpid") if isdbg('reap');
283 # Msg->pid_gone($cpid);
284 $zombies-- if $zombies > 0;
286 dbg("cpid: $cpid") if isdbg('reap');
289 # this is where the input queue is dealt with and things are dispatched off to other parts of
294 my $self = shift @inqueue;
297 my $data = $self->{data};
298 my $dxchan = $self->{dxchan};
300 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
301 return unless defined $sort;
303 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
304 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
307 my $user = $dxchan->user;
308 if ($sort eq 'A' || $sort eq 'O') {
309 $dxchan->start($line, $sort);
310 } elsif ($sort eq 'I') {
311 die "\$user not defined for $call" if !defined $user;
313 $dxchan->normal($line);
314 } elsif ($sort eq 'Z') {
316 } elsif ($sort eq 'D') {
317 ; # ignored (an echo)
318 } elsif ($sort eq 'G') {
319 $dxchan->enhanced($line);
321 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
328 my $t = $systime - $starttime;
329 my $days = int $t / 86400;
331 my $hours = int $t / 3600;
333 my $mins = int $t / 60;
334 return sprintf "%d %02d:%02d", $days, $hours, $mins;
339 AGWMsg::init(\&new_channel);
342 #############################################################
344 # The start of the main line of code
346 #############################################################
348 $starttime = $systime = time;
349 $lang = 'en' unless $lang;
351 # open the debug file, set various FHs to be unbuffered
352 dbginit(\&DXCommandmode::broadcast_debug);
356 STDOUT->autoflush(1);
358 # calculate build number
359 $build += $main::version;
360 $build = "$build.$branch" if $branch;
362 Log('cluster', "DXSpider V$version, build $build started");
365 dbg("Copyright (c) 1998-2001 Dirk Koopman G1TLH");
366 dbg("DXSpider Version $version, build $build started");
369 dbg("loading prefixes ...");
373 dbg("loading band data ...");
376 # initialise User file system
377 dbg("loading user file system ...");
378 DXUser->init($userfn, 1);
380 # start listening for incoming messages/connects
381 dbg("starting listeners ...");
382 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
383 $conn->conns("Server $clusteraddr/$clusterport");
384 push @listeners, $conn;
385 dbg("Internal port: $clusteraddr $clusterport");
386 foreach my $l (@main::listen) {
387 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
388 $conn->conns("Server $l->[0]/$l->[1]");
389 push @listeners, $conn;
390 dbg("External Port: $l->[0] $l->[1]");
395 dbg("load badwords: " . (BadWords::load or "Ok"));
398 unless ($DB::VERSION) {
399 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
403 $SIG{HUP} = 'IGNORE';
404 $SIG{CHLD} = sub { $zombies++ };
406 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
407 $SIG{IO} = sub { dbg("SIGIO received"); };
408 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
409 $SIG{KILL} = 'DEFAULT'; # as if it matters....
411 # catch the rest with a hopeful message
414 # dbg("Catching SIG $_") if isdbg('chan');
415 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
423 # read in system messages
426 # read in command aliases
429 # initialise the Geomagnetic data engine
433 # initial the Spot stuff
436 # initialise the protocol engine
437 dbg("reading in duplicate spot and WWV info ...");
440 # put in a DXCluster node for us here so we can add users and take them away
441 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
443 # make sure that there is a routing OUTPUT node default file
444 #unless (Filter::read_in('route', 'node_default', 0)) {
445 # my $dxcc = $main::me->dxcc;
446 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
449 # read in any existing message headers and clean out old crap
450 dbg("reading existing message headers ...");
454 # read in any cron jobs
455 dbg("reading cron jobs ...");
458 # read in database descriptors
459 dbg("reading database descriptors ...");
462 # starting local stuff
463 dbg("doing local initialisation ...");
467 dbg("Local::init error $@") if $@;
469 # this, such as it is, is the main loop!
470 dbg("orft we jolly well go ...");
471 my $script = new Script "startup";
472 $script->run($main::me) if $script;
474 #open(DB::OUT, "|tee /tmp/aa");
479 Msg->event_loop(10, 0.010);
481 process_inqueue(); # read in lines from the input queue and despatch them
484 # do timed stuff, ongoing processing happens one a second
485 if ($timenow != $systime) {
488 DXCron::process(); # do cron jobs
489 DXCommandmode::process(); # process ongoing command mode stuff
490 DXProt::process(); # process ongoing ak1a pcxx stuff
492 DXConnect::process();
500 Local::process(); # do any localised processing
502 dbg("Local::process error $@") if $@;
505 last if --$decease <= 0;