ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/lib/KGS/Listener.pm
Revision: 1.1
Committed: Wed May 28 21:30:54 2003 UTC (23 years, 6 months ago) by pcg
Branch: MAIN
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     $self->unlisten;
29     $self->{conn} = $conn;
30     $self->{listen_types} = \@types;
31     $conn->register($self, @types);
32     }
33    
34     sub unlisten {
35     my ($self) = @_;
36    
37     (delete $self->{conn})
38     ->unregister($self, @{$self->{listen_types}}) if $self->{conn};
39     }
40    
41     =item $listener->msg($type, %args);
42    
43     Calls the C<msg> method of the connection when in listen state.
44    
45     =cut
46    
47     sub msg {
48     my $self = shift;
49     $self->{conn}->msg(@_);
50     }
51    
52     sub inject {
53     my ($self, $msg) = @_;
54    
55     if (my $cb = $self->can("inject_$msg->{type}")) {
56     $cb->($self, $msg);
57     } else {
58     $self->inject_fallback ($msg);
59     }
60     }
61    
62     sub inject_fallback {
63     }
64    
65     1;
66