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
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
113 use vars qw($VERSION $BRANCH $build $branch);
114 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
115 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
116 $main::build += 12; # add an offset to make it bigger than last system
117 $main::build += $VERSION;
118 $main::branch += $BRANCH;
121 # send a message to call on conn and disconnect
124 my ($conn, $call, $mess) = @_;
126 $conn->disable_read(1);
127 dbg("-> D $call $mess\n") if isdbg('chan');
128 $conn->send_now("D$call|$mess");
136 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
137 $dxchan->disconnect(1);
140 # handle incoming messages
143 my ($conn, $msg) = @_;
144 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
145 return unless defined $sort;
147 unless (is_callsign($call)) {
148 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
152 # set up the basic channel info
153 # is there one already connected to me - locally?
154 my $user = DXUser->get($call);
155 my $dxchan = DXChannel->get($call);
157 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
158 already_conn($conn, $call, $mess);
163 my $basecall = $call;
164 $basecall =~ s/-\d+$//;
165 my $baseuser = DXUser->get($basecall);
166 my $lock = $user->lockout if $user;
167 if ($baseuser && $baseuser->lockout || $lock) {
168 if (!$user || !defined $lock || $lock) {
169 my $host = $conn->{peerhost} || "unknown";
170 Log('DXCommand', "$call on $host is locked out, disconnected");
177 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
179 $user = DXUser->new($call);
184 if ($user->is_spider) {
185 $dxchan = QXProt->new($call, $conn, $user);
186 } elsif ($user->is_node) {
187 $dxchan = DXProt->new($call, $conn, $user);
188 } elsif ($user->is_user) {
189 $dxchan = DXCommandmode->new($call, $conn, $user);
190 } elsif ($user->is_bbs) {
191 $dxchan = BBS->new($call, $conn, $user);
193 die "Invalid sort of user on $call = $sort";
196 # check that the conn has a callsign
197 $conn->conns($call) if $conn->isa('IntMsg');
200 $conn->set_error(sub {error_handler($dxchan)});
201 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
202 rec($dxchan, $conn, $msg);
207 my ($dxchan, $conn, $msg) = @_;
209 # queue the message and the channel object for later processing
211 my $self = bless {}, "inqueue";
212 $self->{dxchan} = $dxchan;
213 $self->{data} = $msg;
214 push @inqueue, $self;
220 return \&new_channel;
223 # cease running this program, close down all the connections nicely
229 $SIG{'TERM'} = 'IGNORE';
230 $SIG{'INT'} = 'IGNORE';
236 Local::finish(); # end local processing
238 dbg("Local::finish error $@") if $@;
241 foreach $dxchan (DXChannel->get_all_nodes) {
242 $dxchan->disconnect(2) unless $dxchan == $main::me;
244 Msg->event_loop(100, 0.01);
247 foreach $dxchan (DXChannel->get_all_users) {
254 # end everything else
255 Msg->event_loop(100, 0.01);
259 # close all databases
262 # close all listeners
263 foreach my $l (@listeners) {
267 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
268 Log('cluster', "DXSpider V$version, build $build ended");
272 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
276 # the reaper of children
280 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
281 dbg("cpid: $cpid") if isdbg('reap');
282 # Msg->pid_gone($cpid);
283 $zombies-- if $zombies > 0;
285 dbg("cpid: $cpid") if isdbg('reap');
288 # this is where the input queue is dealt with and things are dispatched off to other parts of
293 my $self = shift @inqueue;
296 my $data = $self->{data};
297 my $dxchan = $self->{dxchan};
299 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
300 return unless defined $sort;
302 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
303 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
306 my $user = $dxchan->user;
307 if ($sort eq 'A' || $sort eq 'O') {
308 $dxchan->start($line, $sort);
309 } elsif ($sort eq 'I') {
310 die "\$user not defined for $call" if !defined $user;
312 $dxchan->normal($line);
313 $dxchan->disconnect if ($dxchan->{state} eq 'bye');
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;