From: Matt S Trout Date: Fri, 18 May 2012 00:31:30 +0000 (+0000) Subject: factor handle code out so Object::Remote->new returns a proxy X-Git-Tag: v0.001001~55 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=commitdiff_plain;h=676438a11cbf6bd49102369824c9d87f70964fd3 factor handle code out so Object::Remote->new returns a proxy --- diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index 680197e..47f2906 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -1,85 +1,24 @@ package Object::Remote; use Object::Remote::MiniLoop; -use Object::Remote::Proxy; -use Scalar::Util qw(weaken blessed); -use Object::Remote::Future; -use Module::Runtime qw(use_module); -use Moo; +use Object::Remote::Handle; sub new::on { my ($class, $on, @args) = @_; - __PACKAGE__->new( + Object::Remote::Handle->new( connection => $on, class => $class, args => \@args )->proxy; } -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( - await_future( - $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) = @_; - my $w = wantarray; - $method = "start::${method}" if (caller(0)||'') eq 'start'; - future { - $self->connection->send(call => $self->id, $w, $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 DEMOLISH { - my ($self, $gd) = @_; - return if $gd or $self->disarmed_free; - $self->connection->send_free($self->id); -} - 1; diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index f69275d..eb3c6cb 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -2,6 +2,7 @@ package Object::Remote::Connection; use Object::Remote::Future; use Object::Remote::Null; +use Object::Remote::Handle; use Object::Remote; use IO::Handle; use Module::Runtime qw(use_module); @@ -56,7 +57,7 @@ sub _build__json { return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; ( $remotes->{$id} - or Object::Remote->new(connection => $self, id => $id) + or Object::Remote::Handle->new(connection => $self, id => $id) )->proxy; } ); diff --git a/lib/Object/Remote/Handle.pm b/lib/Object/Remote/Handle.pm new file mode 100644 index 0000000..1a757ab --- /dev/null +++ b/lib/Object/Remote/Handle.pm @@ -0,0 +1,71 @@ +package Object::Remote::Handle; + +use Object::Remote::Proxy; +use Scalar::Util qw(weaken blessed); +use Object::Remote::Future; +use Module::Runtime qw(use_module); +use Moo; + +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( + await_future( + $self->connection->send( + class_call => $args->{class}, 0, + $args->{constructor}||'new', @{$args->{args}||[]} + ) + )->{remote}->disarm_free->id + ); + } + $self->connection->register_remote($self); +} + +sub call { + my ($self, $method, @args) = @_; + my $w = wantarray; + $method = "start::${method}" if (caller(0)||'') eq 'start'; + future { + $self->connection->send(call => $self->id, $w, $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 DEMOLISH { + my ($self, $gd) = @_; + return if $gd or $self->disarmed_free; + $self->connection->send_free($self->id); +} + +1; diff --git a/t/basic.t b/t/basic.t index 77f0c69..07d77ef 100644 --- a/t/basic.t +++ b/t/basic.t @@ -12,17 +12,17 @@ my $connection = Object::Remote::Connector::Local->new->connect; #$Object::Remote::Connection::DEBUG = 1; -my $proxy = Object::Remote->new( +my $remote = Object::Remote->new( connection => $connection, class => 'ORTestClass' -)->proxy; +); -isnt($$, $proxy->pid, 'Different pid on the other side'); +isnt($$, $remote->pid, 'Different pid on the other side'); -is($proxy->counter, 0, 'Counter at 0'); +is($remote->counter, 0, 'Counter at 0'); -is($proxy->increment, 1, 'Increment to 1'); +is($remote->increment, 1, 'Increment to 1'); -is($proxy->counter, 1, 'Counter at 1'); +is($remote->counter, 1, 'Counter at 1'); done_testing; diff --git a/t/sender.t b/t/sender.t index 8916297..02eb0d8 100644 --- a/t/sender.t +++ b/t/sender.t @@ -21,17 +21,17 @@ my $ml = Object::Remote->new( args => { module_sender => $ms }, ); -my $proxy = Object::Remote->new( +my $counter = Object::Remote->new( connection => $connection, class => 'ORTestClass' -)->proxy; +); -isnt($$, $proxy->pid, 'Different pid on the other side'); +isnt($$, $counter->pid, 'Different pid on the other side'); -is($proxy->counter, 0, 'Counter at 0'); +is($counter->counter, 0, 'Counter at 0'); -is($proxy->increment, 1, 'Increment to 1'); +is($counter->increment, 1, 'Increment to 1'); -is($proxy->counter, 1, 'Counter at 1'); +is($counter->counter, 1, 'Counter at 1'); done_testing;