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) $!";
66 @inqueue = (); # the main input queue, an array of hashes
67 $systime = 0; # the time now (in seconds)
68 $version = "1.24"; # the version no of the software
69 $starttime = 0; # the starting time of the cluster
70 $lockfn = "cluster.lock"; # lock file name
72 # handle disconnections
76 return if !defined $dxchan;
77 $dxchan->disconnect();
80 # send a message to call on conn and disconnect
83 my ($conn, $call, $mess) = @_;
85 dbg('chan', "-> D $call $mess\n");
86 $conn->send_now("D$call|$mess");
88 dbg('chan', "-> Z $call bye\n");
89 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
92 # handle incoming messages
95 my ($conn, $msg, $err) = @_;
96 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
98 if (defined $err && $err) {
99 disconnect($dxchan) if defined $dxchan;
103 # set up the basic channel info - this needs a bit more thought - there is duplication here
104 if (!defined $dxchan) {
105 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
107 # is there one already connected elsewhere in the cluster (and not a cluster)
108 my $user = DXUser->get($call);
110 if (($user->sort eq 'A' || $call eq $myalias) && !DXCluster->get_exact($call)) {
113 if (DXCluster->get($call) || DXChannel->get($call)) {
114 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
115 already_conn($conn, $call, $mess);
119 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
121 if (DXCluster->get($call)) {
122 my $mess = DXM::msg($lang, 'conother', $call);
123 already_conn($conn, $call, $mess);
126 $user = DXUser->new($call);
130 if ($user->lockout) {
131 Log('DXCommand', "$call is locked out, disconnected");
132 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
137 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
138 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
139 die "Invalid sort of user on $call = $sort" if !$dxchan;
142 # queue the message and the channel object for later processing
144 my $self = bless {}, "inqueue";
145 $self->{dxchan} = $dxchan;
146 $self->{data} = $msg;
147 push @inqueue, $self;
156 # cease running this program, close down all the connections nicely
162 Local::finish(); # end local processing
164 dbg('local', "Local::finish error $@") if $@;
166 foreach $dxchan (DXChannel->get_all()) {
167 disconnect($dxchan) unless $dxchan == $DXProt::me;
169 Msg->event_loop(1, 0.05);
170 Msg->event_loop(1, 0.05);
171 Msg->event_loop(1, 0.05);
172 Msg->event_loop(1, 0.05);
173 Log('cluster', "DXSpider V$version stopped");
178 # the reaper of children
181 $SIG{'CHLD'} = \&reap;
185 # this is where the input queue is dealt with and things are dispatched off to other parts of
189 my $self = shift @inqueue;
192 my $data = $self->{data};
193 my $dxchan = $self->{dxchan};
194 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
196 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
197 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
200 my $user = $dxchan->user;
201 if ($sort eq 'A' || $sort eq 'O') {
202 $dxchan->start($line, $sort);
203 } elsif ($sort eq 'I') {
204 die "\$user not defined for $call" if !defined $user;
207 $dxchan->normal($line);
209 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
210 } elsif ($sort eq 'Z') {
212 } elsif ($sort eq 'D') {
213 ; # ignored (an echo)
215 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
221 my $t = $systime - $starttime;
222 my $days = int $t / 86400;
224 my $hours = int $t / 3600;
226 my $mins = int $t / 60;
227 return sprintf "%d %02d:%02d", $days, $hours, $mins;
229 #############################################################
231 # The start of the main line of code
233 #############################################################
235 $starttime = $systime = time;
237 # open the debug file, set various FHs to be unbuffered
241 STDOUT->autoflush(1);
243 Log('cluster', "DXSpider V$version started");
246 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
249 print "loading prefixes ...\n";
253 print "loading band data ...\n";
256 # initialise User file system
257 print "loading user file system ...\n";
258 DXUser->init($userfn, 1);
260 # start listening for incoming messages/connects
261 print "starting listener ...\n";
262 Msg->new_server("$clusteraddr", $clusterport, \&login);
265 $SIG{'INT'} = \&cease;
266 $SIG{'TERM'} = \&cease;
267 $SIG{'HUP'} = 'IGNORE';
268 $SIG{'CHLD'} = \&reap;
270 # read in system messages
273 # read in command aliases
276 # initialise the Geomagnetic data engine
279 # initial the Spot stuff
282 # initialise the protocol engine
283 print "reading in duplicate spot and WWV info ...\n";
287 # put in a DXCluster node for us here so we can add users and take them away
288 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
290 # read in any existing message headers and clean out old crap
291 print "reading existing message headers ...\n";
295 # read in any cron jobs
296 print "reading cron jobs ...\n";
299 # starting local stuff
300 print "doing local initialisation ...\n";
304 dbg('local', "Local::init error $@") if $@;
306 # print various flags
307 #print "useful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P\n";
309 # this, such as it is, is the main loop!
310 print "orft we jolly well go ...\n";
313 Msg->event_loop(1, 0.001);
315 process_inqueue(); # read in lines from the input queue and despatch them
317 # do timed stuff, ongoing processing happens one a second
318 if ($timenow != $systime) {
322 DXCron::process(); # do cron jobs
323 DXCommandmode::process(); # process ongoing command mode stuff
324 DXProt::process(); # process ongoing ak1a pcxx stuff
325 DXConnect::process();
327 Local::process(); # do any localised processing
329 dbg('local', "Local::process error $@") if $@;
332 last if --$decease <= 0;