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