fatnode over ssh works
[scpubgit/Object-Remote.git] / lib / Object / Remote.pm
CommitLineData
9e72f0cf 1package Object::Remote;
2
3use Object::Remote::MiniLoop;
4use Object::Remote::Proxy;
5use Scalar::Util qw(weaken);
6use Moo;
7
8has connection => (is => 'ro', required => 1);
9
10has id => (is => 'rwp');
11
ad4f54b2 12has disarmed_free => (is => 'rwp');
13
14sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] }
15
2065b08b 16sub proxy {
17 bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy');
9e72f0cf 18}
19
20sub 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
36sub current_loop {
37 our $Current_Loop ||= Object::Remote::MiniLoop->new
38}
39
40sub call {
9d804009 41 my ($self, $method, @args) = @_;
9e72f0cf 42 $self->_await($self->connection->send(call => $self->id, $method, @args));
43}
44
2065b08b 45sub call_discard {
46 my ($self, $method, @args) = @_;
47 $self->connection->send_discard(call => $self->id, $method, @args);
48}
49
8131a88a 50sub call_discard_free {
51 my ($self, $method, @args) = @_;
52 $self->disarm_free;
53 $self->connection->send_discard(call_free => $self->id, $method, @args);
54}
55
9e72f0cf 56sub _await {
57 my ($self, $future) = @_;
58 my $loop = $self->current_loop;
59 $future->on_ready(sub { $loop->stop });
60 $loop->run;
ad4f54b2 61 ($future->get)[0];
9e72f0cf 62}
63
64sub DEMOLISH {
65 my ($self, $gd) = @_;
ad4f54b2 66 return if $gd or $self->disarmed_free;
9e72f0cf 67 $self->connection->send_free($self->id);
68}
69
701;