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";
25 # $Exporter::Verbose = 1;
53 @inqueue = (); # the main input queue, an array of hashes
54 $systime = 0; # the time now (in seconds)
55 $version = "1.20"; # the version no of the software
56 $starttime = 0; # the starting time of the cluster
58 # handle disconnections
62 return if !defined $dxchan;
63 $dxchan->disconnect();
66 # send a message to call on conn and disconnect
69 my ($conn, $call, $mess) = @_;
71 dbg('chan', "-> D $call $mess\n");
72 $conn->send_now("D$call|$mess");
74 dbg('chan', "-> Z $call bye\n");
75 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
78 # handle incoming messages
81 my ($conn, $msg, $err) = @_;
82 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
84 if (defined $err && $err) {
85 disconnect($dxchan) if defined $dxchan;
89 # set up the basic channel info - this needs a bit more thought - there is duplication here
90 if (!defined $dxchan) {
91 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
93 # is there one already connected elsewhere in the cluster (and not a cluster)
94 my $user = DXUser->get($call);
96 if (($user->sort eq 'A' || $call eq $myalias) && !DXCluster->get_exact($call)) {
99 if (DXCluster->get($call) || DXChannel->get($call)) {
100 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
101 already_conn($conn, $call, $mess);
105 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
107 if (DXCluster->get($call)) {
108 my $mess = DXM::msg($lang, 'conother', $call);
109 already_conn($conn, $call, $mess);
112 $user = DXUser->new($call);
116 if ($user->lockout) {
117 Log('DXCommand', "$call is locked out, disconnected");
118 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
123 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
124 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
125 die "Invalid sort of user on $call = $sort" if !$dxchan;
128 # queue the message and the channel object for later processing
130 my $self = bless {}, "inqueue";
131 $self->{dxchan} = $dxchan;
132 $self->{data} = $msg;
133 push @inqueue, $self;
142 # cease running this program, close down all the connections nicely
148 Local::finish(); # end local processing
150 dbg('local', "Local::finish error $@") if $@;
152 foreach $dxchan (DXChannel->get_all()) {
153 disconnect($dxchan) unless $dxchan == $DXProt::me;
155 Log('cluster', "DXSpider V$version stopped");
159 # the reaper of children
162 $SIG{'CHLD'} = \&reap;
166 # this is where the input queue is dealt with and things are dispatched off to other parts of
170 my $self = shift @inqueue;
173 my $data = $self->{data};
174 my $dxchan = $self->{dxchan};
175 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
177 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
178 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
181 my $user = $dxchan->user;
182 if ($sort eq 'A' || $sort eq 'O') {
183 $dxchan->start($line, $sort);
184 } elsif ($sort eq 'I') {
185 die "\$user not defined for $call" if !defined $user;
188 $dxchan->normal($line);
190 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
191 } elsif ($sort eq 'Z') {
193 } elsif ($sort eq 'D') {
194 ; # ignored (an echo)
196 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
202 my $t = $systime - $starttime;
203 my $days = int $t / 86400;
205 my $hours = int $t / 3600;
207 my $mins = int $t / 60;
208 return sprintf "%d %02d:%02d", $days, $hours, $mins;
210 #############################################################
212 # The start of the main line of code
214 #############################################################
216 $starttime = $systime = time;
218 # open the debug file, set various FHs to be unbuffered
222 STDOUT->autoflush(1);
224 Log('cluster', "DXSpider V$version started");
227 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
230 print "loading prefixes ...\n";
234 print "loading band data ...\n";
237 # initialise User file system
238 print "loading user file system ...\n";
239 DXUser->init($userfn);
241 # start listening for incoming messages/connects
242 print "starting listener ...\n";
243 Msg->new_server("$clusteraddr", $clusterport, \&login);
246 $SIG{'INT'} = \&cease;
247 $SIG{'TERM'} = \&cease;
248 $SIG{'HUP'} = 'IGNORE';
249 $SIG{'CHLD'} = \&reap;
251 # read in system messages
254 # read in command aliases
257 # initialise the Geomagnetic data engine
260 # initial the Spot stuff
263 # initialise the protocol engine
264 print "reading in duplicate spot and WWV info ...\n";
268 # put in a DXCluster node for us here so we can add users and take them away
269 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
271 # read in any existing message headers and clean out old crap
272 print "reading existing message headers ...\n";
276 # read in any cron jobs
277 print "reading cron jobs ...\n";
280 # starting local stuff
281 print "doing local initialisation ...\n";
285 dbg('local', "Local::init error $@") if $@;
289 # print various flags
290 #print "useful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P\n";
292 # this, such as it is, is the main loop!
293 print "orft we jolly well go ...\n";
296 Msg->event_loop(1, 0.001);
298 process_inqueue(); # read in lines from the input queue and despatch them
300 # do timed stuff, ongoing processing happens one a second
301 if ($timenow != $systime) {
305 DXCron::process(); # do cron jobs
306 DXCommandmode::process(); # process ongoing command mode stuff
307 DXProt::process(); # process ongoing ak1a pcxx stuff
308 DXConnect::process();
310 Local::process(); # do any localised processing
312 dbg('local', "Local::process error $@") if $@;
315 last if --$decease <= 0;