started to flesh some of it out.
[spider.git] / perl / DXChannel.pm
1 #
2 # module to manage channel lists & data
3 #
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8 package DXChannel;
9
10 require Exporter;
11 @ISA = qw(Exporter);
12
13 use Msg;
14
15 %connects = undef;
16
17 # create a new connection object [$obj = Connect->new($call, $msg_conn_obj, $user_obj)]
18 sub new
19 {
20   my ($pkg, $call, $conn, $user) = @_;
21   my $self = {};
22   
23   die "trying to create a duplicate Connect for call $call\n" if $connects{$call};
24   $self->{call} = $call;
25   $self->{conn} = $conn;
26   $self->{user} = $user;
27   $self->{t} = time;
28   $self->{state} = 0;
29   bless $self, $pkg; 
30   return $connects{$call} = $self;
31 }
32
33 # obtain a connection object by callsign [$obj = Connect->get($call)]
34 sub get
35 {
36   my ($pkg, $call) = @_;
37   return $connect{$call};
38 }
39
40 # obtain all the connection objects
41 sub get_all
42 {
43   my ($pkg) = @_;
44   return values(%connects);
45 }
46
47 # obtain a connection object by searching for its connection reference
48 sub get_by_cnum
49 {
50   my ($pkg, $conn) = @_;
51   my $self;
52   
53   foreach $self (values(%connects)) {
54     return $self if ($self->{conn} == $conn);
55   }
56   return undef;
57 }
58
59 # get rid of a connection object [$obj->del()]
60 sub del
61 {
62   my $self = shift;
63   delete $connects{$self->{call}};
64 }
65
66
67 # handle out going messages
68 sub send_now
69 {
70   my $self = shift;
71   my $sort = shift;
72   my $call = $self->{call};
73   my $conn = $self->{conn};
74   my $line;
75
76   foreach $line (@_) {
77     print DEBUG "$t > $sort $call $line\n" if defined DEBUG;
78         print "> $sort $call $line\n";
79     $conn->send_now("$sort$call|$line");
80   }
81 }
82
83 sub send_later
84 {
85   my $self = shift;
86   my $sort = shift;
87   my $call = $self->{call};
88   my $conn = $self->{conn};
89   my $line;
90
91   foreach $line (@_) {
92     print DEBUG "$t > $sort $call $line\n" if defined DEBUG;
93     print "> $sort $call $line\n";
94     $conn->send_later("$sort$call|$line");
95   }
96 }
97
98 # send a file (always later)
99 sub send_file
100 {
101   my ($self, $fn) = @_;
102   my $call = $self->{call};
103   my $conn = $self->{conn};
104   my @buf;
105   
106   open(F, $fn) or die "can't open $fn for sending file ($!)";
107   @buf = <F>;
108   close(F);
109   $self->send_later('D', @buf);
110 }
111
112 1;
113 __END__;