Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::MiniLoop; |
2 | |
3 | use IO::Select; |
befabdee |
4 | use Time::HiRes qw(time); |
a63cd862 |
5 | use Object::Remote::Logging qw( :log ); |
9e72f0cf |
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 | |
fbd3b8ec |
15 | has _write_watches => (is => 'ro', default => sub { {} }); |
16 | has _write_select => (is => 'ro', default => sub { IO::Select->new }); |
17 | |
befabdee |
18 | has _timers => (is => 'ro', default => sub { [] }); |
19 | |
9e72f0cf |
20 | sub pass_watches_to { |
21 | my ($self, $new_loop) = @_; |
a63cd862 |
22 | log_debug { "passing watches to new run loop" }; |
9e72f0cf |
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 | } |
fbd3b8ec |
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 | } |
9e72f0cf |
35 | } |
36 | |
37 | sub watch_io { |
38 | my ($self, %watch) = @_; |
39 | my $fh = $watch{handle}; |
a63cd862 |
40 | log_debug { my $type = ref($fh); "Adding watch for ref of type '$type'" }; |
9e72f0cf |
41 | if (my $cb = $watch{on_read_ready}) { |
42 | $self->_read_select->add($fh); |
43 | $self->_read_watches->{$fh} = $cb; |
44 | } |
fbd3b8ec |
45 | if (my $cb = $watch{on_write_ready}) { |
46 | $self->_write_select->add($fh); |
47 | $self->_write_watches->{$fh} = $cb; |
48 | } |
498c4ad5 |
49 | return; |
9e72f0cf |
50 | } |
51 | |
52 | sub unwatch_io { |
53 | my ($self, %watch) = @_; |
54 | my $fh = $watch{handle}; |
a63cd862 |
55 | log_debug { my $type = ref($fh); "Removing watch for ref of type '$type'" }; |
9e72f0cf |
56 | if ($watch{on_read_ready}) { |
57 | $self->_read_select->remove($fh); |
58 | delete $self->_read_watches->{$fh}; |
59 | } |
fbd3b8ec |
60 | if ($watch{on_write_ready}) { |
61 | $self->_write_select->remove($fh); |
62 | delete $self->_write_watches->{$fh}; |
63 | } |
befabdee |
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; |
a63cd862 |
77 | log_debug { "Created new timer with id of '$new' that expires at '$at'" }; |
befabdee |
78 | return "$new"; |
79 | } |
80 | |
81 | sub unwatch_time { |
82 | my ($self, $id) = @_; |
a63cd862 |
83 | log_debug { "Removing timer with id of '$id'" }; |
befabdee |
84 | @$_ = grep !($_ eq $id), @$_ for $self->_timers; |
85 | return; |
9e72f0cf |
86 | } |
87 | |
88 | sub loop_once { |
89 | my ($self) = @_; |
90 | my $read = $self->_read_watches; |
fbd3b8ec |
91 | my $write = $self->_write_watches; |
a63cd862 |
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))) }; |
fbd3b8ec |
94 | my ($readable, $writeable) = IO::Select->select( |
95 | $self->_read_select, $self->_write_select, undef, 0.5 |
96 | ); |
a63cd862 |
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 | }; |
9e72f0cf |
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. |
a63cd862 |
105 | log_trace { "Reading from all ready filehandles" }; |
9e72f0cf |
106 | foreach my $fh (@$readable) { |
fbd3b8ec |
107 | $read->{$fh}() if $read->{$fh}; |
108 | } |
a63cd862 |
109 | log_trace { "Writing to all ready filehandles" }; |
fbd3b8ec |
110 | foreach my $fh (@$writeable) { |
111 | $write->{$fh}() if $write->{$fh}; |
9e72f0cf |
112 | } |
befabdee |
113 | my $timers = $self->_timers; |
114 | my $now = time(); |
a63cd862 |
115 | log_trace { "Checking timers" }; |
befabdee |
116 | while (@$timers and $timers->[0][0] <= $now) { |
117 | (shift @$timers)->[1]->(); |
118 | } |
a63cd862 |
119 | log_debug { "Run loop: single loop is completed" }; |
befabdee |
120 | return; |
9e72f0cf |
121 | } |
122 | |
6c597351 |
123 | sub want_run { |
124 | my ($self) = @_; |
125 | $self->{want_running}++; |
126 | } |
127 | |
128 | sub run_while_wanted { |
129 | my ($self) = @_; |
a63cd862 |
130 | log_debug { "Run loop: run_while_wanted() invoked" }; |
6c597351 |
131 | $self->loop_once while $self->{want_running}; |
a63cd862 |
132 | log_debug { "Run loop: run_while_wanted() completed" }; |
befabdee |
133 | return; |
6c597351 |
134 | } |
135 | |
136 | sub want_stop { |
137 | my ($self) = @_; |
138 | $self->{want_running}-- if $self->{want_running}; |
139 | } |
140 | |
9e72f0cf |
141 | sub run { |
142 | my ($self) = @_; |
a63cd862 |
143 | log_info { "Run loop: run() invoked" }; |
9e72f0cf |
144 | local $self->{is_running} = 1; |
145 | while ($self->is_running) { |
146 | $self->loop_once; |
147 | } |
a63cd862 |
148 | log_info { "Run loop: run() completed" }; |
befabdee |
149 | return; |
9e72f0cf |
150 | } |
151 | |
152 | 1; |