1 package Object::Remote::MiniLoop;
4 use Time::HiRes qw(time);
5 use Object::Remote::Logging qw( :log );
8 # this is ro because we only actually set it using local in sub run
10 has is_running => (is => 'ro', clearer => 'stop');
12 has _read_watches => (is => 'ro', default => sub { {} });
13 has _read_select => (is => 'ro', default => sub { IO::Select->new });
15 has _write_watches => (is => 'ro', default => sub { {} });
16 has _write_select => (is => 'ro', default => sub { IO::Select->new });
18 has _timers => (is => 'ro', default => sub { [] });
21 my ($self, $new_loop) = @_;
22 log_debug { "passing watches to new run loop" };
23 foreach my $fh ($self->_read_select->handles) {
26 on_read_ready => $self->_read_watches->{$fh}
29 foreach my $fh ($self->_write_select->handles) {
32 on_write_ready => $self->_write_watches->{$fh}
38 my ($self, %watch) = @_;
39 my $fh = $watch{handle};
40 log_debug { my $type = ref($fh); "Adding watch for ref of type '$type'" };
41 if (my $cb = $watch{on_read_ready}) {
42 $self->_read_select->add($fh);
43 $self->_read_watches->{$fh} = $cb;
45 if (my $cb = $watch{on_write_ready}) {
46 $self->_write_select->add($fh);
47 $self->_write_watches->{$fh} = $cb;
53 my ($self, %watch) = @_;
54 my $fh = $watch{handle};
55 log_debug { my $type = ref($fh); "Removing watch for ref of type '$type'" };
56 if ($watch{on_read_ready}) {
57 $self->_read_select->remove($fh);
58 delete $self->_read_watches->{$fh};
60 if ($watch{on_write_ready}) {
61 $self->_write_select->remove($fh);
62 delete $self->_write_watches->{$fh};
68 my ($self, %watch) = @_;
69 my $at = $watch{at} || do {
70 die "watch_time requires at or after" unless my $after = $watch{after};
73 die "watch_time requires code" unless my $code = $watch{code};
74 my $timers = $self->_timers;
75 my $new = [ $at => $code ];
76 @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, $new;
77 log_debug { "Created new timer with id of '$new' that expires at '$at'" };
83 log_debug { "Removing timer with id of '$id'" };
84 @$_ = grep !($_ eq $id), @$_ for $self->_timers;
90 my $read = $self->_read_watches;
91 my $write = $self->_write_watches;
93 log_trace { sprintf("Run loop: loop_once() has been invoked by $c[1]:$c[2] with read:%i write:%i", scalar(keys(%$read)), scalar(keys(%$write))) };
94 my ($readable, $writeable) = IO::Select->select(
95 $self->_read_select, $self->_write_select, undef, 0.5
98 my $readable_count = defined $readable ? scalar(@$readable) : 0;
99 my $writable_count = defined $writeable ? scalar(@$writeable) : 0;
100 "run loop has readable:$readable_count writeable:$writable_count";
102 # I would love to trap errors in the select call but IO::Select doesn't
103 # differentiate between an error and a timeout.
105 log_trace { "Reading from all ready filehandles" };
106 foreach my $fh (@$readable) {
107 $read->{$fh}() if $read->{$fh};
109 log_trace { "Writing to all ready filehandles" };
110 foreach my $fh (@$writeable) {
111 $write->{$fh}() if $write->{$fh};
113 my $timers = $self->_timers;
115 log_trace { "Checking timers" };
116 while (@$timers and $timers->[0][0] <= $now) {
117 (shift @$timers)->[1]->();
119 log_debug { "Run loop: single loop is completed" };
125 $self->{want_running}++;
128 sub run_while_wanted {
130 log_debug { "Run loop: run_while_wanted() invoked" };
131 $self->loop_once while $self->{want_running};
132 log_debug { "Run loop: run_while_wanted() completed" };
138 $self->{want_running}-- if $self->{want_running};
143 log_info { "Run loop: run() invoked" };
144 local $self->{is_running} = 1;
145 while ($self->is_running) {
148 log_info { "Run loop: run() completed" };