1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2006-2010 -- leonerd@leonerd.org.uk
package IO::Async::Notifier;
use strict;
use warnings;
our $VERSION = '0.29';
use Carp;
use Scalar::Util qw( weaken );
=head1 NAME
C<IO::Async::Notifier> - base class for C<IO::Async> event objects
=head1 SYNOPSIS
Usually not directly used by a program, but one valid use case may be:
use IO::Async::Notifier;
use IO::Async::Stream;
use IO::Async::Signal;
use IO::Async::Loop;
my $loop = IO::Async::Loop->new();
my $notifier = IO::Async::Notifier->new();
$notifier->add_child(
IO::Async::Stream->new(
read_handle => \*STDIN,
on_read => sub {
my $self = shift;
my ( $buffref, $closed ) = @_;
$$buffref =~ s/^(.*)\n// or return 0;
print "You said $1\n";
return 1;
},
)
);
$notifier->add_child(
IO::Async::Signal->new(
name => 'INT',
on_receipt => sub {
print "Goodbye!\n";
$loop->loop_stop;
},
)
);
$loop->add( $notifier );
$loop->loop_forever;
=head1 DESCRIPTION
This object class forms the basis for all the other event objects that an
C<IO::Async> program uses. It provides the lowest level of integration with a
C<IO::Async::Loop> container, and a facility to collect Notifiers together, in
a tree structure, where any Notifier can contain a collection of children.
Normally, objects in this class would not be directly used by an end program,
as it performs no actual IO work, and generates no actual events. These are all
left to the various subclasses, such as:
=over 4
=item *
L<IO::Async::Handle> - event callbacks for a non-blocking file descriptor
=item *
L<IO::Async::Stream> - read and write buffers around an IO handle
=item *
L<IO::Async::Sequencer> - handle a serial pipeline of requests / responses (EXPERIMENTAL)
=item *
L<IO::Async::Timer> - base class for Notifiers that use timed delays
=item *
L<IO::Async::Signal> - event callback on receipt of a POSIX signal
=back
For more detail, see the SYNOPSIS section in one of the above.
One case where this object class would be used, is when a library wishes to
provide a sub-component which consists of multiple other C<Notifier>
subclasses, such as C<Handle>s and C<Timers>, but no particular object is
suitable to be the root of a tree. In this case, a plain C<Notifier> object
can be used as the tree root, and all the other notifiers added as children of
it.
=cut
=head1 PARAMETERS
A specific subclass of C<IO::Async::Notifier> defines named parameters that
control its behaviour. These may be passed to the C<new> constructor, or to
the C<configure> method. The documentation on each specific subclass will give
details on the parameters that exist, and their uses. Some parameters may only
support being set once at construction time, or only support being changed if
the object is in a particular state.
=cut
=head1 CONSTRUCTOR
=cut
=head2 $notifier = IO::Async::Notifier->new( %params )
This function returns a new instance of a C<IO::Async::Notifier> object with
the given initial values of the named parameters.
Up until C<IO::Async> version 0.19, this module used to implement the IO
handle features now found in the C<IO::Async::Handle> subclass. To allow for a
smooth upgrade of existing code, this constructor check for any C<%params> key
which looks like it belongs there instead. These keys are C<handle>,
C<read_handle>, C<write_handle>, C<on_read_ready> and C<on_write_ready>. If
any of these keys are present, then a C<IO::Async::Handle> is returned.
Do not rely on this feature in new code. This logic exists purely to provide
an upgrade path from older code that still expects C<IO::Async::Notifier> to
provide filehandle operations. This produces a deprecation warning. At some
point in the future this functionallity may be removed.
=cut
sub new
{
my $class = shift;
my %params = @_;
if( $class eq __PACKAGE__ and
grep { exists $params{$_} } qw( handle read_handle write_handle on_read_ready on_write_ready ) ) {
carp "IO::Async::Notifier no longer wraps a filehandle; see instead IO::Async::Handle";
require IO::Async::Handle;
return IO::Async::Handle->new( %params );
}
my $self = bless {
children => [],
parent => undef,
}, $class;
$self->_init( \%params );
$self->configure( %params );
return $self;
}
=head2 $notifier->configure( %params )
Adjust the named parameters of the C<Notifier> as given by the C<%params>
hash.
=cut
# for subclasses to override and call down to
sub configure
{
my $self = shift;
my %params = @_;
# We don't recognise any configure keys at this level
if( keys %params ) {
my $class = ref $self;
croak "Unrecognised configuration keys for $class - " . join( " ", keys %params );
}
}
=head2 $notifier->get_loop
Returns the C<IO::Async::Loop> that this Notifier is a member of.
=cut
sub get_loop
{
my $self = shift;
return $self->{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->{loop} = $loop;
weaken( $self->{loop} ); # To avoid a cycle
$self->_add_to_loop( $self->{loop} ) if $self->{loop};
}
=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 C<IO::Async::Loop> that manages their parent.
=cut
=head2 $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->{parent};
}
=head2 @children = $notifier->children()
Returns a list of the child notifiers contained within this one.
=cut
sub children
{
my $self = shift;
return @{ $self->{children} };
}
=head2 $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->{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->{children} }, $child;
$child->{parent} = $self;
weaken( $child->{parent} );
return;
}
=head2 $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->{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->{parent};
if( defined( my $loop = $self->{loop} ) ) {
$loop->remove( $child );
}
}
=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 $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 $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 $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 $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 $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";
} );
$mref->( 123 );
This is provided as a utility for Notifier subclasses to use to build a
callback CODEref to pass to a Loop method, but which may also want to store
the CODE ref internally for efficiency.
The C<$code> argument may also be a plain string, which will be used as a
method name; the returned CODE ref will then invoke that method on the object.
=cut
sub _capture_weakself
{
my $self = shift;
my ( $code ) = @_; # actually bare method names work too
weaken $self;
return sub { $self->$code( @_ ) };
}
# Keep perl happy; keep Britain tidy
1;
__END__
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
|