| 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-2013 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IO::Async::Loop::Mojo; | ||||||||||||||||||||||||||||
| 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.05'; | |||||||||||||||||||||||||||
| 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 Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 12 | ▶ | use Mojo::Reactor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 12 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | C<IO::Async::Loop::Mojo> - use C<IO::Async> with C<Mojolicious> | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | use IO::Async::Loop::Mojo; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $loop = IO::Async::Loop::Mojo->new(); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | $loop->add( ... ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | ... | ||||||||||||||||||||||||||||
| 35 | # Rest of Mojolicious code here | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | This subclass of L<IO::Async::Loop> uses L<Mojo::Reactor> to perform its IO | ||||||||||||||||||||||||||||
| 40 | operations. It allows the use of L<IO::Async>-based code or modules from | ||||||||||||||||||||||||||||
| 41 | within a L<Mojolicious> application. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head2 $loop = IO::Async::Loop::Mojo->new() | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | This function returns a new instance of a C<IO::Async::Loop::Mojo> object. It | ||||||||||||||||||||||||||||
| 50 | takes no special arguments. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub new | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 12 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 12 | my $self = $class->__new( @_ ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 12 | $self->{reactor} = Mojo::IOLoop->singleton->reactor; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 12 | return $self; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 METHODS | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | There are no special methods in this subclass, other than those provided by | ||||||||||||||||||||||||||||
| 67 | the C<IO::Async::Loop> base class. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub watch_io | ||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 21 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
watch_io: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 21 | my %params = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 21 | ▶ | my $handle = $params{handle} or croak "Expected 'handle'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 21 | my $fd = $handle->fileno; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 21 | my $reactor = $self->{reactor}; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 21 | my $cbs; | |||||||||||||||||||||||||||
| 82 | 21 | ▶ | $cbs = $self->{io_cbs}{$fd} ||= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"io_cbs"}{$fd} ||= do {
$reactor->io($handle, sub {
my($reactor, $writable) = @_;
if ($writable) {
$cbs->[1]->();
}
else {
$cbs->[0]->();
};
}
);
[]
}
MC/DC0%0/2
$self->{"io_cbs"}{$fd} ||= do {
$reactor->io($handle, sub {
my($reactor, $writable) = @_;
if ($writable) {
$cbs->[1]->();
}
else {
$cbs->[0]->();
};
}
);
[]
}
$self->{"io_cbs"}{$fd}
do {
$reactor->io($handle, sub {
my($reactor, $writable) = @_;
if ($writable) {
$cbs->[1]->();
}
else {
$cbs->[0]->();
};
}
);
[]
}
Truth table66%2/3
A ||= B
A= $self->{"io_cbs"}{$fd}B= do {
$reactor->io($handle, sub {
my($reactor, $writable) = @_;
if ($writable) {
$cbs->[1]->();
}
else {
$cbs->[0]->();
};
}
);
[]
}
| |||||||||||||||||||||||||||||
| 83 | # Install the watch function | ||||||||||||||||||||||||||||
| 84 | $reactor->io( $handle => sub { | ||||||||||||||||||||||||||||
| 85 | 41 | ▶ | my ( $reactor, $writable ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 41 | ▶ | if( $writable ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 7 | $cbs->[1]->(); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||
| 90 | 34 | $cbs->[0]->(); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 18 | } ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 18 | []; | |||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 21 | ▶ | if( my $on_read_ready = $params{on_read_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 16 | $cbs->[0] = $on_read_ready; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 21 | ▶ | if( my $on_write_ready = $params{on_write_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 7 | $cbs->[1] = $on_write_ready; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 21 | $reactor->watch( $handle => defined $cbs->[0], defined $cbs->[1] ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub unwatch_io | ||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unwatch_io: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 12 | my %params = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 12 | ▶ | my $handle = $params{handle} or croak "Expected 'handle'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 12 | my $fd = $handle->fileno; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 12 | my $reactor = $self->{reactor}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 12 | ▶ | my $cbs = $self->{io_cbs}{$fd} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 12 | ▶ | if( $params{on_read_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 9 | undef $cbs->[0]; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 12 | ▶ | if( $params{on_write_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 7 | undef $cbs->[1]; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 12 | ▶ | if( defined $cbs->[0] or defined $cbs->[1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cbs->[0] or defined $cbs->[1]
MC/DC50%1/2
defined $cbs->[0] or defined $cbs->[1]
defined $cbs->[0]
defined $cbs->[1]
Truth table66%2/3
A or B
A= defined $cbs->[0]B= defined $cbs->[1]
| |||||||||||||||||||||||||||||
| 129 | 1 | $reactor->watch( $handle => defined $cbs->[0], defined $cbs->[1] ); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||
| 132 | 11 | $reactor->remove( $handle ); | |||||||||||||||||||||||||||
| 133 | 11 | delete $self->{io_cbs}{$fd}; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub watch_time | ||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 27 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
watch_time: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 27 | my ( %params ) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 27 | my $reactor = $self->{reactor}; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 27 | my $delay; | |||||||||||||||||||||||||||
| 145 | 27 | ▶ | if( exists $params{at} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 146 | 1 | ▶ | my $now = exists $params{now} ? $params{now} : $self->time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | $delay = delete($params{at}) - $now; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | elsif( exists $params{after} ) { | ||||||||||||||||||||||||||||
| 151 | 26 | $delay = delete $params{after}; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | else { | ||||||||||||||||||||||||||||
| 154 | 0 | croak "Expected either 'at' or 'after' keys"; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 27 | ▶ | $delay = 0 if $delay < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 27 | my $code = delete $params{code}; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 27 | my $id; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | my $once; | ||||||||||||||||||||||||||||
| 164 | my $callback = sub { | ||||||||||||||||||||||||||||
| 165 | 11 | ▶ | my $reactor = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 166 | 11 | ▶ | $code->() unless $once++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 27 | }; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 27 | return $reactor->timer( $delay => $callback ); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | sub unwatch_time | ||||||||||||||||||||||||||||
| 173 | { | ||||||||||||||||||||||||||||
| 174 | 17 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unwatch_time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 17 | my ( $id ) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 17 | my $reactor = $self->{reactor}; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 17 | $reactor->remove( $id ); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 17 | return; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | sub loop_once | ||||||||||||||||||||||||||||
| 185 | { | ||||||||||||||||||||||||||||
| 186 | 58 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
loop_once: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | 58 | my ( $timeout ) = @_; | |||||||||||||||||||||||||||
| 188 | 58 | my $reactor = $self->{reactor}; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 58 | $self->_adjust_timeout( \$timeout ); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 58 | my $timeout_id; | |||||||||||||||||||||||||||
| 193 | 58 | ▶ | if( defined $timeout ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 57 | ▶ | $timeout_id = $reactor->timer( $timeout => sub {} ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 58 | $reactor->one_tick; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 58 | $self->_manage_queues; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 58 | ▶ | $reactor->remove( $timeout_id ) if $timeout_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | sub run | ||||||||||||||||||||||||||||
| 205 | { | ||||||||||||||||||||||||||||
| 206 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 207 | 5 | my $reactor = $self->{reactor}; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 5 | my $result = []; | |||||||||||||||||||||||||||
| 210 | # Not all Mojo::Reactor classes cope well with nested invocations | ||||||||||||||||||||||||||||
| 211 | 5 | ▶ | if( !exists $self->{result} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 4 | local $self->{result} = $result; | |||||||||||||||||||||||||||
| 213 | 4 | $reactor->start; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | else { | ||||||||||||||||||||||||||||
| 216 | 1 | local $self->{result} = $result; | |||||||||||||||||||||||||||
| 217 | 1 | local $self->{running} = 1; | |||||||||||||||||||||||||||
| 218 | 1 | ▶ | $reactor->one_tick while $self->{running}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 5 | ▶ | return wantarray ? @$result : $result->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | sub stop | ||||||||||||||||||||||||||||
| 225 | { | ||||||||||||||||||||||||||||
| 226 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | 5 | @{ $self->{result} } = @_; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 5 | ▶ | $self->{running} ? undef $self->{running} : $self->{reactor}->stop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 12 | 0x55AA; | |||||||||||||||||||||||||||