Commit | Line | Data |
676438a1 |
1 | package Object::Remote::Handle; |
2 | |
3 | use Object::Remote::Proxy; |
4 | use Scalar::Util qw(weaken blessed); |
07105aca |
5 | use Object::Remote::Logging qw ( :log ); |
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 | |
14 | has connection => ( |
15 | is => 'ro', required => 1, |
16 | coerce => sub { |
17 | blessed($_[0]) |
18 | ? $_[0] |
19 | : use_module('Object::Remote::Connection')->new_from_spec($_[0]) |
20 | }, |
21 | ); |
22 | |
23 | has id => (is => 'rwp'); |
24 | |
25 | has disarmed_free => (is => 'rwp'); |
26 | |
27 | sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] } |
28 | |
29 | sub proxy { |
30 | bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy'); |
31 | } |
32 | |
33 | sub BUILD { |
34 | my ($self, $args) = @_; |
b51a8453 |
35 | log_trace { "constructing remote handle" }; |
1d26d6f9 |
36 | if ($self->id) { |
90a3a7f2 |
37 | log_trace { "disarming free for this handle" }; |
1d26d6f9 |
38 | $self->disarm_free; |
39 | } else { |
676438a1 |
40 | die "No id supplied and no class either" unless $args->{class}; |
41 | ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args}; |
07105aca |
42 | log_trace { "fetching id for handle and disarming free on remote side" }; |
676438a1 |
43 | $self->_set_id( |
44 | await_future( |
f7611866 |
45 | $self->connection->send_class_call( |
46 | 0, $args->{class}, |
676438a1 |
47 | $args->{constructor}||'new', @{$args->{args}||[]} |
48 | ) |
49 | )->{remote}->disarm_free->id |
50 | ); |
51 | } |
90a3a7f2 |
52 | log_trace { "finished constructing remote handle; registering it " . ref($self) }; |
676438a1 |
53 | $self->connection->register_remote($self); |
54 | } |
55 | |
56 | sub call { |
57 | my ($self, $method, @args) = @_; |
58 | my $w = wantarray; |
b51a8453 |
59 | log_trace { my $def = defined $w ? 1 : 0; "call() has been invoked on a remote handle; wantarray: '$def'" }; |
676438a1 |
60 | $method = "start::${method}" if (caller(0)||'') eq 'start'; |
61 | future { |
62 | $self->connection->send(call => $self->id, $w, $method, @args) |
63 | }; |
64 | } |
65 | |
66 | sub call_discard { |
67 | my ($self, $method, @args) = @_; |
07105aca |
68 | log_trace { "invoking send_discard() with 'call' for method '$method' on connection for remote handle" }; |
676438a1 |
69 | $self->connection->send_discard(call => $self->id, $method, @args); |
70 | } |
71 | |
72 | sub call_discard_free { |
73 | my ($self, $method, @args) = @_; |
74 | $self->disarm_free; |
07105aca |
75 | log_trace { "invoking send_discard() with 'call_free' for method '$method' on connection for remote handle" }; |
676438a1 |
76 | $self->connection->send_discard(call_free => $self->id, $method, @args); |
77 | } |
78 | |
79 | sub DEMOLISH { |
80 | my ($self, $gd) = @_; |
07105aca |
81 | log_trace { "Demolishing remote handle" }; |
676438a1 |
82 | return if $gd or $self->disarmed_free; |
83 | $self->connection->send_free($self->id); |
84 | } |
85 | |
86 | 1; |