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.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) $!";
73 @inqueue = (); # the main input queue, an array of hashes
74 $systime = 0; # the time now (in seconds)
75 $version = "1.44"; # the version no of the software
76 $starttime = 0; # the starting time of the cluster
77 $lockfn = "cluster.lock"; # lock file name
78 @outstanding_connects = (); # list of outstanding connects
80 # handle disconnections
84 return if !defined $dxchan;
85 $dxchan->disconnect();
88 # send a message to call on conn and disconnect
91 my ($conn, $call, $mess) = @_;
93 dbg('chan', "-> D $call $mess\n");
94 $conn->send_now("D$call|$mess");
96 dbg('chan', "-> Z $call bye\n");
97 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
102 # handle incoming messages
105 my ($conn, $msg, $err) = @_;
106 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
108 if (!defined $msg || (defined $err && $err)) {
113 $dxchan->conn(undef);
122 # set up the basic channel info - this needs a bit more thought - there is duplication here
123 if (!defined $dxchan) {
124 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
125 return unless defined $sort;
127 # is there one already connected to me - locally?
128 my $user = DXUser->get($call);
129 if (DXChannel->get($call)) {
130 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call);
131 already_conn($conn, $call, $mess);
135 # is there one already connected elsewhere in the cluster?
137 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
140 if (DXCluster->get_exact($call)) {
141 my $mess = DXM::msg($lang, $user->is_node ? 'concluster' : 'conother', $call);
142 already_conn($conn, $call, $mess);
146 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
148 if (DXCluster->get_exact($call)) {
149 my $mess = DXM::msg($lang, 'conother', $call);
150 already_conn($conn, $call, $mess);
153 $user = DXUser->new($call);
157 if ($user->lockout) {
158 Log('DXCommand', "$call is locked out, disconnected");
159 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
164 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
165 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
166 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
167 die "Invalid sort of user on $call = $sort" if !$dxchan;
170 # queue the message and the channel object for later processing
172 my $self = bless {}, "inqueue";
173 $self->{dxchan} = $dxchan;
174 $self->{data} = $msg;
175 push @inqueue, $self;
184 # cease running this program, close down all the connections nicely
189 $SIG{'TERM'} = 'IGNORE';
190 $SIG{'INT'} = 'IGNORE';
195 Local::finish(); # end local processing
197 dbg('local', "Local::finish error $@") if $@;
200 foreach $dxchan (DXChannel->get_all()) {
201 next unless $dxchan->is_node;
202 disconnect($dxchan) unless $dxchan == $DXProt::me;
204 Msg->event_loop(1, 0.05);
205 Msg->event_loop(1, 0.05);
206 Msg->event_loop(1, 0.05);
207 Msg->event_loop(1, 0.05);
208 Msg->event_loop(1, 0.05);
209 Msg->event_loop(1, 0.05);
212 foreach $dxchan (DXChannel->get_all()) {
213 next if $dxchan->is_node;
214 disconnect($dxchan) unless $dxchan == $DXProt::me;
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);
220 Msg->event_loop(1, 0.05);
221 Msg->event_loop(1, 0.05);
225 # close all databases
228 dbg('chan', "DXSpider version $version ended");
229 Log('cluster', "DXSpider V$version stopped");
233 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
237 # the reaper of children
240 $SIG{'CHLD'} = \&reap;
242 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
245 # this is where the input queue is dealt with and things are dispatched off to other parts of
249 my $self = shift @inqueue;
252 my $data = $self->{data};
253 my $dxchan = $self->{dxchan};
255 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
256 return unless defined $sort;
258 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
259 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
262 my $user = $dxchan->user;
263 if ($sort eq 'A' || $sort eq 'O') {
264 $dxchan->start($line, $sort);
265 } elsif ($sort eq 'I') {
266 die "\$user not defined for $call" if !defined $user;
268 $dxchan->normal($line);
269 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
270 } elsif ($sort eq 'Z') {
271 $dxchan->conn(undef);
273 } elsif ($sort eq 'D') {
274 ; # ignored (an echo)
276 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
282 my $t = $systime - $starttime;
283 my $days = int $t / 86400;
285 my $hours = int $t / 3600;
287 my $mins = int $t / 60;
288 return sprintf "%d %02d:%02d", $days, $hours, $mins;
290 #############################################################
292 # The start of the main line of code
294 #############################################################
296 $starttime = $systime = time;
298 # open the debug file, set various FHs to be unbuffered
303 STDOUT->autoflush(1);
305 Log('cluster', "DXSpider V$version started");
308 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2000 Dirk Koopman G1TLH");
311 dbg('err', "loading prefixes ...");
315 dbg('err', "loading band data ...");
318 # initialise User file system
319 dbg('err', "loading user file system ...");
320 DXUser->init($userfn, 1);
322 # start listening for incoming messages/connects
323 dbg('err', "starting listener ...");
324 Msg->new_server("$clusteraddr", $clusterport, \&login);
328 $SIG{TERM} = \&cease;
329 $SIG{HUP} = 'IGNORE';
332 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
333 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
334 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
335 $SIG{KILL} = 'DEFAULT'; # as if it matters....
337 # catch the rest with a hopeful message
340 # dbg('chan', "Catching SIG $_");
341 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
348 # read in system messages
351 # read in command aliases
354 # initialise the Geomagnetic data engine
358 # initial the Spot stuff
361 # initialise the protocol engine
362 dbg('err', "reading in duplicate spot and WWV info ...");
366 # put in a DXCluster node for us here so we can add users and take them away
367 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
369 # read in any existing message headers and clean out old crap
370 dbg('err', "reading existing message headers ...");
374 # read in any cron jobs
375 dbg('err', "reading cron jobs ...");
378 # read in database descriptors
379 dbg('err', "reading database descriptors ...");
382 # starting local stuff
383 dbg('err', "doing local initialisation ...");
387 dbg('local', "Local::init error $@") if $@;
389 # print various flags
390 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
392 # this, such as it is, is the main loop!
393 dbg('err', "orft we jolly well go ...");
395 #open(DB::OUT, "|tee /tmp/aa");
401 Msg->event_loop(1, 0.1);
403 process_inqueue(); # read in lines from the input queue and despatch them
406 # do timed stuff, ongoing processing happens one a second
407 if ($timenow != $systime) {
411 DXCron::process(); # do cron jobs
412 DXCommandmode::process(); # process ongoing command mode stuff
413 DXProt::process(); # process ongoing ak1a pcxx stuff
414 DXConnect::process();
421 Local::process(); # do any localised processing
423 dbg('local', "Local::process error $@") if $@;
426 last if --$decease <= 0;