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