Remote/Connection.pm

filter: Statementstmt 74.7% 248 / 332 Branchbran 62.8% 49 / 78 Conditioncond 40.0% 6 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 67.3% 62 / 92 Podpod 0.0% 0 / 17 totaltotal 67.2% 366 / 544 CC 98 SCAR 14.4
CC
98
Cov
71%
DEMOLISH
20.0
DEMOLISH
20.0
_deobjectify
12.6
CRAP 4.2
« Remote/FatNode.pm ↑ Summary Remote/MiniLoop.pm »
1 package Object::Remote::Connection;
2  
3 22 ▶use Object::Remote::Logging qw (:log :dlog router);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use Object::Remote::Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Object::Remote::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use Object::Remote::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Object::Remote::CodeContainer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Object::Remote::GlobProxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Object::Remote::GlobContainer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Object::Remote::Tied;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Object::Remote;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Scalar::Util qw(weaken blessed refaddr openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use JSON::PP qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 11 ▶BEGIN { router()->exclude_forwarding }
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 11 ▶END {
Subroutine END: called CC=2 SCAR=10.3
25 0 ▶ our %child_pids;
Subroutine END: not called CC=2 SCAR=10.3
26  
27 0 ▶ log_trace { "END handler is being invoked in " . __PACKAGE__ };
Subroutine __ANON__: not called CC=1 SCAR=2.6
28  
29 0 foreach(keys(%child_pids)) {
30 0 ▶ log_debug { "Killing child process '$_'" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
31 0 kill('TERM', $_);
32 }
33 }
34  
35 has _id => ( is => 'ro', required => 1, default => sub { our $NEXT_CONNECTION_ID++ } );
36  
37 has send_to_fh => (
38 is => 'ro', required => 1,
39 trigger => sub {
40 my $self = $_[0];
41 $_[1]->autoflush(1);
42 Dlog_trace { my $id = $self->_id; "connection had send_to_fh set to $_" } $_[1];
43 },
44 );
45  
46 has read_channel => (
47 is => 'ro', required => 1,
48 trigger => sub {
49 my ($self, $ch) = @_;
50 my $id = $self->_id;
51 Dlog_trace { "trigger for read_channel has been invoked for connection $id; file handle is $_" } $ch->fh;
52 weaken($self);
53 $ch->on_line_call(sub { $self->_receive(@_) });
54 $ch->on_close_call(sub {
55 log_trace { "invoking 'done' on on_close handler for connection id '$id'" };
56 $self->on_close->done(@_)
57 unless $self->on_close->is_ready;
58 });
59 },
60 );
61  
62 has on_close => (
63 is => 'rw', default => sub { $_[0]->_install_future_handlers(Object::Remote->current_loop->new_future) },
64 trigger => sub {
65 log_trace { "Installing handlers into future via trigger" };
66 $_[0]->_install_future_handlers($_[1])
67 },
68 );
69  
70 has child_pid => (is => 'ro');
71  
72 has local_objects_by_id => (
73 is => 'ro', default => sub { {} },
74 coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in
75 );
76  
77 has remote_objects_by_id => (
78 is => 'ro', default => sub { {} },
79 coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in
80 );
81  
82 has outstanding_futures => (is => 'ro', default => sub { {} });
83  
84 has _json => (
85 is => 'lazy',
86 handles => {
87 _deserialize => 'decode',
88 _encode => 'encode',
89 },
90 );
91  
92 after BUILD => sub {
93 my ($self) = @_;
94 my $pid = $self->child_pid;
95 our %child_pids;
96 return unless defined $pid;
97 $child_pids{$pid} = 1;
98 return;
99 };
100  
101 sub BUILD { }
102  
103 sub is_valid {
104 19 ▶ my ($self) = @_;
Subroutine is_valid: called CC=1 SCAR=0.2
Pod undocumented
105 19 my $valid = ! $self->on_close->is_ready;
106  
107 log_trace {
108 0 ▶ my $id = $self->_id;
Subroutine __ANON__: called CC=2 SCAR=7.9
109 0 my $text;
110 19 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) { } 1 0 1
111 19 $text = 'yes';
112 } else {
113 19 $text = 'no';
114 }
115 19 "Connection '$id' is valid: '$text'"
116 19 };
117  
118 19 return $valid;
119 }
120  
121 sub _fail_outstanding {
122 19 ▶ my ($self, $error) = @_;
Subroutine _fail_outstanding: not called CC=2 SCAR=8.7
123 19 my $outstanding = $self->outstanding_futures;
124  
125 Dlog_debug {
126 0 ▶ sprintf "Failing %i outstanding futures with '$error'", scalar(keys(%$outstanding))
Subroutine __ANON__: called CC=1 SCAR=6.9
127 0 };
128  
129 168 foreach(keys(%$outstanding)) {
130 1 ▶ log_trace { "Failing future for $_" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
131 1 my $future = $outstanding->{$_};
132 0 $future->fail("$error\n");
133 }
134  
135 19 %$outstanding = ();
136 0 return;
137 }
138  
139 sub _install_future_handlers {
140 38 ▶ my ($self, $f) = @_;
Subroutine _install_future_handlers: called CC=1 SCAR=0.1
141 38 our %child_pids;
142 38 ▶ Dlog_trace { "Installing handlers into future for connection $_" } $self->_id;
Subroutine __ANON__: called CC=1 SCAR=0.0
143 38 weaken($self);
144 $f->on_done(sub {
145 20 ▶ my $pid = $self->child_pid;
Subroutine __ANON__: called CC=4 SCAR=14.8
146 20 ▶ Dlog_trace { "Executing on_done handler in future for connection $_" } $self->_id;
Subroutine __ANON__: called CC=1 SCAR=1.2
147 20 $self->_fail_outstanding("Object::Remote connection lost: " . ($f->get)[0]);
148 20 ▶ return unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 20 20
149 20 ▶ log_debug { "Waiting for child '$pid' to exit" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
150 20 my $ret = waitpid($pid, 0);
151 305 ▶ if ($ret != $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret != $pid) { } 0 1 1
elsif ($? & 127) { } 0 1 1
152 304 ▶ log_debug { "Waited for pid $pid but waitpid() returned $ret" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
153 304 return;
154 } elsif ($? & 127) {
155 304 ▶ log_warn { "Remote interpreter did not exit cleanly" };
Subroutine __ANON__: called CC=1 SCAR=1.2
156 } else {
157 log_verbose {
158 1 ▶ my $exit_value = $? >> 8;
Subroutine __ANON__: called CC=1 SCAR=0.0
159 1 "Remote Perl interpreter exited with value '$exit_value'"
160 1 };
161 }
162  
163 1 delete $child_pids{$pid};
164 38 });
165 38 return $f;
166 };
167  
168 sub _id_to_remote_object {
169 238 ▶ my ($self, $id) = @_;
Subroutine _id_to_remote_object: called CC=3 SCAR=11.0
170 238 ▶ Dlog_trace { "fetching proxy for remote object with id '$id' for connection $_" } $self->_id;
Subroutine __ANON__: called CC=1 SCAR=0.0
171 242 ▶ return bless({}, 'Object::Remote::Null') if $id eq 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "NULL" 147 90 237
172 (
173 95 ▶ $self->remote_objects_by_id->{$id}
Condition66%2/3
$self->remote_objects_by_id->{$id} || 'Object::Remote::Handle'->new('connection', $self, 'id', $id)
l!l&&r!l&&!r
1890
MC/DC0%0/2
$self->remote_objects_by_id->{$id} || 'Object::Remote::Handle'->new('connection', $self, 'id', $id)
$self->remote_objects_by_id->{$id} 'Object::Remote::Handle'->new('connection', $self, 'id', $id)
Truth table66%2/3
A || B
A= $self->remote_objects_by_id->{$id}B= 'Object::Remote::Handle'->new('connection', $self, 'id', $id)
ABresult
1X1
011
000
174 or Object::Remote::Handle->new(connection => $self, id => $id)
175 )->proxy;
176 }
177  
178 sub _build__json {
179 20 ▶ weaken(my $self = shift);
Subroutine _build__json: called CC=1 SCAR=0.0
180 JSON::PP->new->filter_json_single_key_object(
181 __remote_object__ => sub {
182 42 ▶ $self->_id_to_remote_object(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
183 }
184 )->filter_json_single_key_object(
185 __remote_code__ => sub {
186 21 ▶ my $code_container = $self->_id_to_remote_object(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
187 21 ▶ sub { $code_container->call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
188 }
189 )->filter_json_single_key_object(
190 __scalar_ref__ => sub {
191 2 ▶ my $value = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
192 2 return \$value;
193 }
194 )->filter_json_single_key_object(
195 __glob_ref__ => sub {
196 4 ▶ my $glob_container = $self->_id_to_remote_object(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
197 4 my $handle = Symbol::gensym;
198 4 tie *$handle, 'Object::Remote::GlobProxy', $glob_container;
199 4 return $handle;
200 }
201 )->filter_json_single_key_object(
202 __local_object__ => sub {
203 0 ▶ $self->local_objects_by_id->{$_[0]}
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 }
205 )->filter_json_single_key_object(
206 __remote_tied_hash__ => sub {
207 1 ▶ my %tied_hash;
Subroutine __ANON__: called CC=1 SCAR=0.0
208 1 tie %tied_hash, 'Object::Remote::Tied', $self->_id_to_remote_object(@_);
209 1 return \%tied_hash;
210 }
211 )->filter_json_single_key_object(
212 __remote_tied_array__ => sub {
213 1 ▶ my @tied_array;
Subroutine __ANON__: called CC=1 SCAR=0.0
214 1 tie @tied_array, 'Object::Remote::Tied', $self->_id_to_remote_object(@_);
215 1 return \@tied_array;
216 }
217 20 );
218 }
219  
220 sub _load_if_possible {
221 55 ▶ my ($class) = @_;
Subroutine _load_if_possible: called CC=2 SCAR=8.4
222  
223 55 use_module($class);
224  
225 55 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 55 55
226 0 ▶ log_debug { "Attempt at loading '$class' failed with '$@'" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 }
228  
229 }
230  
231 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
232 11 unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef };
233 11 map _load_if_possible($_), qw(
234 Object::Remote::Connector::Local
235 Object::Remote::Connector::LocalSudo
236 Object::Remote::Connector::SSH
237 Object::Remote::Connector::UNIX
238 Object::Remote::Connector::INET
239 );
240 }
241  
242 sub conn_from_spec {
243 18 ▶ my ($class, $spec, @args) = @_;
Subroutine conn_from_spec: called CC=3 SCAR=11.0
Pod undocumented
244 18 foreach my $poss (do { our @Guess }) {
245 18 ▶ if (my $conn = $poss->($spec, @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = &$poss($spec, @args)) 0 18 18
246 36 return $conn;
247 }
248 }
249  
250 18 return undef;
251 }
252  
253 sub new_from_spec {
254 0 ▶ my ($class, $spec, @args) = @_;
Subroutine new_from_spec: called CC=3 SCAR=11.0
Pod undocumented
255 28 ▶ return $spec if blessed $spec;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($spec) 18 18 36
256 28 my $conn = $class->conn_from_spec($spec, @args);
257  
258 18 ▶ die "Couldn't figure out what to do with ${spec}"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 10 18 28
259 unless defined $conn;
260  
261 18 return $conn->maybe::start::connect;
262 }
263  
264 sub remote_object {
265 18 ▶ my ($self, @args) = @_;
Subroutine remote_object: called CC=1 SCAR=0.0
Pod undocumented
266 20 Object::Remote::Handle->new(
267 connection => $self, @args
268 )->proxy;
269 }
270  
271 sub connect {
272 20 ▶ my ($self, $to) = @_;
Subroutine connect: called CC=1 SCAR=3.5
Pod undocumented
273 0 ▶ Dlog_debug { "Creating connection to remote node '$to' for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=6.9
274 0 return await_future(
275 $self->send_class_call(0, 'Object::Remote', connect => $to)
276 );
277 }
278  
279 sub remote_sub {
280 21 ▶ my ($self, $sub) = @_;
Subroutine remote_sub: called CC=1 SCAR=0.1
Pod undocumented
281 21 my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/;
282 21 ▶ Dlog_debug { "Invoking remote sub '$sub' for connection '$_'" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
283 21 return await_future($self->send_class_call(0, $pkg, can => $name));
284 }
285  
286 sub send_class_call {
287 65 ▶ my ($self, $ctx, @call) = @_;
Subroutine send_class_call: called CC=1 SCAR=0.2
Pod undocumented
288 65 ▶ Dlog_trace { "Sending a class call for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
289 65 $self->send(call => class_call_handler => $ctx => call => @call);
290 }
291  
292 sub register_class_call_handler {
293 0 ▶ my ($self) = @_;
Subroutine register_class_call_handler: not called CC=2 SCAR=17.9
Pod undocumented
294 0 ▶ $self->local_objects_by_id->{'class_call_handler'} ||= do {
Condition0%0/3
$self->local_objects_by_id->{"class_call_handler"} ||= do { my $o = $self->new_class_call_handler; $self->_local_object_to_id($o); $o }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->local_objects_by_id->{"class_call_handler"} ||= do { my $o = $self->new_class_call_handler; $self->_local_object_to_id($o); $o }
$self->local_objects_by_id->{"class_call_handler"} do { my $o = $self->new_class_call_handler; $self->_local_object_to_id($o); $o }
Truth table0%0/3
A ||= B
A= $self->local_objects_by_id->{"class_call_handler"}B= do { my $o = $self->new_class_call_handler; $self->_local_object_to_id($o); $o }
ABresult
1X1
011
000
295 0 my $o = $self->new_class_call_handler;
296 0 $self->_local_object_to_id($o);
297 0 $o;
298 };
299 }
300  
301 sub new_class_call_handler {
302 Object::Remote::CodeContainer->new(
303 code => sub {
304 0 ▶ my ($class, $method) = (shift, shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 use_module($class)->$method(@_);
306 }
307 0 ▶ );
Subroutine new_class_call_handler: not called CC=1 SCAR=6.9
Pod undocumented
308 }
309  
310 sub register_remote {
311 128 ▶ my ($self, $remote) = @_;
Subroutine register_remote: called CC=1 SCAR=0.4
Pod undocumented
312 128 ▶ Dlog_trace { my $i = $remote->id; "Registered a remote object with id of '$i' for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=2.6
313 128 weaken($self->remote_objects_by_id->{$remote->id} = $remote);
314 128 return $remote;
315 }
316  
317 sub send_free {
318 19 ▶ my ($self, $id) = @_;
Subroutine send_free: called CC=2 SCAR=7.0
Pod undocumented
319 19 ▶ $id = '' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 36 37
320 19 ▶ Dlog_trace { "sending request to free object '$id' for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
321 #TODO this shows up some times when a remote side dies in the middle of a remote
322 #method invocation - possibly only when the object is being constructed?
323 #(in cleanup) Use of uninitialized value $id in delete at ../Object-Remote/lib/Object/Remote/Connection.
324 19 delete $self->remote_objects_by_id->{$id};
325 19 $self->_send([ free => $id ]);
326 }
327  
328 sub send {
329 152 ▶ my ($self, $type, @call) = @_;
Subroutine send: called CC=1 SCAR=0.0
Pod undocumented
330  
331 152 my $future = Object::Remote->current_loop->new_future;
332 152 my $remote = $self->remote_objects_by_id->{$call[0]};
333  
334 152 unshift @call, $type => $self->_local_object_to_id($future);
335  
336 152 my $outstanding = $self->outstanding_futures;
337 152 $outstanding->{$future} = $future;
338 $future->on_ready(sub {
339 152 ▶ undef($remote);
Subroutine __ANON__: called CC=1 SCAR=0.0
340 152 delete $outstanding->{$future}
341 152 });
342  
343 152 $self->_send(\@call);
344  
345 152 return $future;
346 }
347  
348 sub send_discard {
349 21 ▶ my ($self, $type, @call) = @_;
Subroutine send_discard: called CC=1 SCAR=0.0
Pod undocumented
350  
351 21 unshift @call, $type => 'NULL';
352  
353 21 $self->_send(\@call);
354 }
355  
356 sub _send {
357 192 ▶ my ($self, $to_send) = @_;
Subroutine _send: called CC=6 SCAR=21.8
358 192 my $fh = $self->send_to_fh;
359  
360 192 ▶ unless ($self->is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_valid) 1 191 192
361 1 croak "Attempt to invoke _send on a connection that is not valid";
362 }
363  
364 191 ▶ Dlog_trace { "Starting to serialize data in argument to _send for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
365 191 my $serialized = $self->_serialize($to_send)."\n";
366 191 ▶ Dlog_trace { my $l = length($serialized); "serialization is completed; sending '$l' characters of serialized data to $_" } $fh;
Subroutine __ANON__: not called CC=1 SCAR=2.6
367 191 my $ret;
368 191 eval {
369 #TODO this should be converted over to a non-blocking ::WriteChannel class
370 191 ▶ die "filehandle is not open" unless openhandle($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless &openhandle($fh) 0 191 191
371 191 ▶ log_trace { "file handle has passed openhandle() test; printing to it" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
372 191 $ret = print $fh $serialized;
373 191 ▶ die "print was not successful: $!" unless defined $ret
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 191 191
374 };
375  
376 191 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 191 191
377 0 ▶ Dlog_debug { "exception encountered when trying to write to file handle $_: $@" } $fh;
Subroutine __ANON__: not called CC=1 SCAR=6.9
378 0 my $error = $@;
379 0 chomp($error);
380 0 ▶ $self->on_close->done("could not write to file handle: $error") unless $self->on_close->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_close->is_ready 0 0 0
381 0 return;
382 }
383  
384 191 return $ret;
385 }
386  
387 sub _serialize {
388 191 ▶ my ($self, $data) = @_;
Subroutine _serialize: called CC=2 SCAR=9.2
389 191 local our @New_Ids = (-1);
390 return eval {
391 191 my $flat = $self->_encode($self->_deobjectify($data));
392 191 $flat;
393 191 ▶ } || do {
Condition33%1/3
eval { do { my $flat = $self->_encode($self->_deobjectify($data)); $flat } } || do { my $err = $@; delete @{$self->local_objects_by_id;}{@Object::Remote::Connection::New_Ids}; die "Error serializing: $err" }
l!l&&r!l&&!r
19100
MC/DC0%0/2
eval { do { my $flat = $self->_encode($self->_deobjectify($data)); $flat } } || do { my $err = $@; delete @{$self->local_objects_by_id;}{@Object::Remote::Connection::New_Ids}; die "Error serializing: $err" }
eval { do { my $flat = $self->_encode($self->_deobjectify($data)); $flat } } do { my $err = $@; delete @{$self->local_objects_by_id;}{@Object::Remote::Connection::New_Ids}; die "Error serializing: $err" }
Truth table33%1/3
A || B
A= eval { do { my $flat = $self->_encode($self->_deobjectify($data)); $flat } }B= do { my $err = $@; delete @{$self->local_objects_by_id;}{@Object::Remote::Connection::New_Ids}; die "Error serializing: $err" }
ABresult
1X1
011
000
394 0 my $err = $@; # won't get here if the eval doesn't die
395 # don't keep refs to new things
396 0 delete @{$self->local_objects_by_id}{@New_Ids};
397 0 die "Error serializing: $err";
398 };
399 }
400  
401 sub _local_object_to_id {
402 196 ▶ my ($self, $object) = @_;
Subroutine _local_object_to_id: called CC=3 SCAR=11.2
403 196 my $id = refaddr($object);
404 196 ▶ $self->local_objects_by_id->{$id} ||= do {
Condition33%1/3
$self->local_objects_by_id->{$id} ||= do { push our @New_Ids, $id if @Object::Remote::Connection::New_Ids; $object }
l!l&&r!l&&!r
01960
MC/DC0%0/2
$self->local_objects_by_id->{$id} ||= do { push our @New_Ids, $id if @Object::Remote::Connection::New_Ids; $object }
$self->local_objects_by_id->{$id} do { push our @New_Ids, $id if @Object::Remote::Connection::New_Ids; $object }
Truth table33%1/3
A ||= B
A= $self->local_objects_by_id->{$id}B= do { push our @New_Ids, $id if @Object::Remote::Connection::New_Ids; $object }
ABresult
1X1
011
000
405 196 ▶ push our(@New_Ids), $id if @New_Ids;
Branch: 1 branch
BranchTrueFalseTotal
if @Object::Remote::Connection::New_Ids 44 152 196
406 196 $object;
407 };
408 196 return $id;
409 }
410  
411 sub _deobjectify {
412 1425 ▶ my ($self, $data) = @_;
Subroutine _deobjectify: called CC=12 SCAR=25.4
413 1425 ▶ if (blessed($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($data)) { } 39 1386 1425
elsif (my $ref = ref $data) { } 202 1184 1386
414 39 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa('Object::Remote::Proxy') and $data->{'remote'}->connection == $self) { } 1 38 39
Condition66%2/3
$data->isa("Object::Remote::Proxy") and $data->{"remote"}->connection == $self
!ll&&!rl&&r
3801
MC/DC50%1/2
$data->isa("Object::Remote::Proxy") and $data->{"remote"}->connection == $self
$data->isa("Object::Remote::Proxy") $data->{"remote"}->connection == $self
Truth table66%2/3
A and B
A= $data->isa("Object::Remote::Proxy")B= $data->{"remote"}->connection == $self
ABresult
0X0
100
111
415 $data->isa('Object::Remote::Proxy')
416 and $data->{remote}->connection == $self
417 ) {
418 1 return +{ __local_object__ => $data->{remote}->id };
419 } else {
420 38 return +{ __remote_object__ => $self->_local_object_to_id($data) };
421 }
422 } elsif (my $ref = ref($data)) {
423 202 ▶ if ($ref eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 3 199 202
elsif ($ref eq 'ARRAY') { } 191 8 199
elsif ($ref eq 'CODE') { } 5 3 8
elsif ($ref eq 'SCALAR') { } 2 1 3
elsif ($ref eq 'GLOB') { } 1 0 1
424 3 my $tied_to = tied(%$data);
425 3 ▶ if(defined($tied_to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tied_to) { } 0 3 3
426 0 return +{__remote_tied_hash__ => $self->_local_object_to_id($tied_to)};
427 } else {
428 3 return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data };
429 }
430 } elsif ($ref eq 'ARRAY') {
431 191 my $tied_to = tied(@$data);
432 191 ▶ if (defined($tied_to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tied_to) { } 0 191 191
433 0 return +{__remote_tied_array__ => $self->_local_object_to_id($tied_to)};
434 } else {
435 191 return [ map $self->_deobjectify($_), @$data ];
436 }
437 } elsif ($ref eq 'CODE') {
438 5 my $id = $self->_local_object_to_id(
439 Object::Remote::CodeContainer->new(code => $data)
440 );
441 5 return +{ __remote_code__ => $id };
442 } elsif ($ref eq 'SCALAR') {
443 2 return +{ __scalar_ref__ => $$data };
444 } elsif ($ref eq 'GLOB') {
445 1 return +{ __glob_ref__ => $self->_local_object_to_id(
446 Object::Remote::GlobContainer->new(handle => $data)
447 ) };
448 } else {
449 0 die "Can't collapse reftype $ref";
450 }
451 }
452 1184 return $data; # plain scalar
453 }
454  
455 sub _receive {
456 168 ▶ my ($self, $flat) = @_;
Subroutine _receive: called CC=3 SCAR=12.9
457 168 ▶ Dlog_trace { my $l = length($flat); "Starting to deserialize $l characters of data for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=2.6
458 168 my ($type, @rest) = eval { @{$self->_deserialize($flat)} }
459 168 ▶ or do { warn "Deserialize failed for ${flat}: $@"; return };
Branch: 1 branch
BranchTrueFalseTotal
() = eval { do { @{$self->_deserialize($flat);} } } or do { warn "Deserialize failed for ${flat}: $@"; return } 168 0 168
460 168 ▶ Dlog_trace { "deserialization complete for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
461 168 eval { $self->${\"receive_${type}"}(@rest); 1 }
462 168 ▶ or do { warn "Receive failed for ${flat}: $@"; return };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->${\("receive_$type");}(@rest); 1 } } or do { warn "Receive failed for ${flat}: $@"; return } 168 0 168
463 168 return;
464 }
465  
466 sub receive_free {
467 147 ▶ my ($self, $id) = @_;
Subroutine receive_free: called CC=3 SCAR=11.8
Pod undocumented
468 147 ▶ $id = '' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 147 147
469 147 ▶ Dlog_trace { "got a receive_free for object '$id' for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
470 147 ▶ delete $self->local_objects_by_id->{$id}
Branch: 1 branch
BranchTrueFalseTotal
delete $self->local_objects_by_id->{$id} or warn "Free: no such object $id" 147 0 147
471 or warn "Free: no such object $id";
472 147 return;
473 }
474  
475 sub receive_call {
476 168 ▶ my ($self, $future_id, $id, @rest) = @_;
Subroutine receive_call: called CC=2 SCAR=7.8
Pod undocumented
477 168 ▶ Dlog_trace { "got a receive_call for object '$id' for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
478 168 my $future = $self->_id_to_remote_object($future_id);
479 168 $future->{method} = 'call_discard_free';
480 my $local = $self->local_objects_by_id->{$id}
481 168 ▶ or do { $future->fail("No such object $id"); return };
Branch: 1 branch
BranchTrueFalseTotal
my $local = $self->local_objects_by_id->{$id} or do { $future->fail("No such object $id"); return } 168 0 168
482 168 $self->_invoke($future, $local, @rest);
483 }
484  
485 sub receive_call_free {
486 147 ▶ my ($self, $future, $id, @rest) = @_;
Subroutine receive_call_free: called CC=1 SCAR=0.1
Pod undocumented
487 147 ▶ Dlog_trace { "got a receive_call_free for object '$id' for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
488 147 $self->receive_call($future, $id, undef, @rest);
489 147 $self->receive_free($id);
490 }
491  
492 sub _invoke {
493 168 ▶ my ($self, $future, $local, $ctx, $method, @args) = @_;
Subroutine _invoke: called CC=6 SCAR=21.7
494 168 ▶ Dlog_trace { "got _invoke for a method named '$method' for connection $_" } $self->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
495 168 ▶ if ($method =~ /^start::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^start::/) 0 168 168
496 0 my $f = $local->$method(@args);
497 0 ▶ $f->on_done(sub { undef($f); $future->done(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
498 0 ▶ return unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 0 0
499 0 ▶ $f->on_fail(sub { undef($f); $future->fail(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
500 0 return;
501 }
502 168 ▶ my $do = sub { $local->$method(@args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
503 eval {
504 $future->done(
505 defined($ctx)
506 ? ($ctx ? $do->() : scalar($do->()))
507 168 ▶ : do { $do->(); () }
Branch: 2 branches
BranchTrueFalseTotal
defined $ctx ? : 16 152 168
$ctx ? : 0 16 16
508 );
509 167 1;
510 168 ▶ } or do { $future->fail($@); return; };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $future->done(defined $ctx ? $ctx ? &$do() : scalar &$do() : do { &$do(); () }); 1 } } or do { $future->fail($@); return } 167 1 168
511 167 return;
512 }
513  
514 sub DEMOLISH {
515 0 ▶ my ($self, $in_global_destruction) = @_;
Subroutine DEMOLISH: not called CC=4 SCAR=30.0
Pod undocumented
516  
517 0 ▶ return if $in_global_destruction;
Branch: 1 branch
BranchTrueFalseTotal
if $in_global_destruction 0 0 0
518  
519 0 my $child_pid = $self->child_pid;
520 0 ▶ return unless $child_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $child_pid 0 0 0
521  
522 0 ▶ unless ($self->on_close->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->on_close->is_ready) 0 0 0
523 0 kill('TERM', $child_pid);
524 0 $self->on_close->done('No more references to connection');
525 }
526 }
527  
528 1;
529  
530 =head1 NAME
531  
532 Object::Remote::Connection - An underlying connection for L<Object::Remote>
533  
534 use Object::Remote;
535  
536 my $local = Object::Remote->connect('-');
537 my $remote = Object::Remote->connect('myserver');
538 my $remote_user = Object::Remote->connect('user@myserver');
539 my $local_sudo = Object::Remote->connect('user@');
540  
541 #$remote can be any other connection object
542 my $hostname = Sys::Hostname->can::on($remote, 'hostname');
543  
544 =head1 DESCRIPTION
545  
546 This is the class that supports connections to remote objects.
547  
548 =head1 SEE ALSO
549  
550 =over 4
551  
552 =item C<Object::Remote::Role::Connector::PerlInterpreter>
553  
554 =item C<Object::Remote>
555  
556 =back
557  
558 =cut
« Remote/FatNode.pm ↑ Summary Remote/MiniLoop.pm »