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
17 # root of directory tree for this system
19 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
21 unshift @INC, "$root/perl"; # this IS the right way round!
22 unshift @INC, "$root/local";
24 # try to create and lock a lockfile (this isn't atomic but
26 $lockfn = "$root/perl/cluster.lock"; # lock file name
28 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
31 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
34 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
65 @inqueue = (); # the main input queue, an array of hashes
66 $systime = 0; # the time now (in seconds)
67 $version = "1.23"; # the version no of the software
68 $starttime = 0; # the starting time of the cluster
69 $lockfn = "cluster.lock"; # lock file name
71 # handle disconnections
75 return if !defined $dxchan;
76 $dxchan->disconnect();
79 # send a message to call on conn and disconnect
82 my ($conn, $call, $mess) = @_;
84 dbg('chan', "-> D $call $mess\n");
85 $conn->send_now("D$call|$mess");
87 dbg('chan', "-> Z $call bye\n");
88 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
91 # handle incoming messages
94 my ($conn, $msg, $err) = @_;
95 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
97 if (defined $err && $err) {
98 disconnect($dxchan) if defined $dxchan;
102 # set up the basic channel info - this needs a bit more thought - there is duplication here
103 if (!defined $dxchan) {
104 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
106 # is there one already connected elsewhere in the cluster (and not a cluster)
107 my $user = DXUser->get($call);
109 if (($user->sort eq 'A' || $call eq $myalias) && !DXCluster->get_exact($call)) {
112 if (DXCluster->get($call) || DXChannel->get($call)) {
113 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
114 already_conn($conn, $call, $mess);
118 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
120 if (DXCluster->get($call)) {
121 my $mess = DXM::msg($lang, 'conother', $call);
122 already_conn($conn, $call, $mess);
125 $user = DXUser->new($call);
129 if ($user->lockout) {
130 Log('DXCommand', "$call is locked out, disconnected");
131 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
136 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
137 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
138 die "Invalid sort of user on $call = $sort" if !$dxchan;
141 # queue the message and the channel object for later processing
143 my $self = bless {}, "inqueue";
144 $self->{dxchan} = $dxchan;
145 $self->{data} = $msg;
146 push @inqueue, $self;
155 # cease running this program, close down all the connections nicely
161 Local::finish(); # end local processing
163 dbg('local', "Local::finish error $@") if $@;
165 foreach $dxchan (DXChannel->get_all()) {
166 disconnect($dxchan) unless $dxchan == $DXProt::me;
168 Log('cluster', "DXSpider V$version stopped");
173 # the reaper of children
176 $SIG{'CHLD'} = \&reap;
180 # this is where the input queue is dealt with and things are dispatched off to other parts of
184 my $self = shift @inqueue;
187 my $data = $self->{data};
188 my $dxchan = $self->{dxchan};
189 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
191 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
192 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
195 my $user = $dxchan->user;
196 if ($sort eq 'A' || $sort eq 'O') {
197 $dxchan->start($line, $sort);
198 } elsif ($sort eq 'I') {
199 die "\$user not defined for $call" if !defined $user;
202 $dxchan->normal($line);
204 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
205 } elsif ($sort eq 'Z') {
207 } elsif ($sort eq 'D') {
208 ; # ignored (an echo)
210 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
216 my $t = $systime - $starttime;
217 my $days = int $t / 86400;
219 my $hours = int $t / 3600;
221 my $mins = int $t / 60;
222 return sprintf "%d %02d:%02d", $days, $hours, $mins;
224 #############################################################
226 # The start of the main line of code
228 #############################################################
230 $starttime = $systime = time;
232 # open the debug file, set various FHs to be unbuffered
236 STDOUT->autoflush(1);
238 Log('cluster', "DXSpider V$version started");
241 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
244 print "loading prefixes ...\n";
248 print "loading band data ...\n";
251 # initialise User file system
252 print "loading user file system ...\n";
253 DXUser->init($userfn);
255 # start listening for incoming messages/connects
256 print "starting listener ...\n";
257 Msg->new_server("$clusteraddr", $clusterport, \&login);
260 $SIG{'INT'} = \&cease;
261 $SIG{'TERM'} = \&cease;
262 $SIG{'HUP'} = 'IGNORE';
263 $SIG{'CHLD'} = \&reap;
265 # read in system messages
268 # read in command aliases
271 # initialise the Geomagnetic data engine
274 # initial the Spot stuff
277 # initialise the protocol engine
278 print "reading in duplicate spot and WWV info ...\n";
282 # put in a DXCluster node for us here so we can add users and take them away
283 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
285 # read in any existing message headers and clean out old crap
286 print "reading existing message headers ...\n";
290 # read in any cron jobs
291 print "reading cron jobs ...\n";
294 # starting local stuff
295 print "doing local initialisation ...\n";
299 dbg('local', "Local::init error $@") if $@;
301 # print various flags
302 #print "useful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P\n";
304 # this, such as it is, is the main loop!
305 print "orft we jolly well go ...\n";
308 Msg->event_loop(1, 0.001);
310 process_inqueue(); # read in lines from the input queue and despatch them
312 # do timed stuff, ongoing processing happens one a second
313 if ($timenow != $systime) {
317 DXCron::process(); # do cron jobs
318 DXCommandmode::process(); # process ongoing command mode stuff
319 DXProt::process(); # process ongoing ak1a pcxx stuff
320 DXConnect::process();
322 Local::process(); # do any localised processing
324 dbg('local', "Local::process error $@") if $@;
327 last if --$decease <= 0;