2 # module to manage channel lists & data
4 # This is the base class for all channel operations, which is everything to do
5 # with input and output really.
7 # The instance variable in the outside world will be generally be called $dxchann
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.
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)
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).
22 # Copyright (c) 1998 - Dirk Koopman G1TLH
37 conn => 'Msg Connection ref',
41 state => 'Current State',
42 oldstate => 'Last State',
43 list => 'Dependant DXChannels list',
45 connsort => 'Connection Type'
49 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
52 my ($pkg, $call, $conn, $user) = @_;
55 die "trying to create a duplicate channel for $call" if $channels{$call};
56 $self->{call} = $call;
57 $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
58 $self->{user} = $user if defined $user;
61 $self->{oldstate} = 0;
63 return $channels{$call} = $self;
66 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
69 my ($pkg, $call) = @_;
70 return $channels{$call};
73 # obtain all the channel objects
77 return values(%channels);
80 # obtain a channel object by searching for its connection reference
83 my ($pkg, $conn) = @_;
86 foreach $self (values(%channels)) {
87 return $self if ($self->{conn} == $conn);
92 # get rid of a channel object [$obj->del()]
96 delete $channels{$self->{call}};
100 # handle out going messages, immediately without waiting for the select to drop
101 # this could, in theory, block
105 my $conn = $self->{conn};
107 # is this a list of channels ?
108 if (!defined $conn) {
109 die "tried to send_now to an invalid channel list" if !defined $self->{list};
111 foreach $lself (@$self->{list}) {
112 $lself->send_now(@_); # it's recursive :-)
116 my $call = $self->{call};
121 dbg('chan', "-> $sort $call $line\n");
122 $conn->send_now("$sort$call|$line");
128 # the normal output routine
130 sub send # this is always later and always data
133 my $conn = $self->{conn};
135 # is this a list of channels ?
136 if (!defined $conn) {
137 die "tried to send to an invalid channel list" if !defined $self->{list};
139 foreach $lself (@$self->{list}) {
140 $lself->send(@_); # here as well :-) :-)
143 my $call = $self->{call};
148 dbg('chan', "-> D $call $line\n");
149 $conn->send_later("D$call|$line");
154 # send a file (always later)
157 my ($self, $fn) = @_;
158 my $call = $self->{call};
159 my $conn = $self->{conn};
162 open(F, $fn) or die "can't open $fn for sending file ($!)";
168 # just a shortcut for $dxchan->send(msg(...));
172 $self->send(DXM::msg(@_));
175 # change the state of the channel - lots of scope for debugging here :-)
179 $self->{oldstate} = $self->{state};
180 $self->{state} = shift;
181 dbg('state', "$self->{call} channel state $self->{oldstate} -> $self->{state}\n");
184 # various access routines
187 # return a list of valid elements
196 # return a prompt for a field
201 my ($self, $ele) = @_;
208 my $name = $AUTOLOAD;
210 return if $name =~ /::DESTROY$/;
213 die "Non-existant field '$AUTOLOAD'" if !$valid{$name};
214 @_ ? $self->{$name} = shift : $self->{$name} ;