fixed problems with show/channel
[spider.git] / perl / cluster.pl
1 #!/usr/bin/perl
2 #
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.
5 #
6 # Hence the name of 'spider' (although it may become 'dxspider')
7 #
8 # Copyright (c) 1998 Dirk Koopman G1TLH
9 #
10 # $Id$
11
12
13 # make sure that modules are searched in the order local then perl
14 BEGIN {
15   unshift @INC, '/spider/perl';  # this IS the right way round!
16   unshift @INC, '/spider/local';
17 }
18
19 use Msg;
20 use DXVars;
21 use DXUtil;
22 use DXChannel;
23 use DXUser;
24 use DXM;
25 use DXCommandmode;
26 use DXProt;
27 use DXCluster;
28 use DXDebug;
29
30 package main;
31
32 @inqueue = ();                # the main input queue, an array of hashes
33 $systime = 0;                 # the time now (in seconds)
34
35 # handle disconnections
36 sub disconnect
37 {
38   my $dxchan = shift;
39   return if !defined $dxchan;
40   my $user = $dxchan->{user};
41   my $conn = $dxchan->{conn};
42   $dxchan->finish();
43   $user->close() if defined $user;
44   $conn->disconnect() if defined $conn;
45   $dxchan->del();
46 }
47
48 # handle incoming messages
49 sub rec
50 {
51   my ($conn, $msg, $err) = @_;
52   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
53   
54   if (defined $err && $err) {
55     disconnect($dxchan) if defined $dxchan;
56         return;
57   }
58   
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);
63          if (!defined $user) {
64            $user = DXUser->new($call);
65          }
66          my $sort = $user->sort();
67          
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
75            return;
76      }
77
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
85            return;
86      }
87
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();
94
95          # create the channel
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;
99   }
100   
101   # queue the message and the channel object for later processing
102   if (defined $msg) {
103     my $self = bless {}, "inqueue";
104     $self->{dxchan} = $dxchan;
105     $self->{data} = $msg;
106         push @inqueue, $self;
107   }
108 }
109
110 sub login
111 {
112   return \&rec;
113 }
114
115 # cease running this program, close down all the connections nicely
116 sub cease
117 {
118   my $dxchan;
119   foreach $dxchan (DXChannel->get_all()) {
120     disconnect($dxchan);
121   }
122   exit(0);
123 }
124
125 # this is where the input queue is dealt with and things are dispatched off to other parts of
126 # the cluster
127 sub process_inqueue
128 {
129   my $self = shift @inqueue;
130   return if !$self;
131   
132   my $data = $self->{data};
133   my $dxchan = $self->{dxchan};
134   my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
135   
136   # do the really sexy console interface bit! (Who is going to do the TK interface then?)
137   dbg('chan', "<- $sort $call $line\n");
138   
139   # handle A records
140   my $user = $dxchan->user;
141   if ($sort eq 'A') {
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') {
148     disconnect($dxchan);
149   } else {
150     print STDERR atime, " Unknown command letter ($sort) received from $call\n";
151   }
152 }
153
154 #############################################################
155 #
156 # The start of the main line of code 
157 #
158 #############################################################
159
160 # open the debug file, set various FHs to be unbuffered
161 dbginit($debugfn);
162 foreach(@debug) {
163   dbgadd($_);
164 }
165 STDOUT->autoflush(1);
166
167 # initialise User file system
168 DXUser->init($userfn);
169
170 # start listening for incoming messages/connects
171 Msg->new_server("$clusteraddr", $clusterport, \&login);
172
173 # prime some signals
174 $SIG{'INT'} = \&cease;
175 $SIG{'TERM'} = \&cease;
176 $SIG{'HUP'} = 'IGNORE';
177
178 # this, such as it is, is the main loop!
179 for (;;) {
180   my $timenow;
181   Msg->event_loop(1, 0.001);
182   $timenow = time;
183   process_inqueue();                 # read in lines from the input queue and despatch them
184
185   # do timed stuff, ongoing processing happens one a second
186   if ($timenow != $systime) {
187     $systime = $timenow;
188         $cldate = &cldate();
189         $ztime = &ztime();
190     DXCommandmode::process();     # process ongoing command mode stuff
191     DXProt::process();              # process ongoing ak1a pcxx stuff
192   }
193 }
194