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