ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/lib/KGS/Listener.pm
Revision: 1.11
Committed: Wed Feb 4 02:53:41 2004 UTC (22 years, 9 months ago) by pcg
Branch: MAIN
Changes since 1.10: +70 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 package KGS::Listener;
2    
3 pcg 1.11 =head1 NAME
4    
5     KGS::Listener - a generic base class to listen for kgs messages.
6    
7     =head1 SYNOPSIS
8    
9     use base KGS::Listener;
10    
11     sub new {
12     my $class = shift;
13     my $self = $class->SUPER::new (@_);
14    
15     # for non-channel-related listeners:
16     $self->listen ($self->{conn}, qw(ping req_pic));
17     # for channel-type listener
18     $self->listen ($self->{conn}, qw(join_room: part_room: msg_room:));
19    
20     $self;
21     }
22    
23     sub inject_xxx {
24     # handle msg xxx
25     }
26    
27     # KGS::Listener::Room etc. als require this:
28     sub event_xxx {
29     # handle synthesized event xxx
30     }
31    
32     =head1 DESCRIPTION
33    
34     Please supply a description )
35    
36     The KGS::Listener family has currently these members:
37    
38     KGS::Listener base class for everything
39     KGS::Listener::Channel base class for channels (games, rooms)
40     KGS::Listener::Game base class that handles games
41     KGS::Listener::Room base class for rooms and their game lists
42     KGS::Listener::Roomlist base class for the overall room listing
43     KGS::Listener::User base class for user info, chats etc.
44     KGS::Listener::Debug prints all messages that marc doesn't understand
45    
46     =head2 METHODS
47    
48     =over 4
49    
50     =item new [channel => <id>]...
51    
52     Create a new KGS::Listener project. The L<channel> parameter is optional.
53 pcg 1.1
54     =cut
55    
56     sub new {
57     my $class = shift;
58     bless { @_ }, $class;
59     }
60    
61     =cut
62    
63     =item $listener->listen($conn, [msgtype...])
64    
65 pcg 1.11 Registers the object to receive callback messages of the named type(s).
66    
67     A msgtype is either a packet name like C<login> or C<msg_room>, the
68     string C<any>, which will match any type, or a msgtype postixed with
69     C<:> (e.g. C<msg_room:>), in which case it will only match the C<<
70     $listener->{channel} >> channel.
71    
72     The conenction will be stored in C<< $listener->{conn} >>.
73    
74     In your own new method you should call C<< $self->listen >> once with the
75     connection and the msgtypes you want to listen for.
76    
77     Calling C<listen> multiple times is valid and will works incrementally.
78 pcg 1.1
79     =item $listener->unlisten
80    
81     Unregisters the object again.
82    
83     =cut
84    
85     sub listen {
86     my ($self, $conn, @types) = @_;
87    
88 pcg 1.3 $_ =~ s/:$/:$self->{channel}/ for @types;
89    
90 pcg 1.2 if ($conn) {
91     $self->unlisten;
92     $self->{conn} = $conn;
93     $self->{listen_types} = \@types;
94     $conn->register($self, @types);
95     }
96 pcg 1.1 }
97    
98     sub unlisten {
99     my ($self) = @_;
100    
101     (delete $self->{conn})
102     ->unregister($self, @{$self->{listen_types}}) if $self->{conn};
103     }
104    
105 pcg 1.3 =item $listener->inject($msg)
106 pcg 1.1
107 pcg 1.3 The main injector callback.. all (listened for) messages end up in this
108     method, which will just dispatch a method with name inject_<msgtype>.
109 pcg 1.1
110 pcg 1.11 You do not normally have to overwrite this method, but you should provide
111     methods that are being called with names like C<inject_msg_room> etc.
112    
113 pcg 1.1 =cut
114    
115     sub inject {
116     my ($self, $msg) = @_;
117    
118     if (my $cb = $self->can("inject_$msg->{type}")) {
119 pcg 1.4 $cb->($self, $msg);
120 pcg 1.10 } elsif (my $cb = $self->can("inject_any")) {
121     $cb->($self, $msg);
122 pcg 1.1 } else {
123 pcg 1.8 warn "no handler found for message $msg->{type} in $self\n";
124 pcg 1.1 }
125     }
126    
127 pcg 1.5 =item $listener->send($type, %args);
128 pcg 1.3
129 pcg 1.7 Calls the C<send> method of the connection when in listen state. It does
130     not (yet) supply a default channel id.
131 pcg 1.3
132     =cut
133    
134 pcg 1.5 sub send {
135 pcg 1.7 my ($self, $type, @arg) = @_;
136    
137     $self->{conn}->send($type, @arg) if $self->{conn};
138     }
139    
140     sub DESTROY {
141     my ($self) = @_;
142    
143     $self->unlisten;
144 pcg 1.1 }
145    
146 pcg 1.11 =back
147    
148     =cut
149    
150 pcg 1.1 1;
151