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