Commit | Line | Data |
676438a1 |
1 | package Object::Remote::Handle; |
2 | |
3 | use Object::Remote::Proxy; |
4 | use Scalar::Util qw(weaken blessed); |
998ff9a4 |
5 | use Object::Remote::Logging qw ( :log :dlog router ); |
676438a1 |
6 | use Object::Remote::Future; |
7 | use Module::Runtime qw(use_module); |
8 | use Moo; |
9 | |
f4a85080 |
10 | BEGIN { router()->exclude_forwarding } |
4e446335 |
11 | |
676438a1 |
12 | has connection => ( |
5add5e29 |
13 | is => 'ro', required => 1, handles => ['is_valid'], |
676438a1 |
14 | coerce => sub { |
15 | blessed($_[0]) |
16 | ? $_[0] |
17 | : use_module('Object::Remote::Connection')->new_from_spec($_[0]) |
18 | }, |
19 | ); |
20 | |
21 | has id => (is => 'rwp'); |
22 | |
23 | has disarmed_free => (is => 'rwp'); |
24 | |
25 | sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] } |
26 | |
27 | sub proxy { |
28 | bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy'); |
29 | } |
30 | |
31 | sub BUILD { |
32 | my ($self, $args) = @_; |
7790ca36 |
33 | log_trace { "constructing remote handle" }; |
1d26d6f9 |
34 | if ($self->id) { |
90115979 |
35 | log_trace { "disarming free for this handle" }; |
1d26d6f9 |
36 | $self->disarm_free; |
37 | } else { |
676438a1 |
38 | die "No id supplied and no class either" unless $args->{class}; |
39 | ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args}; |
9031635d |
40 | log_trace { "fetching id for handle and disarming free on remote side" }; |
676438a1 |
41 | $self->_set_id( |
42 | await_future( |
f7611866 |
43 | $self->connection->send_class_call( |
44 | 0, $args->{class}, |
676438a1 |
45 | $args->{constructor}||'new', @{$args->{args}||[]} |
46 | ) |
47 | )->{remote}->disarm_free->id |
48 | ); |
49 | } |
998ff9a4 |
50 | Dlog_trace { "finished constructing remote handle; id is $_" } $self->id; |
676438a1 |
51 | $self->connection->register_remote($self); |
52 | } |
53 | |
54 | sub call { |
55 | my ($self, $method, @args) = @_; |
56 | my $w = wantarray; |
5add5e29 |
57 | my $id = $self->id; |
d2eadebb |
58 | |
676438a1 |
59 | $method = "start::${method}" if (caller(0)||'') eq 'start'; |
5add5e29 |
60 | log_trace { "call('$method') has been invoked on remote handle '$id'; creating future" }; |
61 | |
676438a1 |
62 | future { |
5add5e29 |
63 | log_debug { "Invoking send on connection for handle '$id' method $method" }; |
64 | $self->connection->send(call => $id, $w, $method, @args) |
676438a1 |
65 | }; |
66 | } |
67 | |
68 | sub call_discard { |
69 | my ($self, $method, @args) = @_; |
9031635d |
70 | log_trace { "invoking send_discard() with 'call' for method '$method' on connection for remote handle" }; |
676438a1 |
71 | $self->connection->send_discard(call => $self->id, $method, @args); |
72 | } |
73 | |
74 | sub call_discard_free { |
75 | my ($self, $method, @args) = @_; |
76 | $self->disarm_free; |
9031635d |
77 | log_trace { "invoking send_discard() with 'call_free' for method '$method' on connection for remote handle" }; |
676438a1 |
78 | $self->connection->send_discard(call_free => $self->id, $method, @args); |
79 | } |
80 | |
81 | sub DEMOLISH { |
82 | my ($self, $gd) = @_; |
998ff9a4 |
83 | Dlog_trace { "Demolishing remote handle $_" } $self->id; |
676438a1 |
84 | return if $gd or $self->disarmed_free; |
82ef4e4b |
85 | #this could happen after the connection has gone away |
86 | eval { $self->connection->send_free($self->id) }; |
87 | if ($@ && $@ !~ m/^Attempt to invoke _send on a connection that is not valid/) { |
88 | die "Could not invoke send_free on connection for handle " . $self->id; |
89 | } |
676438a1 |
90 | } |
91 | |
92 | 1; |