Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
=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.
=cut
=head2 _add_to_loop
$notifier->_add_to_loop( $loop )
This method is called when the Notifier has been added to a Loop; either
directly, or indirectly through being a child of a Notifer already in a loop.
This method may be used to perform any initial startup activity required for
the Notifier to be fully functional but which requires a Loop to do so.
=cut
sub _add_to_loop
{
# empty default
}
=head2 _remove_from_loop
$notifier->_remove_from_loop( $loop )
This method is called when the Notifier has been removed from a Loop; either
directly, or indirectly through being a child of a Notifier removed from the
loop.
This method may be used to undo the effects of any setup that the
C<_add_to_loop> method had originally done.
=cut
sub _remove_from_loop
{
# empty default
}
=head1 UTILITY METHODS
=cut
=head2 _capture_weakself
$mref = $notifier->_capture_weakself( $code )
Returns a new CODE ref which, when invoked, will invoke the originally-passed
ref, with additionally a reference to the Notifier as its first argument. The
Notifier reference is stored weakly in C<$mref>, so this CODE ref may be
stored in the Notifier itself without creating a cycle.
For example,
my $mref = $notifier->_capture_weakself( sub {
my ( $notifier, $arg ) = @_;
print "Notifier $notifier got argument $arg\n";
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
}
=head2 can_event
$code = $notifier->can_event( $event_name )
Returns a C<CODE> reference if the object can perform the given event name,
either by a configured C<CODE> reference parameter, or by implementing a
method. If the object is unable to handle this event, C<undef> is returned.
=cut
sub can_event
{
my $self = shift;
my ( $event_name ) = @_;
return $self->{$event_name} || $self->can( $event_name );
}
=head2 make_event_cb
$callback = $notifier->make_event_cb( $event_name )
Returns a C<CODE> reference which, when invoked, will execute the given event
handler. Event handlers may either be subclass methods, or parameters given to
the C<new> or C<configure> method.
The event handler can be passed extra arguments by giving them to the C<CODE>
reference; the first parameter received will be a reference to the notifier
itself. This is stored weakly in the closure, so it is safe to store the
resulting C<CODE> reference in the object itself without causing a reference
cycle.
=cut
sub make_event_cb
{
my $self = shift;
my ( $event_name ) = @_;
my $code = $self->can_event( $event_name )
or croak "$self cannot handle $event_name event";
my $caller = caller;
return $self->_capture_weakself(
!$IO::Async::Debug::DEBUG ? $code : sub {
my $self = $_[0];
$self->_debug_printf_event( $caller, $event_name );
goto &$code;
}
);
}
=head2 maybe_make_event_cb
$callback = $notifier->maybe_make_event_cb( $event_name )
Similar to C<make_event_cb> but will return C<undef> if the object cannot
handle the named event, rather than throwing an exception.
=cut
sub maybe_make_event_cb
{
my $self = shift;
my ( $event_name ) = @_;
my $code = $self->can_event( $event_name )
or return undef;
my $caller = caller;
return $self->_capture_weakself(
!$IO::Async::Debug::DEBUG ? $code : sub {
my $self = $_[0];
$self->_debug_printf_event( $caller, $event_name );
goto &$code;
}
);
}
=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.820 second using v1.01-cache-2.11-cpan-39bf76dae61 )