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