| 1 | |||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # Use AnyEvent for the IO::Event's event handler | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 21 | my $debug = 0; | |||||||||||||||||||
| 7 | 21 | my $debug_timer; | |||||||||||||||||||
| 8 | my $lost_event_timer; | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | { | ||||||||||||||||||||
| 11 | package IO::Event::AnyEvent; | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | # ABSTRACT: Use AnyEvent for the IO::Event event handler | ||||||||||||||||||||
| 14 | 21 | our $VERSION = '0.814'; # VERSION | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 21 | our $lost_event_hack = 2; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 21 | require IO::Event; | |||||||||||||||||||
| 19 | 21 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 21 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 21 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 21 | ▶ | use parent -norequire, 'IO::Event::Common'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 21 | my %selves; | |||||||||||||||||||
| 26 | 21 | my $condvar; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | sub import | ||||||||||||||||||||
| 29 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
import: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 30 | 0 | require IO::Event; | |||||||||||||||||||
| 31 | 0 | IO::Event->import('AnyEvent'); | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | sub new | ||||||||||||||||||||
| 35 | 195 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 36 | 195 | my ($pkg, @stuff) = @_; | |||||||||||||||||||
| 37 | 195 | my $self = $pkg->SUPER::new(@stuff); | |||||||||||||||||||
| 38 | 195 | return $self; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub loop | ||||||||||||||||||||
| 42 | 5 | ▶ | { | ||||||||||||||||||
|
Subroutine
loop: called CC=3 SCAR=14.4
Pod
undocumented
| |||||||||||||||||||||
| 43 | 5 | $condvar = AnyEvent->condvar; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 5 | ▶ | if ($debug) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | ▶ | $debug_timer = AnyEvent->timer(after => 0.1, interval => 0.1, cb => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 47 | 0 | print STDERR "WATCHING:\n"; | |||||||||||||||||||
| 48 | 0 | for my $ie (values %selves) { | |||||||||||||||||||
| 49 | 0 | print STDERR "\t"; | |||||||||||||||||||
| 50 | 0 | ▶ | print STDERR "R" if ${*$ie}{ie_anyevent_read}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | ▶ | print STDERR "W" if ${*$ie}{ie_anyevent_read}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | print STDERR " ${*$ie}{ie_desc}\n"; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | 0 | }); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | 5 | ▶ | if ($lost_event_hack) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | $lost_event_timer = AnyEvent->timer( | ||||||||||||||||||||
| 58 | after => $lost_event_hack, | ||||||||||||||||||||
| 59 | interval => $lost_event_hack, | ||||||||||||||||||||
| 60 | 9 | ▶ | cb => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 61 | 9 | for my $ie (values %selves) { | |||||||||||||||||||
| 62 | 49 | ▶ | next unless ${*$ie}{ie_anyevent_read}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 49 | ▶ | next if ${*$ie}{ie_listener}; # no spurious connections! | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | # print STDERR "DISPATCHING FOR READ for ${*$ie}{ie_desc}\n"; # LOST EVENTS | ||||||||||||||||||||
| 65 | 44 | $ie->ie_dispatch_read(); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||
| 68 | 5 | ); | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 5 | $condvar->recv; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub timer | ||||||||||||||||||||
| 74 | 5 | ▶ | { | ||||||||||||||||||
|
Subroutine
timer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 75 | 5 | IO::Event::AnyEvent::Wrapper->new('Timer', @_); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub unloop | ||||||||||||||||||||
| 79 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
unloop: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 80 | 0 | ▶ | $condvar->send(@_) if $condvar; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | sub unloop_all | ||||||||||||||||||||
| 84 | 3 | ▶ | { | ||||||||||||||||||
|
Subroutine
unloop_all: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 85 | 3 | ▶ | $condvar->send(@_) if $condvar; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | sub idle | ||||||||||||||||||||
| 89 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
idle: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 90 | 1 | IO::Event::AnyEvent::Wrapper->new('Idle', @_); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub set_write_polling | ||||||||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
set_write_polling: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 95 | 0 | my ($self, $new) = @_; | |||||||||||||||||||
| 96 | 0 | my $event = ${*$self}{ie_write}; | |||||||||||||||||||
| 97 | 0 | ▶ | if ($new) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | ${*$self}{ie_anyevent_write} = AnyEvent->io( | |||||||||||||||||||
| 99 | 0 | fh => ${*$self}{ie_fh}, | |||||||||||||||||||
| 100 | 0 | ▶ | cb => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 101 | # print STDERR "<Write ${*$self}{ie_desc}>"; # LOST EVENTS | ||||||||||||||||||||
| 102 | 0 | $self->ie_dispatch_write(); | |||||||||||||||||||
| 103 | }, | ||||||||||||||||||||
| 104 | 0 | poll => 'w', | |||||||||||||||||||
| 105 | ); | ||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||
| 107 | 0 | delete ${*$self}{ie_anyevent_write}; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | sub set_read_polling | ||||||||||||||||||||
| 112 | 379 | ▶ | { | ||||||||||||||||||
|
Subroutine
set_read_polling: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 113 | 379 | my ($self, $new) = @_; | |||||||||||||||||||
| 114 | 379 | my $event = ${*$self}{ie_event}; | |||||||||||||||||||
| 115 | 379 | ▶ | if ($new) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 195 | ${*$self}{ie_anyevent_read} = AnyEvent->io( | |||||||||||||||||||
| 117 | 195 | fh => ${*$self}{ie_fh}, | |||||||||||||||||||
| 118 | 218 | ▶ | cb => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 119 | # print STDERR "<READ ${*$self}{ie_desc}>"; # LOST EVENTS | ||||||||||||||||||||
| 120 | 218 | $self->ie_dispatch_read(); | |||||||||||||||||||
| 121 | }, | ||||||||||||||||||||
| 122 | 195 | poll => 'r', | |||||||||||||||||||
| 123 | ); | ||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||
| 125 | 184 | delete ${*$self}{ie_anyevent_read}; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | sub ie_register | ||||||||||||||||||||
| 130 | 195 | ▶ | { | ||||||||||||||||||
|
Subroutine
ie_register: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 131 | 195 | my ($self) = @_; | |||||||||||||||||||
| 132 | 195 | my ($fh, $fileno) = $self->SUPER::ie_register(); | |||||||||||||||||||
| 133 | 195 | $self->set_read_polling(${*$self}{ie_want_read_events} = ! ${*$self}{ie_readclosed}); | |||||||||||||||||||
| 134 | 195 | ${*$self}{ie_want_write_events} = ''; | |||||||||||||||||||
| 135 | 195 | $selves{refaddr($self)} = $self; | |||||||||||||||||||
| 136 | 0 | ▶ | print STDERR "registered ${*$self}{ie_fileno}:${*$self}{ie_desc} $self $fh ${*$self}{ie_event}\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | if $debug; | ||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | sub ie_deregister | ||||||||||||||||||||
| 141 | 165 | ▶ | { | ||||||||||||||||||
|
Subroutine
ie_deregister: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 142 | 165 | my ($self) = @_; | |||||||||||||||||||
| 143 | 165 | $self->SUPER::ie_deregister(); | |||||||||||||||||||
| 144 | 165 | delete ${*$self}{ie_anyevent_write}; | |||||||||||||||||||
| 145 | 165 | delete ${*$self}{ie_anyevent_read}; | |||||||||||||||||||
| 146 | 165 | delete $selves{refaddr($self)}; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 21 | }{package IO::Event::AnyEvent::Wrapper; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 21 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 152 | 21 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 153 | 21 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 21 | my %handlers; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | sub new | ||||||||||||||||||||
| 158 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 159 | 6 | my ($pkg, $type, $req_pkg, %param) = @_; | |||||||||||||||||||
| 160 | 6 | my ($cpkg, $file, $line, $sub) = caller; | |||||||||||||||||||
| 161 | 6 | my $desc; | |||||||||||||||||||
| 162 | { | ||||||||||||||||||||
| 163 | 21 | ▶ | no warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 164 | 6 | ▶ | $desc = $param{desc} || "\u$type\E event defined in ${cpkg}::${sub} at $file:$line"; | ||||||||||||||||||
|
Condition100%2/2
$param{'desc'} || "\u$type event defined in ${cpkg}::$sub at ${file}:$line"
MC/DC100%1/1
$param{'desc'} || "\u$type event defined in ${cpkg}::$sub at ${file}:$line"
$param{'desc'}
Truth table100%2/2
A
A= $param{'desc'}
| |||||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | 6 | ▶ | if (ref($param{cb}) eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | 2 | my ($obj, $meth) = @{$param{cb}}; | |||||||||||||||||||
| 168 | 102 | ▶ | $param{cb} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 169 | 102 | $obj->$meth(); | |||||||||||||||||||
| 170 | 2 | }; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | 6 | ▶ | $param{after} ||= $param{interval}; | ||||||||||||||||||
|
Condition66%2/3
$param{"after"} ||= $param{"interval"}
MC/DC50%1/2
$param{"after"} ||= $param{"interval"}
$param{"after"}
$param{"interval"}
Truth table66%2/3
A ||= B
A= $param{"after"}B= $param{"interval"}
| |||||||||||||||||||||
| 173 | 6 | my $self = bless { | |||||||||||||||||||
| 174 | type => lc($type), | ||||||||||||||||||||
| 175 | desc => $desc, | ||||||||||||||||||||
| 176 | param => \%param, | ||||||||||||||||||||
| 177 | }, $pkg; | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 6 | $self->start(); | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 6 | return $self; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | sub start | ||||||||||||||||||||
| 185 | 156 | ▶ | { | ||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 186 | 156 | my ($self) = @_; | |||||||||||||||||||
| 187 | 156 | $handlers{refaddr($self)} = $self; | |||||||||||||||||||
| 188 | 156 | my $type = $self->{type}; | |||||||||||||||||||
| 189 | 156 | $self->{handler} = AnyEvent->$type(%{$self->{param}}); | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | sub again | ||||||||||||||||||||
| 193 | 150 | ▶ | { | ||||||||||||||||||
|
Subroutine
again: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 194 | 150 | my ($self) = @_; | |||||||||||||||||||
| 195 | 150 | $self->start; | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | sub now | ||||||||||||||||||||
| 199 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
now: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 200 | 0 | my ($self) = @_; | |||||||||||||||||||
| 201 | 0 | $self->{param}{cb}->($self); | |||||||||||||||||||
| 202 | } | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | sub stop | ||||||||||||||||||||
| 205 | 150 | ▶ | { | ||||||||||||||||||
|
Subroutine
stop: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 206 | 150 | my ($self) = @_; | |||||||||||||||||||
| 207 | 150 | delete $self->{handler}; | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | sub cancel | ||||||||||||||||||||
| 211 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
cancel: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 212 | 0 | my ($self) = @_; | |||||||||||||||||||
| 213 | 0 | $self->stop(); | |||||||||||||||||||
| 214 | 0 | delete $handlers{refaddr($self)}; | |||||||||||||||||||
| 215 | } | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | sub is_cancelled | ||||||||||||||||||||
| 218 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
is_cancelled: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 219 | 0 | my ($self) = @_; | |||||||||||||||||||
| 220 | 0 | return ! $handlers{refaddr($self)}; | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | sub is_active | ||||||||||||||||||||
| 224 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
is_active: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 225 | 0 | my ($self) = @_; | |||||||||||||||||||
| 226 | 0 | return ! ! $self->{handler}; | |||||||||||||||||||
| 227 | } | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | sub is_running | ||||||||||||||||||||
| 230 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
is_running: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 231 | 0 | return; | |||||||||||||||||||
| 232 | } | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | sub pending | ||||||||||||||||||||
| 235 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
pending: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 236 | 0 | return; | |||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | }#end package | ||||||||||||||||||||
| 241 | 21 | 1; | |||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||