ad71b688ac7ce8e86f46f685deb4e6d3a06c1259
[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 #       require Exporter;
23 #       $Exporter::Verbose = 1;
24 }
25
26 use Msg;
27 use DXVars;
28 use DXDebug;
29 use DXLog;
30 use DXLogPrint;
31 use DXUtil;
32 use DXChannel;
33 use DXUser;
34 use DXM;
35 use DXCommandmode;
36 use DXProt;
37 use DXMsg;
38 use DXCluster;
39 use DXCron;
40 use DXConnect;
41 use Prefix;
42 use Bands;
43 use Geomag;
44 use CmdAlias;
45 use Carp;
46
47 package main;
48
49 @inqueue = ();                                  # the main input queue, an array of hashes
50 $systime = 0;                                   # the time now (in seconds)
51 $version = "1.13";                              # the version no of the software
52 $starttime = 0;                 # the starting time of the cluster   
53  
54 # handle disconnections
55 sub disconnect
56 {
57         my $dxchan = shift;
58         return if !defined $dxchan;
59         $dxchan->disconnect();
60 }
61
62 # send a message to call on conn and disconnect
63 sub already_conn
64 {
65         my ($conn, $call, $mess) = @_;
66         
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 }
73
74 # handle incoming messages
75 sub rec
76 {
77         my ($conn, $msg, $err) = @_;
78         my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
79         
80         if (defined $err && $err) {
81                 disconnect($dxchan) if defined $dxchan;
82                 return;
83         }
84         
85         # set up the basic channel info - this needs a bit more thought - there is duplication here
86         if (!defined $dxchan) {
87                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
88                 
89                 # is there one already connected elsewhere in the cluster (and not a cluster)
90                 my $user = DXUser->get($call);
91                 if ($user) {
92                         if ($user->sort eq 'A' && !DXCluster->get_exact($call)) {
93                                 ;
94                         } elsif ($user->sort eq 'U' && $call eq $main::myalias && !DXCluster->get_exact($call)) {
95                                 ;
96                         } else {
97                                 if (DXChannel->get($call)) {
98                                         my $mess = DXM::msg($lang, $user->sort eq 'A' ? 'concluster' : 'conother', $call);
99                                         already_conn($conn, $call, $mess);
100                                         return;
101                                 }
102                         }
103                 } else {
104                         if (DXChannel->get($call)) {
105                                 my $mess = DXM::msg($lang, 'conother', $call);
106                                 already_conn($conn, $call, $mess);
107                                 return;
108                         }
109                 }
110
111                 
112                 # the user MAY have an SSID if local, but otherwise doesn't
113                 my $user = DXUser->get($call);
114                 if (!defined $user) {
115                         $user = DXUser->new($call);
116                 } else {
117                         $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
118                 }
119                 
120                 # is he locked out ?
121                 if ($user->lockout) {
122                         Log('DXCommand', "$call is locked out, disconnected");
123                         $conn->send_now("Z$call|bye"); # this will cause 'client' to disconnect
124                         return;
125                 }
126
127                 # create the channel
128                 $dxchan = DXCommandmode->new($call, $conn, $user) if ($user->sort eq 'U');
129                 $dxchan = DXProt->new($call, $conn, $user) if ($user->sort eq 'A');
130                 die "Invalid sort of user on $call = $sort" if !$dxchan;
131         }
132         
133         # queue the message and the channel object for later processing
134         if (defined $msg) {
135                 my $self = bless {}, "inqueue";
136                 $self->{dxchan} = $dxchan;
137                 $self->{data} = $msg;
138                 push @inqueue, $self;
139         }
140 }
141
142 sub login
143 {
144         return \&rec;
145 }
146
147 # cease running this program, close down all the connections nicely
148 sub cease
149 {
150         my $dxchan;
151         foreach $dxchan (DXChannel->get_all()) {
152                 disconnect($dxchan);
153         }
154         Log('cluster', "DXSpider V$version stopped");
155         exit(0);
156 }
157
158 # the reaper of children
159 sub reap
160 {
161         my $cpid = wait;
162 }
163
164 # this is where the input queue is dealt with and things are dispatched off to other parts of
165 # the cluster
166 sub process_inqueue
167 {
168         my $self = shift @inqueue;
169         return if !$self;
170         
171         my $data = $self->{data};
172         my $dxchan = $self->{dxchan};
173         my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
174         
175         # do the really sexy console interface bit! (Who is going to do the TK interface then?)
176         dbg('chan', "<- $sort $call $line\n") unless $sort eq 'D';
177         
178         # handle A records
179         my $user = $dxchan->user;
180         if ($sort eq 'A' || $sort eq 'O') {
181                 $dxchan->start($line, $sort);  
182         } elsif ($sort eq 'I') {
183                 die "\$user not defined for $call" if !defined $user;
184                 
185                 # normal input
186                 $dxchan->normal($line);
187                 
188                 disconnect($dxchan) if ($dxchan->{state} eq 'bye');
189         } elsif ($sort eq 'Z') {
190                 disconnect($dxchan);
191         } elsif ($sort eq 'D') {
192                 ;                       # ignored (an echo)
193         } else {
194                 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
195         }
196 }
197
198 sub uptime
199 {
200         my $t = $systime - $starttime;
201         my $days = int $t / 86400;
202         $t -= $days * 86400;
203         my $hours = int $t / 3600;
204         $t -= $hours * 3600;
205         my $mins = int $t / 60;
206         return sprintf "%d %02d:%02d", $days, $hours, $mins;
207 }
208 #############################################################
209 #
210 # The start of the main line of code 
211 #
212 #############################################################
213
214 $starttime = $systime = time;
215
216 # open the debug file, set various FHs to be unbuffered
217 foreach (@debug) {
218         dbgadd($_);
219 }
220 STDOUT->autoflush(1);
221
222 Log('cluster', "DXSpider V$version started");
223
224 # banner
225 print "DXSpider DX Cluster Version $version\nCopyright (c) 1998 Dirk Koopman G1TLH\n";
226
227 # load Prefixes
228 print "loading prefixes ...\n";
229 Prefix::load();
230
231 # load band data
232 print "loading band data ...\n";
233 Bands::load();
234
235 # initialise User file system
236 print "loading user file system ...\n"; 
237 DXUser->init($userfn);
238
239 # start listening for incoming messages/connects
240 print "starting listener ...\n";
241 Msg->new_server("$clusteraddr", $clusterport, \&login);
242
243 # prime some signals
244 $SIG{'INT'} = \&cease;
245 $SIG{'TERM'} = \&cease;
246 $SIG{'HUP'} = 'IGNORE';
247 $SIG{'CHLD'} = \&reap;
248
249 # read in system messages
250 DXM->init();
251
252 # read in command aliases
253 CmdAlias->init();
254
255 # initialise the protocol engine
256 DXProt->init();
257
258 # initialise the Geomagnetic data engine
259 Geomag->init();
260
261 # initial the Spot stuff
262 Spot->init();
263
264 # put in a DXCluster node for us here so we can add users and take them away
265 DXNode->new(0, $mycall, 0, 1, $DXProt::myprot_version); 
266
267 # read in any existing message headers and clean out old crap
268 print "reading existing message headers\n";
269 DXMsg->init();
270 DXMsg::clean_old();
271
272 # read in any cron jobs
273 print "reading cron jobs\n";
274 DXCron->init();
275
276 # this, such as it is, is the main loop!
277 print "orft we jolly well go ...\n";
278 for (;;) {
279         my $timenow;
280         Msg->event_loop(1, 0.001);
281         $timenow = time;
282         process_inqueue();                      # read in lines from the input queue and despatch them
283         
284         # do timed stuff, ongoing processing happens one a second
285         if ($timenow != $systime) {
286                 $systime = $timenow;
287                 $cldate = &cldate();
288                 $ztime = &ztime();
289                 DXCron::process();      # do cron jobs
290                 DXCommandmode::process(); # process ongoing command mode stuff
291                 DXProt::process();              # process ongoing ak1a pcxx stuff
292                 DXConnect::process();
293         }
294         if ($decease) {
295                 last if --$decease <= 0;
296         }
297 }
298
299