Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote; |
2 | |
3 | use Object::Remote::MiniLoop; |
4 | use Object::Remote::Proxy; |
5 | use Scalar::Util qw(weaken); |
6 | use Moo; |
7 | |
8 | has connection => (is => 'ro', required => 1); |
9 | |
10 | has id => (is => 'rwp'); |
11 | |
ad4f54b2 |
12 | has disarmed_free => (is => 'rwp'); |
13 | |
14 | sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] } |
15 | |
2065b08b |
16 | sub proxy { |
17 | bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy'); |
9e72f0cf |
18 | } |
19 | |
20 | sub BUILD { |
21 | my ($self, $args) = @_; |
22 | unless ($self->id) { |
23 | die "No id supplied and no class either" unless $args->{class}; |
24 | $self->_set_id( |
25 | $self->_await( |
26 | $self->connection->send( |
27 | class_call => $args->{class}, |
28 | $args->{constructor}||'new', @{$args->{args}||[]} |
29 | ) |
ad4f54b2 |
30 | )->{remote}->disarm_free->id |
9e72f0cf |
31 | ); |
32 | } |
33 | $self->connection->register_remote($self); |
34 | } |
35 | |
36 | sub current_loop { |
37 | our $Current_Loop ||= Object::Remote::MiniLoop->new |
38 | } |
39 | |
40 | sub call { |
9d804009 |
41 | my ($self, $method, @args) = @_; |
9e72f0cf |
42 | $self->_await($self->connection->send(call => $self->id, $method, @args)); |
43 | } |
44 | |
2065b08b |
45 | sub call_discard { |
46 | my ($self, $method, @args) = @_; |
47 | $self->connection->send_discard(call => $self->id, $method, @args); |
48 | } |
49 | |
9e72f0cf |
50 | sub _await { |
51 | my ($self, $future) = @_; |
52 | my $loop = $self->current_loop; |
53 | $future->on_ready(sub { $loop->stop }); |
54 | $loop->run; |
ad4f54b2 |
55 | ($future->get)[0]; |
9e72f0cf |
56 | } |
57 | |
58 | sub DEMOLISH { |
59 | my ($self, $gd) = @_; |
ad4f54b2 |
60 | return if $gd or $self->disarmed_free; |
9e72f0cf |
61 | $self->connection->send_free($self->id); |
62 | } |
63 | |
64 | 1; |