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