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";
41 @inqueue = (); # the main input queue, an array of hashes
42 $systime = 0; # the time now (in seconds)
43 $version = 1.1; # the version no of the software
45 # handle disconnections
49 return if !defined $dxchan;
50 $dxchan->disconnect();
53 # handle incoming messages
56 my ($conn, $msg, $err) = @_;
57 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
59 if (defined $err && $err) {
60 disconnect($dxchan) if defined $dxchan;
64 # set up the basic channel info - this needs a bit more thought - there is duplication here
65 if (!defined $dxchan) {
66 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
68 # is there one already connected?
69 if (DXChannel->get($call)) {
70 my $mess = DXM::msg('conother', $call);
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
79 # is there one already connected elsewhere in the cluster?
80 if (DXCluster->get($call)) {
81 my $mess = DXM::msg('concluster', $call);
82 dbg('chan', "-> D $call $mess\n");
83 $conn->send_now("D$call|$mess");
85 dbg('chan', "-> Z $call bye\n");
86 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
90 my $user = DXUser->get($call);
92 $user = DXUser->new($call);
96 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
97 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
98 die "Invalid sort of user on $call = $sort" if !$dxchan;
101 # queue the message and the channel object for later processing
103 my $self = bless {}, "inqueue";
104 $self->{dxchan} = $dxchan;
105 $self->{data} = $msg;
106 push @inqueue, $self;
115 # cease running this program, close down all the connections nicely
119 foreach $dxchan (DXChannel->get_all()) {
125 # this is where the input queue is dealt with and things are dispatched off to other parts of
129 my $self = shift @inqueue;
132 my $data = $self->{data};
133 my $dxchan = $self->{dxchan};
134 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
136 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
137 dbg('chan', "<- $sort $call $line\n");
140 my $user = $dxchan->user;
142 $dxchan->start($line);
143 } elsif ($sort eq 'D') {
144 die "\$user not defined for $call" if !defined $user;
145 if ($dxchan->{func}) {
146 # call an ongoing routine if there is a function specified
147 &{$dxchan->{func}} ($dxchan, $line);
150 $dxchan->normal($line);
152 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
153 } elsif ($sort eq 'Z') {
156 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
160 #############################################################
162 # The start of the main line of code
164 #############################################################
166 # open the debug file, set various FHs to be unbuffered
171 STDOUT->autoflush(1);
174 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
177 print "loading prefixes ...\n";
181 print "loading band data ...\n";
184 # initialise User file system
185 print "loading user file system ...\n";
186 DXUser->init($userfn);
188 # start listening for incoming messages/connects
189 print "starting listener ...\n";
190 Msg->new_server("$clusteraddr", $clusterport, \&login);
193 $SIG{'INT'} = \&cease;
194 $SIG{'TERM'} = \&cease;
195 $SIG{'HUP'} = 'IGNORE';
197 # initialise the protocol engine
200 # put in a DXCluster node for us here so we can add users and take them away
201 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
203 # read in any existing message headers
204 print "reading existing message headers\n";
207 # read in any cron jobs
208 print "reading cron jobs\n";
211 # this, such as it is, is the main loop!
212 print "orft we jolly well go ...\n";
215 Msg->event_loop(1, 0.001);
217 process_inqueue(); # read in lines from the input queue and despatch them
219 # do timed stuff, ongoing processing happens one a second
220 if ($timenow != $systime) {
224 DXCommandmode::process(); # process ongoing command mode stuff
225 DXProt::process(); # process ongoing ak1a pcxx stuff
227 DXConnect::process();