| 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, 2012 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IO::Async::Loop::Event; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 12 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 12 | 12 | ▶ | use constant API_VERSION => '0.49'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use base qw( IO::Async::Loop ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 12 | IO::Async::Loop->VERSION( '0.49' ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 12 | ▶ | use constant _CAN_SUBSECOND_ACCURATELY => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 12 | ▶ | use Event; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | C<IO::Async::Loop::Event> - use C<IO::Async> with C<Event> | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | use IO::Async::Loop::Event; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $loop = IO::Async::Loop::Event->new(); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | $loop->add( ... ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | $loop->run; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | This subclass of L<IO::Async::Loop> uses L<Event> to perform its work. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub new | ||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | 11 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 11 | my $self = $class->SUPER::__new( @_ ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 11 | $self->{$_} = {} for qw( watch_io watch_sig watch_idle ); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 11 | return $self; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub loop_once | ||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 63 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
loop_once: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 63 | my ( $timeout ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 63 | ▶ | if( defined $timeout ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 62 | Event::one_event( $timeout ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 1 | Event::one_event; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub run | ||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||
| 68 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | my $result = Event::loop(); | |||||||||||||||||||||||||||
| 71 | 5 | ▶ | wantarray ? @$result : $result->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub stop | ||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 5 | my @result = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 5 | Event::unloop( \@result ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub watch_io | ||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
watch_io: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 14 | my %params = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 14 | ▶ | my $handle = $params{handle} or die "Need a handle"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 14 | ▶ | my $w = $self->{watch_io}{$handle} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"watch_io"}{$handle} ||= []
MC/DC100%1/1
$self->{"watch_io"}{$handle} ||= []
$self->{"watch_io"}{$handle}
Truth table100%2/2
A
A= $self->{"watch_io"}{$handle}
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 14 | ▶ | if( my $on_read_ready = $params{on_read_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 9 | $w->[1] = $on_read_ready; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 14 | ▶ | if( my $on_write_ready = $params{on_write_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 7 | $w->[2] = $on_write_ready; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 14 | ▶ | my $poll = ( $w->[1] ? "r" : "" ) . ( $w->[2] ? "w" : "" ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 14 | ▶ | if( $w->[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 3 | $w->[0]->poll( $poll ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||
| 105 | $w->[0] = Event->io( | ||||||||||||||||||||||||||||
| 106 | poll => $poll, | ||||||||||||||||||||||||||||
| 107 | fd => $handle, | ||||||||||||||||||||||||||||
| 108 | cb => sub { | ||||||||||||||||||||||||||||
| 109 | 14 | ▶ | my $e = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 110 | 14 | ▶ | if( $e->got =~ m/r/ and $w->[1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$e->got =~ /r/ and $w->[1]
MC/DC50%1/2
$e->got =~ /r/ and $w->[1]
$e->got =~ /r/
$w->[1]
Truth table66%2/3
A and B
A= $e->got =~ /r/B= $w->[1]
| |||||||||||||||||||||||||||||
| 111 | 12 | $w->[1]->(); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 14 | ▶ | if( $e->got =~ m/w/ and $w->[2] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$e->got =~ /w/ and $w->[2]
MC/DC100%2/2
$e->got =~ /w/ and $w->[2]
$e->got =~ /w/
$w->[2]
Truth table100%3/3
A and B
A= $e->got =~ /w/B= $w->[2]
| |||||||||||||||||||||||||||||
| 114 | 7 | $w->[2]->(); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 11 | ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub unwatch_io | ||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 11 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unwatch_io: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 11 | my %params = @_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 11 | ▶ | my $handle = $params{handle} or die "Need a handle"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 11 | ▶ | my $w = $self->{watch_io}{$handle} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 11 | ▶ | if( $params{on_read_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 8 | undef $w->[1]; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | ▶ | if( $params{on_write_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 7 | undef $w->[2]; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 11 | ▶ | my $poll = ( $w->[1] ? "r" : "" ) . ( $w->[2] ? "w" : "" ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 11 | ▶ | if( length $poll ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 1 | $w->[0]->poll( $poll ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | 10 | $w->[0]->cancel; | |||||||||||||||||||||||||||
| 145 | 10 | delete $self->{watch_io}{$handle}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub watch_time | ||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 27 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
watch_time: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 27 | my %params = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 27 | ▶ | my $code = $params{code} or croak "Expected 'code' as CODE ref"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 27 | my $w; | |||||||||||||||||||||||||||
| 157 | 27 | ▶ | if( defined $params{after} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 26 | my $delay = $params{after}; | |||||||||||||||||||||||||||
| 159 | 26 | ▶ | $delay = 0 if $delay < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 26 | $w = Event->timer( after => $delay, cb => $code ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||
| 163 | 1 | $w = Event->timer( at => $params{at}, cb => $code ); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 27 | return $w; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub unwatch_time | ||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 17 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unwatch_time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 17 | my ( $w ) = @_; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 17 | $w->cancel; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub watch_signal | ||||||||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||||||||
| 179 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
watch_signal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 9 | my ( $name, $code ) = @_; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 9 | ▶ | exists $SIG{$name} or croak "Unrecognised signal name $name"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 8 | my $w = Event->signal( signal => $name, cb => $code ); | |||||||||||||||||||||||||||
| 185 | 8 | $self->{watch_sig}{$name} = $w; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | sub unwatch_signal | ||||||||||||||||||||||||||||
| 189 | { | ||||||||||||||||||||||||||||
| 190 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unwatch_signal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 2 | my ( $name ) = @_; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 2 | ( delete $self->{watch_sig}{$name} )->cancel; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub watch_idle | ||||||||||||||||||||||||||||
| 197 | { | ||||||||||||||||||||||||||||
| 198 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
watch_idle: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 199 | 6 | my %params = @_; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 6 | ▶ | my $when = delete $params{when} or croak "Expected 'when'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 6 | ▶ | my $code = delete $params{code} or croak "Expected 'code' as a CODE ref"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 6 | ▶ | $when eq "later" or croak "Expected 'when' to be 'later'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 6 | my $key; | |||||||||||||||||||||||||||
| 208 | 6 | my $idles = $self->{watch_idle}; | |||||||||||||||||||||||||||
| 209 | 6 | my $w = Event->timer( | |||||||||||||||||||||||||||
| 210 | after => 0, | ||||||||||||||||||||||||||||
| 211 | cb => $code, | ||||||||||||||||||||||||||||
| 212 | prio => -1, | ||||||||||||||||||||||||||||
| 213 | ); | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 6 | $key = "$w"; | |||||||||||||||||||||||||||
| 216 | 6 | $idles->{$key} = $w; | |||||||||||||||||||||||||||
| 217 | 6 | return $key; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | sub unwatch_idle | ||||||||||||||||||||||||||||
| 221 | { | ||||||||||||||||||||||||||||
| 222 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unwatch_idle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 223 | 1 | my ( $id ) = @_; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | ( delete $self->{watch_idle}{$id} )->cancel; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 12 | 0x55AA; | |||||||||||||||||||||||||||