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