The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POE::Component::IKC::Responder - POE IKC state handler

SYNOPSIS

    use POE;
    use POE::Component::IKC::Responder;
    create_ikc_responder();
    ...
    $kernel->post('IKC', 'post', $to_state, $state);

    $ikc->publish('my_name', [qw(state1 state2 state3)]);

DESCRIPTION

This module implements an POE IKC state handling. The responder handles posting states to foreign kernels and calling states in the local kernel at the request of foreign kernels.

There are 2 interfaces to the responder. Either by sending states to the 'IKC' session or the object interface. While the latter is faster, the better behaved, because POE is a cooperative system.

STATES/METHODS

spawn

    POE::Component::IKC::Responder->spawn();

This function creates the Responder session and object. Normally, IKC::Client or IKC::Server does this for you. But in some applications you want to make sure that the Responder is up and running before then.

post

Sends an state request to a foreign kernel. Returns logical true if the state was sent and logical false if it was unable to send the request to the foreign kernel. This does not mean that the foreign kernel was able to post the state, however. Parameters are as follows :

foreign_state

Specifier for the foreign state. See POE::Component::IKC::Specifier.

parameters

A reference to anything you want the foreign state to get as ARG0. If you want to specify several parameters, use an array ref and have the foreign state dereference it.

    $kernel->post('IKC', 'post', 
        {kernel=>'Syslog', session=>'logger', state=>'log'},
        [$faculty, $priority, $message];

or

    $ikc->post('poe://Syslog/logger/log', [$faculty, $priority, $message]);

This logs an state with a hypothetical logger.

See the "Note about SENDER" below.

call

This is identical to post, except it has a 3rd parameter that describes what state should receive the return value from the foreign kernel.

    $kernel->post('IKC', 'call', 
                'poe://Pulse/timeserver/time', '',
                'poe:get_time');

or

    $ikc->call({kernel=>'Pulse', session=>'timeserver', state=>'time'},
                '', 'poe://me/get_time');

This asks the foreign kernel 'Pulse' for the time. 'get_time' state in the current session is posted with whatever the foreign state returned.

You do not have to publish callback messages, because they are temporarily published. How temporary? They can be posted from a remote kernel ONCE only. This, of course, is a problem because someone else could get in a post before the callback. Such is life.

foreign_state

Identical to the post foreign_state parameter.

parameters

Identical to the post parameters parameter.

rsvp

Event identification for the callback. That is, this state is called with the return value of the foreign state. Can be a foreign_state specifier or simply the name of an state in the current session.

    $kernel->call('IKC', 'post', 
        {kernel=>'e-comm', session=>'CC', state=>'check'},
        {CC=>$cc, expiry=>$expiry}, folder=>$holder},
        'is_valid');
    # or
    $ikc->call('poe://e-comm/CC/check',
        {CC=>$cc, expiry=>$expiry}, folder=>$holder},
        'poe://me/is_valid');

This asks the e-comm server to check if a credit card number is "well formed". Yes, this would probably be massive overkill.

The rsvp state does not need to be published. IKC keeps track of the rsvp state and will allow the foreign kernel to post to it.

See the "Note about SENDER" below.

default

Sets the default foreign kernel. You must be connected to the foreign kernel first.

Unique parameter is the name of the foreign kernel kernel.

Returns logical true on success.

register

Registers foreign kernel names with the responder. This is done during the negociation phase of IKC and is normaly handled by IKC::Channel. Will define the default kernel if no previous default kernel exists.

First parameter is either a single kernel name. Second optional parameter is an array ref of kernel aliases to be registered.

unregister

Unregisters one or more foreign kernel names with the responder. This is done when the foreign kernel disconnects by IKC::Channel. If this is the default kernel, there is no more default kernel.

First parameter is either a single kernel name or a kernel alias. Second optional parameter is an array ref of kernel aliases to be unregistered. This second parameter is a tad silly, because if you unregister a remote kernel, it goes without saying that all it's aliases get unregistered also.

register_local

Registers new aliases for local kernel with the responder. This is done internally by IKC::Server and IKC::Client. Will NOT define the default kernel.

First and only parameter is an array ref of kernel aliases to be registered.

publish

Tell IKC that some states in the current session are available for use by foreign sessions.

session

A session alias by which the foreign kernels will call it. The alias must already have been registered with the local kernel.

states

Arrayref of states that foreign kernels may post.

    $kernel->post('IKC', 'publish', 'me', [qw(foo bar baz)]);
    # or
    $ikc->publish('me', [qw(foo bar baz)]);

retract

Tell IKC that some states should no longer be available for use by foreign sessions. You do not have to retract all published states.

session

Same as in publish

states

Same as in publish. If not supplied, *all* published states are retracted.

    $kernel->post('IKC', 'retract', 'me', [qw(foo mibble dot)]);
    # or
    $ikc->retract('me', [qw(foo)]);

published

    $list=$kernel->call(IKC=>'published', $session);

Returns a list of all the published states.

    $hash=$kernel->call(IKC=>'published');

Returns a hashref, keyed on session IDs. Values are arrayref of states published by that session. =over 2

session

A session alias that you wish the list of states for.

subscribe

Subscribe to foreign sessions or states. When you have subscribed to a foreign session, a proxy session is created on the local kernel that will allow you to post to it like any other local session.

specifiers

An arrayref of the session or state specifiers you wish to subscribe to. While the wildcard '*' kernel may be used, only the first kernel that acknowledges the subscription will be proxied.

callback

Either an state (for the state interface) or a coderef (for the object interface) that is posted (or called) when all subscription requests have either been replied to, or have timed out.

When called, it has a single parameter, an arrayref of all the specifiers that IKC was able to subscribe to. It is up to you to see if you have enough of the foreign sessions or states to get the job done, or if you should give up.

While callback isn't required, it makes a lot of sense to use it because it is only way to find out when the proxy sessions become available.

Example :

    $ikc->subscribe([qw(poe://Pulse/timeserver)], 
            sub { $kernel->post('poe://Pulse/timeserver', 'connect') });

(OK, that's a bad example because we don't check if we actually managed to subscribe or not.)

    $kernel->post('IKC', 'subscribe', 
                    [qw(poe://e-comm/CC poe://TouchNet/validation
                        poe://Cantax/JDE poe://Informatrix/JDE)
                    ],
                    'poe:subscribed',
                  );
    # and in state 'subscribed'
    sub subscribed
    {
        my($kernel, $specs)=@_[KERNEL, ARG0];
        if(@$specs != 4)
        {
            die "Unable to find all the foreign sessions needed";
        }
        $kernel->post('poe://Cantax/JDE', 'write', {...somevalues...});
    }                
    

This is a bit of a mess. You might want to use the subscribe parameter to create_ikc_client instead.

Subscription receipt timeout is currently set to 120 seconds.

unsubscribe

Reverse of the subscribe method. However, it is currently not documented well.

ping

Responds with 'PONG'. This is auto-published, so it can be called from remote kernels to see if the local kernel is still around. In fact, I don't see any other use for this.

    $kernel->post('poe://remote/IKC', 'ping', 'some_state');
    $kernel->delay('some_state', 60);   # timeout
    
    sub some_state
    {
        my($pong)=$_[ARG0];
        return if $pong;            # all is cool
        
        # YOW!  Remote kernel timed out.  RUN AROUND SCREAMING!
    }

shutdown

Hopefully causes IKC and all peripheral sessions to dissapear in a puff of smoke. At the very least, any sessions left will be either not related to IKC or barely breathing (that is, only have aliases keeping them from GC). This should allow you to sanely shut down your process.

monitor

Allows a session to monitor the state of remote kernels. Currently, a session is informed when a remote kernel is registered, unregistered, subscribed to or unsubscribed from. One should make sure that the IKC alias exists before trying to monitor. Do this by calling create_ikc_responder or in an on_connect callback.

    $kernel->post('IKC', 'monitor', $remote_kernel_id, $states);
$remote_kernel_id

Name or alias or IKC specifier of the remote kernel you wish to monitor. You can also specify * to monitor ALL remote kernels. If you do, your monitor will be called several times for a given kernel. This is because a kernel has one name and many aliases. For example, a remote kernel will have a unique ID within the local kernel, a name (passed to or generated by create_ikc_{kernel,client}) and a globaly unique ID assigned by the remote kernel via $kernel->ID. This suprises some people, but see the short note after the explanation of the callback parameters.

Note: An effort has been made to insure that when monitoring *, register is first called with the remote kernel's unique ID, and subsequent calls are aliases. This can't be guaranteed at this time, however.

$states

Hashref that specifies what callback states are called when something interesting happens. If $state is empty or undef, the session will no longer monitor the given remote kernel.

Callback states

The following states can be monitored:

register

Called when a remote kernel or alias is registered. This is equivalent to when the connection phase is finished.

unregister

Called when a remote kernel or alias is unregistered. This is equivalent to when the remote kernel disconnects.

subscribe

Called when IKC succeeds in subscribing to a remote session. ARG3 is an IKC::Specifier of what was subscribed to.

unsubscribe

Called when IKC succeeds in unsubscribing from a remote session.

shutdown

You are informed whenever someone tries to do a sane shutdown of IKC and all peripheral sessions. This will called only once, after somebody posts an IKC/shutdown event.

data

Little bit of data (can be scalar or reference) that is passed to the callback. This allows you to more magic.

The callback states are called the following parameters :

ARG0

Name of the kernel that was passed to poe://*/IKC/monitor

ARG1

ID or alias of remote kernel from IKC's point of view.

ARG2

A flag. If this is true, then ARG1 is the remote kernel unique ID, if false, then ARG1 is an alias. This is mostly useful when monitoring * and is in fact a bit bloatful.

ARG3

$state-{data}> ie any data you want.

ARG4 ... ARGN

Callback-specific parameters. See above.

Most of the time, ARG0 and ARG1 will be the same. Exceptions are if you are monitoring * or if you supplied a full IKC event specifier to IKC/monitor rather then just a plain kernel name.

Short note about monitoring all kernels with *

There are 2 reasons circonstances in which you will be monitoring all remote kernels : names known in advance and names unknown in advance.

If you know kernel names in advance, you might be better off monitoring a given kernel name. However, you might prefer doing a case-like compare on ARG1 (with regexes, say). This would be useful for clustering, where various redundant kernels could follow a naming convention like [application]-[host], so you could compare ARG1 with /^credit-/ to find out if you want to set up specific things for that kernel.

Not knowing the name of a kernel in advance, you could be doing some sort of autodiscovery or maybe just monitoring for debuging, logging or book-keeping purposes. You obviously don't want to do autodiscovery for every alias of every kernel, only for the "cannonical name", hence the need for ARG2.

Short note the second

You are more then allowed (in fact, you are encouraged) to use the same callback states when monitoring multiple kernels. In this case, you will find ARG0 useful for telling them apart.

    $kernel->post('IKC', 'monitor', '*', 
                    {register=>'remote_register',
                     unregister=>'remote_unregister',
                     subscribe=>'remote_subscribe',
                     unsubscribe=>'remote_unsubscribe',
                     data=>'magic box'});

Now remote_{register,unregister,subscribe,unsubscribe} is called for any remote kernel.

    $kernel->post('IKC', 'monitor', 'Pulse', {register=>'pulse_connected'});

pulse_connected will be called in current session when you succeed in connecting to a kernel called 'Pulse'.

    $kernel->post('IKC', 'monitor', '*');

Session is no longer monitoring all kernels, only 'Pulse'.

    $kernel->post('IKC', 'monitor', 'Pulse', {});

Now we aren't even interested in 'Pulse';

EXPORTED FUNCTIONS

create_ikc_responder

This function creates the Responder session and object. However, you don't need to call this directly, because IKC::Client or IKC::Server does this for you.

Deprecated, use spawn.

"Note about SENDER"

An attempt is made to provide a sane SENDER param to called or posted states. If the calling session has published some states, SENDER is usable during the called state, but not afterwards. Don't try keeping a reference to this session. This makes callbacks a tad easier.

Furthur, if you IKC/call a remote state, the SENDER during the callback will point back to the remote session.

BUGS

Sending session references and coderefs to a foreign kernel is a bad idea :) At some point it would be desirable to recurse through the paramerters and and turn any session references into state specifiers.

rsvp state in call is a bit problematic. IKC allows it to be posted to once, but doesn't check to see if the foreign kernel is the right one.

retract does not currently tell foreign kernels that have subscribed to a session/state that it has been retracted.

call()ing a state in a proxied foreign session doesn't work, for obvious reasons.

AUTHOR

Philip Gwyn, <perl-ikc at pied.nu>

SEE ALSO

POE, POE::Component::IKC::Server, POE::Component::IKC::Client

4 POD Errors

The following errors were encountered while parsing the POD:

Around line 1635:

'=item' outside of any '=over'

Around line 1709:

You forgot a '=back' before '=head2'

Around line 1775:

You forgot a '=back' before '=head2'

Around line 1873:

=back without =over