sorted out inheritance
[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   $dxchan->finish();
37   $user->close() if defined $user;
38   $conn->disconnect() if defined $conn;
39   $dxchan->del();
40 }
41
42 # handle incoming messages
43 sub rec
44 {
45   my ($conn, $msg, $err) = @_;
46   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
47   
48   if (defined $err && $err) {
49     disconnect($dxchan) if defined $dxchan;
50         return;
51   }
52   
53   # set up the basic channel info - this needs a bit more thought - there is duplication here
54   if (!defined $dxchan) {
55      my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
56      my $user = DXUser->get($call);
57          $user = DXUser->new($call) if !defined $user;
58          $user->sort('U') if (!$user->sort());
59          my $sort = $user->sort();
60      $dxchan = DXCommandmode->new($call, $conn, $user) if ($sort eq 'U');
61      $dxchan = DXProt->new($call, $conn, $user) if ($sort eq 'A');
62          die "Invalid sort of user on $call = $sort" if !$dxchan;
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     $dxchan->start($line);  
108   } elsif ($sort eq 'D') {
109     die "\$user not defined for $call" if !defined $user;
110         $dxchan->normal($line);  
111     disconnect($dxchan) if ($dxchan->{state} eq 'bye');
112   } elsif ($sort eq 'Z') {
113     disconnect($dxchan);
114   } else {
115     print STDERR atime, " Unknown command letter ($sort) received from $call\n";
116   }
117 }
118
119 #############################################################
120 #
121 # The start of the main line of code 
122 #
123 #############################################################
124
125 # open the debug file, set various FHs to be unbuffered
126 open(DEBUG, ">>$debugfn") or die "can't open $debugfn($!)";
127 select DEBUG; $| = 1;
128 select STDOUT; $| = 1;
129
130 # initialise User file system
131 DXUser->init($userfn);
132
133 # start listening for incoming messages/connects
134 Msg->new_server("$clusteraddr", $clusterport, \&login);
135
136 # prime some signals
137 $SIG{'INT'} = \&cease;
138 $SIG{'TERM'} = \&cease;
139 $SIG{'HUP'} = 'IGNORE';
140
141 # this, such as it is, is the main loop!
142 for (;;) {
143   my $timenow;
144   Msg->event_loop(1, 0.001);
145   $timenow = time;
146   if ($timenow != $systime) {
147     $systime = $timenow;
148         $cldate = &cldate();
149         $ztime = &ztime();
150   }
151   process_inqueue();                 # read in lines from the input queue and despatch them
152   DXCommandmode::process();     # process ongoing command mode stuff
153   DXProt::process();              # process ongoing ak1a pcxx stuff
154 }
155