blib/lib/Mojo/Reactor/POE.pm

filter: Statementstmt 99.3% 162 / 163 Branchbran 77.5% 45 / 58 Conditioncond 44.8% 13 / 29 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 100% 33 / 33 Podpod 100% 8 / 8 totaltotal 85.3% 263 / 308 CC 41 SCAR 12.0
CC
41
Cov
88%
remove
8.2
_event_set_io
5.1
_event_adjust_timer
4.6
CRAP 3.3
↑ Summary
1 package Mojo::Reactor::POE;
2  
3 1 ▶use POE; # Loaded early to avoid event loop confusion
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶BEGIN { POE::Kernel->run } # silence run() warning
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mojo::Base 'Mojo::Reactor::Poll';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶$ENV{MOJO_REACTOR} ||= 'Mojo::Reactor::POE';
Condition50%1/2
$ENV{"MOJO_REACTOR"} ||= "Mojo::Reactor::POE"
l!l
10
MC/DC0%0/1
$ENV{"MOJO_REACTOR"} ||= "Mojo::Reactor::POE"
$ENV{"MOJO_REACTOR"}
Truth table50%1/2
A
A= $ENV{"MOJO_REACTOR"}
Aresult
11
00
9  
10 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mojo::Util qw(md5_sum steady_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use constant { POE_IO_READ => 0, POE_IO_WRITE => 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant DEBUG => $ENV{MOJO_REACTOR_POE_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_REACTOR_POE_DEBUG'} || 0
l!l
01
MC/DC0%0/1
$ENV{'MOJO_REACTOR_POE_DEBUG'} || 0
$ENV{'MOJO_REACTOR_POE_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_REACTOR_POE_DEBUG'}
Aresult
11
00
16  
17 1 our $VERSION = '1.001';
18  
19 1 my $POE;
20  
21 # We have to fall back to Mojo::Reactor::Poll, since POE::Kernel is unique
22 6 ▶sub new { $POE++ ? Mojo::Reactor::Poll->new : shift->SUPER::new }
Subroutine new: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$POE++ ? : 3 3 6
23  
24 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
25 2 my $self = shift;
26 2 $self->reset; # Close session
27 2 undef $POE;
28 }
29  
30 9 ▶sub again {
Subroutine again: called CC=4 SCAR=13.9
Pod documented
31 9 my ($self, $id, $after) = @_;
32 9 ▶ croak 'Timer not active' unless my $timer = $self->{timers}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless my $timer = $self->{"timers"}{$id} 1 8 9
33 8 ▶ $timer->{after} = $after if defined $after;
Branch: 1 branch
BranchTrueFalseTotal
if defined $after 2 6 8
34 8 $timer->{time} = steady_time + $timer->{after};
35 # If session doesn't exist, the time will be set when it starts
36 8 ▶ $self->_session_call(mojo_adjust_timer => $id) if $self->_session_exists;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_session_exists 8 0 8
37 }
38  
39 7838 ▶sub one_tick {
Subroutine one_tick: called CC=4 SCAR=13.9
Pod documented
40 7838 my $self = shift;
41 7838 $self->_init_session;
42
43 # Stop automatically if there is nothing to watch
44 7838 ▶ return $self->stop unless keys %{$self->{timers}} || keys %{$self->{io}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"timers"};} or keys %{$$self{"io"};} 5 7833 7838
Condition100%3/3
keys %{$$self{"timers"};} or keys %{$$self{"io"};}
l!l&&r!l&&!r
783215
MC/DC100%2/2
keys %{$$self{"timers"};} or keys %{$$self{"io"};}
keys %{$$self{"timers"};} keys %{$$self{"io"};}
Truth table100%3/3
A or B
A= keys %{$$self{"timers"};}B= keys %{$$self{"io"};}
ABresult
1X1
011
000
45
46 # Just one tick
47 7833 ▶ local $self->{running} = 1 unless $self->{running};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"running"} 55 7778 7833
48
49 7833 POE::Kernel->run_one_timeslice;
50 }
51  
52 4 ▶sub recurring { shift->_timer(1, @_) }
Subroutine recurring: called CC=1 SCAR=0.0
Pod documented
53  
54 11 ▶sub remove {
Subroutine remove: called CC=8 SCAR=21.1
Pod documented
55 11 my ($self, $remove) = @_;
56 11 ▶ return unless defined $remove;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remove 0 11 11
57 11 ▶ if (ref $remove) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $remove) { } 5 6 11
58 5 ▶ my $fileno = fileno($remove) // croak 'Handle is closed';
Condition50%1/2
fileno $remove // croak("Handle is closed")
l!l
50
MC/DC0%0/1
fileno $remove // croak("Handle is closed")
fileno $remove
Truth table50%1/2
A
A= fileno $remove
Aresult
11
00
59 5 ▶ if (exists $self->{io}{$fileno}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"io"}{$fileno}) 3 2 5
60 3 warn "-- Removed IO watcher for $fileno\n" if DEBUG;
61 # If session doesn't exist, the watcher won't be re-added
62 3 ▶ $self->_session_call(mojo_clear_io => $fileno) if $self->_session_exists;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_session_exists 3 0 3
63 }
64 5 return !!delete $self->{io}{$fileno};
65 } else {
66 6 ▶ if (exists $self->{timers}{$remove}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"timers"}{$remove}) 5 1 6
67 5 warn "-- Removed timer $remove\n" if DEBUG;
68 # If session doesn't exist, the timer won't be re-added
69 5 ▶ $self->_session_call(mojo_clear_timer => $remove) if $self->_session_exists;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_session_exists 5 0 5
70 }
71 6 return !!delete $self->{timers}{$remove};
72 }
73 }
74  
75 6 ▶sub reset {
Subroutine reset: called CC=3 SCAR=11.0
Pod documented
76 6 my $self = shift;
77 # If session doesn't exist, watchers won't be re-added
78 6 ▶ if ($self->_session_exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_session_exists) 3 3 6
79 3 $self->_session_call('mojo_clear_timers');
80 3 $self->_session_call(mojo_clear_io => $_) for keys %{$self->{io}};
81 }
82 6 $self->SUPER::reset;
83 }
84  
85 32 ▶sub timer { shift->_timer(0, @_) }
Subroutine timer: called CC=1 SCAR=0.0
Pod documented
86  
87 25 ▶sub watch {
Subroutine watch: called CC=2 SCAR=6.9
Pod documented
88 25 my ($self, $handle, $read, $write) = @_;
89  
90 25 my $fileno = fileno $handle;
91 25 ▶ croak 'I/O watcher not active' unless my $io = $self->{io}{$fileno};
Branch: 1 branch
BranchTrueFalseTotal
unless my $io = $self->{"io"}{$fileno} 1 24 25
92 24 $io->{handle} = $handle;
93 24 $io->{read} = $read;
94 24 $io->{write} = $write;
95
96 24 warn "-- Set IO watcher for $fileno\n" if DEBUG;
97
98 24 $self->_init_session->_session_call(mojo_set_io => $fileno);
99
100 24 return $self;
101 }
102  
103 36 ▶sub _id {
Subroutine _id: called CC=2 SCAR=7.0
104 36 my $self = shift;
105 36 my $id;
106 36 ▶ do { $id = md5_sum 't' . steady_time . rand } while $self->{timers}{$id};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"timers"}{$id} and do { do { $id = md5_sum('t' . &steady_time() . (rand)) } } 0 36 36
107 36 return $id;
108 }
109  
110 36 ▶sub _timer {
Subroutine _timer: called CC=1 SCAR=0.0
111 36 my ($self, $recurring, $after, $cb) = @_;
112
113 36 my $id = $self->_id;
114 36 my $timer = $self->{timers}{$id} = {
115 cb => $cb,
116 after => $after,
117 recurring => $recurring,
118 time => steady_time + $after,
119 };
120
121 36 if (DEBUG) {
122 my $is_recurring = $recurring ? ' (recurring)' : '';
123 warn "-- Set timer $id after $after seconds$is_recurring\n";
124 }
125
126 36 $self->_init_session->_session_call(mojo_set_timer => $id);
127
128 36 return $id;
129 }
130  
131 8513 ▶sub _try {
Subroutine _try: called CC=2 SCAR=6.9
132 8513 my ($self, $what, $cb) = (shift, shift, shift);
133 8513 ▶ eval { $self->$cb(@_); 1 } or $self->emit(error => "$what failed: $@");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->$cb(@_); 1 } } or $self->emit("error", "$what failed: $@") 8512 1 8513
134 }
135  
136 8236 ▶sub _init_session {
Subroutine _init_session: called CC=2 SCAR=6.9
137 8236 my $self = shift;
138 8236 ▶ unless ($self->_session_exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_session_exists) 9 8227 8236
139 9 my $session = POE::Session->create(
140 inline_states => {
141 _start => \&_event_start,
142 _stop => \&_event_stop,
143 mojo_set_timer => \&_event_set_timer,
144 mojo_clear_timer => \&_event_clear_timer,
145 mojo_adjust_timer => \&_event_adjust_timer,
146 mojo_clear_timers => \&_event_clear_timers,
147 mojo_set_io => \&_event_set_io,
148 mojo_clear_io => \&_event_clear_io,
149 mojo_timer => \&_event_timer,
150 mojo_io => \&_event_io,
151 },
152 heap => { mojo_reactor => $self },
153 );
154 9 weaken $session->get_heap()->{mojo_reactor};
155 9 $self->{session_id} = $session->ID;
156 }
157 8236 return $self;
158 }
159  
160 8258 ▶sub _session_exists {
Subroutine _session_exists: called CC=1 SCAR=0.0
161 8258 my $self = shift;
162 8258 return defined $self->{session_id};
163 }
164  
165 420 ▶sub _session_call {
Subroutine _session_call: called CC=2 SCAR=7.0
166 420 my $self = shift;
167 420 ▶ croak 'Session call on nonexistent session' unless defined $self->{session_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"session_id"} 0 420 420
168 420 POE::Kernel->call($self->{session_id}, @_);
169 420 return $self;
170 }
171  
172 9 ▶sub _event_start {
Subroutine _event_start: called CC=1 SCAR=0.0
173 9 my $self = $_[HEAP]{mojo_reactor};
174 9 my $session = $_[SESSION];
175
176 9 warn "-- POE session started\n" if DEBUG;
177 }
178  
179 8 ▶sub _event_stop {
Subroutine _event_stop: called CC=2 SCAR=7.1
180 8 my $self = $_[HEAP]{mojo_reactor};
181
182 8 warn "-- POE session stopped\n" if DEBUG;
183
184 8 ▶ delete $self->{session_id} if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 8 0 8
185 }
186  
187 374 ▶sub _event_set_timer {
Subroutine _event_set_timer: called CC=3 SCAR=11.3
188 374 my $self = $_[HEAP]{mojo_reactor};
189 374 my $id = $_[ARG0];
190 return unless exists $self->{timers}{$id}
191 374 ▶ and defined $self->{timers}{$id}{time};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"time"} 0 374 374
Condition33%1/3
exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"time"}
!ll&&!rl&&r
00374
MC/DC0%0/2
exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"time"}
exists $self->{"timers"}{$id} defined $self->{"timers"}{$id}{"time"}
Truth table33%1/3
A and B
A= exists $self->{"timers"}{$id}B= defined $self->{"timers"}{$id}{"time"}
ABresult
0X0
100
111
192 374 my $timer = $self->{timers}{$id};
193 374 my $delay_time = $timer->{time} - steady_time;
194 374 my $poe_id = POE::Kernel->delay_set(mojo_timer => $delay_time, $id);
195 374 $timer->{poe_id} = $poe_id;
196
197 374 warn "-- Set POE timer $poe_id in $delay_time seconds\n" if DEBUG;
198 }
199  
200 5 ▶sub _event_clear_timer {
Subroutine _event_clear_timer: called CC=3 SCAR=11.6
201 5 my $self = $_[HEAP]{mojo_reactor};
202 5 my $id = $_[ARG0];
203 return unless exists $self->{timers}{$id}
204 5 ▶ and defined $self->{timers}{$id}{poe_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"poe_id"} 0 5 5
Condition33%1/3
exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"poe_id"}
!ll&&!rl&&r
005
MC/DC0%0/2
exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"poe_id"}
exists $self->{"timers"}{$id} defined $self->{"timers"}{$id}{"poe_id"}
Truth table33%1/3
A and B
A= exists $self->{"timers"}{$id}B= defined $self->{"timers"}{$id}{"poe_id"}
ABresult
0X0
100
111
205 5 my $timer = $self->{timers}{$id};
206 5 POE::Kernel->alarm_remove($timer->{poe_id});
207
208 5 warn "-- Cleared POE timer $timer->{poe_id}\n" if DEBUG;
209 }
210  
211 8 ▶sub _event_adjust_timer {
Subroutine _event_adjust_timer: called CC=4 SCAR=15.2
212 8 my $self = $_[HEAP]{mojo_reactor};
213 8 my $id = $_[ARG0];
214 return unless exists $self->{timers}{$id}
215 and defined $self->{timers}{$id}{time}
216 8 ▶ and defined $self->{timers}{$id}{poe_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"time"} and defined $self->{"timers"}{$id}{"poe_id"} 0 8 8
Condition33%2/6
exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"time"} and defined $self->{"timers"}{$id}{"poe_id"}
!ll&&!rl&&r
008
exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"time"}
!ll&&!rl&&r
008
MC/DC0%0/3
exists $self->{"timers"}{$id} and defined $self->{"timers"}{$id}{"time"} and defined $self->{"timers"}{$id}{"poe_id"}
exists $self->{"timers"}{$id} defined $self->{"timers"}{$id}{"time"} defined $self->{"timers"}{$id}{"poe_id"}
Truth table25%1/4
A and B and C
A= exists $self->{"timers"}{$id}B= defined $self->{"timers"}{$id}{"time"}C= defined $self->{"timers"}{$id}{"poe_id"}
ABCresult
0XX0
10X0
1100
1111
217 8 my $timer = $self->{timers}{$id};
218 8 my $new_delay = $timer->{time} - steady_time;
219 8 POE::Kernel->delay_adjust($timer->{poe_id}, $new_delay);
220
221 8 warn "-- Adjusted POE timer $timer->{poe_id} to $new_delay seconds\n"
222 if DEBUG;
223 }
224  
225 3 ▶sub _event_clear_timers {
Subroutine _event_clear_timers: called CC=2 SCAR=7.1
226 3 ▶ my $self = $_[HEAP]{mojo_reactor} // return;
Condition50%1/2
$_[3]{"mojo_reactor"} // (return)
l!l
30
MC/DC0%0/1
$_[3]{"mojo_reactor"} // (return)
$_[3]{"mojo_reactor"}
Truth table50%1/2
A
A= $_[3]{"mojo_reactor"}
Aresult
11
00
227 3 POE::Kernel->alarm_remove_all;
228
229 3 warn "-- Cleared all POE timers\n" if DEBUG;
230 }
231  
232 24 ▶sub _event_set_io {
Subroutine _event_set_io: called CC=5 SCAR=16.3
233 24 my $self = $_[HEAP]{mojo_reactor};
234 24 my $fd = $_[ARG0];
235 return unless exists $self->{io}{$fd}
236 24 ▶ and defined $self->{io}{$fd}{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"io"}{$fd} and defined $self->{"io"}{$fd}{"handle"} 0 24 24
Condition33%1/3
exists $self->{"io"}{$fd} and defined $self->{"io"}{$fd}{"handle"}
!ll&&!rl&&r
0024
MC/DC0%0/2
exists $self->{"io"}{$fd} and defined $self->{"io"}{$fd}{"handle"}
exists $self->{"io"}{$fd} defined $self->{"io"}{$fd}{"handle"}
Truth table33%1/3
A and B
A= exists $self->{"io"}{$fd}B= defined $self->{"io"}{$fd}{"handle"}
ABresult
0X0
100
111
237 24 my $io = $self->{io}{$fd};
238 24 ▶ if ($io->{read}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io->{'read'}) { } 19 5 24
239 19 POE::Kernel->select_read($io->{handle}, 'mojo_io');
240 } else {
241 5 POE::Kernel->select_read($io->{handle});
242 }
243 24 ▶ if ($io->{write}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io->{'write'}) { } 17 7 24
244 17 POE::Kernel->select_write($io->{handle}, 'mojo_io');
245 } else {
246 7 POE::Kernel->select_write($io->{handle});
247 }
248
249 24 warn "-- Set POE IO watcher for $fd " .
250 "with read: $io->{read}, write: $io->{write}\n" if DEBUG;
251 }
252  
253 6 ▶sub _event_clear_io {
Subroutine _event_clear_io: called CC=3 SCAR=11.3
254 6 my $self = $_[HEAP]{mojo_reactor};
255 6 my $fd = $_[ARG0];
256 return unless exists $self->{io}{$fd}
257 6 ▶ and defined $self->{io}{$fd}{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"io"}{$fd} and defined $self->{"io"}{$fd}{"handle"} 0 6 6
Condition33%1/3
exists $self->{"io"}{$fd} and defined $self->{"io"}{$fd}{"handle"}
!ll&&!rl&&r
006
MC/DC0%0/2
exists $self->{"io"}{$fd} and defined $self->{"io"}{$fd}{"handle"}
exists $self->{"io"}{$fd} defined $self->{"io"}{$fd}{"handle"}
Truth table33%1/3
A and B
A= exists $self->{"io"}{$fd}B= defined $self->{"io"}{$fd}{"handle"}
ABresult
0X0
100
111
258 6 my $io = $self->{io}{$fd};
259 6 POE::Kernel->select_read($io->{handle});
260 6 POE::Kernel->select_write($io->{handle});
261 6 delete $io->{handle};
262
263 6 warn "-- Cleared POE IO watcher for $fd\n" if DEBUG;
264 }
265  
266 364 ▶sub _event_timer {
Subroutine _event_timer: called CC=2 SCAR=6.9
267 364 my $self = $_[HEAP]{mojo_reactor};
268 364 my $id = $_[ARG0];
269
270 364 my $timer = $self->{timers}{$id};
271 364 warn "-- Event fired for timer $id\n" if DEBUG;
272 364 ▶ if ($timer->{recurring}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer->{'recurring'}) { } 338 26 364
273 338 $timer->{time} = steady_time + $timer->{after};
274 338 $self->_init_session->_session_call(mojo_set_timer => $id);
275 } else {
276 26 delete $self->{timers}{$id};
277 }
278
279 364 $self->_try('Timer', $timer->{cb});
280 }
281  
282 8149 ▶sub _event_io {
Subroutine _event_io: called CC=3 SCAR=11.2
283 8149 my $self = $_[HEAP]{mojo_reactor};
284 8149 my ($handle, $mode) = @_[ARG0, ARG1];
285
286 8149 my $io = $self->{io}{fileno $handle};
287 #warn "-- Event fired for IO watcher ".fileno($handle)."\n" if DEBUG;
288 8149 ▶ if ($mode == POE_IO_READ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode == 0) { } 7491 658 8149
elsif ($mode == 1) { } 658 0 658
289 7491 $self->_try('I/O watcher', $io->{cb}, 0);
290 } elsif ($mode == POE_IO_WRITE) {
291 658 $self->_try('I/O watcher', $io->{cb}, 1);
292 } else {
293 0 die "Unknown POE I/O mode $mode";
294 }
295 }
296  
297 1 1;
298  
299 =head1 NAME
300  
301 Mojo::Reactor::POE - POE backend for Mojo::Reactor
302  
303 =head1 SYNOPSIS
304  
305 use Mojo::Reactor::POE;
306  
307 # Watch if handle becomes readable or writable
308 my $reactor = Mojo::Reactor::POE->new;
309 $reactor->io($first => sub {
310 my ($reactor, $writable) = @_;
311 say $writable ? 'First handle is writable' : 'First handle is readable';
312 });
313  
314 # Change to watching only if handle becomes writable
315 $reactor->watch($first, 0, 1);
316  
317 # Turn file descriptor into handle and watch if it becomes readable
318 my $second = IO::Handle->new_from_fd($fd, 'r');
319 $reactor->io($second => sub {
320 my ($reactor, $writable) = @_;
321 say $writable ? 'Second handle is writable' : 'Second handle is readable';
322 })->watch($second, 1, 0);
323  
324 # Add a timer
325 $reactor->timer(15 => sub {
326 my $reactor = shift;
327 $reactor->remove($first);
328 $reactor->remove($second);
329 say 'Timeout!';
330 });
331  
332 # Start reactor if necessary
333 $reactor->start unless $reactor->is_running;
334  
335 # Or in an application using Mojo::IOLoop
336 use POE qw(Loop::IO_Poll);
337 use Mojo::Reactor::POE;
338 use Mojo::IOLoop;
339
340 # Or in a Mojolicious application
341 $ MOJO_REACTOR=Mojo::Reactor::POE POE_EVENT_LOOP=POE::Loop::IO_Poll hypnotoad script/myapp
342  
343 =head1 DESCRIPTION
344  
345 L<Mojo::Reactor::POE> is an event reactor for L<Mojo::IOLoop> that uses L<POE>.
346 The usage is exactly the same as other L<Mojo::Reactor> implementations such as
347 L<Mojo::Reactor::Poll>. L<Mojo::Reactor::POE> will be used as the default
348 backend for L<Mojo::IOLoop> if it is loaded before L<Mojo::IOLoop> or any
349 module using the loop. However, when invoking a L<Mojolicious> application
350 through L<morbo> or L<hypnotoad>, the reactor must be set as the default by
351 setting the C<MOJO_REACTOR> environment variable to C<Mojo::Reactor::POE>.
352  
353 Note that if L<POE> detects multiple potential event loops it will fail. This
354 includes L<IO::Poll> and L<Mojo::IOLoop> (loaded by L<Mojolicious>) if the
355 appropriate L<POE::Loop> modules are installed. To avoid this, load L<POE>
356 before any L<Mojolicious> module, or specify the L<POE> event loop explicitly.
357 This means that for L<Mojolicious> applications invoked through L<morbo> or
358 L<hypnotoad>, the L<POE> event loop may also need to be set in the environment.
359 See L<POE::Kernel/"Using POE with Other Event Loops">.
360  
361 =head1 EVENTS
362  
363 L<Mojo::Reactor::POE> inherits all events from L<Mojo::Reactor::Poll>.
364  
365 =head1 METHODS
366  
367 L<Mojo::Reactor::POE> inherits all methods from L<Mojo::Reactor::Poll> and
368 implements the following new ones.
369  
370 =head2 new
371  
372 my $reactor = Mojo::Reactor::POE->new;
373  
374 Construct a new L<Mojo::Reactor::POE> object.
375  
376 =head2 again
377  
378 $reactor->again($id);
379 $reactor->again($id, 0.5);
380  
381 Restart timer and optionally change the invocation time. Note that this method
382 requires an active timer.
383  
384 =head2 one_tick
385  
386 $reactor->one_tick;
387  
388 Run reactor until an event occurs or no events are being watched anymore. Note
389 that this method can recurse back into the reactor, so you need to be careful.
390  
391 # Don't block longer than 0.5 seconds
392 my $id = $reactor->timer(0.5 => sub {});
393 $reactor->one_tick;
394 $reactor->remove($id);
395  
396 =head2 recurring
397  
398 my $id = $reactor->recurring(0.25 => sub {...});
399  
400 Create a new recurring timer, invoking the callback repeatedly after a given
401 amount of time in seconds.
402  
403 =head2 remove
404  
405 my $bool = $reactor->remove($handle);
406 my $bool = $reactor->remove($id);
407  
408 Remove handle or timer.
409  
410 =head2 reset
411  
412 $reactor->reset;
413  
414 Remove all handles and timers.
415  
416 =head2 timer
417  
418 my $id = $reactor->timer(0.5 => sub {...});
419  
420 Create a new timer, invoking the callback after a given amount of time in
421 seconds.
422  
423 =head2 watch
424  
425 $reactor = $reactor->watch($handle, $readable, $writable);
426  
427 Change I/O events to watch handle for with true and false values. Note that
428 this method requires an active I/O watcher.
429  
430 # Watch only for readable events
431 $reactor->watch($handle, 1, 0);
432  
433 # Watch only for writable events
434 $reactor->watch($handle, 0, 1);
435  
436 # Watch for readable and writable events
437 $reactor->watch($handle, 1, 1);
438  
439 # Pause watching for events
440 $reactor->watch($handle, 0, 0);
441  
442 =head1 CAVEATS
443  
444 When using L<Mojo::IOLoop> with L<POE>, the event loop must be controlled by
445 L<Mojo::IOLoop> or L<Mojo::Reactor::POE>, such as with the methods
446 L<Mojo::Reactor::Poll/"start">, L<Mojo::Reactor::Poll/"stop">, and
447 L</"one_tick">. Starting or stopping the event loop through L<POE> will not
448 provide required functionality to L<Mojo::IOLoop> applications.
449  
450 Externally-added sessions will not keep the L<Mojo::IOLoop> running if
451 L<Mojo::Reactor::POE> has nothing left to watch. This can be worked around by
452 adding a recurring timer for the reactor to watch.
453  
454 =head1 BUGS
455  
456 L<POE> has a complex session system which may lead to bugs when used in this
457 manner. Report any issues on the public bugtracker.
458  
459 =head1 AUTHOR
460  
461 Dan Book, C<dbook@cpan.org>
462  
463 =head1 COPYRIGHT AND LICENSE
464  
465 Copyright 2015, Dan Book.
466  
467 This library is free software; you may redistribute it and/or modify it under
468 the terms of the Artistic License version 2.0.
469  
470 =head1 SEE ALSO
471  
472 L<Mojolicious>, L<Mojo::IOLoop>, L<POE>
↑ Summary