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