did some work on the cluster database related things
[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 use vars qw(%channels %valid);
36
37 %channels = undef;
38
39 %valid = (
40   call => '0,Callsign',
41   conn => '9,Msg Conn ref',
42   user => '9,DXUser ref',
43   startt => '0,Start Time,atime',
44   t => '9,Time,atime',
45   pc50_t => '9,Last PC50 Time,atime',
46   priv => '9,Privilege',
47   state => '0,Current State',
48   oldstate => '5,Last State',
49   list => '9,Dep Chan List',
50   name => '0,User Name',
51   consort => '9,Connection Type',
52   sort => '9,Type of Channel',
53   wwv => '0,Want WWV,yesno',
54   talk => '0,Want Talk,yesno',
55   ann => '0,Want Announce,yesno',
56   here => '0,Here?,yesno',
57   confmode => '0,In Conference?,yesno',
58   dx => '0,DX Spots,yesno',
59   redirect => '0,Redirect messages to',
60 );
61
62 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
63 sub alloc
64 {
65   my ($pkg, $call, $conn, $user) = @_;
66   my $self = {};
67   
68   die "trying to create a duplicate channel for $call" if $channels{$call};
69   $self->{call} = $call;
70   $self->{conn} = $conn if defined $conn;   # if this isn't defined then it must be a list
71   $self->{user} = $user if defined $user; 
72   $self->{startt} = $self->{t} = time;
73   $self->{state} = 0;
74   $self->{oldstate} = 0;
75   bless $self, $pkg; 
76   return $channels{$call} = $self;
77 }
78
79 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
80 sub get
81 {
82   my ($pkg, $call) = @_;
83   return $channels{$call};
84 }
85
86 # obtain all the channel objects
87 sub get_all
88 {
89   my ($pkg) = @_;
90   return values(%channels);
91 }
92
93 # obtain a channel object by searching for its connection reference
94 sub get_by_cnum
95 {
96   my ($pkg, $conn) = @_;
97   my $self;
98   
99   foreach $self (values(%channels)) {
100     return $self if ($self->{conn} == $conn);
101   }
102   return undef;
103 }
104
105 # get rid of a channel object [$obj->del()]
106 sub del
107 {
108   my $self = shift;
109   delete $channels{$self->{call}};
110 }
111
112 # is it an ak1a cluster ?
113 sub is_ak1a
114 {
115   my $self = shift;
116   return $self->{sort} eq 'A';
117 }
118
119 # is it a user?
120 sub is_user
121 {
122   my $self = shift;
123   return $self->{sort} eq 'U';
124 }
125
126 # handle out going messages, immediately without waiting for the select to drop
127 # this could, in theory, block
128 sub send_now
129 {
130   my $self = shift;
131   my $conn = $self->{conn};
132   my $sort = shift;
133   my $call = $self->{call};
134   my $line;
135         
136   foreach $line (@_) {
137     chomp $line;
138         dbg('chan', "-> $sort $call $line\n") if $conn;
139         $conn->send_now("$sort$call|$line") if $conn;
140   }
141   $self->{t} = time;
142 }
143
144 #
145 # the normal output routine
146 #
147 sub send              # this is always later and always data
148 {
149   my $self = shift;
150   my $conn = $self->{conn};
151   my $call = $self->{call};
152   my $line;
153
154   foreach $line (@_) {
155     chomp $line;
156         dbg('chan', "-> D $call $line\n") if $conn;
157         $conn->send_later("D$call|$line") if $conn;
158   }
159   $self->{t} = time;
160 }
161
162 # send a file (always later)
163 sub send_file
164 {
165   my ($self, $fn) = @_;
166   my $call = $self->{call};
167   my $conn = $self->{conn};
168   my @buf;
169   
170   open(F, $fn) or die "can't open $fn for sending file ($!)";
171   @buf = <F>;
172   close(F);
173   $self->send(@buf);
174 }
175
176 # just a shortcut for $dxchan->send(msg(...));
177 sub msg
178 {
179   my $self = shift;
180   $self->send(DXM::msg(@_));
181 }
182
183 # change the state of the channel - lots of scope for debugging here :-)
184 sub state
185 {
186   my $self = shift;
187   $self->{oldstate} = $self->{state};
188   $self->{state} = shift;
189   dbg('state', "$self->{call} channel state $self->{oldstate} -> $self->{state}\n");
190 }
191
192 # various access routines
193
194 #
195 # return a list of valid elements 
196
197
198 sub fields
199 {
200   return keys(%valid);
201 }
202
203 #
204 # return a prompt for a field
205 #
206
207 sub field_prompt
208
209   my ($self, $ele) = @_;
210   return $valid{$ele};
211 }
212
213 no strict;
214 sub AUTOLOAD
215 {
216   my $self = shift;
217   my $name = $AUTOLOAD;
218   return if $name =~ /::DESTROY$/;
219   $name =~ s/.*:://o;
220   
221   confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
222   @_ ? $self->{$name} = shift : $self->{$name} ;
223 }
224
225 1;
226 __END__;