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