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