started on the prefix stuff, got most of it done
[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 use Prefix;
30
31 package main;
32
33 @inqueue = ();                # the main input queue, an array of hashes
34 $systime = 0;                 # the time now (in seconds)
35
36 # handle disconnections
37 sub disconnect
38 {
39   my $dxchan = shift;
40   return if !defined $dxchan;
41   my $user = $dxchan->{user};
42   my $conn = $dxchan->{conn};
43   $dxchan->finish();
44   $user->close() if defined $user;
45   $conn->disconnect() if defined $conn;
46   $dxchan->del();
47 }
48
49 # handle incoming messages
50 sub rec
51 {
52   my ($conn, $msg, $err) = @_;
53   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
54   
55   if (defined $err && $err) {
56     disconnect($dxchan) if defined $dxchan;
57         return;
58   }
59   
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+)\|(.*)$/;
63
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");
69            sleep(1);
70            dbg('chan', "-> Z $call bye\n");
71        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
72            return;
73      }
74
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");
80            sleep(1);
81            dbg('chan', "-> Z $call bye\n");
82        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
83            return;
84      }
85
86      my $user = DXUser->get($call);
87          if (!defined $user) {
88            $user = DXUser->new($call);
89          }
90
91      # create the channel
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;
95   }
96   
97   # queue the message and the channel object for later processing
98   if (defined $msg) {
99     my $self = bless {}, "inqueue";
100     $self->{dxchan} = $dxchan;
101     $self->{data} = $msg;
102         push @inqueue, $self;
103   }
104 }
105
106 sub login
107 {
108   return \&rec;
109 }
110
111 # cease running this program, close down all the connections nicely
112 sub cease
113 {
114   my $dxchan;
115   foreach $dxchan (DXChannel->get_all()) {
116     disconnect($dxchan);
117   }
118   exit(0);
119 }
120
121 # this is where the input queue is dealt with and things are dispatched off to other parts of
122 # the cluster
123 sub process_inqueue
124 {
125   my $self = shift @inqueue;
126   return if !$self;
127   
128   my $data = $self->{data};
129   my $dxchan = $self->{dxchan};
130   my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
131   
132   # do the really sexy console interface bit! (Who is going to do the TK interface then?)
133   dbg('chan', "<- $sort $call $line\n");
134   
135   # handle A records
136   my $user = $dxchan->user;
137   if ($sort eq 'A') {
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') {
144     disconnect($dxchan);
145   } else {
146     print STDERR atime, " Unknown command letter ($sort) received from $call\n";
147   }
148 }
149
150 #############################################################
151 #
152 # The start of the main line of code 
153 #
154 #############################################################
155
156 # open the debug file, set various FHs to be unbuffered
157 dbginit($debugfn);
158 foreach(@debug) {
159   dbgadd($_);
160 }
161 STDOUT->autoflush(1);
162
163 # load Prefixes
164 Prefix::load();
165
166 # initialise User file system
167 DXUser->init($userfn);
168
169 # start listening for incoming messages/connects
170 Msg->new_server("$clusteraddr", $clusterport, \&login);
171
172 # prime some signals
173 $SIG{'INT'} = \&cease;
174 $SIG{'TERM'} = \&cease;
175 $SIG{'HUP'} = 'IGNORE';
176
177 # initialise the protocol engine
178 DXProt->init();
179
180 # this, such as it is, is the main loop!
181 for (;;) {
182   my $timenow;
183   Msg->event_loop(1, 0.001);
184   $timenow = time;
185   process_inqueue();                 # read in lines from the input queue and despatch them
186
187   # do timed stuff, ongoing processing happens one a second
188   if ($timenow != $systime) {
189     $systime = $timenow;
190         $cldate = &cldate();
191         $ztime = &ztime();
192     DXCommandmode::process();     # process ongoing command mode stuff
193     DXProt::process();              # process ongoing ak1a pcxx stuff
194   }
195 }
196