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