1 package Object::Remote::MiniLoop;
4 use Time::HiRes qw(time);
7 # this is ro because we only actually set it using local in sub run
9 has is_running => (is => 'ro', clearer => 'stop');
11 has _read_watches => (is => 'ro', default => sub { {} });
12 has _read_select => (is => 'ro', default => sub { IO::Select->new });
14 has _write_watches => (is => 'ro', default => sub { {} });
15 has _write_select => (is => 'ro', default => sub { IO::Select->new });
17 has _timers => (is => 'ro', default => sub { [] });
19 sub _log { shift; printf "[%s] %s\n", scalar(localtime), join '', @_ }
22 my ($self, $new_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 if (my $cb = $watch{on_read_ready}) {
41 $self->_read_select->add($fh);
42 $self->_read_watches->{$fh} = $cb;
44 if (my $cb = $watch{on_write_ready}) {
45 $self->_write_select->add($fh);
46 $self->_write_watches->{$fh} = $cb;
52 my ($self, %watch) = @_;
53 my $fh = $watch{handle};
54 if ($watch{on_read_ready}) {
55 $self->_read_select->remove($fh);
56 delete $self->_read_watches->{$fh};
58 if ($watch{on_write_ready}) {
59 $self->_write_select->remove($fh);
60 delete $self->_write_watches->{$fh};
66 my ($self, %watch) = @_;
67 my $at = $watch{at} || do {
68 die "watch_time requires at or after" unless my $after = $watch{after};
71 die "watch_time requires code" unless my $code = $watch{code};
72 my $timers = $self->_timers;
73 my $new = [ $at => $code ];
74 @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, $new;
80 @$_ = grep !($_ eq $id), @$_ for $self->_timers;
86 my $read = $self->_read_watches;
87 my $write = $self->_write_watches;
88 my ($readable, $writeable) = IO::Select->select(
89 $self->_read_select, $self->_write_select, undef, 0.5
91 # I would love to trap errors in the select call but IO::Select doesn't
92 # differentiate between an error and a timeout.
94 foreach my $fh (@$readable) {
95 $self->_log("got a readable: $fh");
96 $read->{$fh}() if $read->{$fh};
98 foreach my $fh (@$writeable) {
99 $write->{$fh}() if $write->{$fh};
101 my $timers = $self->_timers;
103 while (@$timers and $timers->[0][0] <= $now) {
104 (shift @$timers)->[1]->();
111 $self->{want_running}++;
114 sub run_while_wanted {
116 $self->loop_once while $self->{want_running};
122 $self->{want_running}-- if $self->{want_running};
127 local $self->{is_running} = 1;
128 while ($self->is_running) {