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