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