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.43"; # 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) = DXChannel::decode_input(0, $msg);
124 return unless defined $sort;
126 # is there one already connected to me - locally?
127 my $user = DXUser->get($call);
128 if (DXChannel->get($call)) {
129 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call);
130 already_conn($conn, $call, $mess);
134 # is there one already connected elsewhere in the cluster?
136 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
139 if (DXCluster->get_exact($call)) {
140 my $mess = DXM::msg($lang, $user->is_node ? 'concluster' : 'conother', $call);
141 already_conn($conn, $call, $mess);
145 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
147 if (DXCluster->get_exact($call)) {
148 my $mess = DXM::msg($lang, 'conother', $call);
149 already_conn($conn, $call, $mess);
152 $user = DXUser->new($call);
156 if ($user->lockout) {
157 Log('DXCommand', "$call is locked out, disconnected");
158 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
163 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
164 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
165 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
166 die "Invalid sort of user on $call = $sort" if !$dxchan;
169 # queue the message and the channel object for later processing
171 my $self = bless {}, "inqueue";
172 $self->{dxchan} = $dxchan;
173 $self->{data} = $msg;
174 push @inqueue, $self;
183 # cease running this program, close down all the connections nicely
188 $SIG{'TERM'} = 'IGNORE';
189 $SIG{'INT'} = 'IGNORE';
192 Local::finish(); # end local processing
194 dbg('local', "Local::finish error $@") if $@;
197 foreach $dxchan (DXChannel->get_all()) {
198 next unless $dxchan->is_node;
199 disconnect($dxchan) unless $dxchan == $DXProt::me;
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);
206 Msg->event_loop(1, 0.05);
209 foreach $dxchan (DXChannel->get_all()) {
210 next if $dxchan->is_node;
211 disconnect($dxchan) unless $dxchan == $DXProt::me;
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);
218 Msg->event_loop(1, 0.05);
221 # close all databases
224 dbg('chan', "DXSpider version $version ended");
225 Log('cluster', "DXSpider V$version stopped");
229 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
233 # the reaper of children
236 $SIG{'CHLD'} = \&reap;
238 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
241 # this is where the input queue is dealt with and things are dispatched off to other parts of
245 my $self = shift @inqueue;
248 my $data = $self->{data};
249 my $dxchan = $self->{dxchan};
251 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
252 return unless defined $sort;
254 # translate any crappy characters into hex characters
255 if ($line =~ /[\x00-\x06\x08\x0a-\x1f\x7f-\xff]/o) {
256 $line =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
259 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
260 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
263 my $user = $dxchan->user;
264 if ($sort eq 'A' || $sort eq 'O') {
265 $dxchan->start($line, $sort);
266 } elsif ($sort eq 'I') {
267 die "\$user not defined for $call" if !defined $user;
269 $dxchan->normal($line);
270 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
271 } elsif ($sort eq 'Z') {
272 $dxchan->conn(undef);
274 } elsif ($sort eq 'D') {
275 ; # ignored (an echo)
277 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
283 my $t = $systime - $starttime;
284 my $days = int $t / 86400;
286 my $hours = int $t / 3600;
288 my $mins = int $t / 60;
289 return sprintf "%d %02d:%02d", $days, $hours, $mins;
291 #############################################################
293 # The start of the main line of code
295 #############################################################
297 $starttime = $systime = time;
299 # open the debug file, set various FHs to be unbuffered
304 STDOUT->autoflush(1);
306 Log('cluster', "DXSpider V$version started");
309 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2000 Dirk Koopman G1TLH");
312 dbg('err', "loading prefixes ...");
316 dbg('err', "loading band data ...");
319 # initialise User file system
320 dbg('err', "loading user file system ...");
321 DXUser->init($userfn, 1);
323 # start listening for incoming messages/connects
324 dbg('err', "starting listener ...");
325 Msg->new_server("$clusteraddr", $clusterport, \&login);
328 $SIG{'INT'} = \&cease;
329 $SIG{'TERM'} = \&cease;
330 $SIG{'HUP'} = 'IGNORE';
331 $SIG{'CHLD'} = \&reap;
333 # read in system messages
336 # read in command aliases
339 # initialise the Geomagnetic data engine
343 # initial the Spot stuff
346 # initialise the protocol engine
347 dbg('err', "reading in duplicate spot and WWV info ...");
351 # put in a DXCluster node for us here so we can add users and take them away
352 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
354 # read in any existing message headers and clean out old crap
355 dbg('err', "reading existing message headers ...");
359 # read in any cron jobs
360 dbg('err', "reading cron jobs ...");
363 # read in database descriptors
364 dbg('err', "reading database descriptors ...");
367 # starting local stuff
368 dbg('err', "doing local initialisation ...");
372 dbg('local', "Local::init error $@") if $@;
374 # print various flags
375 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
377 # this, such as it is, is the main loop!
378 dbg('err', "orft we jolly well go ...");
380 #open(DB::OUT, "|tee /tmp/aa");
384 dbg('err', "Broken PIPE signal received");
391 Msg->event_loop(1, 0.1);
393 process_inqueue(); # read in lines from the input queue and despatch them
396 # do timed stuff, ongoing processing happens one a second
397 if ($timenow != $systime) {
401 DXCron::process(); # do cron jobs
402 DXCommandmode::process(); # process ongoing command mode stuff
403 DXProt::process(); # process ongoing ak1a pcxx stuff
404 DXConnect::process();
408 Local::process(); # do any localised processing
410 dbg('local', "Local::process error $@") if $@;
413 last if --$decease <= 0;