split want_run versus blocking run for ::Future
[scpubgit/Object-Remote.git] / lib / Object / Remote / Handle.pm
CommitLineData
676438a1 1package Object::Remote::Handle;
2
3use Object::Remote::Proxy;
4use Scalar::Util qw(weaken blessed);
5use Object::Remote::Future;
6use Module::Runtime qw(use_module);
7use Moo;
8
9has connection => (
10 is => 'ro', required => 1,
11 coerce => sub {
12 blessed($_[0])
13 ? $_[0]
14 : use_module('Object::Remote::Connection')->new_from_spec($_[0])
15 },
16);
17
18has id => (is => 'rwp');
19
20has disarmed_free => (is => 'rwp');
21
22sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] }
23
24sub proxy {
25 bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy');
26}
27
28sub BUILD {
29 my ($self, $args) = @_;
30 unless ($self->id) {
31 die "No id supplied and no class either" unless $args->{class};
32 ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args};
33 $self->_set_id(
34 await_future(
35 $self->connection->send(
36 class_call => $args->{class}, 0,
37 $args->{constructor}||'new', @{$args->{args}||[]}
38 )
39 )->{remote}->disarm_free->id
40 );
41 }
42 $self->connection->register_remote($self);
43}
44
45sub call {
46 my ($self, $method, @args) = @_;
47 my $w = wantarray;
48 $method = "start::${method}" if (caller(0)||'') eq 'start';
49 future {
50 $self->connection->send(call => $self->id, $w, $method, @args)
51 };
52}
53
54sub call_discard {
55 my ($self, $method, @args) = @_;
56 $self->connection->send_discard(call => $self->id, $method, @args);
57}
58
59sub call_discard_free {
60 my ($self, $method, @args) = @_;
61 $self->disarm_free;
62 $self->connection->send_discard(call_free => $self->id, $method, @args);
63}
64
65sub DEMOLISH {
66 my ($self, $gd) = @_;
67 return if $gd or $self->disarmed_free;
68 $self->connection->send_free($self->id);
69}
70
711;