Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::MiniLoop; |
2 | |
3 | use IO::Select; |
befabdee |
4 | use Time::HiRes qw(time); |
0511910e |
5 | use Object::Remote::Logging qw( :log :dlog ); |
9e72f0cf |
6 | use Moo; |
7 | |
8 | # this is ro because we only actually set it using local in sub run |
9 | |
10 | has is_running => (is => 'ro', clearer => 'stop'); |
11 | |
12 | has _read_watches => (is => 'ro', default => sub { {} }); |
13 | has _read_select => (is => 'ro', default => sub { IO::Select->new }); |
14 | |
fbd3b8ec |
15 | has _write_watches => (is => 'ro', default => sub { {} }); |
16 | has _write_select => (is => 'ro', default => sub { IO::Select->new }); |
17 | |
befabdee |
18 | has _timers => (is => 'ro', default => sub { [] }); |
19 | |
9e72f0cf |
20 | sub pass_watches_to { |
21 | my ($self, $new_loop) = @_; |
edfc193b |
22 | log_debug { "passing watches to new run loop" }; |
9e72f0cf |
23 | foreach my $fh ($self->_read_select->handles) { |
24 | $new_loop->watch_io( |
25 | handle => $fh, |
26 | on_read_ready => $self->_read_watches->{$fh} |
27 | ); |
28 | } |
fbd3b8ec |
29 | foreach my $fh ($self->_write_select->handles) { |
30 | $new_loop->watch_io( |
31 | handle => $fh, |
32 | on_write_ready => $self->_write_watches->{$fh} |
33 | ); |
34 | } |
9e72f0cf |
35 | } |
36 | |
37 | sub watch_io { |
38 | my ($self, %watch) = @_; |
39 | my $fh = $watch{handle}; |
90a3a7f2 |
40 | Dlog_debug { "Adding IO watch for $_" } $fh; |
41 | |
42 | #TODO if this works out non-blocking support |
43 | #will need to be integrated in a way that |
44 | #is compatible with Windows which has no |
b51a8453 |
45 | #non-blocking support - see also ::ReadChannel |
5953edf6 |
46 | if (0) { |
47 | Dlog_warn { "setting file handle to be non-blocking: $_" } $fh; |
48 | use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); |
49 | my $flags = fcntl($fh, F_GETFL, 0) |
50 | or die "Can't get flags for the socket: $!\n"; |
51 | $flags = fcntl($fh, F_SETFL, $flags | O_NONBLOCK) |
52 | or die "Can't set flags for the socket: $!\n"; |
53 | } |
54 | |
9e72f0cf |
55 | if (my $cb = $watch{on_read_ready}) { |
90a3a7f2 |
56 | log_trace { "IO watcher is registering with select for reading" }; |
9e72f0cf |
57 | $self->_read_select->add($fh); |
58 | $self->_read_watches->{$fh} = $cb; |
59 | } |
fbd3b8ec |
60 | if (my $cb = $watch{on_write_ready}) { |
90a3a7f2 |
61 | log_trace { "IO watcher is registering with select for writing" }; |
fbd3b8ec |
62 | $self->_write_select->add($fh); |
63 | $self->_write_watches->{$fh} = $cb; |
64 | } |
498c4ad5 |
65 | return; |
9e72f0cf |
66 | } |
67 | |
68 | sub unwatch_io { |
69 | my ($self, %watch) = @_; |
70 | my $fh = $watch{handle}; |
2d81cf18 |
71 | Dlog_debug { "Removing IO watch for $_" } $fh; |
9e72f0cf |
72 | if ($watch{on_read_ready}) { |
2d81cf18 |
73 | log_trace { "IO watcher is removing read from select()" }; |
9e72f0cf |
74 | $self->_read_select->remove($fh); |
75 | delete $self->_read_watches->{$fh}; |
76 | } |
fbd3b8ec |
77 | if ($watch{on_write_ready}) { |
2d81cf18 |
78 | log_trace { "IO watcher is removing write from select()" }; |
fbd3b8ec |
79 | $self->_write_select->remove($fh); |
80 | delete $self->_write_watches->{$fh}; |
81 | } |
befabdee |
82 | return; |
83 | } |
84 | |
8ed52376 |
85 | sub _sort_timers { |
86 | my ($self, @new) = @_; |
87 | my $timers = $self->_timers; |
88 | |
89 | log_trace { "Sorting timers" }; |
90 | |
91 | @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, @new; |
92 | return; |
93 | } |
94 | |
befabdee |
95 | sub watch_time { |
96 | my ($self, %watch) = @_; |
8ed52376 |
97 | my $at; |
98 | |
99 | Dlog_trace { "watch_time() invoked with $_" } \%watch; |
100 | |
101 | if (exists($watch{every})) { |
102 | $at = time() + $watch{every}; |
103 | } elsif (exists($watch{after})) { |
104 | $at = time() + $watch{after}; |
105 | } elsif (exists($watch{at})) { |
106 | $at = $watch{at}; |
107 | } else { |
108 | die "watch_time requires every, after or at"; |
109 | } |
110 | |
befabdee |
111 | die "watch_time requires code" unless my $code = $watch{code}; |
112 | my $timers = $self->_timers; |
8ed52376 |
113 | my $new = [ $at => $code, $watch{every} ]; |
114 | $self->_sort_timers($new); |
69aaad21 |
115 | log_debug { "Created new timer with id '$new' that expires at '$at'" }; |
befabdee |
116 | return "$new"; |
117 | } |
118 | |
119 | sub unwatch_time { |
120 | my ($self, $id) = @_; |
edfc193b |
121 | log_debug { "Removing timer with id of '$id'" }; |
befabdee |
122 | @$_ = grep !($_ eq $id), @$_ for $self->_timers; |
123 | return; |
9e72f0cf |
124 | } |
125 | |
0511910e |
126 | sub _next_timer_expires_delay { |
127 | my ($self) = @_; |
128 | my $timers = $self->_timers; |
23361929 |
129 | #undef means no timeout, select only returns |
0511910e |
130 | #when data is ready - when the system |
131 | #deadlocks the chatter from the timeout in |
132 | #select clogs up the logs |
2d81cf18 |
133 | #TODO should make this an attribute |
0511910e |
134 | my $delay_max = undef; |
135 | |
136 | return $delay_max unless @$timers; |
137 | my $duration = $timers->[0]->[0] - time; |
138 | |
139 | log_trace { "next timer fires in '$duration' seconds " }; |
140 | |
141 | if ($duration < 0) { |
142 | $duration = 0; |
23361929 |
143 | } elsif (defined $delay_max && $duration > $delay_max) { |
144 | $duration = $delay_max; |
0511910e |
145 | } |
23361929 |
146 | |
0511910e |
147 | return $duration; |
148 | } |
149 | |
9e72f0cf |
150 | sub loop_once { |
151 | my ($self) = @_; |
152 | my $read = $self->_read_watches; |
fbd3b8ec |
153 | my $write = $self->_write_watches; |
5953edf6 |
154 | our $Loop_Entered = 1; |
0511910e |
155 | my $read_count = 0; |
156 | my $write_count = 0; |
edfc193b |
157 | my @c = caller; |
0511910e |
158 | my $wait_time = $self->_next_timer_expires_delay; |
5953edf6 |
159 | log_trace { sprintf("Run loop: loop_once() has been invoked by $c[1]:$c[2] with read:%i write:%i select timeout:%s", |
0511910e |
160 | scalar(keys(%$read)), scalar(keys(%$write)), defined $wait_time ? $wait_time : 'indefinite' ) }; |
fbd3b8ec |
161 | my ($readable, $writeable) = IO::Select->select( |
0511910e |
162 | $self->_read_select, $self->_write_select, undef, $wait_time |
163 | ); |
5953edf6 |
164 | log_trace { |
0511910e |
165 | my $readable_count = defined $readable ? scalar(@$readable) : 0; |
166 | my $writable_count = defined $writeable ? scalar(@$writeable) : 0; |
167 | "Run loop: select returned readable:$readable_count writeable:$writable_count"; |
edfc193b |
168 | }; |
9e72f0cf |
169 | # I would love to trap errors in the select call but IO::Select doesn't |
170 | # differentiate between an error and a timeout. |
171 | # -- no, love, mst. |
5953edf6 |
172 | |
173 | local $Loop_Entered; |
174 | |
edfc193b |
175 | log_trace { "Reading from all ready filehandles" }; |
9e72f0cf |
176 | foreach my $fh (@$readable) { |
0511910e |
177 | next unless $read->{$fh}; |
178 | $read_count++; |
179 | $read->{$fh}(); |
5953edf6 |
180 | last if $Loop_Entered; |
fbd3b8ec |
181 | } |
edfc193b |
182 | log_trace { "Writing to all ready filehandles" }; |
fbd3b8ec |
183 | foreach my $fh (@$writeable) { |
0511910e |
184 | next unless $write->{$fh}; |
185 | $write_count++; |
186 | $write->{$fh}(); |
5953edf6 |
187 | last if $Loop_Entered; |
9e72f0cf |
188 | } |
b51a8453 |
189 | |
0511910e |
190 | log_trace { "Read from $read_count filehandles; wrote to $write_count filehandles" }; |
befabdee |
191 | my $timers = $self->_timers; |
192 | my $now = time(); |
edfc193b |
193 | log_trace { "Checking timers" }; |
befabdee |
194 | while (@$timers and $timers->[0][0] <= $now) { |
8ed52376 |
195 | my $active = $timers->[0]; |
69aaad21 |
196 | Dlog_debug { "Found timer that needs to be executed: '$active'" }; |
8ed52376 |
197 | |
198 | if (defined($active->[2])) { |
199 | #handle the case of an 'every' timer |
200 | $active->[0] = time() + $active->[2]; |
201 | Dlog_trace { "scheduling timer for repeat execution at $_"} $active->[0]; |
202 | $self->_sort_timers; |
203 | } else { |
204 | #it doesn't repeat again so get rid of it |
205 | shift(@$timers); |
206 | } |
207 | |
208 | #execute the timer |
209 | $active->[1]->(); |
210 | |
5953edf6 |
211 | last if $Loop_Entered; |
befabdee |
212 | } |
8ed52376 |
213 | |
5953edf6 |
214 | log_trace { "Run loop: single loop is completed" }; |
befabdee |
215 | return; |
9e72f0cf |
216 | } |
217 | |
6c597351 |
218 | sub want_run { |
219 | my ($self) = @_; |
0511910e |
220 | Dlog_debug { "Run loop: Incrimenting want_running, is now $_" } |
221 | ++$self->{want_running}; |
6c597351 |
222 | } |
223 | |
224 | sub run_while_wanted { |
225 | my ($self) = @_; |
2d81cf18 |
226 | log_debug { my $wr = $self->{want_running}; "Run loop: run_while_wanted() invoked; want_running: $wr" }; |
6c597351 |
227 | $self->loop_once while $self->{want_running}; |
edfc193b |
228 | log_debug { "Run loop: run_while_wanted() completed" }; |
befabdee |
229 | return; |
6c597351 |
230 | } |
231 | |
232 | sub want_stop { |
233 | my ($self) = @_; |
0511910e |
234 | if (! $self->{want_running}) { |
235 | log_debug { "Run loop: want_stop() was called but want_running was not true" }; |
236 | return; |
237 | } |
238 | Dlog_debug { "Run loop: decrimenting want_running, is now $_" } |
239 | --$self->{want_running}; |
6c597351 |
240 | } |
241 | |
9e72f0cf |
242 | sub run { |
243 | my ($self) = @_; |
5953edf6 |
244 | log_trace { "Run loop: run() invoked" }; |
9e72f0cf |
245 | local $self->{is_running} = 1; |
246 | while ($self->is_running) { |
247 | $self->loop_once; |
248 | } |
5953edf6 |
249 | log_trace { "Run loop: run() completed" }; |
befabdee |
250 | return; |
9e72f0cf |
251 | } |
252 | |
253 | 1; |