added logging to runloop
[scpubgit/Object-Remote.git] / lib / Object / Remote / MiniLoop.pm
1 package Object::Remote::MiniLoop;
2
3 use IO::Select;
4 use Time::HiRes qw(time);
5 use Object::Remote::Logging qw( :log );
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
15 has _write_watches => (is => 'ro', default => sub { {} });
16 has _write_select => (is => 'ro', default => sub { IO::Select->new });
17
18 has _timers => (is => 'ro', default => sub { [] });
19
20 sub pass_watches_to {
21   my ($self, $new_loop) = @_;
22   log_debug { "passing watches to new run loop" };
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   }
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   }
35 }
36
37 sub watch_io {
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;
44   }
45   if (my $cb = $watch{on_write_ready}) {
46     $self->_write_select->add($fh);
47     $self->_write_watches->{$fh} = $cb;
48   }
49   return;
50 }
51
52 sub unwatch_io {
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};
59   }
60   if ($watch{on_write_ready}) {
61     $self->_write_select->remove($fh);
62     delete $self->_write_watches->{$fh};
63   }
64   return;
65 }
66
67 sub watch_time {
68   my ($self, %watch) = @_;
69   my $at = $watch{at} || do {
70     die "watch_time requires at or after" unless my $after = $watch{after};
71     time() + $after;
72   };
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'" };
78   return "$new";
79 }
80
81 sub unwatch_time {
82   my ($self, $id) = @_;
83   log_debug { "Removing timer with id of '$id'" };
84   @$_ = grep !($_ eq $id), @$_ for $self->_timers;
85   return;
86 }
87
88 sub loop_once {
89   my ($self) = @_;
90   my $read = $self->_read_watches;
91   my $write = $self->_write_watches;
92   my @c = caller;
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
96   );
97   log_debug { 
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";
101   };
102   # I would love to trap errors in the select call but IO::Select doesn't
103   # differentiate between an error and a timeout.
104   #   -- no, love, mst.
105   log_trace { "Reading from all ready filehandles" };
106   foreach my $fh (@$readable) {
107     $read->{$fh}() if $read->{$fh};
108   }
109   log_trace { "Writing to all ready filehandles" };
110   foreach my $fh (@$writeable) {
111     $write->{$fh}() if $write->{$fh};
112   }
113   my $timers = $self->_timers;
114   my $now = time();
115   log_trace { "Checking timers" };
116   while (@$timers and $timers->[0][0] <= $now) {
117     (shift @$timers)->[1]->();
118   }
119   log_debug { "Run loop: single loop is completed" };
120   return;
121 }
122
123 sub want_run {
124   my ($self) = @_;
125   $self->{want_running}++;
126 }
127
128 sub run_while_wanted {
129   my ($self) = @_;
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" };
133   return;
134 }
135
136 sub want_stop {
137   my ($self) = @_;
138   $self->{want_running}-- if $self->{want_running};
139 }
140
141 sub run {
142   my ($self) = @_;
143   log_info { "Run loop: run() invoked" };
144   local $self->{is_running} = 1;
145   while ($self->is_running) {
146     $self->loop_once;
147   }
148   log_info { "Run loop: run() completed" };
149   return;
150 }
151
152 1;