Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::Connection; |
2 | |
dc28afe8 |
3 | use Object::Remote::Future; |
9d804009 |
4 | use Object::Remote::Null; |
676438a1 |
5 | use Object::Remote::Handle; |
fe6c9a7f |
6 | use Object::Remote::CodeContainer; |
9e72f0cf |
7 | use Object::Remote; |
8 | use IO::Handle; |
9 | use Module::Runtime qw(use_module); |
10 | use Scalar::Util qw(weaken blessed refaddr); |
11 | use JSON::PP qw(encode_json); |
12 | use Moo; |
13 | |
6db5156c |
14 | our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG}; |
ad4f54b2 |
15 | |
9e72f0cf |
16 | has send_to_fh => ( |
17 | is => 'ro', required => 1, |
18 | trigger => sub { $_[1]->autoflush(1) }, |
19 | ); |
20 | |
21 | has receive_from_fh => ( |
22 | is => 'ro', required => 1, |
23 | trigger => sub { |
24 | my ($self, $fh) = @_; |
25 | weaken($self); |
26 | Object::Remote->current_loop |
27 | ->watch_io( |
28 | handle => $fh, |
29 | on_read_ready => sub { $self->_receive_data_from($fh) } |
30 | ); |
31 | }, |
32 | ); |
33 | |
a980b0b8 |
34 | has on_close => (is => 'rw', default => sub { CPS::Future->new }); |
ad4f54b2 |
35 | |
47c83a13 |
36 | has child_pid => (is => 'ro'); |
37 | |
8ba4f2e3 |
38 | has ready_future => (is => 'lazy'); |
39 | |
40 | sub _build_ready_future { CPS::Future->new } |
41 | |
9e72f0cf |
42 | has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x }); |
43 | |
44 | has local_objects_by_id => (is => 'ro', default => sub { {} }); |
45 | |
2065b08b |
46 | has remote_objects_by_id => (is => 'ro', default => sub { {} }); |
9e72f0cf |
47 | |
a980b0b8 |
48 | has outstanding_futures => (is => 'ro', default => sub { {} }); |
49 | |
9e72f0cf |
50 | has _json => ( |
51 | is => 'lazy', |
52 | handles => { |
53 | _deserialize => 'decode', |
54 | _encode => 'encode', |
55 | }, |
56 | ); |
57 | |
fe6c9a7f |
58 | sub _id_to_remote_object { |
59 | my ($self, $id) = @_; |
60 | return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; |
61 | ( |
62 | $self->remote_objects_by_id->{$id} |
63 | or Object::Remote::Handle->new(connection => $self, id => $id) |
64 | )->proxy; |
65 | } |
66 | |
9e72f0cf |
67 | sub _build__json { |
68 | weaken(my $self = shift); |
9e72f0cf |
69 | JSON::PP->new->filter_json_single_key_object( |
70 | __remote_object__ => sub { |
fe6c9a7f |
71 | $self->_id_to_remote_object(@_); |
72 | } |
73 | )->filter_json_single_key_object( |
74 | __remote_code__ => sub { |
75 | my $code_container = $self->_id_to_remote_object(@_); |
76 | sub { $code_container->call(@_) }; |
9e72f0cf |
77 | } |
78 | ); |
79 | } |
80 | |
84b04178 |
81 | BEGIN { |
82 | unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; |
83 | eval { require Object::Remote::Connector::Local }; |
a9fdb55e |
84 | eval { require Object::Remote::Connector::LocalSudo }; |
84b04178 |
85 | eval { require Object::Remote::Connector::SSH }; |
86 | } |
87 | |
88 | sub new_from_spec { |
89 | my ($class, $spec) = @_; |
e144d525 |
90 | return $spec if blessed $spec; |
84b04178 |
91 | foreach my $poss (do { our @Guess }) { |
92 | if (my $obj = $poss->($spec)) { return $obj } |
93 | } |
94 | die "Couldn't figure out what to do with ${spec}"; |
95 | } |
96 | |
e144d525 |
97 | sub new_remote { |
98 | my ($self, @args) = @_; |
99 | Object::Remote::Handle->new( |
100 | connection => $self, @args |
101 | )->proxy; |
102 | } |
103 | |
4c17fea5 |
104 | sub connect { |
105 | my ($self, $to) = @_; |
106 | return await_future($self->send( |
107 | class_call => 'Object::Remote', 0, connect => $to |
108 | )); |
109 | } |
110 | |
c6fe6fbd |
111 | sub get_remote_sub { |
112 | my ($self, $sub) = @_; |
113 | my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/; |
114 | return await_future($self->send(class_call => $pkg, 0, can => $name)); |
115 | } |
116 | |
9e72f0cf |
117 | sub register_remote { |
118 | my ($self, $remote) = @_; |
119 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
120 | return $remote; |
121 | } |
122 | |
8ba4f2e3 |
123 | sub await_ready { |
124 | my ($self) = @_; |
8ba4f2e3 |
125 | await_future($self->ready_future); |
126 | } |
127 | |
9e72f0cf |
128 | sub send_free { |
129 | my ($self, $id) = @_; |
130 | delete $self->remote_objects_by_id->{$id}; |
131 | $self->_send([ free => $id ]); |
132 | } |
133 | |
134 | sub send { |
135 | my ($self, $type, @call) = @_; |
136 | |
137 | unshift @call, $type => my $future = CPS::Future->new; |
138 | |
a980b0b8 |
139 | my $outstanding = $self->outstanding_futures; |
140 | $outstanding->{$future} = $future; |
141 | $future->on_ready(sub { delete $outstanding->{$future} }); |
142 | |
9e72f0cf |
143 | $self->_send(\@call); |
144 | |
145 | return $future; |
146 | } |
147 | |
9d804009 |
148 | sub send_discard { |
149 | my ($self, $type, @call) = @_; |
150 | |
2065b08b |
151 | unshift @call, $type => { __remote_object__ => 'NULL' }; |
9d804009 |
152 | |
153 | $self->_send(\@call); |
154 | } |
155 | |
9e72f0cf |
156 | sub _send { |
157 | my ($self, $to_send) = @_; |
158 | |
e441f00e |
159 | $self->await_ready; |
8ba4f2e3 |
160 | |
9d804009 |
161 | print { $self->send_to_fh } $self->_serialize($to_send)."\n"; |
9e72f0cf |
162 | } |
163 | |
164 | sub _serialize { |
165 | my ($self, $data) = @_; |
166 | local our @New_Ids; |
9d804009 |
167 | return eval { |
ad4f54b2 |
168 | my $flat = $self->_encode($self->_deobjectify($data)); |
169 | warn "$$ >>> ${flat}\n" if $DEBUG; |
170 | $flat; |
70a578ac |
171 | } || do { |
9d804009 |
172 | my $err = $@; # won't get here if the eval doesn't die |
173 | # don't keep refs to new things |
174 | delete @{$self->local_objects_by_id}{@New_Ids}; |
175 | die "Error serializing: $err"; |
176 | }; |
9e72f0cf |
177 | } |
178 | |
a76f2f60 |
179 | sub _local_object_to_id { |
180 | my ($self, $object) = @_; |
181 | my $id = refaddr($object); |
182 | $self->local_objects_by_id->{$id} ||= do { |
183 | push our(@New_Ids), $id; |
184 | $object; |
185 | }; |
186 | return $id; |
187 | } |
188 | |
9e72f0cf |
189 | sub _deobjectify { |
190 | my ($self, $data) = @_; |
191 | if (blessed($data)) { |
a76f2f60 |
192 | return +{ __remote_object__ => $self->_local_object_to_id($data) }; |
9e72f0cf |
193 | } elsif (my $ref = ref($data)) { |
194 | if ($ref eq 'HASH') { |
195 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
196 | } elsif ($ref eq 'ARRAY') { |
197 | return [ map $self->_deobjectify($_), @$data ]; |
fe6c9a7f |
198 | } elsif ($ref eq 'CODE') { |
199 | my $id = $self->_local_object_to_id( |
200 | Object::Remote::CodeContainer->new(code => $data) |
201 | ); |
202 | return +{ __remote_code__ => $id }; |
9e72f0cf |
203 | } else { |
204 | die "Can't collapse reftype $ref"; |
205 | } |
206 | } |
207 | return $data; # plain scalar |
208 | } |
209 | |
210 | sub _receive_data_from { |
211 | my ($self, $fh) = @_; |
212 | my $rb = $self->_receive_data_buffer; |
e441f00e |
213 | my $ready = $self->ready_future->is_ready; |
9d804009 |
214 | if (sysread($fh, $$rb, 1024, length($$rb)) > 0) { |
9e72f0cf |
215 | while ($$rb =~ s/^(.*)\n//) { |
8ba4f2e3 |
216 | if ($ready) { |
217 | $self->_receive($1); |
218 | } else { |
219 | my $line = $1; |
220 | die "New remote container did not send Shere - got ${line}" |
221 | unless $line eq "Shere"; |
e441f00e |
222 | $self->ready_future->done; |
8ba4f2e3 |
223 | } |
9e72f0cf |
224 | } |
ad4f54b2 |
225 | } else { |
a980b0b8 |
226 | Object::Remote->current_loop |
227 | ->unwatch_io( |
228 | handle => $self->receive_from_fh, |
229 | on_read_ready => 1 |
230 | ); |
231 | my $outstanding = $self->outstanding_futures; |
232 | $_->fail("Connection lost") for values %$outstanding; |
233 | %$outstanding = (); |
dc28afe8 |
234 | $self->on_close->done(); |
9e72f0cf |
235 | } |
236 | } |
237 | |
238 | sub _receive { |
ad4f54b2 |
239 | my ($self, $flat) = @_; |
240 | warn "$$ <<< $flat\n" if $DEBUG; |
241 | my ($type, @rest) = eval { @{$self->_deserialize($flat)} } |
242 | or do { warn "Deserialize failed for ${flat}: $@"; return }; |
9e72f0cf |
243 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
ad4f54b2 |
244 | or do { warn "Receive failed for ${flat}: $@"; return }; |
9e72f0cf |
245 | return; |
246 | } |
247 | |
248 | sub receive_free { |
249 | my ($self, $id) = @_; |
9d804009 |
250 | delete $self->local_objects_by_id->{$id} |
251 | or warn "Free: no such object $id"; |
9e72f0cf |
252 | return; |
253 | } |
254 | |
255 | sub receive_call { |
256 | my ($self, $future, $id, @rest) = @_; |
8131a88a |
257 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
258 | my $local = $self->local_objects_by_id->{$id} |
259 | or do { $future->fail("No such object $id"); return }; |
260 | $self->_invoke($future, $local, @rest); |
261 | } |
262 | |
8131a88a |
263 | sub receive_call_free { |
264 | my ($self, $future, $id, @rest) = @_; |
84b04178 |
265 | $self->receive_call($future, $id, undef, @rest); |
8131a88a |
266 | $self->receive_free($id); |
267 | } |
268 | |
9e72f0cf |
269 | sub receive_class_call { |
270 | my ($self, $future, $class, @rest) = @_; |
8131a88a |
271 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
272 | eval { use_module($class) } |
273 | or do { $future->fail("Error loading ${class}: $@"); return }; |
274 | $self->_invoke($future, $class, @rest); |
275 | } |
276 | |
277 | sub _invoke { |
84b04178 |
278 | my ($self, $future, $local, $ctx, $method, @args) = @_; |
dc28afe8 |
279 | if ($method =~ /^start::/) { |
280 | my $f = $local->$method(@args); |
281 | $f->on_done(sub { undef($f); $future->done(@_) }); |
3f1f1e66 |
282 | return unless $f; |
dc28afe8 |
283 | $f->on_fail(sub { undef($f); $future->fail(@_) }); |
284 | return; |
285 | } |
84b04178 |
286 | my $do = sub { $local->$method(@args) }; |
287 | eval { |
288 | $future->done( |
289 | defined($ctx) |
290 | ? ($ctx ? $do->() : scalar($do->())) |
291 | : do { $do->(); () } |
292 | ); |
293 | 1; |
294 | } or do { $future->fail($@); return; }; |
9e72f0cf |
295 | return; |
296 | } |
297 | |
298 | sub DEMOLISH { |
299 | my ($self, $gd) = @_; |
300 | return if $gd; |
301 | Object::Remote->current_loop |
302 | ->unwatch_io( |
303 | handle => $self->receive_from_fh, |
304 | on_read_ready => 1 |
305 | ); |
306 | } |
307 | |
308 | 1; |