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 | |
58 | |
6db5156c |
59 | our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG}; |
9031635d |
60 | #numbering each connection allows it to be |
61 | #tracked along with file handles in |
62 | #the logs |
c824fdf3 |
63 | |
9031635d |
64 | has _id => ( is => 'ro', required => 1, default => sub { our $NEXT_CONNECTION_ID++ } ); |
ad4f54b2 |
65 | |
9e72f0cf |
66 | has send_to_fh => ( |
67 | is => 'ro', required => 1, |
90115979 |
68 | trigger => sub { |
69 | my $self = $_[0]; |
70 | $_[1]->autoflush(1); |
71 | Dlog_trace { my $id = $self->_id; "connection had send_to_fh set to $_" } $_[1]; |
72 | }, |
9e72f0cf |
73 | ); |
74 | |
c824fdf3 |
75 | #TODO see if this is another case of the same bug below |
76 | #where trigger never fires because the attribute isn't |
77 | #actually set at any time |
12fb4a80 |
78 | has read_channel => ( |
9e72f0cf |
79 | is => 'ro', required => 1, |
80 | trigger => sub { |
12fb4a80 |
81 | my ($self, $ch) = @_; |
f8080c1c |
82 | my $id = $self->_id; |
83 | Dlog_trace { "trigger for read_channel has been invoked for connection $id; file handle is $_" } $ch->fh; |
9e72f0cf |
84 | weaken($self); |
12fb4a80 |
85 | $ch->on_line_call(sub { $self->_receive(@_) }); |
f8080c1c |
86 | $ch->on_close_call(sub { |
87 | log_trace { "invoking 'done' on on_close handler for connection id '$id'" }; |
88 | $self->on_close->done(@_); |
89 | }); |
9e72f0cf |
90 | }, |
91 | ); |
92 | |
f8080c1c |
93 | #TODO properly fix this bug - |
94 | #trigger can't ever be invoked with a default |
95 | #value and the on_close attribute is read only.... |
96 | #the future never gets the on_done handler |
97 | #installed |
c824fdf3 |
98 | sub BUILD { |
f8080c1c |
99 | my ($self) = @_; |
c824fdf3 |
100 | $self->on_close(CPS::Future->new); |
f8080c1c |
101 | } |
102 | |
c824fdf3 |
103 | after BUILD => sub { |
104 | my ($self) = @_; |
105 | |
106 | return unless defined $self->child_pid; |
107 | |
108 | log_debug { "Setting process group of child process" }; |
109 | |
110 | setpgrp($self->child_pid, 1); |
111 | }; |
112 | |
113 | |
12fb4a80 |
114 | has on_close => ( |
f8080c1c |
115 | is => 'rw', default => sub { CPS::Future->new }, |
9031635d |
116 | trigger => sub { |
12fb4a80 |
117 | my ($self, $f) = @_; |
9031635d |
118 | Dlog_trace { "trigger for on_close has been invoked for connection $_" } $self->_id; |
12fb4a80 |
119 | weaken($self); |
120 | $f->on_done(sub { |
f8080c1c |
121 | Dlog_trace { "failing all of the outstanding futures for connection $_" } $self->_id; |
c824fdf3 |
122 | $self->_fail_outstanding("Object::Remote connection lost: " . ($f->get)[0]); |
12fb4a80 |
123 | }); |
124 | } |
125 | ); |
ad4f54b2 |
126 | |
47c83a13 |
127 | has child_pid => (is => 'ro'); |
128 | |
11dbd4a0 |
129 | has local_objects_by_id => ( |
130 | is => 'ro', default => sub { {} }, |
131 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
132 | ); |
9e72f0cf |
133 | |
11dbd4a0 |
134 | has remote_objects_by_id => ( |
135 | is => 'ro', default => sub { {} }, |
136 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
137 | ); |
9e72f0cf |
138 | |
a980b0b8 |
139 | has outstanding_futures => (is => 'ro', default => sub { {} }); |
140 | |
12fb4a80 |
141 | sub _fail_outstanding { |
142 | my ($self, $error) = @_; |
9031635d |
143 | Dlog_debug { "Failing outstanding futures with '$error' for connection $_" } $self->_id; |
12fb4a80 |
144 | my $outstanding = $self->outstanding_futures; |
b7a853b3 |
145 | $_->fail("$error\n") for values %$outstanding; |
12fb4a80 |
146 | %$outstanding = (); |
147 | return; |
148 | } |
149 | |
9e72f0cf |
150 | has _json => ( |
151 | is => 'lazy', |
152 | handles => { |
153 | _deserialize => 'decode', |
154 | _encode => 'encode', |
155 | }, |
156 | ); |
157 | |
fe6c9a7f |
158 | sub _id_to_remote_object { |
159 | my ($self, $id) = @_; |
9031635d |
160 | Dlog_trace { "fetching proxy for remote object with id '$id' for connection $_" } $self->_id; |
fe6c9a7f |
161 | return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; |
162 | ( |
163 | $self->remote_objects_by_id->{$id} |
164 | or Object::Remote::Handle->new(connection => $self, id => $id) |
165 | )->proxy; |
166 | } |
167 | |
9e72f0cf |
168 | sub _build__json { |
169 | weaken(my $self = shift); |
9e72f0cf |
170 | JSON::PP->new->filter_json_single_key_object( |
171 | __remote_object__ => sub { |
fe6c9a7f |
172 | $self->_id_to_remote_object(@_); |
173 | } |
174 | )->filter_json_single_key_object( |
175 | __remote_code__ => sub { |
176 | my $code_container = $self->_id_to_remote_object(@_); |
177 | sub { $code_container->call(@_) }; |
9e72f0cf |
178 | } |
6ed5d580 |
179 | )->filter_json_single_key_object( |
180 | __scalar_ref__ => sub { |
181 | my $value = shift; |
182 | return \$value; |
183 | } |
ed5a8a8e |
184 | )->filter_json_single_key_object( |
185 | __glob_ref__ => sub { |
186 | my $glob_container = $self->_id_to_remote_object(@_); |
187 | my $handle = Symbol::gensym; |
188 | tie *$handle, 'Object::Remote::GlobProxy', $glob_container; |
189 | return $handle; |
190 | } |
6163a5aa |
191 | )->filter_json_single_key_object( |
192 | __local_object__ => sub { |
193 | $self->local_objects_by_id->{$_[0]} |
194 | } |
7790ca36 |
195 | )->filter_json_single_key_object( |
196 | __remote_tied_hash__ => sub { |
197 | my %tied_hash; |
198 | tie %tied_hash, 'Object::Remote::Tied', $self->_id_to_remote_object(@_); |
199 | return \%tied_hash; |
200 | } |
201 | )->filter_json_single_key_object( |
202 | __remote_tied_array__ => sub { |
203 | my @tied_array; |
204 | tie @tied_array, 'Object::Remote::Tied', $self->_id_to_remote_object(@_); |
205 | return \@tied_array; |
206 | } |
207 | ); |
9e72f0cf |
208 | } |
209 | |
c824fdf3 |
210 | sub _load_if_possible { |
211 | my ($class) = @_; |
212 | |
213 | eval "require $class"; |
214 | |
215 | if ($@) { |
216 | log_debug { "Attempt at loading '$class' failed with '$@'" }; |
217 | } |
218 | |
219 | } |
220 | |
84b04178 |
221 | BEGIN { |
222 | unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; |
c824fdf3 |
223 | map _load_if_possible($_), qw( |
224 | Object::Remote::Connector::Local |
225 | Object::Remote::Connector::LocalSudo |
226 | Object::Remote::Connector::SSH |
227 | Object::Remote::Connector::UNIX |
228 | ); |
84b04178 |
229 | } |
230 | |
c824fdf3 |
231 | sub conn_from_spec { |
232 | my ($class, $spec, @args) = @_; |
84b04178 |
233 | foreach my $poss (do { our @Guess }) { |
c824fdf3 |
234 | if (my $conn = $poss->($spec, @args)) { |
235 | return $conn; |
fbd3b8ec |
236 | } |
84b04178 |
237 | } |
c824fdf3 |
238 | |
239 | return undef; |
240 | } |
241 | |
242 | sub new_from_spec { |
243 | my ($class, $spec) = @_; |
244 | return $spec if blessed $spec; |
245 | my $conn = $class->conn_from_spec($spec); |
246 | |
247 | die "Couldn't figure out what to do with ${spec}" |
248 | unless defined $conn; |
249 | |
250 | return $conn->maybe::start::connect; |
84b04178 |
251 | } |
252 | |
11dbd4a0 |
253 | sub remote_object { |
e144d525 |
254 | my ($self, @args) = @_; |
255 | Object::Remote::Handle->new( |
256 | connection => $self, @args |
257 | )->proxy; |
258 | } |
259 | |
4c17fea5 |
260 | sub connect { |
261 | my ($self, $to) = @_; |
9031635d |
262 | Dlog_debug { "Creating connection to remote node '$to' for connection $_" } $self->_id; |
deb77aaf |
263 | return await_future( |
264 | $self->send_class_call(0, 'Object::Remote', connect => $to) |
265 | ); |
4c17fea5 |
266 | } |
267 | |
11dbd4a0 |
268 | sub remote_sub { |
c6fe6fbd |
269 | my ($self, $sub) = @_; |
270 | my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/; |
9031635d |
271 | Dlog_debug { "Invoking remote sub '$sub' for connection $_" } $self->_id; |
deb77aaf |
272 | return await_future($self->send_class_call(0, $pkg, can => $name)); |
273 | } |
274 | |
275 | sub send_class_call { |
276 | my ($self, $ctx, @call) = @_; |
9031635d |
277 | Dlog_trace { "Sending a class call for connection $_" } $self->_id; |
deb77aaf |
278 | $self->send(call => class_call_handler => $ctx => call => @call); |
279 | } |
280 | |
281 | sub register_class_call_handler { |
282 | my ($self) = @_; |
3687a42d |
283 | $self->local_objects_by_id->{'class_call_handler'} ||= do { |
c5736e1c |
284 | my $o = $self->new_class_call_handler; |
3687a42d |
285 | $self->_local_object_to_id($o); |
286 | $o; |
287 | }; |
c6fe6fbd |
288 | } |
289 | |
c5736e1c |
290 | sub new_class_call_handler { |
291 | Object::Remote::CodeContainer->new( |
292 | code => sub { |
293 | my ($class, $method) = (shift, shift); |
294 | use_module($class)->$method(@_); |
295 | } |
296 | ); |
297 | } |
298 | |
9e72f0cf |
299 | sub register_remote { |
300 | my ($self, $remote) = @_; |
9031635d |
301 | Dlog_trace { my $i = $remote->id; "Registered a remote object with id of '$i' for connection $_" } $self->_id; |
9e72f0cf |
302 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
303 | return $remote; |
304 | } |
305 | |
306 | sub send_free { |
307 | my ($self, $id) = @_; |
7790ca36 |
308 | Dlog_trace { "sending request to free object '$id' for connection $_" } $self->_id; |
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; |
9031635d |
344 | Dlog_trace { "Starting to serialize data in argument to _send for connection $_" } $self->_id; |
9d64d2d9 |
345 | my $serialized = $self->_serialize($to_send)."\n"; |
6b7b2732 |
346 | Dlog_trace { my $l = length($serialized); "serialization is completed; sending '$l' characters of serialized data to $_" } $fh; |
9d64d2d9 |
347 | #TODO this is very risky for deadlocks unless it's set to non-blocking and then with out extra |
7790ca36 |
348 | #logic it could easily do short-writes to the remote side - how about taking this entire buffer |
349 | #and having the run loop send it to the file handle so this doesn't block while the sending |
350 | #is happening? |
f8080c1c |
351 | my $ret; |
352 | eval { |
353 | local($SIG{PIPE}) = 'IGNORE'; |
354 | die "filehandle is not open" unless openhandle($fh); |
355 | log_trace { "file handle has passed openhandle() test; printing to it" }; |
356 | $ret = print $fh $serialized; |
357 | die "print was not successful: $!" unless defined $ret |
358 | }; |
c824fdf3 |
359 | |
f8080c1c |
360 | if ($@) { |
361 | Dlog_debug { "exception encountered when trying to write to file handle $_: $@" } $fh; |
362 | my $error = $@; chomp($error); |
363 | $self->on_close->done("could not write to file handle: $error") unless $self->on_close->is_ready; |
364 | return; |
365 | } |
366 | |
9d64d2d9 |
367 | return $ret; |
9e72f0cf |
368 | } |
369 | |
370 | sub _serialize { |
371 | my ($self, $data) = @_; |
3687a42d |
372 | local our @New_Ids = (-1); |
9d804009 |
373 | return eval { |
ad4f54b2 |
374 | my $flat = $self->_encode($self->_deobjectify($data)); |
375 | warn "$$ >>> ${flat}\n" if $DEBUG; |
376 | $flat; |
70a578ac |
377 | } || do { |
9d804009 |
378 | my $err = $@; # won't get here if the eval doesn't die |
379 | # don't keep refs to new things |
380 | delete @{$self->local_objects_by_id}{@New_Ids}; |
381 | die "Error serializing: $err"; |
382 | }; |
9e72f0cf |
383 | } |
384 | |
a76f2f60 |
385 | sub _local_object_to_id { |
386 | my ($self, $object) = @_; |
387 | my $id = refaddr($object); |
388 | $self->local_objects_by_id->{$id} ||= do { |
3687a42d |
389 | push our(@New_Ids), $id if @New_Ids; |
a76f2f60 |
390 | $object; |
391 | }; |
392 | return $id; |
393 | } |
394 | |
9e72f0cf |
395 | sub _deobjectify { |
396 | my ($self, $data) = @_; |
397 | if (blessed($data)) { |
6163a5aa |
398 | if ( |
399 | $data->isa('Object::Remote::Proxy') |
400 | and $data->{remote}->connection == $self |
401 | ) { |
402 | return +{ __local_object__ => $data->{remote}->id }; |
403 | } else { |
404 | return +{ __remote_object__ => $self->_local_object_to_id($data) }; |
405 | } |
9e72f0cf |
406 | } elsif (my $ref = ref($data)) { |
407 | if ($ref eq 'HASH') { |
7790ca36 |
408 | my $tied_to = tied(%$data); |
409 | if(defined($tied_to)) { |
410 | return +{__remote_tied_hash__ => $self->_local_object_to_id($tied_to)}; |
411 | } else { |
412 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
413 | } |
9e72f0cf |
414 | } elsif ($ref eq 'ARRAY') { |
7790ca36 |
415 | my $tied_to = tied(@$data); |
416 | if (defined($tied_to)) { |
417 | return +{__remote_tied_array__ => $self->_local_object_to_id($tied_to)}; |
418 | } else { |
419 | return [ map $self->_deobjectify($_), @$data ]; |
420 | } |
fe6c9a7f |
421 | } elsif ($ref eq 'CODE') { |
422 | my $id = $self->_local_object_to_id( |
423 | Object::Remote::CodeContainer->new(code => $data) |
424 | ); |
425 | return +{ __remote_code__ => $id }; |
6ed5d580 |
426 | } elsif ($ref eq 'SCALAR') { |
427 | return +{ __scalar_ref__ => $$data }; |
ed5a8a8e |
428 | } elsif ($ref eq 'GLOB') { |
429 | return +{ __glob_ref__ => $self->_local_object_to_id( |
430 | Object::Remote::GlobContainer->new(handle => $data) |
431 | ) }; |
9e72f0cf |
432 | } else { |
433 | die "Can't collapse reftype $ref"; |
434 | } |
435 | } |
436 | return $data; # plain scalar |
437 | } |
438 | |
9e72f0cf |
439 | sub _receive { |
ad4f54b2 |
440 | my ($self, $flat) = @_; |
441 | warn "$$ <<< $flat\n" if $DEBUG; |
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 |