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) $!";
68 @inqueue = (); # the main input queue, an array of hashes
69 $systime = 0; # the time now (in seconds)
70 $version = "1.28"; # the version no of the software
71 $starttime = 0; # the starting time of the cluster
72 $lockfn = "cluster.lock"; # lock file name
74 # handle disconnections
78 return if !defined $dxchan;
79 $dxchan->disconnect();
82 # send a message to call on conn and disconnect
85 my ($conn, $call, $mess) = @_;
87 dbg('chan', "-> D $call $mess\n");
88 $conn->send_now("D$call|$mess");
90 dbg('chan', "-> Z $call bye\n");
91 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
94 # handle incoming messages
97 my ($conn, $msg, $err) = @_;
98 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
100 if (defined $err && $err) {
101 disconnect($dxchan) if defined $dxchan;
105 # set up the basic channel info - this needs a bit more thought - there is duplication here
106 if (!defined $dxchan) {
107 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
109 # is there one already connected to me ?
110 my $user = DXUser->get($call);
111 if (DXChannel->get($call)) {
112 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
113 already_conn($conn, $call, $mess);
117 # is there one already connected elsewhere in the cluster (and not a cluster)
119 if (($user->sort eq 'A' || $call eq $myalias) && !DXCluster->get_exact($call)) {
122 if (DXCluster->get($call) || DXChannel->get($call)) {
123 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
124 already_conn($conn, $call, $mess);
128 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
130 if (DXCluster->get($call)) {
131 my $mess = DXM::msg($lang, 'conother', $call);
132 already_conn($conn, $call, $mess);
135 $user = DXUser->new($call);
139 if ($user->lockout) {
140 Log('DXCommand', "$call is locked out, disconnected");
141 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
146 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
147 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
148 die "Invalid sort of user on $call = $sort" if !$dxchan;
151 # queue the message and the channel object for later processing
153 my $self = bless {}, "inqueue";
154 $self->{dxchan} = $dxchan;
155 $self->{data} = $msg;
156 push @inqueue, $self;
165 # cease running this program, close down all the connections nicely
170 $SIG{'TERM'} = 'IGNORE';
171 $SIG{'INT'} = 'IGNORE';
174 Local::finish(); # end local processing
176 dbg('local', "Local::finish error $@") if $@;
178 foreach $dxchan (DXChannel->get_all()) {
179 disconnect($dxchan) unless $dxchan == $DXProt::me;
181 Msg->event_loop(1, 0.05);
182 Msg->event_loop(1, 0.05);
183 Msg->event_loop(1, 0.05);
184 Msg->event_loop(1, 0.05);
185 Msg->event_loop(1, 0.05);
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);
192 Msg->event_loop(1, 0.05);
194 dbg('chan', "DXSpider version $version ended");
195 Log('cluster', "DXSpider V$version stopped");
200 # the reaper of children
203 $SIG{'CHLD'} = \&reap;
207 # this is where the input queue is dealt with and things are dispatched off to other parts of
211 my $self = shift @inqueue;
214 my $data = $self->{data};
215 my $dxchan = $self->{dxchan};
216 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
218 # the above regexp must work
219 return unless ($sort && $call && $line);
221 # translate any crappy characters into hex characters
222 if ($line =~ /[\x00-\x06\x08\x0a-\x1f\x7f-\xff]/o) {
223 $line =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
224 # dbg('chan', "<- $sort $call **CRAP**: $line");
228 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
229 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
232 my $user = $dxchan->user;
233 if ($sort eq 'A' || $sort eq 'O') {
234 $dxchan->start($line, $sort);
235 } elsif ($sort eq 'I') {
236 die "\$user not defined for $call" if !defined $user;
239 $dxchan->normal($line);
241 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
242 } elsif ($sort eq 'Z') {
244 } elsif ($sort eq 'D') {
245 ; # ignored (an echo)
247 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
253 my $t = $systime - $starttime;
254 my $days = int $t / 86400;
256 my $hours = int $t / 3600;
258 my $mins = int $t / 60;
259 return sprintf "%d %02d:%02d", $days, $hours, $mins;
261 #############################################################
263 # The start of the main line of code
265 #############################################################
267 $starttime = $systime = time;
269 # open the debug file, set various FHs to be unbuffered
273 STDOUT->autoflush(1);
275 Log('cluster', "DXSpider V$version started");
278 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
281 print "loading prefixes ...\n";
285 print "loading band data ...\n";
288 # initialise User file system
289 print "loading user file system ...\n";
290 DXUser->init($userfn, 1);
292 # start listening for incoming messages/connects
293 print "starting listener ...\n";
294 Msg->new_server("$clusteraddr", $clusterport, \&login);
297 $SIG{'INT'} = \&cease;
298 $SIG{'TERM'} = \&cease;
299 $SIG{'HUP'} = 'IGNORE';
300 $SIG{'CHLD'} = \&reap;
302 # read in system messages
305 # read in command aliases
308 # initialise the Geomagnetic data engine
311 # initial the Spot stuff
314 # initialise the protocol engine
315 print "reading in duplicate spot and WWV info ...\n";
319 # put in a DXCluster node for us here so we can add users and take them away
320 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
322 # read in any existing message headers and clean out old crap
323 print "reading existing message headers ...\n";
327 # read in any cron jobs
328 print "reading cron jobs ...\n";
331 # starting local stuff
332 print "doing local initialisation ...\n";
336 dbg('local', "Local::init error $@") if $@;
338 # print various flags
339 #print "useful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P\n";
341 # this, such as it is, is the main loop!
342 print "orft we jolly well go ...\n";
343 dbg('chan', "DXSpider version $version started...");
346 Msg->event_loop(1, 0.001);
348 process_inqueue(); # read in lines from the input queue and despatch them
350 # do timed stuff, ongoing processing happens one a second
351 if ($timenow != $systime) {
355 DXCron::process(); # do cron jobs
356 DXCommandmode::process(); # process ongoing command mode stuff
357 DXProt::process(); # process ongoing ak1a pcxx stuff
358 DXConnect::process();
360 Local::process(); # do any localised processing
362 dbg('local', "Local::process error $@") if $@;
365 last if --$decease <= 0;