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