KGS::Listener - a generic base class to listen for kgs messages.


KGS documentation Contained in the KGS distribution.

Index


Code Index:

NAME

Top

KGS::Listener - a generic base class to listen for kgs messages.

SYNOPSIS

Top

  use base KGS::Listener;

  sub new {
     my $class = shift;
     my $self = $class->SUPER::new (@_);

     # for non-channel-related listeners:
     $self->listen ($self->{conn}, qw(ping req_pic));
     # for channel-type listener
     $self->listen ($self->{conn}, qw(join_room: part_room: msg_room:));

     $self;
  }

  sub inject_xxx {
     # handle msg xxx
  }

  # KGS::Listener::Room etc. als require this:
  sub event_xxx {
     # handle synthesized event xxx
  }

DESCRIPTION

Top

Please supply a description )

The KGS::Listener family has currently these members:

  KGS::Listener              base class for everything
  KGS::Listener::Channel     base class for channels (games, rooms)
  KGS::Listener::Game        base class that handles games
  KGS::Listener::Room        base class for rooms and their game lists
  KGS::Listener::Roomlist    base class for the overall room listing
  KGS::Listener::User        base class for user info, chats etc.
  KGS::Listener::Debug       prints all messages that marc doesn't understand

METHODS

new [channel => <id>]...

Create a new KGS::Listener project. The channel parameter is optional.

$listener->listen ($conn, [msgtype...])

Registers the object to receive callback messages of the named type(s). If $conn is undef, returns immediately. It's safe to call this function repeatedly.

A msgtype is either a packet name like login or msg_room, the string any, which will match any type, or a msgtype postdixed with : (e.g. msg_room:), in which case it will only match the $listener->{channel} channel.

The connection will be stored in $listener->{conn}.

In your own new method you should call $self->listen once with the connection and the msgtypes you want to listen for.

$listener->unlisten

Unregisters the object again.

$listener->inject ($msg)

The main injector callback.. all (listened for) messages end up in this method, which will just dispatch a method with name inject_<msgtype>.

You do not normally have to overwrite this method, but you should provide methods that are being called with names like inject_msg_room etc.

$listener->send ($type, %args);

Calls the send method of the connection when in listen state. It does not (yet) supply a default channel id.


KGS documentation Contained in the KGS distribution.
package KGS::Listener;

sub new {
   my $class = shift;
   bless { @_ }, $class;
}

sub listen {
   my ($self, $conn, @types) = @_;

   if ($conn) {
      $self->unlisten;
      $self->{conn} = $conn;
      $_ =~ s/:$/:$self->{channel}/ for @types;
      $self->{listen_types} = \@types;
      $self->{conn}->register ($self, "quit", @types);
   }
}

sub unlisten {
   my ($self) = @_;

   (delete $self->{conn})->unregister ($self, @{$self->{listen_types}})
      if $self->{conn};
}

sub inject {
   my ($self, $msg) = @_;

   if (my $cb = $self->can ("inject_$msg->{type}")) {
      $cb->($self, $msg);
   } elsif (my $cb = $self->can ("inject_any")) {
      $cb->($self, $msg);
   } else {
      warn "no handler found for message $msg->{type} in $self\n";
   }
}

sub send {
   my ($self, $type, @arg) = @_;

   $self->{conn}->send ($type, @arg) if $self->{conn};
}

sub inject_quit {
   my ($self) = @_;

   $self->event_quit;
}

sub event_quit {
   my ($self) = @_;

   $self->unlisten;
}

sub DESTROY {
   my ($self) = @_;

   $self->unlisten;
}

1;