X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=blobdiff_plain;f=lib%2FObject%2FRemote.pm;h=8894cc7588e3bb67fa50e6ee4734c3297c109c71;hp=a81550181cc09dd6ffebf4bc8db6e34ad564e77b;hb=4c17fea55a562db315a029e6bd67b7dbb028a8e9;hpb=8131a88a11a61f4d829e88e609575cf2855f7428 diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index a815501..8894cc7 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -1,70 +1,27 @@ package Object::Remote; use Object::Remote::MiniLoop; -use Object::Remote::Proxy; -use Scalar::Util qw(weaken); -use Moo; +use Object::Remote::Handle; +use Module::Runtime qw(use_module); -has connection => (is => 'ro', required => 1); - -has id => (is => 'rwp'); - -has disarmed_free => (is => 'rwp'); - -sub disarm_free { $_[0]->_set_disarmed_free(1); $_[0] } +sub new::on { + my ($class, $on, @args) = @_; + my $conn = __PACKAGE__->connect($on); + return $conn->new_remote(class => $class, args => \@args); +} -sub proxy { - bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy'); +sub new { + shift; + Object::Remote::Handle->new(@_)->proxy; } -sub BUILD { - my ($self, $args) = @_; - unless ($self->id) { - die "No id supplied and no class either" unless $args->{class}; - $self->_set_id( - $self->_await( - $self->connection->send( - class_call => $args->{class}, - $args->{constructor}||'new', @{$args->{args}||[]} - ) - )->{remote}->disarm_free->id - ); - } - $self->connection->register_remote($self); +sub connect { + my ($class, $to) = @_; + use_module('Object::Remote::Connection')->new_from_spec($to); } sub current_loop { our $Current_Loop ||= Object::Remote::MiniLoop->new } -sub call { - my ($self, $method, @args) = @_; - $self->_await($self->connection->send(call => $self->id, $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; - ($future->get)[0]; -} - -sub DEMOLISH { - my ($self, $gd) = @_; - return if $gd or $self->disarmed_free; - $self->connection->send_free($self->id); -} - 1;