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