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;
51 @inqueue = (); # the main input queue, an array of hashes
52 $systime = 0; # the time now (in seconds)
53 $version = "1.13"; # the version no of the software
54 $starttime = 0; # the starting time of the cluster
56 # handle disconnections
60 return if !defined $dxchan;
61 $dxchan->disconnect();
64 # send a message to call on conn and disconnect
67 my ($conn, $call, $mess) = @_;
69 dbg('chan', "-> D $call $mess\n");
70 $conn->send_now("D$call|$mess");
72 dbg('chan', "-> Z $call bye\n");
73 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
76 # handle incoming messages
79 my ($conn, $msg, $err) = @_;
80 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
82 if (defined $err && $err) {
83 disconnect($dxchan) if defined $dxchan;
87 # set up the basic channel info - this needs a bit more thought - there is duplication here
88 if (!defined $dxchan) {
89 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
91 # is there one already connected elsewhere in the cluster (and not a cluster)
92 my $user = DXUser->get($call);
94 if (($user->sort eq 'A' || $call == $myalias) && !DXCluster->get_exact($call)) {
97 if (DXCluster->get($call) || DXChannel->get($call)) {
98 my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
99 already_conn($conn, $call, $mess);
103 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
105 if (DXCluster->get($call)) {
106 my $mess = DXM::msg($lang, 'conother', $call);
107 already_conn($conn, $call, $mess);
110 $user = DXUser->new($call);
114 if ($user->lockout) {
115 Log('DXCommand', "$call is locked out, disconnected");
116 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
121 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
122 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
123 die "Invalid sort of user on $call = $sort" if !$dxchan;
126 # queue the message and the channel object for later processing
128 my $self = bless {}, "inqueue";
129 $self->{dxchan} = $dxchan;
130 $self->{data} = $msg;
131 push @inqueue, $self;
140 # cease running this program, close down all the connections nicely
144 foreach $dxchan (DXChannel->get_all()) {
145 disconnect($dxchan) unless $dxchan == $DXProt::me;
147 Log('cluster', "DXSpider V$version stopped");
151 # the reaper of children
157 # this is where the input queue is dealt with and things are dispatched off to other parts of
161 my $self = shift @inqueue;
164 my $data = $self->{data};
165 my $dxchan = $self->{dxchan};
166 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
168 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
169 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
172 my $user = $dxchan->user;
173 if ($sort eq 'A' || $sort eq 'O') {
174 $dxchan->start($line, $sort);
175 } elsif ($sort eq 'I') {
176 die "\$user not defined for $call" if !defined $user;
179 $dxchan->normal($line);
181 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
182 } elsif ($sort eq 'Z') {
184 } elsif ($sort eq 'D') {
185 ; # ignored (an echo)
187 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
193 my $t = $systime - $starttime;
194 my $days = int $t / 86400;
196 my $hours = int $t / 3600;
198 my $mins = int $t / 60;
199 return sprintf "%d %02d:%02d", $days, $hours, $mins;
201 #############################################################
203 # The start of the main line of code
205 #############################################################
207 $starttime = $systime = time;
209 # open the debug file, set various FHs to be unbuffered
213 STDOUT->autoflush(1);
215 Log('cluster', "DXSpider V$version started");
218 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
221 print "loading prefixes ...\n";
225 print "loading band data ...\n";
228 # initialise User file system
229 print "loading user file system ...\n";
230 DXUser->init($userfn);
232 # start listening for incoming messages/connects
233 print "starting listener ...\n";
234 Msg->new_server("$clusteraddr", $clusterport, \&login);
237 $SIG{'INT'} = \&cease;
238 $SIG{'TERM'} = \&cease;
239 $SIG{'HUP'} = 'IGNORE';
240 $SIG{'CHLD'} = \&reap;
242 # read in system messages
245 # read in command aliases
248 # initialise the protocol engine
251 # initialise the Geomagnetic data engine
254 # initial the Spot stuff
257 # put in a DXCluster node for us here so we can add users and take them away
258 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
260 # read in any existing message headers and clean out old crap
261 print "reading existing message headers\n";
265 # read in any cron jobs
266 print "reading cron jobs\n";
269 # this, such as it is, is the main loop!
270 print "orft we jolly well go ...\n";
273 Msg->event_loop(1, 0.001);
275 process_inqueue(); # read in lines from the input queue and despatch them
277 # do timed stuff, ongoing processing happens one a second
278 if ($timenow != $systime) {
282 DXCron::process(); # do cron jobs
283 DXCommandmode::process(); # process ongoing command mode stuff
284 DXProt::process(); # process ongoing ak1a pcxx stuff
285 DXConnect::process();
288 last if --$decease <= 0;