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
13 # make sure that modules are searched in the order local then perl
15 # root of directory tree for this system
17 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
19 unshift @INC, "$root/perl"; # this IS the right way round!
20 unshift @INC, "$root/local";
23 # $Exporter::Verbose = 1;
48 @inqueue = (); # the main input queue, an array of hashes
49 $systime = 0; # the time now (in seconds)
50 $version = 1.5; # the version no of the software
51 $starttime = 0; # the starting time of the cluster
53 # handle disconnections
57 return if !defined $dxchan;
58 $dxchan->disconnect();
61 # handle incoming messages
64 my ($conn, $msg, $err) = @_;
65 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
67 if (defined $err && $err) {
68 disconnect($dxchan) if defined $dxchan;
72 # set up the basic channel info - this needs a bit more thought - there is duplication here
73 if (!defined $dxchan) {
74 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
76 # is there one already connected?
77 if (DXChannel->get($call)) {
78 my $mess = DXM::msg($lang, 'conother', $call);
79 dbg('chan', "-> D $call $mess\n");
80 $conn->send_now("D$call|$mess");
82 dbg('chan', "-> Z $call bye\n");
83 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
87 # is there one already connected elsewhere in the cluster?
88 if (DXCluster->get($call)) {
89 my $mess = DXM::msg($lang, 'concluster', $call);
90 dbg('chan', "-> D $call $mess\n");
91 $conn->send_now("D$call|$mess");
93 dbg('chan', "-> Z $call bye\n");
94 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
98 my $user = DXUser->get($call);
100 $user = DXUser->new($call);
102 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
107 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
108 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
109 die "Invalid sort of user on $call = $sort" if !$dxchan;
112 # queue the message and the channel object for later processing
114 my $self = bless {}, "inqueue";
115 $self->{dxchan} = $dxchan;
116 $self->{data} = $msg;
117 push @inqueue, $self;
126 # cease running this program, close down all the connections nicely
130 foreach $dxchan (DXChannel->get_all()) {
133 Log('cluster', "DXSpider V$version stopped");
137 # the reaper of children
143 # this is where the input queue is dealt with and things are dispatched off to other parts of
147 my $self = shift @inqueue;
150 my $data = $self->{data};
151 my $dxchan = $self->{dxchan};
152 my ($sort, $call, $line) = $data =~ /^(\w)(\w+)\|(.*)$/;
154 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
155 dbg('chan', "<- $sort $call $line\n");
158 my $user = $dxchan->user;
159 if ($sort eq 'A' || $sort eq 'O') {
160 $dxchan->start($line, $sort);
161 } elsif ($sort eq 'D') {
162 die "\$user not defined for $call" if !defined $user;
165 $dxchan->normal($line);
167 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
168 } elsif ($sort eq 'Z') {
171 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
177 my $t = $systime - $starttime;
178 my $days = int $t / 86400;
180 my $hours = int $t / 3600;
182 my $mins = int $t / 60;
183 return sprintf "%d %02d:%02d", $days, $hours, $mins;
185 #############################################################
187 # The start of the main line of code
189 #############################################################
191 $starttime = $systime = time;
193 # open the debug file, set various FHs to be unbuffered
197 STDOUT->autoflush(1);
199 Log('cluster', "DXSpider V$version started");
202 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
205 print "loading prefixes ...\n";
209 print "loading band data ...\n";
212 # initialise User file system
213 print "loading user file system ...\n";
214 DXUser->init($userfn);
216 # start listening for incoming messages/connects
217 print "starting listener ...\n";
218 Msg->new_server("$clusteraddr", $clusterport, \&login);
221 $SIG{'INT'} = \&cease;
222 $SIG{'TERM'} = \&cease;
223 $SIG{'HUP'} = 'IGNORE';
224 $SIG{'CHLD'} = \&reap;
226 # read in system messages
229 # read in command aliases
232 # initialise the protocol engine
235 # initialise the Geomagnetic data engine
238 # initial the Spot stuff
241 # put in a DXCluster node for us here so we can add users and take them away
242 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
244 # read in any existing message headers and clean out old crap
245 print "reading existing message headers\n";
249 # read in any cron jobs
250 print "reading cron jobs\n";
253 # this, such as it is, is the main loop!
254 print "orft we jolly well go ...\n";
257 Msg->event_loop(1, 0.001);
259 process_inqueue(); # read in lines from the input queue and despatch them
261 # do timed stuff, ongoing processing happens one a second
262 if ($timenow != $systime) {
266 DXCron::process(); # do cron jobs
267 DXCommandmode::process(); # process ongoing command mode stuff
268 DXProt::process(); # process ongoing ak1a pcxx stuff
269 DXConnect::process();
272 last if --$decease <= 0;