File Coverage

blib/lib/IO/Async/Notifier.pm
Criterion Covered Total %
statement 149 172 86.6
branch 53 68 77.9
condition 16 26 61.5
subroutine 36 39 92.3
pod 19 19 100.0
total 273 324 84.2


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2006-2015 -- leonerd@leonerd.org.uk
5              
6             package IO::Async::Notifier;
7              
8 85     85   228925 use strict;
  85         202  
  85         2126  
9 85     85   358 use warnings;
  85         132  
  85         3039  
10              
11             our $VERSION = '0.802';
12              
13 85     85   516 use Carp;
  85         196  
  85         5997  
14 85     85   453 use Scalar::Util qw( weaken );
  85         198  
  85         5066  
15              
16 85     85   41969 use Future 0.26; # ->is_failed
  85         717689  
  85         2211  
17              
18 85     85   31851 use IO::Async::Debug;
  85         210  
  85         3412  
19              
20             # Perl 5.8.4 cannot do trampolines by modiying @_ then goto &$code
21 85     85   502 use constant HAS_BROKEN_TRAMPOLINES => ( $] == "5.008004" );
  85         176  
  85         65015  
22              
23             =head1 NAME
24              
25             C - base class for L event objects
26              
27             =head1 SYNOPSIS
28              
29             Usually not directly used by a program, but one valid use case may be:
30              
31             use IO::Async::Notifier;
32              
33             use IO::Async::Stream;
34             use IO::Async::Signal;
35              
36             use IO::Async::Loop;
37             my $loop = IO::Async::Loop->new;
38              
39             my $notifier = IO::Async::Notifier->new;
40              
41             $notifier->add_child(
42             IO::Async::Stream->new_for_stdin(
43             on_read => sub {
44             my $self = shift;
45             my ( $buffref, $eof ) = @_;
46              
47             while( $$buffref =~ s/^(.*)\n// ) {
48             print "You said $1\n";
49             }
50              
51             return 0;
52             },
53             )
54             );
55              
56             $notifier->add_child(
57             IO::Async::Signal->new(
58             name => 'INT',
59             on_receipt => sub {
60             print "Goodbye!\n";
61             $loop->stop;
62             },
63             )
64             );
65              
66             $loop->add( $notifier );
67              
68             $loop->run;
69              
70             =head1 DESCRIPTION
71              
72             This object class forms the basis for all the other event objects that an
73             L program uses. It provides the lowest level of integration with a
74             L container, and a facility to collect Notifiers together, in
75             a tree structure, where any Notifier can contain a collection of children.
76              
77             Normally, objects in this class would not be directly used by an end program,
78             as it performs no actual IO work, and generates no actual events. These are all
79             left to the various subclasses, such as:
80              
81             =over 4
82              
83             =item *
84              
85             L - event callbacks for a non-blocking file descriptor
86              
87             =item *
88              
89             L - event callbacks and write bufering for a stream
90             filehandle
91              
92             =item *
93              
94             L - event callbacks and send buffering for a socket
95             filehandle
96              
97             =item *
98              
99             L - base class for Notifiers that use timed delays
100              
101             =item *
102              
103             L - event callback on receipt of a POSIX signal
104              
105             =item *
106              
107             L - event callback on exit of a child process
108              
109             =item *
110              
111             L - start and manage a child process
112              
113             =back
114              
115             For more detail, see the SYNOPSIS section in one of the above.
116              
117             One case where this object class would be used, is when a library wishes to
118             provide a sub-component which consists of multiple other C
119             subclasses, such as Cs and C, but no particular object is
120             suitable to be the root of a tree. In this case, a plain C object
121             can be used as the tree root, and all the other notifiers added as children of
122             it.
123              
124             =cut
125              
126             =head1 AS A MIXIN
127              
128             Rather than being used as a subclass this package also supports being used as
129             a non-principle superclass for an object, as a mix-in. It still provides
130             methods and satisfies an C test, even though the constructor is not
131             directly called. This simply requires that the object be based on a normal
132             blessed hash reference and include C somewhere in its
133             C<@ISA> list.
134              
135             The methods in this class all use only keys in the hash prefixed by
136             C<"IO_Async_Notifier__"> for namespace purposes.
137              
138             This is intended mainly for defining a subclass of some other object that is
139             also an C, suitable to be added to an L.
140              
141             package SomeEventSource::Async;
142             use base qw( SomeEventSource IO::Async::Notifier );
143              
144             sub _add_to_loop
145             {
146             my $self = shift;
147             my ( $loop ) = @_;
148              
149             # Code here to set up event handling on $loop that may be required
150             }
151              
152             sub _remove_from_loop
153             {
154             my $self = shift;
155             my ( $loop ) = @_;
156              
157             # Code here to undo the event handling set up above
158             }
159              
160             Since all the methods documented here will be available, the implementation
161             may wish to use the C and C or C
162             methods to implement its own event callbacks.
163              
164             =cut
165              
166             =head1 EVENTS
167              
168             The following events are invoked, either using subclass methods or CODE
169             references in parameters:
170              
171             =head2 on_error $message, $name, @details
172              
173             Invoked by C.
174              
175             =cut
176              
177             =head1 PARAMETERS
178              
179             A specific subclass of C defines named parameters that
180             control its behaviour. These may be passed to the C constructor, or to
181             the C method. The documentation on each specific subclass will give
182             details on the parameters that exist, and their uses. Some parameters may only
183             support being set once at construction time, or only support being changed if
184             the object is in a particular state.
185              
186             The following parameters are supported by all Notifiers:
187              
188             =over 8
189              
190             =item on_error => CODE
191              
192             CODE reference for event handler.
193              
194             =item notifier_name => STRING
195              
196             Optional string used to identify this particular Notifier. This value will be
197             returned by the C method.
198              
199             =back
200              
201             =cut
202              
203             =head1 CONSTRUCTOR
204              
205             =cut
206              
207             =head2 new
208              
209             $notifier = IO::Async::Notifier->new( %params )
210              
211             This function returns a new instance of a C object with
212             the given initial values of the named parameters.
213              
214             Up until L version 0.19, this module used to implement the IO
215             handle features now found in the L subclass. Code that
216             needs to use any of C, C, C,
217             C or C should use L instead.
218              
219             =cut
220              
221             sub new
222             {
223 1419     1419 1 72480 my $class = shift;
224 1419         13751 my %params = @_;
225              
226 1419         6651 my $self = bless {}, $class;
227              
228 1419         14127 $self->_init( \%params );
229              
230 1419         9524 $self->configure( %params );
231              
232 1418         10308 return $self;
233             }
234              
235             =head1 METHODS
236              
237             =cut
238              
239             =head2 configure
240              
241             $notifier->configure( %params )
242              
243             Adjust the named parameters of the C as given by the C<%params>
244             hash.
245              
246             =cut
247              
248             # for subclasses to override and call down to
249             sub configure
250             {
251 1762     1762 1 4223 my $self = shift;
252 1762         4523 my %params = @_;
253              
254 1762         4904 foreach (qw( notifier_name on_error )) {
255 3524 100       11881 $self->{"IO_Async_Notifier__$_"} = delete $params{$_} if exists $params{$_};
256             }
257              
258 1762 100       5716 $self->configure_unknown( %params ) if keys %params;
259             }
260              
261             sub configure_unknown
262             {
263 1     1 1 2 my $self = shift;
264 1         2 my %params = @_;
265              
266 1         2 my $class = ref $self;
267 1         122 croak "Unrecognised configuration keys for $class - " . join( " ", keys %params );
268             }
269              
270             =head2 loop
271              
272             $loop = $notifier->loop
273              
274             Returns the L that this Notifier is a member of.
275              
276             =cut
277              
278             sub loop
279             {
280 20340     20340 1 38257 my $self = shift;
281             return $self->{IO_Async_Notifier__loop}
282 20340         61338 }
283              
284             *get_loop = \&loop;
285              
286             # Only called by IO::Async::Loop, not external interface
287             sub __set_loop
288             {
289 3123     3123   4360 my $self = shift;
290 3123         4926 my ( $loop ) = @_;
291              
292             # early exit if no change
293 3123 100 66     15010 return if !$loop and !$self->loop or
      100        
      66        
      66        
294             $loop and $self->loop and $loop == $self->loop;
295              
296 2557 100       4447 $self->_remove_from_loop( $self->loop ) if $self->loop;
297              
298 2557         7165 $self->{IO_Async_Notifier__loop} = $loop;
299 2557         8024 weaken( $self->{IO_Async_Notifier__loop} ); # To avoid a cycle
300              
301 2557 100       4578 $self->_add_to_loop( $self->loop ) if $self->loop;
302             }
303              
304             =head2 notifier_name
305              
306             $name = $notifier->notifier_name
307              
308             Returns the name to identify this Notifier. If a has not been set, it will
309             return the empty string. Subclasses may wish to override this behaviour to
310             return some more useful information, perhaps from configured parameters.
311              
312             =cut
313              
314             sub notifier_name
315             {
316 50     50 1 1056 my $self = shift;
317 50   100     356 return $self->{IO_Async_Notifier__notifier_name} || "";
318             }
319              
320             =head2 adopt_future
321              
322             $f = $notifier->adopt_future( $f )
323              
324             Stores a reference to the L instance within the notifier itself, so
325             the reference doesn't get lost. This reference will be dropped when the future
326             becomes ready (either by success or failure). Additionally, if the future
327             failed the notifier's C method will be informed.
328              
329             This means that if the notifier does not provide an C handler, nor
330             is there one anywhere in the parent chain, this will be fatal to the caller of
331             C<< $f->fail >>. To avoid this being fatal if the failure is handled
332             elsewhere, use the C method on the future to obtain a sequence one
333             that never fails.
334              
335             $notifier->adopt_future( $f->else_done() )
336              
337             The future itself is returned.
338              
339             =cut
340              
341             sub adopt_future
342             {
343 58     58 1 3914 my $self = shift;
344 58         125 my ( $f ) = @_;
345              
346 58         213 my $fkey = "$f"; # stable stringification
347              
348 58         530 $self->{IO_Async_Notifier__futures}{$fkey} = $f;
349              
350             $f->on_ready( $self->_capture_weakself( sub {
351 58     58   93 my $self = shift;
352 58         138 my ( $f ) = @_;
353              
354 58         173 delete $self->{IO_Async_Notifier__futures}{$fkey};
355              
356 58 100       213 $self->invoke_error( $f->failure ) if $f->is_failed;
357 58         667 }));
358              
359 58         1161 return $f;
360             }
361              
362             =head2 adopted_futures
363              
364             @f = $notifier->adopted_futures
365              
366             I
367              
368             Returns a list of all the adopted and still-pending futures, in no particular
369             order.
370              
371             =cut
372              
373             sub adopted_futures
374             {
375 3     3 1 22 my $self = shift;
376              
377 3         4 return values %{ $self->{IO_Async_Notifier__futures} };
  3         17  
378             }
379              
380             =head1 CHILD NOTIFIERS
381              
382             During the execution of a program, it may be the case that certain IO handles
383             cause other handles to be created; for example, new sockets that have been
384             Ced from a listening socket. To facilitate these, a notifier may
385             contain child notifier objects, that are automatically added to or removed
386             from the L that manages their parent.
387              
388             =cut
389              
390             =head2 parent
391              
392             $parent = $notifier->parent
393              
394             Returns the parent of the notifier, or C if does not have one.
395              
396             =cut
397              
398             sub parent
399             {
400 3329     3329 1 5376 my $self = shift;
401 3329         9499 return $self->{IO_Async_Notifier__parent};
402             }
403              
404             =head2 children
405              
406             @children = $notifier->children
407              
408             Returns a list of the child notifiers contained within this one.
409              
410             =cut
411              
412             sub children
413             {
414 3092     3092 1 4599 my $self = shift;
415 3092 100       8759 return unless $self->{IO_Async_Notifier__children};
416 664         1404 return @{ $self->{IO_Async_Notifier__children} };
  664         3812  
417             }
418              
419             =head2 add_child
420              
421             $notifier->add_child( $child )
422              
423             Adds a child notifier. This notifier will be added to the containing loop, if
424             the parent has one. Only a notifier that does not currently have a parent and
425             is not currently a member of any loop may be added as a child. If the child
426             itself has grandchildren, these will be recursively added to the containing
427             loop.
428              
429             =cut
430              
431             sub add_child
432             {
433 649     649 1 1515 my $self = shift;
434 649         1035 my ( $child ) = @_;
435              
436 649 100       1476 croak "Cannot add a child that already has a parent" if defined $child->{IO_Async_Notifier__parent};
437              
438 648 50       1312 croak "Cannot add a child that is already a member of a loop" if defined $child->loop;
439              
440 648 100       1185 if( defined( my $loop = $self->loop ) ) {
441 626         1925 $loop->add( $child );
442             }
443              
444 643         964 push @{ $self->{IO_Async_Notifier__children} }, $child;
  643         2727  
445 643         2151 $child->{IO_Async_Notifier__parent} = $self;
446 643         2085 weaken( $child->{IO_Async_Notifier__parent} );
447              
448 643         1395 return;
449             }
450              
451             =head2 remove_child
452              
453             $notifier->remove_child( $child )
454              
455             Removes a child notifier. The child will be removed from the containing loop,
456             if the parent has one. If the child itself has grandchildren, these will be
457             recurively removed from the loop.
458              
459             =cut
460              
461             sub remove_child
462             {
463 360     360 1 852 my $self = shift;
464 360         804 my ( $child ) = @_;
465              
466             LOOP: {
467 360         511 my $childrenref = $self->{IO_Async_Notifier__children};
  360         624  
468 360         2037 for my $i ( 0 .. $#$childrenref ) {
469 85     85   613 no warnings 'uninitialized';
  85         243  
  85         110798  
470 406 100       1453 next unless $childrenref->[$i] == $child;
471 360         1080 splice @$childrenref, $i, 1, ();
472 360         1126 last LOOP;
473             }
474              
475 0         0 croak "Cannot remove child from a parent that doesn't contain it";
476             }
477              
478 360         688 undef $child->{IO_Async_Notifier__parent};
479              
480 360 100       826 if( defined( my $loop = $self->loop ) ) {
481 272         1219 $loop->remove( $child );
482             }
483             }
484              
485             =head2 remove_from_parent
486              
487             $notifier->remove_from_parent
488              
489             Removes this notifier object from its parent (either another notifier object
490             or the containing loop) if it has one. If the notifier is not a child of
491             another notifier nor a member of a loop, this method does nothing.
492              
493             =cut
494              
495             sub remove_from_parent
496             {
497 804     804 1 1330 my $self = shift;
498              
499 804 100       2424 if( my $parent = $self->parent ) {
    100          
500 307         1382 $parent->remove_child( $self );
501             }
502             elsif( my $loop = $self->loop ) {
503 476         1989 $loop->remove( $self );
504             }
505             }
506              
507             =head1 SUBCLASS METHODS
508              
509             C is a base class provided so that specific subclasses of
510             it provide more specific behaviour. The base class provides a number of
511             methods that subclasses may wish to override.
512              
513             If a subclass implements any of these, be sure to invoke the superclass method
514             at some point within the code.
515              
516             =cut
517              
518             =head2 _init
519              
520             $notifier->_init( $paramsref )
521              
522             This method is called by the constructor just before calling C.
523             It is passed a reference to the HASH storing the constructor arguments.
524              
525             This method may initialise internal details of the Notifier as required,
526             possibly by using parameters from the HASH. If any parameters are
527             construction-only they should be Cd from the hash.
528              
529             =cut
530              
531             sub _init
532       707     {
533             # empty default
534             }
535              
536             =head2 configure
537              
538             $notifier->configure( %params )
539              
540             This method is called by the constructor to set the initial values of named
541             parameters, and by users of the object to adjust the values once constructed.
542              
543             This method should C from the C<%params> hash any keys it has dealt
544             with, then pass the remaining ones to the C. The base
545             class implementation will throw an exception if there are any unrecognised
546             keys remaining.
547              
548             =cut
549              
550             =head2 configure_unknown
551              
552             $notifier->configure_unknown( %params )
553              
554             This method is called by the base class C method, for any remaining
555             parameters that are not recognised. The default implementation throws an
556             exception using C that lists the unrecognised keys. This method is
557             provided to allow subclasses to override the behaviour, perhaps to store
558             unrecognised keys, or to otherwise inspect the left-over arguments for some
559             other purpose.
560              
561             =cut
562              
563             =head2 _add_to_loop
564              
565             $notifier->_add_to_loop( $loop )
566              
567             This method is called when the Notifier has been added to a Loop; either
568             directly, or indirectly through being a child of a Notifer already in a loop.
569              
570             This method may be used to perform any initial startup activity required for
571             the Notifier to be fully functional but which requires a Loop to do so.
572              
573             =cut
574              
575             sub _add_to_loop
576       514     {
577             # empty default
578             }
579              
580             =head2 _remove_from_loop
581              
582             $notifier->_remove_from_loop( $loop )
583              
584             This method is called when the Notifier has been removed from a Loop; either
585             directly, or indirectly through being a child of a Notifier removed from the
586             loop.
587              
588             This method may be used to undo the effects of any setup that the
589             C<_add_to_loop> method had originally done.
590              
591             =cut
592              
593             sub _remove_from_loop
594       454     {
595             # empty default
596             }
597              
598             =head1 UTILITY METHODS
599              
600             =cut
601              
602             =head2 _capture_weakself
603              
604             $mref = $notifier->_capture_weakself( $code )
605              
606             Returns a new CODE ref which, when invoked, will invoke the originally-passed
607             ref, with additionally a reference to the Notifier as its first argument. The
608             Notifier reference is stored weakly in C<$mref>, so this CODE ref may be
609             stored in the Notifier itself without creating a cycle.
610              
611             For example,
612              
613             my $mref = $notifier->_capture_weakself( sub {
614             my ( $notifier, $arg ) = @_;
615             print "Notifier $notifier got argument $arg\n";
616             } );
617              
618             $mref->( 123 );
619              
620             This is provided as a utility for Notifier subclasses to use to build a
621             callback CODEref to pass to a Loop method, but which may also want to store
622             the CODE ref internally for efficiency.
623              
624             The C<$code> argument may also be a plain string, which will be used as a
625             method name; the returned CODE ref will then invoke that method on the object.
626             In this case the method name is stored symbolically in the returned CODE
627             reference, and dynamically dispatched each time the reference is invoked. This
628             allows it to follow code reloading, dynamic replacement of class methods, or
629             other similar techniques.
630              
631             If the C<$mref> CODE reference is being stored in some object other than the
632             one it refers to, remember that since the Notifier is only weakly captured, it
633             is possible that it has been destroyed by the time the code runs, and so the
634             reference will be passed as C. This should be protected against by the
635             code body.
636              
637             $other_object->{on_event} = $notifier->_capture_weakself( sub {
638             my $notifier = shift or return;
639             my ( @event_args ) = @_;
640             ...
641             } );
642              
643             For stand-alone generic implementation of this behaviour, see also L
644             and C.
645              
646             =cut
647              
648             sub _capture_weakself
649             {
650 2205     2205   61291 my $self = shift;
651 2205         4188 my ( $code ) = @_; # actually bare method names work too
652              
653 2205 100       5900 if( !ref $code ) {
654 130         304 my $class = ref $self;
655             # Don't save this coderef, or it will break dynamic method dispatch,
656             # which means code reloading, dynamic replacement, or other funky
657             # techniques stop working
658 130 100       628 $self->can( $code ) or
659             croak qq(Can't locate object method "$code" via package "$class");
660             }
661              
662 2204         6380 weaken $self;
663              
664             return sub {
665 2262 100   2262   56542 my $cv = ref( $code ) ? $code : $self->can( $code );
666              
667 2262         3394 if( HAS_BROKEN_TRAMPOLINES ) {
668             return $cv->( $self, @_ );
669             }
670             else {
671 2262         5105 unshift @_, $self;
672 2262         14653 goto &$cv;
673             }
674 2204         26349 };
675             }
676              
677             =head2 _replace_weakself
678              
679             $mref = $notifier->_replace_weakself( $code )
680              
681             Returns a new CODE ref which, when invoked, will invoke the originally-passed
682             ref, with a reference to the Notifier replacing its first argument. The
683             Notifier reference is stored weakly in C<$mref>, so this CODE ref may be
684             stored in the Notifier itself without creating a cycle.
685              
686             For example,
687              
688             my $mref = $notifier->_replace_weakself( sub {
689             my ( $notifier, $arg ) = @_;
690             print "Notifier $notifier got argument $arg\n";
691             } );
692              
693             $mref->( $object, 123 );
694              
695             This is provided as a utility for Notifier subclasses to use for event
696             callbacks on other objects, where the delegated object is passed in the
697             function's arguments.
698              
699             The C<$code> argument may also be a plain string, which will be used as a
700             method name; the returned CODE ref will then invoke that method on the object.
701             As with C<_capture_weakself> this is stored symbolically.
702              
703             As with C<_capture_weakself>, care should be taken against Notifier
704             destruction if the C<$mref> CODE reference is stored in some other object.
705              
706             =cut
707              
708             sub _replace_weakself
709             {
710 185     185   449 my $self = shift;
711 185         335 my ( $code ) = @_; # actually bare method names work too
712              
713 185 100       600 if( !ref $code ) {
714             # Don't save this coderef, see _capture_weakself for why
715 14         31 my $class = ref $self;
716 14 50       89 $self->can( $code ) or
717             croak qq(Can't locate object method "$code" via package "$class");
718             }
719              
720 185         602 weaken $self;
721              
722             return sub {
723 41 100   41   449 my $cv = ref( $code ) ? $code : $self->can( $code );
724              
725 41         90 if( HAS_BROKEN_TRAMPOLINES ) {
726             return $cv->( $self, @_[1..$#_] );
727             }
728             else {
729             # Don't assign to $_[0] directly or we will change caller's first argument
730 41         75 shift @_;
731 41         99 unshift @_, $self;
732 41         242 goto &$cv;
733             }
734 185         2549 };
735             }
736              
737             =head2 can_event
738              
739             $code = $notifier->can_event( $event_name )
740              
741             Returns a C reference if the object can perform the given event name,
742             either by a configured C reference parameter, or by implementing a
743             method. If the object is unable to handle this event, C is returned.
744              
745             =cut
746              
747             sub can_event
748             {
749 5250     5250 1 8749 my $self = shift;
750 5250         13165 my ( $event_name ) = @_;
751              
752 5250   100     36368 return $self->{$event_name} || $self->can( $event_name );
753             }
754              
755             =head2 make_event_cb
756              
757             $callback = $notifier->make_event_cb( $event_name )
758              
759             Returns a C reference which, when invoked, will execute the given event
760             handler. Event handlers may either be subclass methods, or parameters given to
761             the C or C method.
762              
763             The event handler can be passed extra arguments by giving them to the C
764             reference; the first parameter received will be a reference to the notifier
765             itself. This is stored weakly in the closure, so it is safe to store the
766             resulting C reference in the object itself without causing a reference
767             cycle.
768              
769             =cut
770              
771             sub make_event_cb
772             {
773 740     740 1 1386 my $self = shift;
774 740         3329 my ( $event_name ) = @_;
775              
776 740 50       1776 my $code = $self->can_event( $event_name )
777             or croak "$self cannot handle $event_name event";
778              
779 740         3787 my $caller = caller;
780              
781             return $self->_capture_weakself(
782             !$IO::Async::Debug::DEBUG ? $code : sub {
783 0     0   0 my $self = $_[0];
784 0         0 $self->_debug_printf_event( $caller, $event_name );
785 0         0 goto &$code;
786             }
787 740 50       5091 );
788             }
789              
790             =head2 maybe_make_event_cb
791              
792             $callback = $notifier->maybe_make_event_cb( $event_name )
793              
794             Similar to C but will return C if the object cannot
795             handle the named event, rather than throwing an exception.
796              
797             =cut
798              
799             sub maybe_make_event_cb
800             {
801 2     2 1 518 my $self = shift;
802 2         4 my ( $event_name ) = @_;
803              
804 2 100       5 my $code = $self->can_event( $event_name )
805             or return undef;
806              
807 1         2 my $caller = caller;
808              
809             return $self->_capture_weakself(
810             !$IO::Async::Debug::DEBUG ? $code : sub {
811 0     0   0 my $self = $_[0];
812 0         0 $self->_debug_printf_event( $caller, $event_name );
813 0         0 goto &$code;
814             }
815 1 50       4 );
816             }
817              
818             =head2 invoke_event
819              
820             @ret = $notifier->invoke_event( $event_name, @args )
821              
822             Invokes the given event handler, passing in the given arguments. Event
823             handlers may either be subclass methods, or parameters given to the C or
824             C method. Returns whatever the underlying method or CODE reference
825             returned.
826              
827             =cut
828              
829             sub invoke_event
830             {
831 1388     1388 1 3282 my $self = shift;
832 1388         5061 my ( $event_name, @args ) = @_;
833              
834 1388 50       3706 my $code = $self->can_event( $event_name )
835             or croak "$self cannot handle $event_name event";
836              
837 1388 50       2915 $self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG;
838 1388         5020 return $code->( $self, @args );
839             }
840              
841             =head2 maybe_invoke_event
842              
843             $retref = $notifier->maybe_invoke_event( $event_name, @args )
844              
845             Similar to C but will return C if the object cannot
846             handle the name event, rather than throwing an exception. In order to
847             distinguish this from an event-handling function that simply returned
848             C, if the object does handle the event, the list that it returns will
849             be returned in an ARRAY reference.
850              
851             =cut
852              
853             sub maybe_invoke_event
854             {
855 1229     1229 1 3746 my $self = shift;
856 1229         5150 my ( $event_name, @args ) = @_;
857              
858 1229 100       3171 my $code = $self->can_event( $event_name )
859             or return undef;
860              
861 73 50       271 $self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG;
862 73         283 return [ $code->( $self, @args ) ];
863             }
864              
865             =head1 DEBUGGING SUPPORT
866              
867             =cut
868              
869             =head2 debug_printf
870              
871             $notifier->debug_printf( $format, @args )
872              
873             Conditionally print a debugging message to C if debugging is enabled.
874             If such a message is printed, it will be printed using C using the
875             given format and arguments. The message will be prefixed with a string, in
876             square brackets, to help identify the C<$notifier> instance. This string will
877             be the class name of the notifier, and any parent notifiers it is contained
878             by, joined by an arrow C<< <- >>. To ensure this string does not grow too
879             long, certain prefixes are abbreviated:
880              
881             IO::Async::Protocol:: => IaP:
882             IO::Async:: => Ia:
883             Net::Async:: => Na:
884              
885             Finally, each notifier that has a name defined using the C
886             parameter has that name appended in braces.
887              
888             For example, invoking
889              
890             $stream->debug_printf( "EVENT on_read" )
891              
892             On an L instance reading and writing a file descriptor
893             whose C is 4, which is a child of an L,
894             will produce a line of output:
895              
896             [Ia:Stream{rw=4}<-IaP:Stream] EVENT on_read
897              
898             =cut
899              
900             sub debug_printf
901             {
902 1092 50   1092 1 3028 $IO::Async::Debug::DEBUG or return;
903              
904 0         0 my $self = shift;
905 0         0 my ( $format, @args ) = @_;
906              
907 0         0 my @id;
908 0         0 while( $self ) {
909 0         0 push @id, ref $self;
910              
911 0         0 my $name = $self->notifier_name;
912 0 0 0     0 $id[-1] .= "{$name}" if defined $name and length $name;
913              
914 0         0 $self = $self->parent;
915             }
916              
917             s/^IO::Async::Protocol::/IaP:/,
918             s/^IO::Async::/Ia:/,
919 0         0 s/^Net::Async::/Na:/ for @id;
920              
921 0         0 IO::Async::Debug::logf "[%s] $format\n", join("<-", @id), @args;
922             }
923              
924             sub _debug_printf_event
925             {
926 0     0   0 my $self = shift;
927 0         0 my ( $caller, $event_name ) = @_;
928              
929 0         0 my $class = ref $self;
930              
931 0 0 0     0 if( $IO::Async::Debug::DEBUG > 1 or $class eq $caller ) {
932             s/^IO::Async::Protocol::/IaP:/,
933             s/^IO::Async::/Ia:/,
934 0         0 s/^Net::Async::/Na:/ for my $str_caller = $caller;
935              
936 0 0       0 $self->debug_printf( "EVENT %s",
937             ( $class eq $caller ? $event_name : "${str_caller}::$event_name" )
938             );
939             }
940             }
941              
942             =head2 invoke_error
943              
944             $notifier->invoke_error( $message, $name, @details )
945              
946             Invokes the stored C event handler, passing in the given arguments.
947             If no handler is defined, it will be passed up to the containing parent
948             notifier, if one exists. If no parent exists, the error message will be thrown
949             as an exception by using C and this method will not return.
950              
951             If a handler is found to handle this error, the method will return as normal.
952             However, as the expected use-case is to handle "fatal" errors that now render
953             the notifier unsuitable to continue, code should be careful not to perform any
954             further work after invoking it. Specifically, sockets may become disconnected,
955             or the entire notifier may now be removed from its containing loop.
956              
957             The C<$name> and C<@details> list should follow similar semantics to L
958             failures. That is, the C<$name> should be a string giving a category of
959             failure, and the C<@details> list should contain additional arguments that
960             relate to that kind of failure.
961              
962             =cut
963              
964             sub invoke_error
965             {
966 5     5 1 811 my $self = shift;
967 5         9 my ( $message, $name, @details ) = @_;
968              
969 5 100 66     27 if( my $code = $self->{IO_Async_Notifier__on_error} || $self->can( "on_error" ) ) {
970 3         9 return $code->( $self, $message, $name, @details );
971             }
972              
973 2 100       5 if( my $parent = $self->parent ) {
974 1         5 return $parent->invoke_error( @_ );
975             }
976              
977 1         6 die "$message\n";
978             }
979              
980             =head1 AUTHOR
981              
982             Paul Evans
983              
984             =cut
985              
986             0x55AA;