well on the way to having a working cluster database
[spider.git] / perl / DXChannel.pm
1 #
2 # module to manage channel lists & data
3 #
4 # This is the base class for all channel operations, which is everything to do 
5 # with input and output really.
6 #
7 # The instance variable in the outside world will be generally be called $dxchann
8 #
9 # This class is 'inherited' (if that is the goobledegook for what I am doing)
10 # by various other modules. The point to understand is that the 'instance variable'
11 # is in fact what normal people would call the state vector and all useful info
12 # about a connection goes in there.
13 #
14 # Another point to note is that a vector may contain a list of other vectors. 
15 # I have simply added another variable to the vector for 'simplicity' (or laziness
16 # as it is more commonly called)
17 #
18 # PLEASE NOTE - I am a C programmer using this as a method of learning perl
19 # firstly and OO about ninthly (if you don't like the design and you can't 
20 # improve it with better OO by make it smaller and more efficient, then tough). 
21 #
22 # Copyright (c) 1998 - Dirk Koopman G1TLH
23 #
24 # $Id$
25 #
26 package DXChannel;
27
28 use Msg;
29 use DXUtil;
30 use DXM;
31 use DXDebug;
32 use Carp;
33
34 use strict;
35
36 my %channels = undef;
37
38 my %valid = (
39   call => '0,Callsign',
40   conn => '9,Msg Conn ref',
41   user => '9,DXUser ref',
42   startt => '0,Start Time,atime',
43   t => '9,Time,atime',
44   pc50_t => '9,Last PC50 Time,atime',
45   priv => '9,Privilege',
46   state => '0,Current State',
47   oldstate => '5,Last State',
48   list => '9,Dep Chan List',
49   name => '0,User Name',
50   consort => '9,Connection Type',
51   sort => '9,Type of Channel',
52   wwv => '0,Want WWV,yesno',
53   talk => '0,Want Talk,yesno',
54   ann => '0,Want Announce,yesno',
55   here => '0,Here?,yesno',
56   confmode => '0,In Conference?,yesno',
57   dx => '0,DX Spots,yesno',
58 );
59
60 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
61 sub alloc
62 {
63   my ($pkg, $call, $conn, $user) = @_;
64   my $self = {};
65   
66   die "trying to create a duplicate channel for $call" if $channels{$call};
67   $self->{call} = $call;
68   $self->{conn} = $conn if defined $conn;   # if this isn't defined then it must be a list
69   $self->{user} = $user if defined $user; 
70   $self->{startt} = $self->{t} = time;
71   $self->{state} = 0;
72   $self->{oldstate} = 0;
73   bless $self, $pkg; 
74   return $channels{$call} = $self;
75 }
76
77 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
78 sub get
79 {
80   my ($pkg, $call) = @_;
81   return $channels{$call};
82 }
83
84 # obtain all the channel objects
85 sub get_all
86 {
87   my ($pkg) = @_;
88   return values(%channels);
89 }
90
91 # obtain a channel object by searching for its connection reference
92 sub get_by_cnum
93 {
94   my ($pkg, $conn) = @_;
95   my $self;
96   
97   foreach $self (values(%channels)) {
98     return $self if ($self->{conn} == $conn);
99   }
100   return undef;
101 }
102
103 # get rid of a channel object [$obj->del()]
104 sub del
105 {
106   my $self = shift;
107   delete $channels{$self->{call}};
108 }
109
110 # is it an ak1a cluster ?
111 sub is_ak1a
112 {
113   my $self = shift;
114   return $self->{sort} eq 'A';
115 }
116
117 # is it a user?
118 sub is_user
119 {
120   my $self = shift;
121   return $self->{sort} eq 'U';
122 }
123
124 # handle out going messages, immediately without waiting for the select to drop
125 # this could, in theory, block
126 sub send_now
127 {
128   my $self = shift;
129   my $conn = $self->{conn};
130   my $sort = shift;
131   my $call = $self->{call};
132   my $line;
133         
134   foreach $line (@_) {
135     chomp $line;
136         dbg('chan', "-> $sort $call $line\n") if $conn;
137         $conn->send_now("$sort$call|$line") if $conn;
138   }
139   $self->{t} = time;
140 }
141
142 #
143 # the normal output routine
144 #
145 sub send              # this is always later and always data
146 {
147   my $self = shift;
148   my $conn = $self->{conn};
149   my $call = $self->{call};
150   my $line;
151
152   foreach $line (@_) {
153     chomp $line;
154         dbg('chan', "-> D $call $line\n") if $conn;
155         $conn->send_later("D$call|$line") if $conn;
156   }
157   $self->{t} = time;
158 }
159
160 # send a file (always later)
161 sub send_file
162 {
163   my ($self, $fn) = @_;
164   my $call = $self->{call};
165   my $conn = $self->{conn};
166   my @buf;
167   
168   open(F, $fn) or die "can't open $fn for sending file ($!)";
169   @buf = <F>;
170   close(F);
171   $self->send(@buf);
172 }
173
174 # just a shortcut for $dxchan->send(msg(...));
175 sub msg
176 {
177   my $self = shift;
178   $self->send(DXM::msg(@_));
179 }
180
181 # change the state of the channel - lots of scope for debugging here :-)
182 sub state
183 {
184   my $self = shift;
185   $self->{oldstate} = $self->{state};
186   $self->{state} = shift;
187   dbg('state', "$self->{call} channel state $self->{oldstate} -> $self->{state}\n");
188 }
189
190 # various access routines
191
192 #
193 # return a list of valid elements 
194
195
196 sub fields
197 {
198   return keys(%valid);
199 }
200
201 #
202 # return a prompt for a field
203 #
204
205 sub field_prompt
206
207   my ($self, $ele) = @_;
208   return $valid{$ele};
209 }
210
211 no strict;
212 sub AUTOLOAD
213 {
214   my $self = shift;
215   my $name = $AUTOLOAD;
216   return if $name =~ /::DESTROY$/;
217   $name =~ s/.*:://o;
218   
219   confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
220   @_ ? $self->{$name} = shift : $self->{$name} ;
221 }
222
223 1;
224 __END__;