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';
39 @inqueue = (); # the main input queue, an array of hashes
40 $systime = 0; # the time now (in seconds)
41 $version = 1.1; # the version no of the software
43 # handle disconnections
47 return if !defined $dxchan;
48 $dxchan->disconnect();
51 # handle incoming messages
54 my ($conn, $msg, $err) = @_;
55 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
57 if (defined $err && $err) {
58 disconnect($dxchan) if defined $dxchan;
62 # set up the basic channel info - this needs a bit more thought - there is duplication here
63 if (!defined $dxchan) {
64 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
66 # is there one already connected?
67 if (DXChannel->get($call)) {
68 my $mess = DXM::msg('conother', $call);
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
77 # is there one already connected elsewhere in the cluster?
78 if (DXCluster->get($call)) {
79 my $mess = DXM::msg('concluster', $call);
80 dbg('chan', "-> D $call $mess\n");
81 $conn->send_now("D$call|$mess");
83 dbg('chan', "-> Z $call bye\n");
84 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
88 my $user = DXUser->get($call);
90 $user = DXUser->new($call);
94 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
95 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
96 die "Invalid sort of user on $call = $sort" if !$dxchan;
99 # queue the message and the channel object for later processing
101 my $self = bless {}, "inqueue";
102 $self->{dxchan} = $dxchan;
103 $self->{data} = $msg;
104 push @inqueue, $self;
113 # cease running this program, close down all the connections nicely
117 foreach $dxchan (DXChannel->get_all()) {
123 # this is where the input queue is dealt with and things are dispatched off to other parts of
127 my $self = shift @inqueue;
130 my $data = $self->{data};
131 my $dxchan = $self->{dxchan};
132 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
134 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
135 dbg('chan', "<- $sort $call $line\n");
138 my $user = $dxchan->user;
140 $dxchan->start($line);
141 } elsif ($sort eq 'D') {
142 die "\$user not defined for $call" if !defined $user;
143 $dxchan->normal($line);
144 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
145 } elsif ($sort eq 'Z') {
148 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
152 #############################################################
154 # The start of the main line of code
156 #############################################################
158 # open the debug file, set various FHs to be unbuffered
163 STDOUT->autoflush(1);
166 print "loading prefixes ...\n";
170 print "loading band data ...\n";
173 # initialise User file system
174 print "loading user file system ...\n";
175 DXUser->init($userfn);
177 # start listening for incoming messages/connects
178 print "starting listener ...\n";
179 Msg->new_server("$clusteraddr", $clusterport, \&login);
182 $SIG{'INT'} = \&cease;
183 $SIG{'TERM'} = \&cease;
184 $SIG{'HUP'} = 'IGNORE';
186 # initialise the protocol engine
189 # put in a DXCluster node for us here so we can add users and take them away
190 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version);
192 # this, such as it is, is the main loop!
193 print "orft we jolly well go ...\n";
196 Msg->event_loop(1, 0.001);
198 process_inqueue(); # read in lines from the input queue and despatch them
200 # do timed stuff, ongoing processing happens one a second
201 if ($timenow != $systime) {
205 DXCommandmode::process(); # process ongoing command mode stuff
206 DXProt::process(); # process ongoing ak1a pcxx stuff