NAME
POE::Kernel - manage events, selects and signals for POE::Session instances
SYNOPSIS
use POE::Kernel;
use POE::Session;
$kernel = new POE::Kernel;
new POE::Session(...); # one or more starting sessions
$kernel->run(); # run sessions; serve events, selects, signals
exit;
DESCRIPTION
POE::Kernel in a nutshell.
It queues and delivers events to instances of
POE::Session. Alarms are implemented as delayed events.It offers select(2) services for files based on IO::Handle. They are implemented as immediate events, allowing sessions to bypass the queue entirely.
It catches signals and passes them as events to
POE::Sessioninstances.It allows sessions to modify their event handlers. Extensions add and remove features by altering code in the caller.
PUBLIC METHODS
- new POE::Kernel;
-
Creates a self-contained
POE::Kernelobject, and returns a reference to it. (Untested: It should be possible to run one Kernel per thread.) - $kernel->run()
-
Starts the kernel, and will not return until all its
POE::Sessioninstances have completed. - $kernel->select($handle, $state_r, $state_w, $state_e)
-
Manages read, write and exception bits for a
IO::Handleobject owned by the currently activePOE::Session. Defined states are added, and undefined ones are removed. When select(2) unblocks, the named event handlers (states) are invoked with$handleto take care of file activity. - $kernel->select_read($handle, $state)
-
Manages just the "read" select(2) vector for a
$handleowned by the currently activePOE::Session. Works like 1/3 of$kernel-select()>. - $kernel->select_write($handle, $state)
-
Manages just the "write" select(2) vector for a
$handleowned by the currently activePOE::Session. Works like 1/3 of$kernel-select()>. - $kernel->select_exception($handle, $state)
-
Manages just the "exception" select(2) vector for a
$handle. owned by the currently activePOE::Session. Works like 1/3 of$kernel-select()>. - $kernel->sig($signal, $state)
-
Add or remove an event handler for the signal named in
$signal}(same names as with%SIG). If$stateis defined, then that state will be invoked when a specified signal. If$stateis undefined, then the$SIG{$signal}handler is removed. - $kernel->post($destination_session, $state_name, @etc)
-
Enqueues an event (
$state) for the$destination_session. Additional parameters (@etc) can be passed along. - $kernel->state($state_name, $state_code)
-
Registers a CODE reference (
$state_code) for the event$state_namein the currently activePOE::Session. If$state_codeis undefined, then the named state will be removed. - $kernel->alarm($state_name, $time, @etc)
-
Posts a state for a specific future time, with possible extra parameters. The time is represented as system time, just like
time()returns. If$timeis zero, then the alarm is cleared. Otherwise$timeis clipped to no earlier than the currenttime().The current session will receive its
$state_nameevent whentime()catches up with$time.Any given
$state_namemay only have one alarm pending. Setting a subsequent alarm for an existing state will clear all pending events for that state, even if the existing states were not enqueued by previous calls toalarm().
PROTECTED METHODS
Not for general use.
- $kernel->session_alloc($session)
-
Enqueues a
_startevent for a session. The session is added to the kernel just before the event is dispatched. - $kernel->session_free($session)
-
Enqueues a
_stopevent for a session. The kernel will deallocate and destroy the session and all its related resources after_stophas been dispatched to the session.
PRIVATE METHODS
Not for general use.
- DESTROY
-
Destroy the Kernel, and all associated resources. Nothing implemented yet.
- _signal_handler
-
Registered as a handle for allmost all the signals in %SIG. It enqueues
_signalevents for every activePOE::Kernel. The kernels relay_signalevents to everyPOE::Sessionregistered for them. - $kernel->_check_session_resources($session)
-
Called after an event has been dispatched. This function stops sessions that have run out of things to do.
- $kernel->_dispatch_state($session, $source_session, $state, \@etc)
-
Immediately dispatches an event (state transition) from a source session to a destination session.
\@etcis an optional array reference that holds additional information that the session expects. - $kernel->_dispatch_selects($select_handles, $selects_index)
-
This helper checks
IO::Selectobjects for activity. It uses_dispatch_stateto notifyPOE::Sessioninstances immediately. - $kernel->_enqueue_state($session, $source_session, $state, $time, \@etc)
-
Combines the parameters into an event (state transition), and enqueues it to be delivered at a particular time. Alarms are implemented as events that are scheduled to happen at a future time.
$timeis clipped totime(). - $kernel->_internal_select($session, $handle, $state, $select, $select_index)
-
The guts of select(2) management. Registers or removes a select bit for
IO::Handle. When select unblocks, an event ($state) will be immediately dispatched to$session, along with the$handleso it can be taken care of. - $kernel->_maybe_add_handle($handle)
-
Register a handle resource (
$handle) with this kernel, if one is not already there. - $kernel->_maybe_remove_handle($handle)
-
Remove a handle resource (
$handle) from this kernel, if one exists. - $kernel->_kernel_select($session, $handle, $state_r, $state_w, $state_e)
-
Register or remove read, write and exception states for a handle all at once.
EXAMPLES
Please see the tests directory that comes with the POE bundle.
BUGS
DESTROY is not implemented. This has not been a problem so far.
CONTACT AND COPYRIGHT
Copyright 1998 Rocco Caputo <troc@netrus.net>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.