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) $!";
72 @inqueue = (); # the main input queue, an array of hashes
73 $systime = 0; # the time now (in seconds)
74 $version = "1.41"; # the version no of the software
75 $starttime = 0; # the starting time of the cluster
76 $lockfn = "cluster.lock"; # lock file name
77 @outstanding_connects = (); # list of outstanding connects
79 # handle disconnections
83 return if !defined $dxchan;
84 $dxchan->disconnect();
87 # send a message to call on conn and disconnect
90 my ($conn, $call, $mess) = @_;
92 dbg('chan', "-> D $call $mess\n");
93 $conn->send_now("D$call|$mess");
95 dbg('chan', "-> Z $call bye\n");
96 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
101 # handle incoming messages
104 my ($conn, $msg, $err) = @_;
105 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
107 if (!defined $msg || (defined $err && $err)) {
116 # set up the basic channel info - this needs a bit more thought - there is duplication here
117 if (!defined $dxchan) {
118 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
120 # is there one already connected to me - locally?
121 my $user = DXUser->get($call);
122 if (DXChannel->get($call)) {
123 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call);
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 (DXCluster->get_exact($call)) {
134 my $mess = DXM::msg($lang, $user->is_node ? 'concluster' : 'conother', $call);
135 already_conn($conn, $call, $mess);
139 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
141 if (DXCluster->get_exact($call)) {
142 my $mess = DXM::msg($lang, 'conother', $call);
143 already_conn($conn, $call, $mess);
146 $user = DXUser->new($call);
150 if ($user->lockout) {
151 Log('DXCommand', "$call is locked out, disconnected");
152 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
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;
163 # queue the message and the channel object for later processing
165 my $self = bless {}, "inqueue";
166 $self->{dxchan} = $dxchan;
167 $self->{data} = $msg;
168 push @inqueue, $self;
177 # cease running this program, close down all the connections nicely
182 $SIG{'TERM'} = 'IGNORE';
183 $SIG{'INT'} = 'IGNORE';
186 Local::finish(); # end local processing
188 dbg('local', "Local::finish error $@") if $@;
191 foreach $dxchan (DXChannel->get_all()) {
192 next unless $dxchan->is_node;
193 disconnect($dxchan) unless $dxchan == $DXProt::me;
195 Msg->event_loop(1, 0.05);
196 Msg->event_loop(1, 0.05);
197 Msg->event_loop(1, 0.05);
198 Msg->event_loop(1, 0.05);
199 Msg->event_loop(1, 0.05);
200 Msg->event_loop(1, 0.05);
203 foreach $dxchan (DXChannel->get_all()) {
204 next if $dxchan->is_node;
205 disconnect($dxchan) unless $dxchan == $DXProt::me;
207 Msg->event_loop(1, 0.05);
208 Msg->event_loop(1, 0.05);
209 Msg->event_loop(1, 0.05);
210 Msg->event_loop(1, 0.05);
211 Msg->event_loop(1, 0.05);
212 Msg->event_loop(1, 0.05);
215 # close all databases
218 dbg('chan', "DXSpider version $version ended");
219 Log('cluster', "DXSpider V$version stopped");
223 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
227 # the reaper of children
230 $SIG{'CHLD'} = \&reap;
232 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
235 # this is where the input queue is dealt with and things are dispatched off to other parts of
239 my $self = shift @inqueue;
242 my $data = $self->{data};
243 my $dxchan = $self->{dxchan};
244 my ($sort, $call, $line) = $data =~ /^(\w)([^\|]+)\|(.*)$/;
247 # the above regexp must work
248 return unless ($sort && $call && $line);
250 # translate any crappy characters into hex characters
251 if ($line =~ /[\x00-\x06\x08\x0a-\x1f\x7f-\xff]/o) {
252 $line =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
255 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
256 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
259 my $user = $dxchan->user;
260 if ($sort eq 'A' || $sort eq 'O') {
261 $dxchan->start($line, $sort);
262 } elsif ($sort eq 'I') {
263 die "\$user not defined for $call" if !defined $user;
265 $dxchan->normal($line);
266 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
267 } elsif ($sort eq 'Z') {
268 $dxchan->conn(undef);
270 } elsif ($sort eq 'D') {
271 ; # ignored (an echo)
273 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
279 my $t = $systime - $starttime;
280 my $days = int $t / 86400;
282 my $hours = int $t / 3600;
284 my $mins = int $t / 60;
285 return sprintf "%d %02d:%02d", $days, $hours, $mins;
287 #############################################################
289 # The start of the main line of code
291 #############################################################
293 $starttime = $systime = time;
295 # open the debug file, set various FHs to be unbuffered
300 STDOUT->autoflush(1);
302 Log('cluster', "DXSpider V$version started");
305 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2000 Dirk Koopman G1TLH");
308 dbg('err', "loading prefixes ...");
312 dbg('err', "loading band data ...");
315 # initialise User file system
316 dbg('err', "loading user file system ...");
317 DXUser->init($userfn, 1);
319 # start listening for incoming messages/connects
320 dbg('err', "starting listener ...");
321 Msg->new_server("$clusteraddr", $clusterport, \&login);
324 $SIG{'INT'} = \&cease;
325 $SIG{'TERM'} = \&cease;
326 $SIG{'HUP'} = 'IGNORE';
327 $SIG{'CHLD'} = \&reap;
329 # read in system messages
332 # read in command aliases
335 # initialise the Geomagnetic data engine
339 # initial the Spot stuff
342 # initialise the protocol engine
343 dbg('err', "reading in duplicate spot and WWV info ...");
347 # put in a DXCluster node for us here so we can add users and take them away
348 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
350 # read in any existing message headers and clean out old crap
351 dbg('err', "reading existing message headers ...");
355 # read in any cron jobs
356 dbg('err', "reading cron jobs ...");
359 # read in database descriptors
360 dbg('err', "reading database descriptors ...");
363 # starting local stuff
364 dbg('err', "doing local initialisation ...");
368 dbg('local', "Local::init error $@") if $@;
370 # print various flags
371 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
373 # this, such as it is, is the main loop!
374 dbg('err', "orft we jolly well go ...");
376 #open(DB::OUT, "|tee /tmp/aa");
382 Msg->event_loop(1, 0.1);
384 process_inqueue(); # read in lines from the input queue and despatch them
387 # do timed stuff, ongoing processing happens one a second
388 if ($timenow != $systime) {
392 DXCron::process(); # do cron jobs
393 DXCommandmode::process(); # process ongoing command mode stuff
394 DXProt::process(); # process ongoing ak1a pcxx stuff
395 DXConnect::process();
399 Local::process(); # do any localised processing
401 dbg('local', "Local::process error $@") if $@;
404 last if --$decease <= 0;