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