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