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;
49 @inqueue = (); # the main input queue, an array of hashes
50 $systime = 0; # the time now (in seconds)
51 $version = "1.13"; # the version no of the software
52 $starttime = 0; # the starting time of the cluster
54 # handle disconnections
58 return if !defined $dxchan;
59 $dxchan->disconnect();
62 # send a message to call on conn and disconnect
65 my ($conn, $call, $mess) = @_;
67 dbg('chan', "-> D $call $mess\n");
68 $conn->send_now("D$call|$mess");
70 dbg('chan', "-> Z $call bye\n");
71 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
74 # handle incoming messages
77 my ($conn, $msg, $err) = @_;
78 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
80 if (defined $err && $err) {
81 disconnect($dxchan) if defined $dxchan;
85 # set up the basic channel info - this needs a bit more thought - there is duplication here
86 if (!defined $dxchan) {
87 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
89 # is there one already connected elsewhere in the cluster (and not a cluster)
90 my $user = DXUser->get($call);
92 if (($user->sort eq 'A' || $call == $myalias) && !DXCluster->get_exact($call)) {
95 if (DXCluster->get($call) || DXChannel->get($call)) {
96 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
97 already_conn($conn, $call, $mess);
102 if (DXCluster->get($call) || DXChannel->get($call)) {
103 my $mess = DXM::msg($lang, 'conother', $call);
104 already_conn($conn, $call, $mess);
110 # the user MAY have an SSID if local, but otherwise doesn't
111 $user = DXUser->get($call);
112 if (!defined $user) {
113 $user = DXUser->new($call);
115 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
119 if ($user->lockout) {
120 Log('DXCommand', "$call is locked out, disconnected");
121 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
126 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
127 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
128 die "Invalid sort of user on $call = $sort" if !$dxchan;
131 # queue the message and the channel object for later processing
133 my $self = bless {}, "inqueue";
134 $self->{dxchan} = $dxchan;
135 $self->{data} = $msg;
136 push @inqueue, $self;
145 # cease running this program, close down all the connections nicely
149 foreach $dxchan (DXChannel->get_all()) {
150 disconnect($dxchan) unless $dxchan == $DXProt::me;
152 Log('cluster', "DXSpider V$version stopped");
156 # the reaper of children
162 # this is where the input queue is dealt with and things are dispatched off to other parts of
166 my $self = shift @inqueue;
169 my $data = $self->{data};
170 my $dxchan = $self->{dxchan};
171 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
173 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
174 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
177 my $user = $dxchan->user;
178 if ($sort eq 'A' || $sort eq 'O') {
179 $dxchan->start($line, $sort);
180 } elsif ($sort eq 'I') {
181 die "\$user not defined for $call" if !defined $user;
184 $dxchan->normal($line);
186 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
187 } elsif ($sort eq 'Z') {
189 } elsif ($sort eq 'D') {
190 ; # ignored (an echo)
192 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
198 my $t = $systime - $starttime;
199 my $days = int $t / 86400;
201 my $hours = int $t / 3600;
203 my $mins = int $t / 60;
204 return sprintf "%d %02d:%02d", $days, $hours, $mins;
206 #############################################################
208 # The start of the main line of code
210 #############################################################
212 $starttime = $systime = time;
214 # open the debug file, set various FHs to be unbuffered
218 STDOUT->autoflush(1);
220 Log('cluster', "DXSpider V$version started");
223 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
226 print "loading prefixes ...\n";
230 print "loading band data ...\n";
233 # initialise User file system
234 print "loading user file system ...\n";
235 DXUser->init($userfn);
237 # start listening for incoming messages/connects
238 print "starting listener ...\n";
239 Msg->new_server("$clusteraddr", $clusterport, \&login);
242 $SIG{'INT'} = \&cease;
243 $SIG{'TERM'} = \&cease;
244 $SIG{'HUP'} = 'IGNORE';
245 $SIG{'CHLD'} = \&reap;
247 # read in system messages
250 # read in command aliases
253 # initialise the protocol engine
256 # initialise the Geomagnetic data engine
259 # initial the Spot stuff
262 # put in a DXCluster node for us here so we can add users and take them away
263 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
265 # read in any existing message headers and clean out old crap
266 print "reading existing message headers\n";
270 # read in any cron jobs
271 print "reading cron jobs\n";
274 # this, such as it is, is the main loop!
275 print "orft we jolly well go ...\n";
278 Msg->event_loop(1, 0.001);
280 process_inqueue(); # read in lines from the input queue and despatch them
282 # do timed stuff, ongoing processing happens one a second
283 if ($timenow != $systime) {
287 DXCron::process(); # do cron jobs
288 DXCommandmode::process(); # process ongoing command mode stuff
289 DXProt::process(); # process ongoing ak1a pcxx stuff
290 DXConnect::process();
293 last if --$decease <= 0;