Commit | Line | Data |
676438a1 |
1 | package Object::Remote::Handle; |
2 | |
3 | use Object::Remote::Proxy; |
4 | use Scalar::Util qw(weaken blessed); |
c3d5ef8a |
5 | use Object::Remote::Logging qw ( :log router ); |
676438a1 |
6 | use Object::Remote::Future; |
23591f5f |
7 | #must find way to exclude certain log events |
8 | #from being forwarded - log events generated in |
9 | #response to log events cause exploding |
10 | #use Object::Remote::Logging qw(:log); |
676438a1 |
11 | use Module::Runtime qw(use_module); |
12 | use Moo; |
13 | |
c3d5ef8a |
14 | BEGIN { router()->exclude_forwarding } |
21988035 |
15 | |
676438a1 |
16 | has connection => ( |
17 | is => 'ro', required => 1, |
18 | coerce => sub { |
19 | blessed($_[0]) |
20 | ? $_[0] |
21 | : use_module('Object::Remote::Connection')->new_from_spec($_[0]) |
22 | }, |
23 | ); |
24 | |
25 | has id => (is => 'rwp'); |
26 | |
27 | has disarmed_free => (is => 'rwp'); |
28 | |
29 | sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] } |
30 | |
31 | sub proxy { |
32 | bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy'); |
33 | } |
34 | |
35 | sub BUILD { |
36 | my ($self, $args) = @_; |
b51a8453 |
37 | log_trace { "constructing remote handle" }; |
1d26d6f9 |
38 | if ($self->id) { |
90a3a7f2 |
39 | log_trace { "disarming free for this handle" }; |
1d26d6f9 |
40 | $self->disarm_free; |
41 | } else { |
676438a1 |
42 | die "No id supplied and no class either" unless $args->{class}; |
43 | ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args}; |
07105aca |
44 | log_trace { "fetching id for handle and disarming free on remote side" }; |
676438a1 |
45 | $self->_set_id( |
46 | await_future( |
f7611866 |
47 | $self->connection->send_class_call( |
48 | 0, $args->{class}, |
676438a1 |
49 | $args->{constructor}||'new', @{$args->{args}||[]} |
50 | ) |
51 | )->{remote}->disarm_free->id |
52 | ); |
53 | } |
90a3a7f2 |
54 | log_trace { "finished constructing remote handle; registering it " . ref($self) }; |
676438a1 |
55 | $self->connection->register_remote($self); |
56 | } |
57 | |
58 | sub call { |
59 | my ($self, $method, @args) = @_; |
60 | my $w = wantarray; |
b51a8453 |
61 | log_trace { my $def = defined $w ? 1 : 0; "call() has been invoked on a remote handle; wantarray: '$def'" }; |
676438a1 |
62 | $method = "start::${method}" if (caller(0)||'') eq 'start'; |
63 | future { |
64 | $self->connection->send(call => $self->id, $w, $method, @args) |
65 | }; |
66 | } |
67 | |
68 | sub call_discard { |
69 | my ($self, $method, @args) = @_; |
07105aca |
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; |
07105aca |
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) = @_; |
07105aca |
83 | log_trace { "Demolishing remote handle" }; |
676438a1 |
84 | return if $gd or $self->disarmed_free; |
85 | $self->connection->send_free($self->id); |
86 | } |
87 | |
88 | 1; |