065a78c8d7af7e06293117a99332935634cfbab2
[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 require Exporter;
29 @ISA = qw(DXCommandmode DXProt Exporter);
30
31 use Msg;
32 use DXUtil;
33 use DXM;
34
35 %channels = undef;
36
37 # create a new connection object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
38 sub new
39 {
40   my ($pkg, $call, $conn, $user) = @_;
41   my $self = {};
42   
43   die "trying to create a duplicate channel for $call" if $channels{$call};
44   $self->{call} = $call;
45   $self->{conn} = $conn if defined $conn;   # if this isn't defined then it must be a list
46   $self->{user} = $user if defined $user; 
47   $self->{t} = time;
48   $self->{state} = 0;
49   bless $self, $pkg; 
50   return $channels{$call} = $self;
51 }
52
53 # obtain a connection object by callsign [$obj = DXChannel->get($call)]
54 sub get
55 {
56   my ($pkg, $call) = @_;
57   return $connect{$call};
58 }
59
60 # obtain all the connection objects
61 sub get_all
62 {
63   my ($pkg) = @_;
64   return values(%channels);
65 }
66
67 # obtain a connection object by searching for its connection reference
68 sub get_by_cnum
69 {
70   my ($pkg, $conn) = @_;
71   my $self;
72   
73   foreach $self (values(%channels)) {
74     return $self if ($self->{conn} == $conn);
75   }
76   return undef;
77 }
78
79 # get rid of a connection object [$obj->del()]
80 sub del
81 {
82   my $self = shift;
83   delete $channels{$self->{call}};
84 }
85
86
87 # handle out going messages, immediately without waiting for the select to drop
88 # this could, in theory, block
89 sub send_now
90 {
91   my $self = shift;
92   my $conn = $self->{conn};
93
94   # is this a list of channels ?
95   if (!defined $conn) {
96     die "tried to send_now to an invalid channel list" if !defined $self->{list};
97         my $lself;
98         foreach $lself (@$self->{list}) {
99           $lself->send_now(@_);             # it's recursive :-)
100         }
101   } else {
102     my $sort = shift;
103     my $call = $self->{call};
104     my $line;
105         
106     foreach $line (@_) {
107       my $t = atime;
108           chomp $line;
109       print main::DEBUG "$t > $sort $call $line\n" if defined DEBUG;
110           print "> $sort $call $line\n";
111       $conn->send_now("$sort$call|$line");
112         }
113   }
114 }
115
116 #
117 # the normal output routine
118 #
119 sub send              # this is always later and always data
120 {
121   my $self = shift;
122   my $conn = $self->{conn};
123  
124   # is this a list of channels ?
125   if (!defined $conn) {
126     die "tried to send to an invalid channel list" if !defined $self->{list};
127         my $lself;
128         foreach $lself (@$self->{list}) {
129           $lself->send(@_);                 # here as well :-) :-)
130         }
131   } else {
132     my $call = $self->{call};
133     my $line;
134
135     foreach $line (@_) {
136       my $t = atime;
137           chomp $line;
138           print main::DEBUG "$t > D $call $line\n" if defined DEBUG;
139           print "> D $call $line\n";
140           $conn->send_later("D$call|$line");
141         }
142   }
143 }
144
145 # send a file (always later)
146 sub send_file
147 {
148   my ($self, $fn) = @_;
149   my $call = $self->{call};
150   my $conn = $self->{conn};
151   my @buf;
152   
153   open(F, $fn) or die "can't open $fn for sending file ($!)";
154   @buf = <F>;
155   close(F);
156   $self->send(@buf);
157 }
158
159 # just a shortcut for $dxchan->send(msg(...));
160 sub msg
161 {
162   my $self = shift;
163   $self->send(DXM::msg(@_));
164 }
165
166 1;
167 __END__;