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