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