Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
=head2 loop
$loop = $notifier->loop
Returns the L<IO::Async::Loop> that this Notifier is a member of.
=cut
sub loop
{
my $self = shift;
return $self->{IO_Async_Notifier__loop}
}
*get_loop = \&loop;
# Only called by IO::Async::Loop, not external interface
sub __set_loop
{
my $self = shift;
my ( $loop ) = @_;
# early exit if no change
return if !$loop and !$self->loop or
$loop and $self->loop and $loop == $self->loop;
$self->_remove_from_loop( $self->loop ) if $self->loop;
$self->{IO_Async_Notifier__loop} = $loop;
weaken( $self->{IO_Async_Notifier__loop} ); # To avoid a cycle
$self->_add_to_loop( $self->loop ) if $self->loop;
}
=head2 notifier_name
$name = $notifier->notifier_name
Returns the name to identify this Notifier. If a has not been set, it will
return the empty string. Subclasses may wish to override this behaviour to
return some more useful information, perhaps from configured parameters.
=cut
sub notifier_name
{
my $self = shift;
return $self->{IO_Async_Notifier__notifier_name} || "";
}
=head2 adopt_future
$f = $notifier->adopt_future( $f )
Stores a reference to the L<Future> instance within the notifier itself, so
the reference doesn't get lost. This reference will be dropped when the future
becomes ready (either by success or failure). Additionally, if the future
failed the notifier's C<invoke_error> method will be informed.
This means that if the notifier does not provide an C<on_error> handler, nor
is there one anywhere in the parent chain, this will be fatal to the caller of
C<< $f->fail >>. To avoid this being fatal if the failure is handled
elsewhere, use the C<else_done> method on the future to obtain a sequence one
that never fails.
$notifier->adopt_future( $f->else_done() )
The future itself is returned.
=cut
sub adopt_future
{
my $self = shift;
my ( $f ) = @_;
my $fkey = "$f"; # stable stringification
$self->{IO_Async_Notifier__futures}{$fkey} = $f;
$f->on_ready( $self->_capture_weakself( sub {
my $self = shift;
my ( $f ) = @_;
delete $self->{IO_Async_Notifier__futures}{$fkey};
$self->invoke_error( $f->failure ) if $f->is_failed;
}));
return $f;
}
=head1 CHILD NOTIFIERS
During the execution of a program, it may be the case that certain IO handles
cause other handles to be created; for example, new sockets that have been
C<accept()>ed from a listening socket. To facilitate these, a notifier may
contain child notifier objects, that are automatically added to or removed
from the L<IO::Async::Loop> that manages their parent.
=cut
=head2 parent
$parent = $notifier->parent
Returns the parent of the notifier, or C<undef> if does not have one.
=cut
sub parent
{
my $self = shift;
return $self->{IO_Async_Notifier__parent};
}
=head2 children
@children = $notifier->children
Returns a list of the child notifiers contained within this one.
=cut
sub children
{
my $self = shift;
return unless $self->{IO_Async_Notifier__children};
return @{ $self->{IO_Async_Notifier__children} };
}
=head2 add_child
$notifier->add_child( $child )
Adds a child notifier. This notifier will be added to the containing loop, if
the parent has one. Only a notifier that does not currently have a parent and
is not currently a member of any loop may be added as a child. If the child
itself has grandchildren, these will be recursively added to the containing
loop.
=cut
sub add_child
{
my $self = shift;
my ( $child ) = @_;
croak "Cannot add a child that already has a parent" if defined $child->{IO_Async_Notifier__parent};
croak "Cannot add a child that is already a member of a loop" if defined $child->loop;
if( defined( my $loop = $self->loop ) ) {
$loop->add( $child );
}
push @{ $self->{IO_Async_Notifier__children} }, $child;
$child->{IO_Async_Notifier__parent} = $self;
weaken( $child->{IO_Async_Notifier__parent} );
return;
}
=head2 remove_child
$notifier->remove_child( $child )
Removes a child notifier. The child will be removed from the containing loop,
if the parent has one. If the child itself has grandchildren, these will be
recurively removed from the loop.
=cut
sub remove_child
{
my $self = shift;
my ( $child ) = @_;
LOOP: {
my $childrenref = $self->{IO_Async_Notifier__children};
for my $i ( 0 .. $#$childrenref ) {
next unless $childrenref->[$i] == $child;
splice @$childrenref, $i, 1, ();
last LOOP;
}
croak "Cannot remove child from a parent that doesn't contain it";
}
undef $child->{IO_Async_Notifier__parent};
if( defined( my $loop = $self->loop ) ) {
$loop->remove( $child );
}
}
=head2 remove_from_parent
$notifier->remove_from_parent
Removes this notifier object from its parent (either another notifier object
or the containing loop) if it has one. If the notifier is not a child of
another notifier nor a member of a loop, this method does nothing.
=cut
sub remove_from_parent
{
my $self = shift;
if( my $parent = $self->parent ) {
$parent->remove_child( $self );
}
elsif( my $loop = $self->loop ) {
$loop->remove( $self );
}
}
=head1 SUBCLASS METHODS
C<IO::Async::Notifier> is a base class provided so that specific subclasses of
it provide more specific behaviour. The base class provides a number of
methods that subclasses may wish to override.
If a subclass implements any of these, be sure to invoke the superclass method
at some point within the code.
=cut
=head2 _init
$notifier->_init( $paramsref )
This method is called by the constructor just before calling C<configure>.
It is passed a reference to the HASH storing the constructor arguments.
This method may initialise internal details of the Notifier as required,
possibly by using parameters from the HASH. If any parameters are
construction-only they should be C<delete>d from the hash.
=cut
sub _init
{
# empty default
}
=head2 configure
$notifier->configure( %params )
This method is called by the constructor to set the initial values of named
parameters, and by users of the object to adjust the values once constructed.
This method should C<delete> from the C<%params> hash any keys it has dealt
with, then pass the remaining ones to the C<SUPER::configure>. The base
class implementation will throw an exception if there are any unrecognised
keys remaining.
=cut
=head2 configure_unknown
$notifier->configure_unknown( %params )
This method is called by the base class C<configure> method, for any remaining
parameters that are not recognised. The default implementation throws an
exception using C<Carp> that lists the unrecognised keys. This method is
provided to allow subclasses to override the behaviour, perhaps to store
unrecognised keys, or to otherwise inspect the left-over arguments for some
other purpose.
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
=head2 invoke_event
@ret = $notifier->invoke_event( $event_name, @args )
Invokes the given event handler, passing in the given arguments. Event
handlers may either be subclass methods, or parameters given to the C<new> or
C<configure> method. Returns whatever the underlying method or CODE reference
returned.
=cut
sub invoke_event
{
my $self = shift;
my ( $event_name, @args ) = @_;
my $code = $self->can_event( $event_name )
or croak "$self cannot handle $event_name event";
$self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG;
return $code->( $self, @args );
}
=head2 maybe_invoke_event
$retref = $notifier->maybe_invoke_event( $event_name, @args )
Similar to C<invoke_event> but will return C<undef> if the object cannot
handle the name event, rather than throwing an exception. In order to
distinguish this from an event-handling function that simply returned
C<undef>, if the object does handle the event, the list that it returns will
be returned in an ARRAY reference.
=cut
sub maybe_invoke_event
{
my $self = shift;
my ( $event_name, @args ) = @_;
my $code = $self->can_event( $event_name )
or return undef;
$self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG;
return [ $code->( $self, @args ) ];
}
=head1 DEBUGGING SUPPORT
=cut
=head2 debug_printf
$notifier->debug_printf( $format, @args )
Conditionally print a debugging message to C<STDERR> if debugging is enabled.
If such a message is printed, it will be printed using C<printf> using the
given format and arguments. The message will be prefixed with an string, in
square brackets, to help identify the C<$notifier> instance. This string will
be the class name of the notifier, and any parent notifiers it is contained
by, joined by an arrow C<< <- >>. To ensure this string does not grow too
long, certain prefixes are abbreviated:
IO::Async::Protocol:: => IaP:
IO::Async:: => Ia:
Net::Async:: => Na:
Finally, each notifier that has a name defined using the C<notifier_name>
parameter has that name appended in braces.
For example, invoking
$stream->debug_printf( "EVENT on_read" )
On an L<IO::Async::Stream> instance reading and writing a file descriptor
whose C<fileno> is 4, which is a child of an L<IO::Async::Protocol::Stream>,
will produce a line of output:
[Ia:Stream{rw=4}<-IaP:Stream] EVENT on_read
=cut
sub debug_printf
{
$IO::Async::Debug::DEBUG or return;
my $self = shift;
my ( $format, @args ) = @_;
my @id;
while( $self ) {
push @id, ref $self;
my $name = $self->notifier_name;
$id[-1] .= "{$name}" if defined $name and length $name;
$self = $self->parent;
}
s/^IO::Async::Protocol::/IaP:/,
s/^IO::Async::/Ia:/,
s/^Net::Async::/Na:/ for @id;
IO::Async::Debug::logf "[%s] $format\n", join("<-", @id), @args;
}
sub _debug_printf_event
{
my $self = shift;
my ( $caller, $event_name ) = @_;
my $class = ref $self;
if( $IO::Async::Debug::DEBUG > 1 or $class eq $caller ) {
s/^IO::Async::Protocol::/IaP:/,
s/^IO::Async::/Ia:/,
s/^Net::Async::/Na:/ for my $str_caller = $caller;
$self->debug_printf( "EVENT %s",
( $class eq $caller ? $event_name : "${str_caller}::$event_name" )
);
}
}
=head2 invoke_error
$notifier->invoke_error( $message, $name, @details )
Invokes the stored C<on_error> event handler, passing in the given arguments.
If no handler is defined, it will be passed up to the containing parent
notifier, if one exists. If no parent exists, the error message will be thrown
as an exception by using C<die()> and this method will not return.
If a handler is found to handle this error, the method will return as normal.
However, as the expected use-case is to handle "fatal" errors that now render
the notifier unsuitable to continue, code should be careful not to perform any
further work after invoking it. Specifically, sockets may become disconnected,
or the entire notifier may now be removed from its containing loop.
The C<$name> and C<@details> list should follow similar semantics to L<Future>
failures. That is, the C<$name> should be a string giving a category of
failure, and the C<@details> list should contain additional arguments that
relate to that kind of failure.
=cut
sub invoke_error
{
my $self = shift;
my ( $message, $name, @details ) = @_;
if( my $code = $self->{IO_Async_Notifier__on_error} || $self->can( "on_error" ) ) {
return $code->( $self, $message, $name, @details );
}
if( my $parent = $self->parent ) {
return $parent->invoke_error( @_ );
}
die "$message\n";
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
( run in 0.519 second using v1.01-cache-2.11-cpan-39bf76dae61 )