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) $!";
69 @inqueue = (); # the main input queue, an array of hashes
70 $systime = 0; # the time now (in seconds)
71 $version = "1.40"; # the version no of the software
72 $starttime = 0; # the starting time of the cluster
73 $lockfn = "cluster.lock"; # lock file name
74 @outstanding_connects = (); # list of outstanding connects
76 # handle disconnections
80 return if !defined $dxchan;
81 $dxchan->disconnect();
84 # send a message to call on conn and disconnect
87 my ($conn, $call, $mess) = @_;
89 dbg('chan', "-> D $call $mess\n");
90 $conn->send_now("D$call|$mess");
92 dbg('chan', "-> Z $call bye\n");
93 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
98 # handle incoming messages
101 my ($conn, $msg, $err) = @_;
102 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
104 if (!defined $msg || (defined $err && $err)) {
113 # set up the basic channel info - this needs a bit more thought - there is duplication here
114 if (!defined $dxchan) {
115 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
117 # is there one already connected to me - locally?
118 my $user = DXUser->get($call);
119 if (DXChannel->get($call)) {
120 my $mess = DXM::msg($lang, ($user && $user->sort eq 'A') ? 'concluster' : 'conother', $call);
121 already_conn($conn, $call, $mess);
125 # is there one already connected elsewhere in the cluster?
127 if (($user->sort eq 'A' || $call eq $myalias) && !DXCluster->get_exact($call)) {
130 if (DXCluster->get_exact($call)) {
131 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
132 already_conn($conn, $call, $mess);
136 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
138 if (DXCluster->get_exact($call)) {
139 my $mess = DXM::msg($lang, 'conother', $call);
140 already_conn($conn, $call, $mess);
143 $user = DXUser->new($call);
147 if ($user->lockout) {
148 Log('DXCommand', "$call is locked out, disconnected");
149 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
154 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
155 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
156 $dxchan = BBS->new($call, $conn, $user) if ($user->sort eq 'B');
157 die "Invalid sort of user on $call = $sort" if !$dxchan;
160 # queue the message and the channel object for later processing
162 my $self = bless {}, "inqueue";
163 $self->{dxchan} = $dxchan;
164 $self->{data} = $msg;
165 push @inqueue, $self;
174 # cease running this program, close down all the connections nicely
179 $SIG{'TERM'} = 'IGNORE';
180 $SIG{'INT'} = 'IGNORE';
183 Local::finish(); # end local processing
185 dbg('local', "Local::finish error $@") if $@;
188 foreach $dxchan (DXChannel->get_all()) {
189 next unless $dxchan->is_ak1a;
190 disconnect($dxchan) unless $dxchan == $DXProt::me;
192 Msg->event_loop(1, 0.05);
193 Msg->event_loop(1, 0.05);
194 Msg->event_loop(1, 0.05);
195 Msg->event_loop(1, 0.05);
196 Msg->event_loop(1, 0.05);
197 Msg->event_loop(1, 0.05);
200 foreach $dxchan (DXChannel->get_all()) {
201 next if $dxchan->is_ak1a;
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 # close all databases
215 dbg('chan', "DXSpider version $version ended");
216 Log('cluster', "DXSpider V$version stopped");
220 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
224 # the reaper of children
227 $SIG{'CHLD'} = \&reap;
229 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
232 # this is where the input queue is dealt with and things are dispatched off to other parts of
236 my $self = shift @inqueue;
239 my $data = $self->{data};
240 my $dxchan = $self->{dxchan};
241 my ($sort, $call, $line) = $data =~ /^(\w)([A-Z0-9\-]+)\|(.*)$/;
244 # the above regexp must work
245 return unless ($sort && $call && $line);
247 # translate any crappy characters into hex characters
248 if ($line =~ /[\x00-\x06\x08\x0a-\x1f\x7f-\xff]/o) {
249 $line =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
252 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
253 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
256 my $user = $dxchan->user;
257 if ($sort eq 'A' || $sort eq 'O') {
258 $dxchan->start($line, $sort);
259 } elsif ($sort eq 'I') {
260 die "\$user not defined for $call" if !defined $user;
262 $dxchan->normal($line);
263 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
264 } elsif ($sort eq 'Z') {
265 $dxchan->conn(undef);
267 } elsif ($sort eq 'D') {
268 ; # ignored (an echo)
270 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
276 my $t = $systime - $starttime;
277 my $days = int $t / 86400;
279 my $hours = int $t / 3600;
281 my $mins = int $t / 60;
282 return sprintf "%d %02d:%02d", $days, $hours, $mins;
284 #############################################################
286 # The start of the main line of code
288 #############################################################
290 $starttime = $systime = time;
292 # open the debug file, set various FHs to be unbuffered
297 STDOUT->autoflush(1);
299 Log('cluster', "DXSpider V$version started");
302 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-1999 Dirk Koopman G1TLH");
305 dbg('err', "loading prefixes ...");
309 dbg('err', "loading band data ...");
312 # initialise User file system
313 dbg('err', "loading user file system ...");
314 DXUser->init($userfn, 1);
316 # start listening for incoming messages/connects
317 dbg('err', "starting listener ...");
318 Msg->new_server("$clusteraddr", $clusterport, \&login);
321 $SIG{'INT'} = \&cease;
322 $SIG{'TERM'} = \&cease;
323 $SIG{'HUP'} = 'IGNORE';
324 $SIG{'CHLD'} = \&reap;
326 # read in system messages
329 # read in command aliases
332 # initialise the Geomagnetic data engine
335 # initial the Spot stuff
338 # initialise the protocol engine
339 dbg('err', "reading in duplicate spot and WWV info ...");
343 # put in a DXCluster node for us here so we can add users and take them away
344 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
346 # read in any existing message headers and clean out old crap
347 dbg('err', "reading existing message headers ...");
351 # read in any cron jobs
352 dbg('err', "reading cron jobs ...");
355 # read in database descriptors
356 dbg('err', "reading database descriptors ...");
359 # starting local stuff
360 dbg('err', "doing local initialisation ...");
364 dbg('local', "Local::init error $@") if $@;
366 # print various flags
367 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
369 # this, such as it is, is the main loop!
370 dbg('err', "orft we jolly well go ...");
372 #open(DB::OUT, "|tee /tmp/aa");
378 Msg->event_loop(1, 0.1);
380 process_inqueue(); # read in lines from the input queue and despatch them
383 # do timed stuff, ongoing processing happens one a second
384 if ($timenow != $systime) {
388 DXCron::process(); # do cron jobs
389 DXCommandmode::process(); # process ongoing command mode stuff
390 DXProt::process(); # process ongoing ak1a pcxx stuff
391 DXConnect::process();
395 Local::process(); # do any localised processing
397 dbg('local', "Local::process error $@") if $@;
400 last if --$decease <= 0;