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)) {
112 $dxchan->conn(undef);
121 # set up the basic channel info - this needs a bit more thought - there is duplication here
122 if (!defined $dxchan) {
123 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
125 # is there one already connected to me - locally?
126 my $user = DXUser->get($call);
127 if (DXChannel->get($call)) {
128 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call);
129 already_conn($conn, $call, $mess);
133 # is there one already connected elsewhere in the cluster?
135 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
138 if (DXCluster->get_exact($call)) {
139 my $mess = DXM::msg($lang, $user->is_node ? 'concluster' : 'conother', $call);
140 already_conn($conn, $call, $mess);
144 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
146 if (DXCluster->get_exact($call)) {
147 my $mess = DXM::msg($lang, 'conother', $call);
148 already_conn($conn, $call, $mess);
151 $user = DXUser->new($call);
155 if ($user->lockout) {
156 Log('DXCommand', "$call is locked out, disconnected");
157 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
162 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
163 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
164 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
165 die "Invalid sort of user on $call = $sort" if !$dxchan;
168 # queue the message and the channel object for later processing
170 my $self = bless {}, "inqueue";
171 $self->{dxchan} = $dxchan;
172 $self->{data} = $msg;
173 push @inqueue, $self;
182 # cease running this program, close down all the connections nicely
187 $SIG{'TERM'} = 'IGNORE';
188 $SIG{'INT'} = 'IGNORE';
191 Local::finish(); # end local processing
193 dbg('local', "Local::finish error $@") if $@;
196 foreach $dxchan (DXChannel->get_all()) {
197 next unless $dxchan->is_node;
198 disconnect($dxchan) unless $dxchan == $DXProt::me;
200 Msg->event_loop(1, 0.05);
201 Msg->event_loop(1, 0.05);
202 Msg->event_loop(1, 0.05);
203 Msg->event_loop(1, 0.05);
204 Msg->event_loop(1, 0.05);
205 Msg->event_loop(1, 0.05);
208 foreach $dxchan (DXChannel->get_all()) {
209 next if $dxchan->is_node;
210 disconnect($dxchan) unless $dxchan == $DXProt::me;
212 Msg->event_loop(1, 0.05);
213 Msg->event_loop(1, 0.05);
214 Msg->event_loop(1, 0.05);
215 Msg->event_loop(1, 0.05);
216 Msg->event_loop(1, 0.05);
217 Msg->event_loop(1, 0.05);
220 # close all databases
223 dbg('chan', "DXSpider version $version ended");
224 Log('cluster', "DXSpider V$version stopped");
228 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
232 # the reaper of children
235 $SIG{'CHLD'} = \&reap;
237 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
240 # this is where the input queue is dealt with and things are dispatched off to other parts of
244 my $self = shift @inqueue;
247 my $data = $self->{data};
248 my $dxchan = $self->{dxchan};
249 my ($sort, $call, $line) = $data =~ /^(\w)([^\|]+)\|(.*)$/;
252 # the above regexp must work
253 return unless ($sort && $call && $line);
255 # translate any crappy characters into hex characters
256 if ($line =~ /[\x00-\x06\x08\x0a-\x1f\x7f-\xff]/o) {
257 $line =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
260 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
261 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
264 my $user = $dxchan->user;
265 if ($sort eq 'A' || $sort eq 'O') {
266 $dxchan->start($line, $sort);
267 } elsif ($sort eq 'I') {
268 die "\$user not defined for $call" if !defined $user;
270 $dxchan->normal($line);
271 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
272 } elsif ($sort eq 'Z') {
273 $dxchan->conn(undef);
275 } elsif ($sort eq 'D') {
276 ; # ignored (an echo)
278 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
284 my $t = $systime - $starttime;
285 my $days = int $t / 86400;
287 my $hours = int $t / 3600;
289 my $mins = int $t / 60;
290 return sprintf "%d %02d:%02d", $days, $hours, $mins;
292 #############################################################
294 # The start of the main line of code
296 #############################################################
298 $starttime = $systime = time;
300 # open the debug file, set various FHs to be unbuffered
305 STDOUT->autoflush(1);
307 Log('cluster', "DXSpider V$version started");
310 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2000 Dirk Koopman G1TLH");
313 dbg('err', "loading prefixes ...");
317 dbg('err', "loading band data ...");
320 # initialise User file system
321 dbg('err', "loading user file system ...");
322 DXUser->init($userfn, 1);
324 # start listening for incoming messages/connects
325 dbg('err', "starting listener ...");
326 Msg->new_server("$clusteraddr", $clusterport, \&login);
329 $SIG{'INT'} = \&cease;
330 $SIG{'TERM'} = \&cease;
331 $SIG{'HUP'} = 'IGNORE';
332 $SIG{'CHLD'} = \&reap;
334 # read in system messages
337 # read in command aliases
340 # initialise the Geomagnetic data engine
344 # initial the Spot stuff
347 # initialise the protocol engine
348 dbg('err', "reading in duplicate spot and WWV info ...");
352 # put in a DXCluster node for us here so we can add users and take them away
353 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
355 # read in any existing message headers and clean out old crap
356 dbg('err', "reading existing message headers ...");
360 # read in any cron jobs
361 dbg('err', "reading cron jobs ...");
364 # read in database descriptors
365 dbg('err', "reading database descriptors ...");
368 # starting local stuff
369 dbg('err', "doing local initialisation ...");
373 dbg('local', "Local::init error $@") if $@;
375 # print various flags
376 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
378 # this, such as it is, is the main loop!
379 dbg('err', "orft we jolly well go ...");
381 #open(DB::OUT, "|tee /tmp/aa");
385 dbg('err', "Broken PIPE signal received");
392 Msg->event_loop(1, 0.1);
394 process_inqueue(); # read in lines from the input queue and despatch them
397 # do timed stuff, ongoing processing happens one a second
398 if ($timenow != $systime) {
402 DXCron::process(); # do cron jobs
403 DXCommandmode::process(); # process ongoing command mode stuff
404 DXProt::process(); # process ongoing ak1a pcxx stuff
405 DXConnect::process();
409 Local::process(); # do any localised processing
411 dbg('local', "Local::process error $@") if $@;
414 last if --$decease <= 0;