9511435d72200b4b3cb3f76c726383dcc57882ab
[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   # root of directory tree for this system
16   $root = "/spider"; 
17   $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
18
19   unshift @INC, "$root/perl";  # this IS the right way round!
20   unshift @INC, "$root/local";
21 }
22
23 use Msg;
24 use DXVars;
25 use DXUtil;
26 use DXChannel;
27 use DXUser;
28 use DXM;
29 use DXCommandmode;
30 use DXProt;
31 use DXMsg;
32 use DXCluster;
33 use DXDebug;
34 use DXCron;
35 use DXConnect;
36 use Prefix;
37 use Bands;
38
39 package main;
40
41 @inqueue = ();                # the main input queue, an array of hashes
42 $systime = 0;                 # the time now (in seconds)
43 $version = 1.1;               # the version no of the software
44
45 # handle disconnections
46 sub disconnect
47 {
48   my $dxchan = shift;
49   return if !defined $dxchan;
50   $dxchan->disconnect();
51 }
52
53 # handle incoming messages
54 sub rec
55 {
56   my ($conn, $msg, $err) = @_;
57   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
58   
59   if (defined $err && $err) {
60     disconnect($dxchan) if defined $dxchan;
61         return;
62   }
63   
64   # set up the basic channel info - this needs a bit more thought - there is duplication here
65   if (!defined $dxchan) {
66      my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
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            sleep(1);
74            dbg('chan', "-> Z $call bye\n");
75        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
76            return;
77      }
78
79          # is there one already connected elsewhere in the cluster?
80          if (DXCluster->get($call)) {
81            my $mess = DXM::msg('concluster', $call);
82            dbg('chan', "-> D $call $mess\n"); 
83        $conn->send_now("D$call|$mess");
84            sleep(1);
85            dbg('chan', "-> Z $call bye\n");
86        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
87            return;
88      }
89
90      my $user = DXUser->get($call);
91          if (!defined $user) {
92            $user = DXUser->new($call);
93          }
94
95      # create the channel
96      $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
97      $dxchan = DXProt->new($call, $conn, $user) if ($user->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         if ($dxchan->{func}) {   
146           # call an ongoing routine if there is a function specified
147           &{$dxchan->{func}} ($dxchan, $line);
148         } else {
149           # normal input
150           $dxchan->normal($line);
151         }
152     disconnect($dxchan) if ($dxchan->{state} eq 'bye');
153   } elsif ($sort eq 'Z') {
154     disconnect($dxchan);
155   } else {
156     print STDERR atime, " Unknown command letter ($sort) received from $call\n";
157   }
158 }
159
160 #############################################################
161 #
162 # The start of the main line of code 
163 #
164 #############################################################
165
166 # open the debug file, set various FHs to be unbuffered
167 dbginit($debugfn);
168 foreach(@debug) {
169   dbgadd($_);
170 }
171 STDOUT->autoflush(1);
172
173 # banner
174 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
175
176 # load Prefixes
177 print "loading prefixes ...\n";
178 Prefix::load();
179
180 # load band data
181 print "loading band data ...\n";
182 Bands::load();
183
184 # initialise User file system
185 print "loading user file system ...\n"; 
186 DXUser->init($userfn);
187
188 # start listening for incoming messages/connects
189 print "starting listener ...\n";
190 Msg->new_server("$clusteraddr", $clusterport, \&login);
191
192 # prime some signals
193 $SIG{'INT'} = \&cease;
194 $SIG{'TERM'} = \&cease;
195 $SIG{'HUP'} = 'IGNORE';
196
197 # initialise the protocol engine
198 DXProt->init();
199
200 # put in a DXCluster node for us here so we can add users and take them away
201 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version); 
202
203 # read in any existing message headers
204 print "reading existing message headers\n";
205 DXMsg->init();
206
207 # read in any cron jobs
208 print "reading cron jobs\n";
209 DXCron->init();
210
211 # this, such as it is, is the main loop!
212 print "orft we jolly well go ...\n";
213 for (;;) {
214   my $timenow;
215   Msg->event_loop(1, 0.001);
216   $timenow = time;
217   process_inqueue();                 # read in lines from the input queue and despatch them
218
219   # do timed stuff, ongoing processing happens one a second
220   if ($timenow != $systime) {
221     $systime = $timenow;
222         $cldate = &cldate();
223         $ztime = &ztime();
224     DXCommandmode::process();     # process ongoing command mode stuff
225     DXProt::process();              # process ongoing ak1a pcxx stuff
226         DXCron::process();
227         DXConnect::process();
228   }
229 }
230