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 unshift @INC, '/spider/perl'; # this IS the right way round!
16 unshift @INC, '/spider/local';
33 @inqueue = (); # the main input queue, an array of hashes
34 $systime = 0; # the time now (in seconds)
36 # handle disconnections
40 return if !defined $dxchan;
41 my $user = $dxchan->{user};
42 my $conn = $dxchan->{conn};
44 $user->close() if defined $user;
45 $conn->disconnect() if defined $conn;
49 # handle incoming messages
52 my ($conn, $msg, $err) = @_;
53 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
55 if (defined $err && $err) {
56 disconnect($dxchan) if defined $dxchan;
60 # set up the basic channel info - this needs a bit more thought - there is duplication here
61 if (!defined $dxchan) {
62 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
64 # is there one already connected?
65 if (DXChannel->get($call)) {
66 my $mess = DXM::msg('conother', $call);
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
75 # is there one already connected elsewhere in the cluster?
76 if (DXCluster->get($call)) {
77 my $mess = DXM::msg('concluster', $call);
78 dbg('chan', "-> D $call $mess\n");
79 $conn->send_now("D$call|$mess");
81 dbg('chan', "-> Z $call bye\n");
82 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
86 my $user = DXUser->get($call);
88 $user = DXUser->new($call);
92 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
93 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
94 die "Invalid sort of user on $call = $sort" if !$dxchan;
97 # queue the message and the channel object for later processing
99 my $self = bless {}, "inqueue";
100 $self->{dxchan} = $dxchan;
101 $self->{data} = $msg;
102 push @inqueue, $self;
111 # cease running this program, close down all the connections nicely
115 foreach $dxchan (DXChannel->get_all()) {
121 # this is where the input queue is dealt with and things are dispatched off to other parts of
125 my $self = shift @inqueue;
128 my $data = $self->{data};
129 my $dxchan = $self->{dxchan};
130 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
132 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
133 dbg('chan', "<- $sort $call $line\n");
136 my $user = $dxchan->user;
138 $dxchan->start($line);
139 } elsif ($sort eq 'D') {
140 die "\$user not defined for $call" if !defined $user;
141 $dxchan->normal($line);
142 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
143 } elsif ($sort eq 'Z') {
146 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
150 #############################################################
152 # The start of the main line of code
154 #############################################################
156 # open the debug file, set various FHs to be unbuffered
161 STDOUT->autoflush(1);
166 # initialise User file system
167 DXUser->init($userfn);
169 # start listening for incoming messages/connects
170 Msg->new_server("$clusteraddr", $clusterport, \&login);
173 $SIG{'INT'} = \&cease;
174 $SIG{'TERM'} = \&cease;
175 $SIG{'HUP'} = 'IGNORE';
177 # initialise the protocol engine
180 # this, such as it is, is the main loop!
183 Msg->event_loop(1, 0.001);
185 process_inqueue(); # read in lines from the input queue and despatch them
187 # do timed stuff, ongoing processing happens one a second
188 if ($timenow != $systime) {
192 DXCommandmode::process(); # process ongoing command mode stuff
193 DXProt::process(); # process ongoing ak1a pcxx stuff