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.34"; # the version no of the software
72 $starttime = 0; # the starting time of the cluster
73 $lockfn = "cluster.lock"; # lock file name
75 # handle disconnections
79 return if !defined $dxchan;
80 $dxchan->disconnect();
83 # send a message to call on conn and disconnect
86 my ($conn, $call, $mess) = @_;
88 dbg('chan', "-> D $call $mess\n");
89 $conn->send_now("D$call|$mess");
91 dbg('chan', "-> Z $call bye\n");
92 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
97 # handle incoming messages
100 my ($conn, $msg, $err) = @_;
101 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
103 if (defined $err && $err) {
104 disconnect($dxchan) if defined $dxchan;
108 # set up the basic channel info - this needs a bit more thought - there is duplication here
109 if (!defined $dxchan) {
110 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
112 # is there one already connected to me ?
113 my $user = DXUser->get($call);
114 if (DXChannel->get($call)) {
115 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
116 already_conn($conn, $call, $mess);
120 # is there one already connected elsewhere in the cluster (and not a cluster)
122 if (($user->sort eq 'A' || $call eq $myalias) && !DXCluster->get_exact($call)) {
125 if (DXCluster->get($call) || DXChannel->get($call)) {
126 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
127 already_conn($conn, $call, $mess);
131 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
133 if (DXCluster->get($call)) {
134 my $mess = DXM::msg($lang, 'conother', $call);
135 already_conn($conn, $call, $mess);
138 $user = DXUser->new($call);
142 if ($user->lockout) {
143 Log('DXCommand', "$call is locked out, disconnected");
144 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
149 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
150 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
151 die "Invalid sort of user on $call = $sort" if !$dxchan;
154 # queue the message and the channel object for later processing
156 my $self = bless {}, "inqueue";
157 $self->{dxchan} = $dxchan;
158 $self->{data} = $msg;
159 push @inqueue, $self;
168 # cease running this program, close down all the connections nicely
173 $SIG{'TERM'} = 'IGNORE';
174 $SIG{'INT'} = 'IGNORE';
177 Local::finish(); # end local processing
179 dbg('local', "Local::finish error $@") if $@;
182 foreach $dxchan (DXChannel->get_all()) {
183 next if $dxchan->is_ak1a;
184 disconnect($dxchan) unless $dxchan == $DXProt::me;
186 Msg->event_loop(1, 0.05);
187 Msg->event_loop(1, 0.05);
188 Msg->event_loop(1, 0.05);
189 Msg->event_loop(1, 0.05);
190 Msg->event_loop(1, 0.05);
191 Msg->event_loop(1, 0.05);
194 foreach $dxchan (DXChannel->get_all()) {
195 next unless $dxchan->is_ak1a;
196 disconnect($dxchan) unless $dxchan == $DXProt::me;
198 Msg->event_loop(1, 0.05);
199 Msg->event_loop(1, 0.05);
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);
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;
231 # this is where the input queue is dealt with and things are dispatched off to other parts of
235 my $self = shift @inqueue;
238 my $data = $self->{data};
239 my $dxchan = $self->{dxchan};
240 my ($sort, $call, $line) = $data =~ /^(\w)([A-Z0-9\-]+)\|(.*)$/;
243 # the above regexp must work
244 return unless ($sort && $call && $line);
246 # translate any crappy characters into hex characters
247 if ($line =~ /[\x00-\x06\x08\x0a-\x1f\x7f-\xff]/o) {
248 $line =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
251 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
252 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
255 my $user = $dxchan->user;
256 if ($sort eq 'A' || $sort eq 'O') {
257 $dxchan->start($line, $sort);
258 } elsif ($sort eq 'I') {
259 die "\$user not defined for $call" if !defined $user;
261 $dxchan->normal($line);
262 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
263 } elsif ($sort eq 'Z') {
265 } elsif ($sort eq 'D') {
266 ; # ignored (an echo)
268 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
274 my $t = $systime - $starttime;
275 my $days = int $t / 86400;
277 my $hours = int $t / 3600;
279 my $mins = int $t / 60;
280 return sprintf "%d %02d:%02d", $days, $hours, $mins;
282 #############################################################
284 # The start of the main line of code
286 #############################################################
288 $starttime = $systime = time;
290 # open the debug file, set various FHs to be unbuffered
295 STDOUT->autoflush(1);
297 Log('cluster', "DXSpider V$version started");
300 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
303 print "loading prefixes ...\n";
307 print "loading band data ...\n";
310 # initialise User file system
311 print "loading user file system ...\n";
312 DXUser->init($userfn, 1);
314 # start listening for incoming messages/connects
315 print "starting listener ...\n";
316 Msg->new_server("$clusteraddr", $clusterport, \&login);
319 $SIG{'INT'} = \&cease;
320 $SIG{'TERM'} = \&cease;
321 $SIG{'HUP'} = 'IGNORE';
322 $SIG{'CHLD'} = \&reap;
324 # read in system messages
327 # read in command aliases
330 # initialise the Geomagnetic data engine
333 # initial the Spot stuff
336 # initialise the protocol engine
337 print "reading in duplicate spot and WWV info ...\n";
341 # put in a DXCluster node for us here so we can add users and take them away
342 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
344 # read in any existing message headers and clean out old crap
345 print "reading existing message headers ...\n";
349 # read in any cron jobs
350 print "reading cron jobs ...\n";
353 # read in database descriptors
354 print "reading database descriptors ...\n";
357 # starting local stuff
358 print "doing local initialisation ...\n";
362 dbg('local', "Local::init error $@") if $@;
364 # print various flags
365 #print "useful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P\n";
367 # this, such as it is, is the main loop!
368 print "orft we jolly well go ...\n";
369 dbg('chan', "DXSpider version $version started...");
372 Msg->event_loop(1, 0.1);
374 process_inqueue(); # read in lines from the input queue and despatch them
376 # do timed stuff, ongoing processing happens one a second
377 if ($timenow != $systime) {
381 DXCron::process(); # do cron jobs
382 DXCommandmode::process(); # process ongoing command mode stuff
383 DXProt::process(); # process ongoing ak1a pcxx stuff
384 DXConnect::process();
388 Local::process(); # do any localised processing
390 dbg('local', "Local::process error $@") if $@;
393 last if --$decease <= 0;