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