Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::Connection; |
2 | |
69aaad21 |
3 | use Object::Remote::Logging qw (:log :dlog); |
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; |
19 | |
69aaad21 |
20 | BEGIN { |
21 | #this will reap child processes as soon |
22 | #as they are done executing so the process |
23 | #table cleans up as fast as possible but |
24 | #anything that needs to call waitpid() |
25 | #in the future to get the exit value of |
26 | #a child will get trash results if |
27 | #the signal handler was running. |
28 | #If creating a child and getting the |
29 | #exit value is required then set |
30 | #a localized version of the signal |
31 | #handler for CHLD to be 'IGNORE' |
32 | #in the smallest block possible |
33 | #and outside the block send |
34 | #the process a CHLD signal |
35 | #to reap anything that may |
36 | #have exited while blocked |
37 | #in waitpid() |
38 | $SIG{CHLD} = sub { |
39 | my $kid; |
40 | log_debug { "CHLD signal handler is executing" }; |
41 | do { |
42 | $kid = waitpid(-1, WNOHANG); |
43 | log_trace { "waitpid() returned '$kid'" }; |
44 | } while $kid > 0; |
45 | log_trace { "CHLD signal handler is done" }; |
46 | }; |
47 | } |
48 | |
49 | END { |
50 | log_debug { "Killing all child processes in the process group" }; |
51 | |
52 | #send SIGINT to the process group for our children |
53 | kill(1, -2); |
54 | } |
55 | |
56 | |
6db5156c |
57 | our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG}; |
07105aca |
58 | #numbering each connection allows it to be |
59 | #tracked along with file handles in |
60 | #the logs |
69aaad21 |
61 | |
07105aca |
62 | has _id => ( is => 'ro', required => 1, default => sub { our $NEXT_CONNECTION_ID++ } ); |
ad4f54b2 |
63 | |
9e72f0cf |
64 | has send_to_fh => ( |
65 | is => 'ro', required => 1, |
90a3a7f2 |
66 | trigger => sub { |
67 | my $self = $_[0]; |
68 | $_[1]->autoflush(1); |
69 | Dlog_trace { my $id = $self->_id; "connection had send_to_fh set to $_" } $_[1]; |
70 | }, |
9e72f0cf |
71 | ); |
72 | |
69aaad21 |
73 | #TODO see if this is another case of the same bug below |
74 | #where trigger never fires because the attribute isn't |
75 | #actually set at any time |
12fb4a80 |
76 | has read_channel => ( |
9e72f0cf |
77 | is => 'ro', required => 1, |
78 | trigger => sub { |
12fb4a80 |
79 | my ($self, $ch) = @_; |
8ed52376 |
80 | my $id = $self->_id; |
81 | Dlog_trace { "trigger for read_channel has been invoked for connection $id; file handle is $_" } $ch->fh; |
9e72f0cf |
82 | weaken($self); |
12fb4a80 |
83 | $ch->on_line_call(sub { $self->_receive(@_) }); |
8ed52376 |
84 | $ch->on_close_call(sub { |
85 | log_trace { "invoking 'done' on on_close handler for connection id '$id'" }; |
86 | $self->on_close->done(@_); |
87 | }); |
9e72f0cf |
88 | }, |
89 | ); |
90 | |
8ed52376 |
91 | #TODO properly fix this bug - |
92 | #trigger can't ever be invoked with a default |
93 | #value and the on_close attribute is read only.... |
94 | #the future never gets the on_done handler |
95 | #installed |
69aaad21 |
96 | sub BUILD { |
8ed52376 |
97 | my ($self) = @_; |
69aaad21 |
98 | $self->on_close(CPS::Future->new); |
8ed52376 |
99 | } |
100 | |
69aaad21 |
101 | after BUILD => sub { |
102 | my ($self) = @_; |
103 | |
104 | return unless defined $self->child_pid; |
105 | |
106 | log_debug { "Setting process group of child process" }; |
107 | |
108 | setpgrp($self->child_pid, 1); |
109 | }; |
110 | |
111 | |
112 | |
12fb4a80 |
113 | has on_close => ( |
8ed52376 |
114 | is => 'rw', default => sub { CPS::Future->new }, |
07105aca |
115 | trigger => sub { |
12fb4a80 |
116 | my ($self, $f) = @_; |
07105aca |
117 | Dlog_trace { "trigger for on_close has been invoked for connection $_" } $self->_id; |
12fb4a80 |
118 | weaken($self); |
119 | $f->on_done(sub { |
8ed52376 |
120 | Dlog_trace { "failing all of the outstanding futures for connection $_" } $self->_id; |
69aaad21 |
121 | $self->_fail_outstanding("Object::Remote connection lost: " . ($f->get)[0]); |
12fb4a80 |
122 | }); |
123 | } |
124 | ); |
ad4f54b2 |
125 | |
47c83a13 |
126 | has child_pid => (is => 'ro'); |
127 | |
11dbd4a0 |
128 | has local_objects_by_id => ( |
129 | is => 'ro', default => sub { {} }, |
130 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
131 | ); |
9e72f0cf |
132 | |
11dbd4a0 |
133 | has remote_objects_by_id => ( |
134 | is => 'ro', default => sub { {} }, |
135 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
136 | ); |
9e72f0cf |
137 | |
a980b0b8 |
138 | has outstanding_futures => (is => 'ro', default => sub { {} }); |
139 | |
12fb4a80 |
140 | sub _fail_outstanding { |
141 | my ($self, $error) = @_; |
07105aca |
142 | Dlog_debug { "Failing outstanding futures with '$error' for connection $_" } $self->_id; |
12fb4a80 |
143 | my $outstanding = $self->outstanding_futures; |
144 | $_->fail($error) for values %$outstanding; |
145 | %$outstanding = (); |
146 | return; |
147 | } |
148 | |
9e72f0cf |
149 | has _json => ( |
150 | is => 'lazy', |
151 | handles => { |
152 | _deserialize => 'decode', |
153 | _encode => 'encode', |
154 | }, |
155 | ); |
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 { |
192 | my %tied_hash; |
193 | tie %tied_hash, 'Object::Remote::Tied', $self->_id_to_remote_object(@_); |
194 | return \%tied_hash; |
195 | } |
196 | )->filter_json_single_key_object( |
197 | __remote_tied_array__ => sub { |
198 | my @tied_array; |
199 | tie @tied_array, 'Object::Remote::Tied', $self->_id_to_remote_object(@_); |
200 | return \@tied_array; |
201 | } |
202 | ); |
9e72f0cf |
203 | } |
204 | |
69aaad21 |
205 | sub _load_if_possible { |
206 | my ($class) = @_; |
207 | |
208 | eval "require $class"; |
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; |
9e72f0cf |
304 | delete $self->remote_objects_by_id->{$id}; |
305 | $self->_send([ free => $id ]); |
306 | } |
307 | |
308 | sub send { |
309 | my ($self, $type, @call) = @_; |
310 | |
deb77aaf |
311 | my $future = CPS::Future->new; |
a2d43709 |
312 | my $remote = $self->remote_objects_by_id->{$call[0]}; |
deb77aaf |
313 | |
314 | unshift @call, $type => $self->_local_object_to_id($future); |
9e72f0cf |
315 | |
a980b0b8 |
316 | my $outstanding = $self->outstanding_futures; |
317 | $outstanding->{$future} = $future; |
a2d43709 |
318 | $future->on_ready(sub { |
319 | undef($remote); |
320 | delete $outstanding->{$future} |
321 | }); |
a980b0b8 |
322 | |
9e72f0cf |
323 | $self->_send(\@call); |
324 | |
325 | return $future; |
326 | } |
327 | |
9d804009 |
328 | sub send_discard { |
329 | my ($self, $type, @call) = @_; |
330 | |
deb77aaf |
331 | unshift @call, $type => 'NULL'; |
9d804009 |
332 | |
333 | $self->_send(\@call); |
334 | } |
335 | |
9e72f0cf |
336 | sub _send { |
337 | my ($self, $to_send) = @_; |
2d81cf18 |
338 | my $fh = $self->send_to_fh; |
07105aca |
339 | Dlog_trace { "Starting to serialize data in argument to _send for connection $_" } $self->_id; |
2d81cf18 |
340 | my $serialized = $self->_serialize($to_send)."\n"; |
5953edf6 |
341 | Dlog_trace { my $l = length($serialized); "serialization is completed; sending '$l' characters of serialized data to $_" } $fh; |
2d81cf18 |
342 | #TODO this is very risky for deadlocks unless it's set to non-blocking and then with out extra |
b51a8453 |
343 | #logic it could easily do short-writes to the remote side - how about taking this entire buffer |
344 | #and having the run loop send it to the file handle so this doesn't block while the sending |
345 | #is happening? |
8ed52376 |
346 | my $ret; |
347 | eval { |
348 | local($SIG{PIPE}) = 'IGNORE'; |
349 | die "filehandle is not open" unless openhandle($fh); |
350 | log_trace { "file handle has passed openhandle() test; printing to it" }; |
351 | $ret = print $fh $serialized; |
352 | die "print was not successful: $!" unless defined $ret |
353 | }; |
69aaad21 |
354 | |
8ed52376 |
355 | if ($@) { |
356 | Dlog_debug { "exception encountered when trying to write to file handle $_: $@" } $fh; |
357 | my $error = $@; chomp($error); |
358 | $self->on_close->done("could not write to file handle: $error") unless $self->on_close->is_ready; |
359 | return; |
360 | } |
361 | |
2d81cf18 |
362 | return $ret; |
9e72f0cf |
363 | } |
364 | |
365 | sub _serialize { |
366 | my ($self, $data) = @_; |
3687a42d |
367 | local our @New_Ids = (-1); |
9d804009 |
368 | return eval { |
ad4f54b2 |
369 | my $flat = $self->_encode($self->_deobjectify($data)); |
370 | warn "$$ >>> ${flat}\n" if $DEBUG; |
371 | $flat; |
70a578ac |
372 | } || do { |
9d804009 |
373 | my $err = $@; # won't get here if the eval doesn't die |
374 | # don't keep refs to new things |
375 | delete @{$self->local_objects_by_id}{@New_Ids}; |
376 | die "Error serializing: $err"; |
377 | }; |
9e72f0cf |
378 | } |
379 | |
a76f2f60 |
380 | sub _local_object_to_id { |
381 | my ($self, $object) = @_; |
382 | my $id = refaddr($object); |
383 | $self->local_objects_by_id->{$id} ||= do { |
3687a42d |
384 | push our(@New_Ids), $id if @New_Ids; |
a76f2f60 |
385 | $object; |
386 | }; |
387 | return $id; |
388 | } |
389 | |
9e72f0cf |
390 | sub _deobjectify { |
391 | my ($self, $data) = @_; |
392 | if (blessed($data)) { |
a76f2f60 |
393 | return +{ __remote_object__ => $self->_local_object_to_id($data) }; |
9e72f0cf |
394 | } elsif (my $ref = ref($data)) { |
395 | if ($ref eq 'HASH') { |
b51a8453 |
396 | my $tied_to = tied(%$data); |
397 | if(defined($tied_to)) { |
398 | return +{__remote_tied_hash__ => $self->_local_object_to_id($tied_to)}; |
399 | } else { |
400 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
401 | } |
9e72f0cf |
402 | } elsif ($ref eq 'ARRAY') { |
b51a8453 |
403 | my $tied_to = tied(@$data); |
404 | if (defined($tied_to)) { |
405 | return +{__remote_tied_array__ => $self->_local_object_to_id($tied_to)}; |
406 | } else { |
407 | return [ map $self->_deobjectify($_), @$data ]; |
408 | } |
fe6c9a7f |
409 | } elsif ($ref eq 'CODE') { |
410 | my $id = $self->_local_object_to_id( |
411 | Object::Remote::CodeContainer->new(code => $data) |
412 | ); |
413 | return +{ __remote_code__ => $id }; |
6ed5d580 |
414 | } elsif ($ref eq 'SCALAR') { |
415 | return +{ __scalar_ref__ => $$data }; |
ed5a8a8e |
416 | } elsif ($ref eq 'GLOB') { |
417 | return +{ __glob_ref__ => $self->_local_object_to_id( |
418 | Object::Remote::GlobContainer->new(handle => $data) |
419 | ) }; |
9e72f0cf |
420 | } else { |
421 | die "Can't collapse reftype $ref"; |
422 | } |
423 | } |
424 | return $data; # plain scalar |
425 | } |
426 | |
9e72f0cf |
427 | sub _receive { |
ad4f54b2 |
428 | my ($self, $flat) = @_; |
429 | warn "$$ <<< $flat\n" if $DEBUG; |
07105aca |
430 | Dlog_trace { my $l = length($flat); "Starting to deserialize $l characters of data for connection $_" } $self->_id; |
ad4f54b2 |
431 | my ($type, @rest) = eval { @{$self->_deserialize($flat)} } |
432 | or do { warn "Deserialize failed for ${flat}: $@"; return }; |
07105aca |
433 | Dlog_trace { "deserialization complete for connection $_" } $self->_id; |
9e72f0cf |
434 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
ad4f54b2 |
435 | or do { warn "Receive failed for ${flat}: $@"; return }; |
9e72f0cf |
436 | return; |
437 | } |
438 | |
439 | sub receive_free { |
440 | my ($self, $id) = @_; |
07105aca |
441 | Dlog_trace { "got a receive_free for object '$id' for connection $_" } $self->_id; |
9d804009 |
442 | delete $self->local_objects_by_id->{$id} |
443 | or warn "Free: no such object $id"; |
9e72f0cf |
444 | return; |
445 | } |
446 | |
447 | sub receive_call { |
deb77aaf |
448 | my ($self, $future_id, $id, @rest) = @_; |
07105aca |
449 | Dlog_trace { "got a receive_call for object '$id' for connection $_" } $self->_id; |
deb77aaf |
450 | my $future = $self->_id_to_remote_object($future_id); |
8131a88a |
451 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
452 | my $local = $self->local_objects_by_id->{$id} |
453 | or do { $future->fail("No such object $id"); return }; |
454 | $self->_invoke($future, $local, @rest); |
455 | } |
456 | |
8131a88a |
457 | sub receive_call_free { |
458 | my ($self, $future, $id, @rest) = @_; |
07105aca |
459 | Dlog_trace { "got a receive_call_free for object '$id' for connection $_" } $self->_id; |
84b04178 |
460 | $self->receive_call($future, $id, undef, @rest); |
8131a88a |
461 | $self->receive_free($id); |
462 | } |
463 | |
9e72f0cf |
464 | sub _invoke { |
84b04178 |
465 | my ($self, $future, $local, $ctx, $method, @args) = @_; |
07105aca |
466 | Dlog_trace { "got _invoke for a method named '$method' for connection $_" } $self->_id; |
dc28afe8 |
467 | if ($method =~ /^start::/) { |
468 | my $f = $local->$method(@args); |
469 | $f->on_done(sub { undef($f); $future->done(@_) }); |
3f1f1e66 |
470 | return unless $f; |
dc28afe8 |
471 | $f->on_fail(sub { undef($f); $future->fail(@_) }); |
472 | return; |
473 | } |
84b04178 |
474 | my $do = sub { $local->$method(@args) }; |
475 | eval { |
476 | $future->done( |
477 | defined($ctx) |
478 | ? ($ctx ? $do->() : scalar($do->())) |
479 | : do { $do->(); () } |
480 | ); |
481 | 1; |
482 | } or do { $future->fail($@); return; }; |
9e72f0cf |
483 | return; |
484 | } |
485 | |
9e72f0cf |
486 | 1; |
b9a9982d |
487 | |
488 | =head1 NAME |
489 | |
490 | Object::Remote::Connection - An underlying connection for L<Object::Remote> |
491 | |
492 | =head1 LAME |
493 | |
494 | Shipping prioritised over writing this part up. Blame mst. |
495 | |
496 | =cut |