X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FObject%2FRemote.pm;h=6cfb9bbef1f18c24d3f1760b1bb061dd637d8a83;hb=c6fe6fbd88a05e860ecdc8942cb86940915172ed;hp=64355afc143cb3bedba7873aa8f47ea221eec84a;hpb=84b04178bd25e342d7f522e8f60c6d695b09576a;p=scpubgit%2FObject-Remote.git diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index 64355af..6cfb9bb 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -1,90 +1,22 @@ package Object::Remote; use Object::Remote::MiniLoop; -use Object::Remote::Proxy; -use Scalar::Util qw(weaken blessed); +use Object::Remote::Handle; use Module::Runtime qw(use_module); -use Moo; sub new::on { my ($class, $on, @args) = @_; - __PACKAGE__->new( - connection => $on, - class => $class, - args => \@args - )->proxy; + my $conn = use_module('Object::Remote::Connection')->new_from_spec($on); + return $conn->new_remote(class => $class, args => \@args); } -has connection => ( - is => 'ro', required => 1, - coerce => sub { - blessed($_[0]) - ? $_[0] - : use_module('Object::Remote::Connection')->new_from_spec($_[0]) - }, -); - -has id => (is => 'rwp'); - -has disarmed_free => (is => 'rwp'); - -sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] } - -sub proxy { - bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy'); -} - -sub BUILD { - my ($self, $args) = @_; - unless ($self->id) { - die "No id supplied and no class either" unless $args->{class}; - ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args}; - $self->_set_id( - $self->_await( - $self->connection->send( - class_call => $args->{class}, 0, - $args->{constructor}||'new', @{$args->{args}||[]} - ) - )->{remote}->disarm_free->id - ); - } - $self->connection->register_remote($self); +sub new { + shift; + Object::Remote::Handle->new(@_)->proxy; } sub current_loop { our $Current_Loop ||= Object::Remote::MiniLoop->new } -sub call { - my ($self, $method, @args) = @_; - $self->_await( - $self->connection->send(call => $self->id, wantarray, $method, @args) - ); -} - -sub call_discard { - my ($self, $method, @args) = @_; - $self->connection->send_discard(call => $self->id, $method, @args); -} - -sub call_discard_free { - my ($self, $method, @args) = @_; - $self->disarm_free; - $self->connection->send_discard(call_free => $self->id, $method, @args); -} - -sub _await { - my ($self, $future) = @_; - my $loop = $self->current_loop; - $future->on_ready(sub { $loop->stop }); - $loop->run; - wantarray ? $future->get : ($future->get)[0]; -} - -sub DEMOLISH { - my ($self, $gd) = @_; - return if $gd or $self->disarmed_free; - $self->connection->send_free($self->id); -} - 1;