79c5b5c86300b7622d7bad57288e08c6f5f6d383
[spider.git] / perl / cluster.pl
1 #!/usr/bin/perl
2 #
3 # A thing that implements dxcluster 'protocol'
4 #
5 # This is a perl module/program that sits on the end of a dxcluster
6 # 'protocol' connection and deals with anything that might come along.
7 #
8 # this program is called by ax25d and gets raw ax25 text on its input
9 #
10 # Copyright (c) 1998 Dirk Koopman G1TLH
11 #
12 # $Id$
13
14
15 use Msg;
16 use DXVars;
17 use DXUtil;
18 use DXChannel;
19 use DXUser;
20 use DXM;
21 use DXCommandmode;
22 use DXProt;
23 use DXCluster;
24 use DXDebug;
25
26 package main;
27
28 @inqueue = ();                # the main input queue, an array of hashes
29 $systime = 0;                 # the time now (in seconds)
30
31 # handle disconnections
32 sub disconnect
33 {
34   my $dxchan = shift;
35   return if !defined $dxchan;
36   my $user = $dxchan->{user};
37   my $conn = $dxchan->{conn};
38   $dxchan->finish();
39   $user->close() if defined $user;
40   $conn->disconnect() if defined $conn;
41   $dxchan->del();
42 }
43
44 # handle incoming messages
45 sub rec
46 {
47   my ($conn, $msg, $err) = @_;
48   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
49   
50   if (defined $err && $err) {
51     disconnect($dxchan) if defined $dxchan;
52         return;
53   }
54   
55   # set up the basic channel info - this needs a bit more thought - there is duplication here
56   if (!defined $dxchan) {
57      my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
58      my $user = DXUser->get($call);
59          if (!defined $user) {
60            $user = DXUser->new($call);
61          }
62          my $sort = $user->sort();
63          
64          # is there one already connected?
65          if (DXChannel->get($call)) {
66            my $mess = DXM::msg('conother', $call);
67            dbg('chan', "-> D $call $mess\n"); 
68        $conn->send_now("D$call|$mess");
69            dbg('chan', "-> Z $call bye\n");
70        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
71            return;
72      }
73
74          # is there one already connected elsewhere in the cluster?
75          if (DXCluster->get($call)) {
76            my $mess = DXM::msg('concluster', $call);
77            dbg('chan', "-> D $call $mess\n"); 
78        $conn->send_now("D$call|$mess");
79            dbg('chan', "-> Z $call bye\n");
80        $conn->send_now("Z$call|bye");          # this will cause 'client' to disconnect
81            return;
82      }
83
84      # set some necessary flags on the user if they are connecting
85          $user->wwv(1) if !$user->wwv();
86          $user->talk(1) if !$user->talk();
87          $user->ann(1) if !$user->ann();
88          $user->here(1) if !$user->here();
89          $user->sort('U') if !$user->sort();
90
91          # create the channel
92      $dxchan = DXCommandmode->new($call, $conn, $user) if ($sort eq 'U');
93      $dxchan = DXProt->new($call, $conn, $user) if ($sort eq 'A');
94          die "Invalid sort of user on $call = $sort" if !$dxchan;
95   }
96   
97   # queue the message and the channel object for later processing
98   if (defined $msg) {
99     my $self = bless {}, "inqueue";
100     $self->{dxchan} = $dxchan;
101     $self->{data} = $msg;
102         push @inqueue, $self;
103   }
104 }
105
106 sub login
107 {
108   return \&rec;
109 }
110
111 # cease running this program, close down all the connections nicely
112 sub cease
113 {
114   my $dxchan;
115   foreach $dxchan (DXChannel->get_all()) {
116     disconnect($dxchan);
117   }
118   exit(0);
119 }
120
121 # this is where the input queue is dealt with and things are dispatched off to other parts of
122 # the cluster
123 sub process_inqueue
124 {
125   my $self = shift @inqueue;
126   return if !$self;
127   
128   my $data = $self->{data};
129   my $dxchan = $self->{dxchan};
130   my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
131   
132   # do the really sexy console interface bit! (Who is going to do the TK interface then?)
133   dbg('chan', "<- $sort $call $line\n");
134   
135   # handle A records
136   my $user = $dxchan->user;
137   if ($sort eq 'A') {
138     $dxchan->start($line);  
139   } elsif ($sort eq 'D') {
140     die "\$user not defined for $call" if !defined $user;
141         $dxchan->normal($line);  
142     disconnect($dxchan) if ($dxchan->{state} eq 'bye');
143   } elsif ($sort eq 'Z') {
144     disconnect($dxchan);
145   } else {
146     print STDERR atime, " Unknown command letter ($sort) received from $call\n";
147   }
148 }
149
150 #############################################################
151 #
152 # The start of the main line of code 
153 #
154 #############################################################
155
156 # open the debug file, set various FHs to be unbuffered
157 dbginit($debugfn);
158 foreach(@debug) {
159   dbgadd($_);
160 }
161 STDOUT->autoflush(1);
162
163 # initialise User file system
164 DXUser->init($userfn);
165
166 # start listening for incoming messages/connects
167 Msg->new_server("$clusteraddr", $clusterport, \&login);
168
169 # prime some signals
170 $SIG{'INT'} = \&cease;
171 $SIG{'TERM'} = \&cease;
172 $SIG{'HUP'} = 'IGNORE';
173
174 # this, such as it is, is the main loop!
175 for (;;) {
176   my $timenow;
177   Msg->event_loop(1, 0.001);
178   $timenow = time;
179   if ($timenow != $systime) {
180     $systime = $timenow;
181         $cldate = &cldate();
182         $ztime = &ztime();
183   }
184   process_inqueue();                 # read in lines from the input queue and despatch them
185   DXCommandmode::process();     # process ongoing command mode stuff
186   DXProt::process();              # process ongoing ak1a pcxx stuff
187 }
188