b661510040eebe5954bf7063b24169a2a435fa5c
[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 use DXUtil;
15
16 %connects = undef;
17
18 # create a new connection object [$obj = Connect->new($call, $msg_conn_obj, $user_obj)]
19 sub new
20 {
21   my ($pkg, $call, $conn, $user) = @_;
22   my $self = {};
23   
24   die "trying to create a duplicate channel for $call" if $connects{$call};
25   $self->{call} = $call;
26   $self->{conn} = $conn;
27   $self->{user} = $user;
28   $self->{t} = time;
29   $self->{state} = 0;
30   bless $self, $pkg; 
31   return $connects{$call} = $self;
32 }
33
34 # obtain a connection object by callsign [$obj = Connect->get($call)]
35 sub get
36 {
37   my ($pkg, $call) = @_;
38   return $connect{$call};
39 }
40
41 # obtain all the connection objects
42 sub get_all
43 {
44   my ($pkg) = @_;
45   return values(%connects);
46 }
47
48 # obtain a connection object by searching for its connection reference
49 sub get_by_cnum
50 {
51   my ($pkg, $conn) = @_;
52   my $self;
53   
54   foreach $self (values(%connects)) {
55     return $self if ($self->{conn} == $conn);
56   }
57   return undef;
58 }
59
60 # get rid of a connection object [$obj->del()]
61 sub del
62 {
63   my $self = shift;
64   delete $connects{$self->{call}};
65 }
66
67
68 # handle out going messages
69 sub send_now
70 {
71   my $self = shift;
72   my $sort = shift;
73   my $call = $self->{call};
74   my $conn = $self->{conn};
75   my $line;
76
77   foreach $line (@_) {
78     my $t = atime;
79         chomp $line;
80     print main::DEBUG "$t > $sort $call $line\n" if defined DEBUG;
81         print "> $sort $call $line\n";
82     $conn->send_now("$sort$call|$line");
83   }
84 }
85
86 sub send_later
87 {
88   my $self = shift;
89   my $sort = shift;
90   my $call = $self->{call};
91   my $conn = $self->{conn};
92   my $line;
93
94   foreach $line (@_) {
95     my $t = atime;
96         chomp $line;
97     print main::DEBUG "$t > $sort $call $line\n" if defined DEBUG;
98     print "> $sort $call $line\n";
99     $conn->send_later("$sort$call|$line");
100   }
101 }
102
103 # send a file (always later)
104 sub send_file
105 {
106   my ($self, $fn) = @_;
107   my $call = $self->{call};
108   my $conn = $self->{conn};
109   my @buf;
110   
111   open(F, $fn) or die "can't open $fn for sending file ($!)";
112   @buf = <F>;
113   close(F);
114   $self->send_later('D', @buf);
115 }
116
117 1;
118 __END__;