| 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, 2009-2011 -- leonerd@leonerd.org.uk | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | package IO::Async::Signal; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 89461 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 61 |  | 
| 9 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 55 |  | 
| 10 | 2 |  |  | 2 |  | 10 | use base qw( IO::Async::Notifier ); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 637 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '0.801'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 2 |  |  | 2 |  | 15 | use Carp; | 
|  | 2 |  |  |  |  | 16 |  | 
|  | 2 |  |  |  |  | 841 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 NAME | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | C - event callback on receipt of a POSIX signal | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | use IO::Async::Signal; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | use IO::Async::Loop; | 
| 25 |  |  |  |  |  |  | my $loop = IO::Async::Loop->new; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | my $signal = IO::Async::Signal->new( | 
| 28 |  |  |  |  |  |  | name => "HUP", | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | on_receipt => sub { | 
| 31 |  |  |  |  |  |  | print "I caught SIGHUP\n"; | 
| 32 |  |  |  |  |  |  | }, | 
| 33 |  |  |  |  |  |  | ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | $loop->add( $signal ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | $loop->run; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | This subclass of L invokes its callback when a particular | 
| 42 |  |  |  |  |  |  | POSIX signal is received. | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | Multiple objects can be added to a C that all watch for the same signal. | 
| 45 |  |  |  |  |  |  | The callback functions will all be invoked, in no particular order. | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =cut | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head1 EVENTS | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | The following events are invoked, either using subclass methods or CODE | 
| 52 |  |  |  |  |  |  | references in parameters: | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | =head2 on_receipt | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | Invoked when the signal is received. | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =cut | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =head1 PARAMETERS | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | The following named parameters may be passed to C or C: | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =head2 name => STRING | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | The name of the signal to watch. This should be a bare name like C. Can | 
| 67 |  |  |  |  |  |  | only be given at construction time. | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | =head2 on_receipt => CODE | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | CODE reference for the C event. | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | Once constructed, the C will need to be added to the C before it | 
| 74 |  |  |  |  |  |  | will work. | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =cut | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub _init | 
| 79 |  |  |  |  |  |  | { | 
| 80 | 4 |  |  | 4 |  | 11 | my $self = shift; | 
| 81 | 4 |  |  |  |  | 8 | my ( $params ) = @_; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 4 | 50 |  |  |  | 21 | my $name = delete $params->{name} or croak "Expected 'name'"; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 4 |  |  |  |  | 12 | $name =~ s/^SIG//; # Trim a leading "SIG" | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 4 |  |  |  |  | 15 | $self->{name} = $name; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 4 |  |  |  |  | 21 | $self->SUPER::_init( $params ); | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | sub configure | 
| 93 |  |  |  |  |  |  | { | 
| 94 | 5 |  |  | 5 | 1 | 1213 | my $self = shift; | 
| 95 | 5 |  |  |  |  | 16 | my %params = @_; | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 5 | 100 |  |  |  | 53 | if( exists $params{on_receipt} ) { | 
| 98 | 4 |  |  |  |  | 13 | $self->{on_receipt} = delete $params{on_receipt}; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 4 |  |  |  |  | 10 | undef $self->{cb}; # Will be lazily constructed when needed | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 4 | 100 |  |  |  | 19 | if( my $loop = $self->loop ) { | 
| 103 | 1 |  |  |  |  | 4 | $self->_remove_from_loop( $loop ); | 
| 104 | 1 |  |  |  |  | 4 | $self->_add_to_loop( $loop ); | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 5 | 50 |  |  |  | 25 | unless( $self->can_event( 'on_receipt' ) ) { | 
| 109 | 0 |  |  |  |  | 0 | croak 'Expected either a on_receipt callback or an ->on_receipt method'; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 5 |  |  |  |  | 22 | $self->SUPER::configure( %params ); | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub _add_to_loop | 
| 116 |  |  |  |  |  |  | { | 
| 117 | 5 |  |  | 5 |  | 10 | my $self = shift; | 
| 118 | 5 |  |  |  |  | 12 | my ( $loop ) = @_; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 5 |  | 33 |  |  | 35 | $self->{cb} ||= $self->make_event_cb( 'on_receipt' ); | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 5 |  |  |  |  | 24 | $self->{id} = $loop->attach_signal( $self->{name}, $self->{cb} ); | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub _remove_from_loop | 
| 126 |  |  |  |  |  |  | { | 
| 127 | 3 |  |  | 3 |  | 6 | my $self = shift; | 
| 128 | 3 |  |  |  |  | 5 | my ( $loop ) = @_; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 3 |  |  |  |  | 15 | $loop->detach_signal( $self->{name}, $self->{id} ); | 
| 131 | 3 |  |  |  |  | 14 | undef $self->{id}; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub notifier_name | 
| 135 |  |  |  |  |  |  | { | 
| 136 | 1 |  |  | 1 | 1 | 1995 | my $self = shift; | 
| 137 | 1 | 50 |  |  |  | 9 | if( length( my $name = $self->SUPER::notifier_name ) ) { | 
| 138 | 0 |  |  |  |  | 0 | return $name; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 1 |  |  |  |  | 6 | return $self->{name}; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head1 AUTHOR | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | Paul Evans | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =cut | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | 0x55AA; |