ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/lib/KGS/Listener.pm
Revision: 1.14
Committed: Mon May 31 18:18:26 2004 UTC (22 years, 6 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +23 -12 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 pcg 1.13 =item $listener->listen ($conn, [msgtype...])
64 pcg 1.1
65 pcg 1.11 Registers the object to receive callback messages of the named type(s).
66 root 1.14 If C<$conn> is C<undef>, returns immediately. It's safe to call this
67     function repeatedly.
68 pcg 1.11
69     A msgtype is either a packet name like C<login> or C<msg_room>, the
70 pcg 1.12 string C<any>, which will match any type, or a msgtype postdixed with
71 pcg 1.11 C<:> (e.g. C<msg_room:>), in which case it will only match the C<<
72     $listener->{channel} >> channel.
73    
74 root 1.14 The connection will be stored in C<< $listener->{conn} >>.
75 pcg 1.11
76     In your own new method you should call C<< $self->listen >> once with the
77     connection and the msgtypes you want to listen for.
78    
79 pcg 1.1 =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.2 if ($conn) {
89     $self->unlisten;
90     $self->{conn} = $conn;
91 root 1.14 $_ =~ s/:$/:$self->{channel}/ for @types;
92 pcg 1.2 $self->{listen_types} = \@types;
93 root 1.14 $self->{conn}->register ($self, "quit", @types);
94 pcg 1.2 }
95 pcg 1.1 }
96    
97     sub unlisten {
98     my ($self) = @_;
99    
100 root 1.14 (delete $self->{conn})->unregister ($self, @{$self->{listen_types}})
101     if $self->{conn};
102 pcg 1.1 }
103    
104 pcg 1.13 =item $listener->inject ($msg)
105 pcg 1.1
106 pcg 1.3 The main injector callback.. all (listened for) messages end up in this
107     method, which will just dispatch a method with name inject_<msgtype>.
108 pcg 1.1
109 pcg 1.11 You do not normally have to overwrite this method, but you should provide
110     methods that are being called with names like C<inject_msg_room> etc.
111    
112 pcg 1.1 =cut
113    
114     sub inject {
115     my ($self, $msg) = @_;
116    
117 root 1.14 if (my $cb = $self->can ("inject_$msg->{type}")) {
118 pcg 1.4 $cb->($self, $msg);
119 root 1.14 } elsif (my $cb = $self->can ("inject_any")) {
120 pcg 1.10 $cb->($self, $msg);
121 pcg 1.1 } else {
122 pcg 1.8 warn "no handler found for message $msg->{type} in $self\n";
123 pcg 1.1 }
124     }
125    
126 root 1.14 =item $listener->send ($type, %args);
127 pcg 1.3
128 pcg 1.7 Calls the C<send> method of the connection when in listen state. It does
129     not (yet) supply a default channel id.
130 pcg 1.3
131     =cut
132    
133 pcg 1.5 sub send {
134 pcg 1.7 my ($self, $type, @arg) = @_;
135    
136 root 1.14 $self->{conn}->send ($type, @arg) if $self->{conn};
137     }
138    
139     sub inject_quit {
140     my ($self) = @_;
141    
142     $self->event_quit;
143     }
144    
145     sub event_quit {
146     my ($self) = @_;
147    
148     $self->unlisten;
149 pcg 1.7 }
150    
151     sub DESTROY {
152     my ($self) = @_;
153    
154     $self->unlisten;
155 pcg 1.1 }
156    
157 pcg 1.11 =back
158    
159     =cut
160    
161 pcg 1.1 1;
162