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) $!";
74 @inqueue = (); # the main input queue, an array of hashes
75 $systime = 0; # the time now (in seconds)
76 $version = "1.45"; # the version no of the software
77 $starttime = 0; # the starting time of the cluster
78 $lockfn = "cluster.lock"; # lock file name
79 @outstanding_connects = (); # list of outstanding connects
81 # handle disconnections
85 return if !defined $dxchan;
86 $dxchan->disconnect();
89 # send a message to call on conn and disconnect
92 my ($conn, $call, $mess) = @_;
94 dbg('chan', "-> D $call $mess\n");
95 $conn->send_now("D$call|$mess");
97 dbg('chan', "-> Z $call bye\n");
98 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
103 # handle incoming messages
106 my ($conn, $msg, $err) = @_;
107 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
109 if (!defined $msg || (defined $err && $err)) {
114 $dxchan->conn(undef);
123 # set up the basic channel info - this needs a bit more thought - there is duplication here
124 if (!defined $dxchan) {
125 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
126 return unless defined $sort;
128 # is there one already connected to me - locally?
129 my $user = DXUser->get($call);
130 if (DXChannel->get($call)) {
131 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call);
132 already_conn($conn, $call, $mess);
136 # is there one already connected elsewhere in the cluster?
138 if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
141 if (DXCluster->get_exact($call)) {
142 my $mess = DXM::msg($lang, $user->is_node ? 'concluster' : 'conother', $call);
143 already_conn($conn, $call, $mess);
147 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
149 if (DXCluster->get_exact($call)) {
150 my $mess = DXM::msg($lang, 'conother', $call);
151 already_conn($conn, $call, $mess);
154 $user = DXUser->new($call);
158 if ($user->lockout) {
159 Log('DXCommand', "$call is locked out, disconnected");
160 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
165 $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
166 $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
167 $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
168 die "Invalid sort of user on $call = $sort" if !$dxchan;
171 # queue the message and the channel object for later processing
173 my $self = bless {}, "inqueue";
174 $self->{dxchan} = $dxchan;
175 $self->{data} = $msg;
176 push @inqueue, $self;
185 # cease running this program, close down all the connections nicely
190 $SIG{'TERM'} = 'IGNORE';
191 $SIG{'INT'} = 'IGNORE';
196 Local::finish(); # end local processing
198 dbg('local', "Local::finish error $@") if $@;
201 foreach $dxchan (DXChannel->get_all()) {
202 next unless $dxchan->is_node;
203 disconnect($dxchan) unless $dxchan == $DXProt::me;
205 Msg->event_loop(1, 0.05);
206 Msg->event_loop(1, 0.05);
207 Msg->event_loop(1, 0.05);
208 Msg->event_loop(1, 0.05);
209 Msg->event_loop(1, 0.05);
210 Msg->event_loop(1, 0.05);
213 foreach $dxchan (DXChannel->get_all()) {
214 next if $dxchan->is_node;
215 disconnect($dxchan) unless $dxchan == $DXProt::me;
217 Msg->event_loop(1, 0.05);
218 Msg->event_loop(1, 0.05);
219 Msg->event_loop(1, 0.05);
220 Msg->event_loop(1, 0.05);
221 Msg->event_loop(1, 0.05);
222 Msg->event_loop(1, 0.05);
226 # close all databases
229 dbg('chan', "DXSpider version $version ended");
230 Log('cluster', "DXSpider V$version stopped");
234 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
238 # the reaper of children
241 $SIG{'CHLD'} = \&reap;
243 @outstanding_connects = grep {$_->{pid} != $cpid} @outstanding_connects;
246 # this is where the input queue is dealt with and things are dispatched off to other parts of
250 my $self = shift @inqueue;
253 my $data = $self->{data};
254 my $dxchan = $self->{dxchan};
256 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
257 return unless defined $sort;
259 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
260 dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
263 my $user = $dxchan->user;
264 if ($sort eq 'A' || $sort eq 'O') {
265 $dxchan->start($line, $sort);
266 } elsif ($sort eq 'I') {
267 die "\$user not defined for $call" if !defined $user;
269 $dxchan->normal($line);
270 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
271 } elsif ($sort eq 'Z') {
272 $dxchan->conn(undef);
274 } elsif ($sort eq 'D') {
275 ; # ignored (an echo)
277 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
283 my $t = $systime - $starttime;
284 my $days = int $t / 86400;
286 my $hours = int $t / 3600;
288 my $mins = int $t / 60;
289 return sprintf "%d %02d:%02d", $days, $hours, $mins;
291 #############################################################
293 # The start of the main line of code
295 #############################################################
297 $starttime = $systime = time;
299 # open the debug file, set various FHs to be unbuffered
304 STDOUT->autoflush(1);
306 Log('cluster', "DXSpider V$version started");
309 dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2000 Dirk Koopman G1TLH");
312 dbg('err', "loading prefixes ...");
316 dbg('err', "loading band data ...");
319 # initialise User file system
320 dbg('err', "loading user file system ...");
321 DXUser->init($userfn, 1);
323 # start listening for incoming messages/connects
324 dbg('err', "starting listener ...");
325 Msg->new_server("$clusteraddr", $clusterport, \&login);
328 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
332 $SIG{TERM} = \&cease;
333 $SIG{HUP} = 'IGNORE';
336 $SIG{PIPE} = sub { dbg('err', "Broken PIPE signal received"); };
337 $SIG{IO} = sub { dbg('err', "SIGIO received"); };
338 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
339 $SIG{KILL} = 'DEFAULT'; # as if it matters....
341 # catch the rest with a hopeful message
344 # dbg('chan', "Catching SIG $_");
345 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
352 # read in system messages
355 # read in command aliases
358 # initialise the Geomagnetic data engine
362 # initial the Spot stuff
365 # initialise the protocol engine
366 dbg('err', "reading in duplicate spot and WWV info ...");
370 # put in a DXCluster node for us here so we can add users and take them away
371 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
373 # read in any existing message headers and clean out old crap
374 dbg('err', "reading existing message headers ...");
378 # read in any cron jobs
379 dbg('err', "reading cron jobs ...");
382 # read in database descriptors
383 dbg('err', "reading database descriptors ...");
386 # starting local stuff
387 dbg('err', "doing local initialisation ...");
391 dbg('local', "Local::init error $@") if $@;
393 # print various flags
394 #dbg('err', "seful info - \$^D: $^D \$^W: $^W \$^S: $^S \$^P: $^P");
396 # this, such as it is, is the main loop!
397 dbg('err', "orft we jolly well go ...");
399 #open(DB::OUT, "|tee /tmp/aa");
405 Msg->event_loop(1, 0.1);
407 process_inqueue(); # read in lines from the input queue and despatch them
410 # do timed stuff, ongoing processing happens one a second
411 if ($timenow != $systime) {
415 DXCron::process(); # do cron jobs
416 DXCommandmode::process(); # process ongoing command mode stuff
417 DXProt::process(); # process ongoing ak1a pcxx stuff
418 DXConnect::process();
425 Local::process(); # do any localised processing
427 dbg('local', "Local::process error $@") if $@;
430 last if --$decease <= 0;