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