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';
32 @inqueue = (); # the main input queue, an array of hashes
33 $systime = 0; # the time now (in seconds)
35 # handle disconnections
39 return if !defined $dxchan;
40 my $user = $dxchan->{user};
41 my $conn = $dxchan->{conn};
43 $user->close() if defined $user;
44 $conn->disconnect() if defined $conn;
48 # handle incoming messages
51 my ($conn, $msg, $err) = @_;
52 my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
54 if (defined $err && $err) {
55 disconnect($dxchan) if defined $dxchan;
59 # set up the basic channel info - this needs a bit more thought - there is duplication here
60 if (!defined $dxchan) {
61 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
63 # is there one already connected?
64 if (DXChannel->get($call)) {
65 my $mess = DXM::msg('conother', $call);
66 dbg('chan', "-> D $call $mess\n");
67 $conn->send_now("D$call|$mess");
69 dbg('chan', "-> Z $call bye\n");
70 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
74 # is there one already connected elsewhere in the cluster?
75 if (DXCluster->get($call)) {
76 my $mess = DXM::msg('concluster', $call);
77 dbg('chan', "-> D $call $mess\n");
78 $conn->send_now("D$call|$mess");
80 dbg('chan', "-> Z $call bye\n");
81 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
85 my $user = DXUser->get($call);
87 $user = DXUser->new($call);
91 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
92 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
93 die "Invalid sort of user on $call = $sort" if !$dxchan;
96 # queue the message and the channel object for later processing
98 my $self = bless {}, "inqueue";
99 $self->{dxchan} = $dxchan;
100 $self->{data} = $msg;
101 push @inqueue, $self;
110 # cease running this program, close down all the connections nicely
114 foreach $dxchan (DXChannel->get_all()) {
120 # this is where the input queue is dealt with and things are dispatched off to other parts of
124 my $self = shift @inqueue;
127 my $data = $self->{data};
128 my $dxchan = $self->{dxchan};
129 my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
131 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
132 dbg('chan', "<- $sort $call $line\n");
135 my $user = $dxchan->user;
137 $dxchan->start($line);
138 } elsif ($sort eq 'D') {
139 die "\$user not defined for $call" if !defined $user;
140 $dxchan->normal($line);
141 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
142 } elsif ($sort eq 'Z') {
145 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
149 #############################################################
151 # The start of the main line of code
153 #############################################################
155 # open the debug file, set various FHs to be unbuffered
160 STDOUT->autoflush(1);
162 # initialise User file system
163 DXUser->init($userfn);
165 # start listening for incoming messages/connects
166 Msg->new_server("$clusteraddr", $clusterport, \&login);
169 $SIG{'INT'} = \&cease;
170 $SIG{'TERM'} = \&cease;
171 $SIG{'HUP'} = 'IGNORE';
173 # initialise the protocol engine
176 # this, such as it is, is the main loop!
179 Msg->event_loop(1, 0.001);
181 process_inqueue(); # read in lines from the input queue and despatch them
183 # do timed stuff, ongoing processing happens one a second
184 if ($timenow != $systime) {
188 DXCommandmode::process(); # process ongoing command mode stuff
189 DXProt::process(); # process ongoing ak1a pcxx stuff