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