make it actually exit when dead
[scpubgit/Object-Remote.git] / lib / Object / Remote.pm
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
12 has disarmed_free => (is => 'rwp');
13
14 sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] }
15
16 sub proxy {
17   bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy');
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         )
30       )->{remote}->disarm_free->id
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 {
41   my ($self, $method, @args) = @_;
42   $self->_await($self->connection->send(call => $self->id, $method, @args));
43 }
44
45 sub call_discard {
46   my ($self, $method, @args) = @_;
47   $self->connection->send_discard(call => $self->id, $method, @args);
48 }
49
50 sub _await {
51   my ($self, $future) = @_;
52   my $loop = $self->current_loop;
53   $future->on_ready(sub { $loop->stop });
54   $loop->run;
55   ($future->get)[0];
56 }
57
58 sub DEMOLISH {
59   my ($self, $gd) = @_;
60   return if $gd or $self->disarmed_free;
61   $self->connection->send_free($self->id);
62 }
63
64 1;