2f96af8814e9a2e2333e0d9118716bf61e28210e
[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
24 package main;
25
26 @inqueue = ();                # the main input queue, an array of hashes
27 $systime = 0;                 # the time now (in seconds)
28
29 # handle disconnections
30 sub disconnect
31 {
32   my $dxchan = shift;
33   return if !defined $dxchan;
34   my $user = $dxchan->{user};
35   my $conn = $dxchan->{conn};
36   if ($user->{sort} eq 'A') {           # and here (when I find out how to write it!)
37     $dxchan->pc_finish();  
38   } else {
39     $dxchan->user_finish();
40   }
41   $user->close() if defined $user;
42   $conn->disconnect() if defined $conn;
43   $dxchan->del();
44 }
45
46 # handle incoming messages
47 sub rec
48 {
49   my ($conn, $msg, $err) = @_;
50   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
51   
52   if (defined $err && $err) {
53     disconnect($dxchan) if defined $dxchan;
54         return;
55   }
56   
57   # set up the basic channel info - this needs a bit more thought - there is duplication here
58   if (!defined $dxchan) {
59      my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
60      my $user = DXUser->get($call);
61          $user = DXUser->new($call) if !defined $user;
62      $dxchan = DXChannel->new($call, $conn, $user);  
63   }
64   
65   # queue the message and the channel object for later processing
66   if (defined $msg) {
67     my $self = bless {}, "inqueue";
68     $self->{dxchan} = $dxchan;
69     $self->{data} = $msg;
70         push @inqueue, $self;
71   }
72 }
73
74 sub login
75 {
76   return \&rec;
77 }
78
79 # cease running this program, close down all the connections nicely
80 sub cease
81 {
82   my $dxchan;
83   foreach $dxchan (DXChannel->get_all()) {
84     disconnect($dxchan);
85   }
86   exit(0);
87 }
88
89 # this is where the input queue is dealt with and things are dispatched off to other parts of
90 # the cluster
91 sub process_inqueue
92 {
93   my $self = shift @inqueue;
94   return if !$self;
95   
96   my $data = $self->{data};
97   my $dxchan = $self->{dxchan};
98   my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
99   
100   # do the really sexy console interface bit! (Who is going to do the TK interface then?)
101   print DEBUG atime, " <- $sort $call $line\n" if defined DEBUG;
102   print "<- $sort $call $line\n";
103   
104   # handle A records
105   my $user = $dxchan->{user};
106   if ($sort eq 'A') {
107         $user->{sort} = 'U' if !defined $user->{sort};
108     if ($user->{sort} eq 'A') {
109           $dxchan->pc_start($line);  
110         } else {
111           $dxchan->user_start($line);
112         }
113   } elsif ($sort eq 'D') {
114     die "\$user not defined for $call" if !defined $user;
115     if ($user->{sort} eq 'A') {           # we will have a symbolic ref to a proc here
116           $dxchan->pc_normal($line);  
117         } else {
118           $dxchan->user_normal($line);
119         }
120     disconnect($dxchan) if ($dxchan->{state} eq 'bye');
121   } elsif ($sort eq 'Z') {
122     disconnect($dxchan);
123   } else {
124     print STDERR atime, " Unknown command letter ($sort) received from $call\n";
125   }
126 }
127
128 #############################################################
129 #
130 # The start of the main line of code 
131 #
132 #############################################################
133
134 # open the debug file, set various FHs to be unbuffered
135 open(DEBUG, ">>$debugfn") or die "can't open $debugfn($!)";
136 select DEBUG; $| = 1;
137 select STDOUT; $| = 1;
138
139 # initialise User file system
140 DXUser->init($userfn);
141
142 # start listening for incoming messages/connects
143 Msg->new_server("$clusteraddr", $clusterport, \&login);
144
145 # prime some signals
146 $SIG{'INT'} = \&cease;
147 $SIG{'TERM'} = \&cease;
148 $SIG{'HUP'} = 'IGNORE';
149
150 # this, such as it is, is the main loop!
151 for (;;) {
152   my $timenow;
153   Msg->event_loop(1, 0.001);
154   $timenow = time;
155   if ($timenow != $systime) {
156     $systime = $timenow;
157         $cldate = &cldate();
158         $ztime = &ztime();
159   }
160   process_inqueue();                 # read in lines from the input queue and despatch them
161   DXCommandmode::user_process();     # process ongoing command mode stuff
162   DXProt::pc_process();              # process ongoing ak1a pcxx stuff
163 }
164