Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::Connection; |
2 | |
c3d5ef8a |
3 | use Object::Remote::Logging qw (:log :dlog router); |
dc28afe8 |
4 | use Object::Remote::Future; |
9d804009 |
5 | use Object::Remote::Null; |
676438a1 |
6 | use Object::Remote::Handle; |
fe6c9a7f |
7 | use Object::Remote::CodeContainer; |
ed5a8a8e |
8 | use Object::Remote::GlobProxy; |
9 | use Object::Remote::GlobContainer; |
b51a8453 |
10 | use Object::Remote::Tied; |
9e72f0cf |
11 | use Object::Remote; |
ed5a8a8e |
12 | use Symbol; |
9e72f0cf |
13 | use IO::Handle; |
69aaad21 |
14 | use POSIX ":sys_wait_h"; |
9e72f0cf |
15 | use Module::Runtime qw(use_module); |
8ed52376 |
16 | use Scalar::Util qw(weaken blessed refaddr openhandle); |
9e72f0cf |
17 | use JSON::PP qw(encode_json); |
18 | use Moo; |
29d4ad76 |
19 | use Carp qw(croak); |
9e72f0cf |
20 | |
69aaad21 |
21 | BEGIN { |
c3d5ef8a |
22 | router()->exclude_forwarding; |
0b51243d |
23 | $SIG{PIPE} = sub { log_debug { "Got a PIPE signal" } }; |
69aaad21 |
24 | } |
25 | |
26 | END { |
27 | log_debug { "Killing all child processes in the process group" }; |
28 | |
29 | #send SIGINT to the process group for our children |
30 | kill(1, -2); |
31 | } |
32 | |
07105aca |
33 | has _id => ( is => 'ro', required => 1, default => sub { our $NEXT_CONNECTION_ID++ } ); |
ad4f54b2 |
34 | |
9e72f0cf |
35 | has send_to_fh => ( |
36 | is => 'ro', required => 1, |
90a3a7f2 |
37 | trigger => sub { |
38 | my $self = $_[0]; |
39 | $_[1]->autoflush(1); |
40 | Dlog_trace { my $id = $self->_id; "connection had send_to_fh set to $_" } $_[1]; |
41 | }, |
9e72f0cf |
42 | ); |
43 | |
12fb4a80 |
44 | has read_channel => ( |
9e72f0cf |
45 | is => 'ro', required => 1, |
46 | trigger => sub { |
12fb4a80 |
47 | my ($self, $ch) = @_; |
8ed52376 |
48 | my $id = $self->_id; |
0b51243d |
49 | Dlog_trace { "trigger for read_channel has been invoked for connection $id; file handle is $_" } $ch->fh; |
9e72f0cf |
50 | weaken($self); |
12fb4a80 |
51 | $ch->on_line_call(sub { $self->_receive(@_) }); |
8ed52376 |
52 | $ch->on_close_call(sub { |
0b51243d |
53 | log_trace { "invoking 'done' on on_close handler for connection id '$id'" }; |
1a7f821f |
54 | $self->on_close->done(@_); |
8ed52376 |
55 | }); |
9e72f0cf |
56 | }, |
57 | ); |
58 | |
12fb4a80 |
59 | has on_close => ( |
b1f39f94 |
60 | is => 'rw', default => sub { $_[0]->_install_future_handlers(CPS::Future->new) }, |
0b51243d |
61 | trigger => sub { |
62 | log_trace { "Installing handlers into future via trigger" }; |
63 | $_[0]->_install_future_handlers($_[1]) |
64 | }, |
12fb4a80 |
65 | ); |
ad4f54b2 |
66 | |
47c83a13 |
67 | has child_pid => (is => 'ro'); |
68 | |
11dbd4a0 |
69 | has local_objects_by_id => ( |
70 | is => 'ro', default => sub { {} }, |
71 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
72 | ); |
9e72f0cf |
73 | |
11dbd4a0 |
74 | has remote_objects_by_id => ( |
75 | is => 'ro', default => sub { {} }, |
76 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
77 | ); |
9e72f0cf |
78 | |
a980b0b8 |
79 | has outstanding_futures => (is => 'ro', default => sub { {} }); |
80 | |
b1f39f94 |
81 | has _json => ( |
82 | is => 'lazy', |
83 | handles => { |
84 | _deserialize => 'decode', |
85 | _encode => 'encode', |
86 | }, |
87 | ); |
88 | |
89 | after BUILD => sub { |
0b51243d |
90 | my ($self) = @_; |
91 | my $pid = $self->child_pid; |
b1f39f94 |
92 | |
0b51243d |
93 | unless (defined $pid) { |
94 | log_trace { "After BUILD invoked for connection but there was no pid" }; |
95 | return; |
96 | } |
97 | |
98 | log_trace { "Setting process group of child process '$pid'" }; |
b1f39f94 |
99 | |
100 | setpgrp($self->child_pid, 1); |
101 | }; |
102 | |
103 | sub BUILD { } |
104 | |
2a4b201a |
105 | sub is_valid { |
106 | my ($self) = @_; |
107 | my $closed = $self->on_close->is_ready; |
108 | |
109 | log_trace { "Connection closed: $closed" }; |
110 | return ! $closed; |
111 | } |
112 | |
12fb4a80 |
113 | sub _fail_outstanding { |
114 | my ($self, $error) = @_; |
115 | my $outstanding = $self->outstanding_futures; |
2a4b201a |
116 | |
117 | Dlog_debug { |
118 | sprintf "Failing %i outstanding futures with '$error'", scalar(keys(%$outstanding)) |
119 | }; |
120 | |
121 | foreach(keys(%$outstanding)) { |
122 | log_trace { "Failing future for $_" }; |
123 | my $future = $outstanding->{$_}; |
6aaa14ea |
124 | $future->fail("$error\n"); |
2a4b201a |
125 | } |
126 | |
12fb4a80 |
127 | %$outstanding = (); |
128 | return; |
129 | } |
130 | |
b1f39f94 |
131 | sub _install_future_handlers { |
132 | my ($self, $f) = @_; |
0b51243d |
133 | Dlog_trace { "Installing handlers into future for connection $_" } $self->_id; |
b1f39f94 |
134 | weaken($self); |
135 | $f->on_done(sub { |
0b51243d |
136 | my $pid = $self->child_pid; |
137 | Dlog_trace { "Executing on_done handler in future for connection $_" } $self->_id; |
b1f39f94 |
138 | $self->_fail_outstanding("Object::Remote connection lost: " . ($f->get)[0]); |
0b51243d |
139 | return unless defined $pid; |
140 | log_debug { "Waiting for child '$pid' to exit" }; |
141 | my $ret = waitpid($pid, 0); |
142 | if ($ret != $pid) { |
143 | log_debug { "Waited for pid $pid but waitpid() returned $ret" }; |
144 | return; |
145 | } elsif ($? & 127) { |
146 | log_warn { "Remote interpreter did not exit cleanly" }; |
147 | } else { |
148 | log_verbose { |
149 | my $exit_value = $? >> 8; |
150 | "Remote Perl interpreter exited with value '$exit_value'" |
151 | }; |
152 | } |
b1f39f94 |
153 | }); |
154 | return $f; |
155 | }; |
9e72f0cf |
156 | |
fe6c9a7f |
157 | sub _id_to_remote_object { |
158 | my ($self, $id) = @_; |
07105aca |
159 | Dlog_trace { "fetching proxy for remote object with id '$id' for connection $_" } $self->_id; |
fe6c9a7f |
160 | return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; |
161 | ( |
162 | $self->remote_objects_by_id->{$id} |
163 | or Object::Remote::Handle->new(connection => $self, id => $id) |
164 | )->proxy; |
165 | } |
166 | |
9e72f0cf |
167 | sub _build__json { |
168 | weaken(my $self = shift); |
9e72f0cf |
169 | JSON::PP->new->filter_json_single_key_object( |
170 | __remote_object__ => sub { |
fe6c9a7f |
171 | $self->_id_to_remote_object(@_); |
172 | } |
173 | )->filter_json_single_key_object( |
174 | __remote_code__ => sub { |
175 | my $code_container = $self->_id_to_remote_object(@_); |
176 | sub { $code_container->call(@_) }; |
9e72f0cf |
177 | } |
6ed5d580 |
178 | )->filter_json_single_key_object( |
179 | __scalar_ref__ => sub { |
180 | my $value = shift; |
181 | return \$value; |
182 | } |
ed5a8a8e |
183 | )->filter_json_single_key_object( |
184 | __glob_ref__ => sub { |
185 | my $glob_container = $self->_id_to_remote_object(@_); |
186 | my $handle = Symbol::gensym; |
187 | tie *$handle, 'Object::Remote::GlobProxy', $glob_container; |
188 | return $handle; |
189 | } |
b51a8453 |
190 | )->filter_json_single_key_object( |
191 | __remote_tied_hash__ => sub { |
1a7f821f |
192 | my %tied_hash; |
193 | tie %tied_hash, 'Object::Remote::Tied', $self->_id_to_remote_object(@_); |
194 | return \%tied_hash; |
b51a8453 |
195 | } |
196 | )->filter_json_single_key_object( |
197 | __remote_tied_array__ => sub { |
1a7f821f |
198 | my @tied_array; |
199 | tie @tied_array, 'Object::Remote::Tied', $self->_id_to_remote_object(@_); |
200 | return \@tied_array; |
b51a8453 |
201 | } |
202 | ); |
9e72f0cf |
203 | } |
204 | |
69aaad21 |
205 | sub _load_if_possible { |
206 | my ($class) = @_; |
207 | |
62796659 |
208 | use_module($class); |
69aaad21 |
209 | |
210 | if ($@) { |
211 | log_debug { "Attempt at loading '$class' failed with '$@'" }; |
212 | } |
213 | |
214 | } |
215 | |
84b04178 |
216 | BEGIN { |
217 | unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; |
69aaad21 |
218 | map _load_if_possible($_), qw( |
219 | Object::Remote::Connector::Local |
220 | Object::Remote::Connector::LocalSudo |
221 | Object::Remote::Connector::SSH |
222 | Object::Remote::Connector::UNIX |
223 | ); |
84b04178 |
224 | } |
225 | |
69aaad21 |
226 | sub conn_from_spec { |
227 | my ($class, $spec, @args) = @_; |
84b04178 |
228 | foreach my $poss (do { our @Guess }) { |
69aaad21 |
229 | if (my $conn = $poss->($spec, @args)) { |
230 | return $conn; |
fbd3b8ec |
231 | } |
84b04178 |
232 | } |
69aaad21 |
233 | |
234 | return undef; |
235 | } |
236 | |
237 | sub new_from_spec { |
238 | my ($class, $spec) = @_; |
239 | return $spec if blessed $spec; |
240 | my $conn = $class->conn_from_spec($spec); |
241 | |
242 | die "Couldn't figure out what to do with ${spec}" |
243 | unless defined $conn; |
244 | |
245 | return $conn->maybe::start::connect; |
84b04178 |
246 | } |
247 | |
11dbd4a0 |
248 | sub remote_object { |
e144d525 |
249 | my ($self, @args) = @_; |
250 | Object::Remote::Handle->new( |
251 | connection => $self, @args |
252 | )->proxy; |
253 | } |
254 | |
4c17fea5 |
255 | sub connect { |
256 | my ($self, $to) = @_; |
07105aca |
257 | Dlog_debug { "Creating connection to remote node '$to' for connection $_" } $self->_id; |
deb77aaf |
258 | return await_future( |
259 | $self->send_class_call(0, 'Object::Remote', connect => $to) |
260 | ); |
4c17fea5 |
261 | } |
262 | |
11dbd4a0 |
263 | sub remote_sub { |
c6fe6fbd |
264 | my ($self, $sub) = @_; |
265 | my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/; |
07105aca |
266 | Dlog_debug { "Invoking remote sub '$sub' for connection $_" } $self->_id; |
deb77aaf |
267 | return await_future($self->send_class_call(0, $pkg, can => $name)); |
268 | } |
269 | |
270 | sub send_class_call { |
271 | my ($self, $ctx, @call) = @_; |
07105aca |
272 | Dlog_trace { "Sending a class call for connection $_" } $self->_id; |
deb77aaf |
273 | $self->send(call => class_call_handler => $ctx => call => @call); |
274 | } |
275 | |
276 | sub register_class_call_handler { |
277 | my ($self) = @_; |
3687a42d |
278 | $self->local_objects_by_id->{'class_call_handler'} ||= do { |
c5736e1c |
279 | my $o = $self->new_class_call_handler; |
3687a42d |
280 | $self->_local_object_to_id($o); |
281 | $o; |
282 | }; |
c6fe6fbd |
283 | } |
284 | |
c5736e1c |
285 | sub new_class_call_handler { |
286 | Object::Remote::CodeContainer->new( |
287 | code => sub { |
288 | my ($class, $method) = (shift, shift); |
289 | use_module($class)->$method(@_); |
290 | } |
291 | ); |
292 | } |
293 | |
9e72f0cf |
294 | sub register_remote { |
295 | my ($self, $remote) = @_; |
07105aca |
296 | Dlog_trace { my $i = $remote->id; "Registered a remote object with id of '$i' for connection $_" } $self->_id; |
9e72f0cf |
297 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
298 | return $remote; |
299 | } |
300 | |
301 | sub send_free { |
302 | my ($self, $id) = @_; |
b51a8453 |
303 | Dlog_trace { "sending request to free object '$id' for connection $_" } $self->_id; |
52a81cde |
304 | #TODO this shows up some times when a remote side dies in the middle of a remote |
305 | #method invocation - possibly only when the object is being constructed? |
306 | #(in cleanup) Use of uninitialized value $id in delete at ../Object-Remote/lib/Object/Remote/Connection. |
9e72f0cf |
307 | delete $self->remote_objects_by_id->{$id}; |
308 | $self->_send([ free => $id ]); |
309 | } |
310 | |
311 | sub send { |
312 | my ($self, $type, @call) = @_; |
313 | |
deb77aaf |
314 | my $future = CPS::Future->new; |
a2d43709 |
315 | my $remote = $self->remote_objects_by_id->{$call[0]}; |
deb77aaf |
316 | |
317 | unshift @call, $type => $self->_local_object_to_id($future); |
9e72f0cf |
318 | |
a980b0b8 |
319 | my $outstanding = $self->outstanding_futures; |
320 | $outstanding->{$future} = $future; |
a2d43709 |
321 | $future->on_ready(sub { |
322 | undef($remote); |
323 | delete $outstanding->{$future} |
324 | }); |
a980b0b8 |
325 | |
9e72f0cf |
326 | $self->_send(\@call); |
327 | |
328 | return $future; |
329 | } |
330 | |
9d804009 |
331 | sub send_discard { |
332 | my ($self, $type, @call) = @_; |
333 | |
deb77aaf |
334 | unshift @call, $type => 'NULL'; |
9d804009 |
335 | |
336 | $self->_send(\@call); |
337 | } |
338 | |
9e72f0cf |
339 | sub _send { |
340 | my ($self, $to_send) = @_; |
2d81cf18 |
341 | my $fh = $self->send_to_fh; |
2a4b201a |
342 | |
343 | unless ($self->is_valid) { |
29d4ad76 |
344 | croak "Attempt to invoke _send on a connection that is not valid"; |
2a4b201a |
345 | } |
346 | |
07105aca |
347 | Dlog_trace { "Starting to serialize data in argument to _send for connection $_" } $self->_id; |
2d81cf18 |
348 | my $serialized = $self->_serialize($to_send)."\n"; |
5953edf6 |
349 | Dlog_trace { my $l = length($serialized); "serialization is completed; sending '$l' characters of serialized data to $_" } $fh; |
8ed52376 |
350 | my $ret; |
351 | eval { |
b1f39f94 |
352 | #TODO this should be converted over to a non-blocking ::WriteChannel class |
353 | die "filehandle is not open" unless openhandle($fh); |
354 | log_trace { "file handle has passed openhandle() test; printing to it" }; |
355 | $ret = print $fh $serialized; |
356 | die "print was not successful: $!" unless defined $ret |
8ed52376 |
357 | }; |
69aaad21 |
358 | |
8ed52376 |
359 | if ($@) { |
b1f39f94 |
360 | Dlog_debug { "exception encountered when trying to write to file handle $_: $@" } $fh; |
0b51243d |
361 | my $error = $@; |
362 | chomp($error); |
b1f39f94 |
363 | $self->on_close->done("could not write to file handle: $error") unless $self->on_close->is_ready; |
364 | return; |
8ed52376 |
365 | } |
366 | |
2d81cf18 |
367 | return $ret; |
9e72f0cf |
368 | } |
369 | |
370 | sub _serialize { |
371 | my ($self, $data) = @_; |
3687a42d |
372 | local our @New_Ids = (-1); |
9d804009 |
373 | return eval { |
ad4f54b2 |
374 | my $flat = $self->_encode($self->_deobjectify($data)); |
ad4f54b2 |
375 | $flat; |
70a578ac |
376 | } || do { |
9d804009 |
377 | my $err = $@; # won't get here if the eval doesn't die |
378 | # don't keep refs to new things |
379 | delete @{$self->local_objects_by_id}{@New_Ids}; |
380 | die "Error serializing: $err"; |
381 | }; |
9e72f0cf |
382 | } |
383 | |
a76f2f60 |
384 | sub _local_object_to_id { |
385 | my ($self, $object) = @_; |
386 | my $id = refaddr($object); |
387 | $self->local_objects_by_id->{$id} ||= do { |
3687a42d |
388 | push our(@New_Ids), $id if @New_Ids; |
a76f2f60 |
389 | $object; |
390 | }; |
391 | return $id; |
392 | } |
393 | |
9e72f0cf |
394 | sub _deobjectify { |
395 | my ($self, $data) = @_; |
396 | if (blessed($data)) { |
a76f2f60 |
397 | return +{ __remote_object__ => $self->_local_object_to_id($data) }; |
9e72f0cf |
398 | } elsif (my $ref = ref($data)) { |
399 | if ($ref eq 'HASH') { |
b51a8453 |
400 | my $tied_to = tied(%$data); |
401 | if(defined($tied_to)) { |
402 | return +{__remote_tied_hash__ => $self->_local_object_to_id($tied_to)}; |
403 | } else { |
404 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
405 | } |
9e72f0cf |
406 | } elsif ($ref eq 'ARRAY') { |
b51a8453 |
407 | my $tied_to = tied(@$data); |
408 | if (defined($tied_to)) { |
409 | return +{__remote_tied_array__ => $self->_local_object_to_id($tied_to)}; |
410 | } else { |
411 | return [ map $self->_deobjectify($_), @$data ]; |
412 | } |
fe6c9a7f |
413 | } elsif ($ref eq 'CODE') { |
414 | my $id = $self->_local_object_to_id( |
415 | Object::Remote::CodeContainer->new(code => $data) |
416 | ); |
417 | return +{ __remote_code__ => $id }; |
6ed5d580 |
418 | } elsif ($ref eq 'SCALAR') { |
419 | return +{ __scalar_ref__ => $$data }; |
ed5a8a8e |
420 | } elsif ($ref eq 'GLOB') { |
421 | return +{ __glob_ref__ => $self->_local_object_to_id( |
422 | Object::Remote::GlobContainer->new(handle => $data) |
423 | ) }; |
9e72f0cf |
424 | } else { |
425 | die "Can't collapse reftype $ref"; |
426 | } |
427 | } |
428 | return $data; # plain scalar |
429 | } |
430 | |
9e72f0cf |
431 | sub _receive { |
ad4f54b2 |
432 | my ($self, $flat) = @_; |
07105aca |
433 | Dlog_trace { my $l = length($flat); "Starting to deserialize $l characters of data for connection $_" } $self->_id; |
ad4f54b2 |
434 | my ($type, @rest) = eval { @{$self->_deserialize($flat)} } |
435 | or do { warn "Deserialize failed for ${flat}: $@"; return }; |
07105aca |
436 | Dlog_trace { "deserialization complete for connection $_" } $self->_id; |
9e72f0cf |
437 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
ad4f54b2 |
438 | or do { warn "Receive failed for ${flat}: $@"; return }; |
9e72f0cf |
439 | return; |
440 | } |
441 | |
442 | sub receive_free { |
443 | my ($self, $id) = @_; |
07105aca |
444 | Dlog_trace { "got a receive_free for object '$id' for connection $_" } $self->_id; |
9d804009 |
445 | delete $self->local_objects_by_id->{$id} |
446 | or warn "Free: no such object $id"; |
9e72f0cf |
447 | return; |
448 | } |
449 | |
450 | sub receive_call { |
deb77aaf |
451 | my ($self, $future_id, $id, @rest) = @_; |
07105aca |
452 | Dlog_trace { "got a receive_call for object '$id' for connection $_" } $self->_id; |
deb77aaf |
453 | my $future = $self->_id_to_remote_object($future_id); |
8131a88a |
454 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
455 | my $local = $self->local_objects_by_id->{$id} |
456 | or do { $future->fail("No such object $id"); return }; |
457 | $self->_invoke($future, $local, @rest); |
458 | } |
459 | |
8131a88a |
460 | sub receive_call_free { |
461 | my ($self, $future, $id, @rest) = @_; |
07105aca |
462 | Dlog_trace { "got a receive_call_free for object '$id' for connection $_" } $self->_id; |
84b04178 |
463 | $self->receive_call($future, $id, undef, @rest); |
8131a88a |
464 | $self->receive_free($id); |
465 | } |
466 | |
9e72f0cf |
467 | sub _invoke { |
84b04178 |
468 | my ($self, $future, $local, $ctx, $method, @args) = @_; |
07105aca |
469 | Dlog_trace { "got _invoke for a method named '$method' for connection $_" } $self->_id; |
dc28afe8 |
470 | if ($method =~ /^start::/) { |
471 | my $f = $local->$method(@args); |
472 | $f->on_done(sub { undef($f); $future->done(@_) }); |
3f1f1e66 |
473 | return unless $f; |
dc28afe8 |
474 | $f->on_fail(sub { undef($f); $future->fail(@_) }); |
475 | return; |
476 | } |
84b04178 |
477 | my $do = sub { $local->$method(@args) }; |
478 | eval { |
479 | $future->done( |
480 | defined($ctx) |
481 | ? ($ctx ? $do->() : scalar($do->())) |
482 | : do { $do->(); () } |
483 | ); |
484 | 1; |
485 | } or do { $future->fail($@); return; }; |
9e72f0cf |
486 | return; |
487 | } |
488 | |
9e72f0cf |
489 | 1; |
b9a9982d |
490 | |
491 | =head1 NAME |
492 | |
493 | Object::Remote::Connection - An underlying connection for L<Object::Remote> |
494 | |
495 | =head1 LAME |
496 | |
497 | Shipping prioritised over writing this part up. Blame mst. |
498 | |
499 | =cut |