ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/lib/KGS/Listener.pm
Revision: 1.2
Committed: Fri May 30 03:26:01 2003 UTC (23 years, 6 months ago) by pcg
Branch: MAIN
CVS Tags: aa50
Changes since 1.1: +7 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 package KGS::Listener;
2    
3     =item new
4    
5     =cut
6    
7     sub new {
8     my $class = shift;
9     bless { @_ }, $class;
10     }
11    
12     =cut
13    
14     =item $listener->listen($conn, [msgtype...])
15    
16     Registers the object to receive callback messages of the named type(s). No
17     msgtypes means all msgtypes.
18    
19     =item $listener->unlisten
20    
21     Unregisters the object again.
22    
23     =cut
24    
25     sub listen {
26     my ($self, $conn, @types) = @_;
27    
28 pcg 1.2 if ($conn) {
29     $self->unlisten;
30     $self->{conn} = $conn;
31     $self->{listen_types} = \@types;
32     $conn->register($self, @types);
33     }
34 pcg 1.1 }
35    
36     sub unlisten {
37     my ($self) = @_;
38    
39     (delete $self->{conn})
40     ->unregister($self, @{$self->{listen_types}}) if $self->{conn};
41     }
42    
43     =item $listener->msg($type, %args);
44    
45     Calls the C<msg> method of the connection when in listen state.
46    
47     =cut
48    
49     sub msg {
50     my $self = shift;
51 pcg 1.2 $self->{conn}->msg(@_) if $self->{conn};
52 pcg 1.1 }
53    
54     sub inject {
55     my ($self, $msg) = @_;
56    
57     if (my $cb = $self->can("inject_$msg->{type}")) {
58     $cb->($self, $msg);
59     } else {
60     $self->inject_fallback ($msg);
61     }
62     }
63    
64     sub inject_fallback {
65     }
66    
67     1;
68