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+)\|(.*)$/;
62 my $user = DXUser->get($call);
64 $user = DXUser->new($call);
66 my $sort = $user->sort();
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");
73 dbg('chan', "-> Z $call bye\n");
74 $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
78 # is there one already connected elsewhere in the cluster?
79 if (DXCluster->get($call)) {
80 my $mess = DXM::msg('concluster', $call);
81 dbg('chan', "-> D $call $mess\n");
82 $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 # set some necessary flags on the user if they are connecting
89 $user->wwv(1) if !$user->wwv();
90 $user->talk(1) if !$user->talk();
91 $user->ann(1) if !$user->ann();
92 $user->here(1) if !$user->here();
93 $user->sort('U') if !$user->sort();
96 $dxchan = DXCommandmode->new($call, $conn, $user) if ($sort eq 'U');
97 $dxchan = DXProt->new($call, $conn, $user) if ($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 $dxchan->normal($line);
146 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
147 } elsif ($sort eq 'Z') {
150 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
154 #############################################################
156 # The start of the main line of code
158 #############################################################
160 # open the debug file, set various FHs to be unbuffered
165 STDOUT->autoflush(1);
167 # initialise User file system
168 DXUser->init($userfn);
170 # start listening for incoming messages/connects
171 Msg->new_server("$clusteraddr", $clusterport, \&login);
174 $SIG{'INT'} = \&cease;
175 $SIG{'TERM'} = \&cease;
176 $SIG{'HUP'} = 'IGNORE';
178 # this, such as it is, is the main loop!
181 Msg->event_loop(1, 0.001);
183 process_inqueue(); # read in lines from the input queue and despatch them
185 # do timed stuff, ongoing processing happens one a second
186 if ($timenow != $systime) {
190 DXCommandmode::process(); # process ongoing command mode stuff
191 DXProt::process(); # process ongoing ak1a pcxx stuff