SYNOPSIS

 package POE::Component::IRC;

 use strict;
 use warnings;
 use POE;
 use base 'POE::Component::Syndicator';

 # our constructor
 sub spawn {
     my ($package, %args) = @_;

     # process arguments...

     my $self = bless \%args, $package;

     # set up our plugin system and POE session
     $self->_syndicator_init(
         prefix        => 'irc_',
         reg_prefix    => 'PCI_',
         types         => [SERVER => 'S', USER => 'U'],
         object_states => [qw(
             syndicator_started
             shutdown
         )],
     );

     return $self;
 }

 sub syndicator_started {
     my ($kernel, $self) = @_[KERNEL, OBJECT];

     # connect to a server, etc...
 }

 # plugin handler for SERVER event 'hlagh'
 sub S_hlagh {
     # ...
 }

 sub shutdown {
     my ($kernel, $self) = @_[KERNEL, OBJECT];

     # disconnect from a server, etc...

     # shut down the syndicator
     $self->_syndicator_destroy();
 }

DESCRIPTION

POE::Component::Syndicator is a base class for \s-1POE\s0 components which need to handle a persistent resource (e.g. a connection to an \s-1IRC\s0 server) for one or more sessions in an extendable way.

This module (as well as Object::Pluggable, which this module inherits from) was born out of POE::Component::IRC, the guts of which quickly spread to other \s-1POE\s0 components. Now they can all inherit from this module instead.

The component provides an event queue, which can be managed with the methods documented below. It handles delivery of events to the object itself, all interested plugins, and all interested sessions.

Component lifetime

You start by calling \*(C`_syndicator_init\*(C', which will create a \s-1POE\s0 session with your object as its heap, and a few event handlers installed. The events described in \*(L"Local events\*(R" delimit the start and end of the session's lifetime. In between those, interested plugins and sessions will receive various events, usually starting with \*(C`syndicator_registered\*(C'. In this phase, your subclass and plugins can call the methods and send the events documented below. When the component has been shut down, sessions (but not plugins) will receive a \*(C`syndicator_shutdown\*(C' event. After this, the component will become unusable.

A note on events

In this document, an event (unless explicitly referred to as a \s-1POE\s0 event) is defined as a message originating from POE::Component::Syndicator, delivered to plugins (and the subclass) via plugin methods and to registered sessions as \s-1POE\s0 events.

Interested sessions are considered consumers only, so they always receive copies of event arguments, whereas interested plugins and subclasses receive scalar references to them. This allows them to alter, add, or remove event arguments before sessions (or even other plugins) receive them. For more information about plugins, see Object::Pluggable's documentation. A subclass does not have to register for plugin events.

Two event types are supported: \s-1SERVER\s0 and \s-1USER\s0, though their names can be overridden (see \*(C`_syndicator_init\*(C').

\s-1SERVER\s0 events

These represent data received from the network or some other outside resource (usually a server, hence the default name).

\s-1SERVER\s0 events are generated by the \*(C`send_event*\*(C' methods. These events are delivered to the subclass and plugins (method \*(C`S_foo\*(C') and interested sessions (event \*(C`syndicator_foo\*(C').

\s-1USER\s0 events

These represent commands about to be sent to a server or some other resource.

\s-1USER\s0 events are generated by \*(C`send_user_event\*(C'. In addition, all \s-1POE\s0 events sent to this component's session (e.g. with \*(C`yield\*(C') which do not have a handler will generate corresponding \s-1USER\s0 events. \s-1USER\s0 events are considered more private, so they are only delivered to the subclass and plugins, not to sessions.

PRIVATE METHODS

The following methods should only be called by a subclass. You should call this in your constructor. It initializes Object::Pluggable, creates the Syndicator's \s-1POE\s0 session, and calls the \*(C`syndicator_started\*(C' \s-1POE\s0 events. It takes the following arguments:

'prefix', a prefix for all your event names, when sent to interested sessions. If you don't supply this, Object::Pluggable's default ('pluggable') will be used.

'reg_prefix', the prefix for the \*(C`register()\*(C'/\*(C`unregister()\*(C' plugin methods If you don't supply this, Object::Pluggable's default ('plugin_') will be used.

'debug', a boolean, if true, will cause a warning to be printed every time a plugin event handler raises an exception.

'types', a 2-element arrayref of the types of events that your component will support, or a 4-element (2 pairs) arrayref where the event types are keys and their abbrevations (used as plugin event method prefixes) are values (see \*(L"A note on events\*(R" and Object::Pluggable for more information). The two event types are fundamentally different, so make sure you supply them in the right order. If you don't provide this argument, \*(C`[ SERVER => 'S', USER => 'U' ]\*(C' will be used.

'register_signal', the name of the register signal (see \*(L"\s-1SIGNALS\s0\*(R"). Defaults to '\s-1SYNDICATOR_REGISTER\s0'.

'shutdown_signal', the name of the shutdown signal (see \*(L"\s-1SIGNALS\s0\*(R"). Defaults to '\s-1SYNDICATOR_SHUTDOWN\s0'.

'object_states' an arrayref of additional object states to add to the \s-1POE\s0 session. Same as the 'object_states' argument to POE::Session's \*(C`create\*(C' method. You'll want to add a handler for at least the \*(C`syndicator_started\*(C' event.

'options', a hash of options for POE::Session's constructor.

If you call \*(C`_syndicator_init\*(C' from inside another \s-1POE\s0 session, the component will automatically register that session as wanting all events. That session will first receive a \*(C`syndicator_registered\*(C' event. Call this method when you want Syndicator to clean up (delete all plugins, etc) and make sure it won't keep the \s-1POE\s0 session alive after all remaining events have been processed. A \*(C`syndicator_shutdown\*(C' event (or similar, depending on the prefix you chose) will be generated. Any argument passed to \*(C`_syndicator_destroy\*(C' will be passed along with that event.

Note: this method will clear all alarms for the \s-1POE\s0 session.

PUBLIC METHODS

Returns the component's \s-1POE\s0 session id. Returns the component's \s-1POE\s0 session alias. This method provides an alternative, object-based means of posting events to the component. First argument is the event to post, following arguments are sent as arguments to the resultant post. This method provides an alternative, object-based means of calling events to the component. First argument is the event to call, following arguments are sent as arguments to the resultant call. Adds a new \s-1SERVER\s0 event onto the end of the queue. The event will be processed after other pending events, if any. First argument is an event name, the rest are the event arguments.

$component->send_event('irc_public, '[email protected]', ['#mychan'], 'message'); Adds a new \s-1SERVER\s0 event to the start of the queue. The event will be the next one to be processed. First argument is an event name, the rest are the event arguments. Sends a new \s-1SERVER\s0 event immediately. Execution of the current \s-1POE\s0 event will be suspended (i.e. this call will block) until the new event has been processed by the component class and all plugins. First argument is an event name, the rest are the event arguments. Sends a new \s-1USER\s0 event immediately. You should call this before every command you send to your remote server/resource. Only the subclass and plugins will see this event. Takes two arguments, an event name and an arrayref of arguments. Returns one of the \*(C`EAT\*(C' constants listed in Object::Pluggable::Constants. After this method returns, the arrayref's contents may have been modified by the subclass or plugins.

$component->send_user_event('PRIVMSG', '#mychan', 'message'); This method provides a way of posting delayed events to the component. The first argument is an arrayref consisting of the delayed command to post and any command arguments. The second argument is the time in seconds that one wishes to delay the command being posted.

my $alarm_id = $component->delay(['mode', $channel, '+o', $dude], 60); This method removes a previously scheduled delayed event from the component. Takes one argument, the \*(C`alarm_id\*(C' that was returned by a \*(C`delay\*(C' method call. Returns an arrayref of arguments to the event that was originally requested to be delayed.

my $arrayref = $component->delay_remove($alarm_id);

EVENTS

Local events

The component will send the following \s-1POE\s0 events to its session.

\*(C`syndicator_started\*(C'

Called after the session has been started (like \*(C`_start\*(C' in POE::Kernel. This is where you should do your POE-related setup work such as adding new event handlers to the session.

\*(C`syndicator_stopped\*(C'

Called right before the session is about to die (like \*(C`_stop\*(C' in POE::Kernel).

Input events

Other \s-1POE\s0 sessions can send the following \s-1POE\s0 events to the Syndicator's session.

\*(C`register\*(C'

Takes any amount of arguments: a list of event names that your session wants to listen for, minus the prefix (specified in \*(L"_syndicator_init\*(R" in \*(C`syndicator_init\*(C').

$kernel->post('my syndicator', 'register', qw(join part quit kick));

Registering for the special event 'all' will cause it to send all events to your session. Calling it with no event names is equivalent to calling it with 'all' as an argumente.

Registering will generate a \*(C`syndicator_registered\*(C' event that your session can trap.

Registering with multiple component sessions can be tricky, especially if one wants to marry up sessions/objects, etc. Check the \s-1SIGNALS\s0 section for an alternative method of registering with multiple components.

\*(C`unregister\*(C'

Takes any amount of arguments: a list of event names which you don't want to receive. If you've previously done a \*(C`register\*(C' for a particular event which you no longer care about, this event will tell the component to stop sending them to you. (If you haven't, it just ignores you. No big deal.) Calling it with no event names is equivalent to calling it with 'all' as an argument.

If you have registered for the special event 'all', attempting to unregister individual events will not work. This is a 'feature'.

\*(C`shutdown\*(C'

By default, POE::Component::Syndicator sessions never go away. You can send its session a \*(C`shutdown\*(C' event manually to make it delete itself. Terminating multiple Syndicators can be tricky. Check the \*(L"\s-1SIGNALS\s0\*(R" section for a method of doing that.

\*(C`_default\*(C'

Any \s-1POE\s0 events sent to the Syndicator's session which do not have a handler will go to the Syndicator's \*(C`_default\*(C' handler, will generate \*(L"\s-1USER\s0 events\*(R" of the same name. If you install your own \*(C`_default\*(C' handler, make sure you do the same thing before you handle an event:

use Object::Pluggable::Constants 'PLUGIN_EAT_ALL';

$poe_kernel->state('_default', $self, '_\|_default');

sub _\|_default { my ($self, $event, $args) = @_[OBJECT, ARG0, ARG1];

# do nothing if a plugin eats the event return if $self->send_user_event($event, [@$args]) == PLUGIN_EAT_ALL;

# handle the event # ... }

Note that the handler for the \*(C`_default\*(C' event must be named something other than '_default', because that name is reserved for the plugin-type default handler (see the Object::Pluggable docs).

Output events

The Syndicator will send the following events at various times. The 'syndicator_' prefix in these event names can be customized with a 'prefix' argument to \*(L"_syndicator_init\*(R" in \*(C`_syndicator_init\*(C'.

\*(C`syndicator_registered\*(C'

Sent once to the requesting session on registration (see \*(C`register\*(C'). \*(C`ARG0\*(C' is a reference to the component's object.

\*(C`syndicator_shutdown\*(C'

Sent to all interested sessions when the component has been shut down. See \*(C`_syndicator_destroy\*(C'.

\*(C`syndicator_delay_set\*(C'

Sent to the subclass, plugins, and all interested sessions on a successful addition of a delayed event using the \*(C`delay\*(C' method. \*(C`ARG0\*(C' will be the alarm_id which can be used later with \*(C`delay_remove\*(C'. Subsequent parameters are the arguments that were passed to \*(C`delay\*(C'.

\*(C`syndicator_delay_removed\*(C'

Sent to the subclass, plugins, and all interested sessions when a delayed event is successfully removed. \*(C`ARG0\*(C' will be the alarm_id that was removed. Subsequent parameters are the arguments that were passed to \*(C`delay\*(C'.

All other events

All other events sent by the Syndicator are \s-1USER\s0 events (generated with \*(C`send_user_event\*(C') and \s-1SERVER\s0 events (generated with \*(C`send_event*\*(C') which will be delivered normally. Your subclass and plugins are responsible for generating them.

SIGNALS

The component will handle a number of custom signals that you may send using POE::Kernel's \*(C`signal\*(C' method. They allow any session to communicate with every instance of the component in certain ways without having references to their objects or knowing about their sessions. The names of these signals can be customized with \*(C`_syndicator_init\*(C'. Registers for an event with the component. See \*(C`register\*(C'. Causes a 'shutdown' event to be sent to your session. Any arguments to the signal will be passed along to the event. That's where you should clean up and call \*(C`_syndicator_destroy\*(C'.

AUTHOR

Hinrik O\*:rn Sigur\*(d-sson, [email protected], Chris \*(C`BinGOs\*(C' Williams [email protected], Apocalypse [email protected], and probably others.

LICENSE AND COPYRIGHT

Copyright 2011 Hinrik O\*:rn Sigur\*(d-sson

This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.