From: Matt S Trout Date: Mon, 14 May 2012 08:00:59 +0000 (+0000) Subject: slightly improve things X-Git-Tag: v0.001001~65 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=commitdiff_plain;h=9d804009b5363c739598a9cc4507df57a2d59c55 slightly improve things --- diff --git a/bin/object-remote-node b/bin/object-remote-node old mode 100644 new mode 100755 diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index 326a3a5..35f5c03 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -12,7 +12,7 @@ has id => (is => 'rwp'); has proxy => (is => 'lazy', weak_ref => 1); sub _build_proxy { - bless({ handle => $_[0] }, 'Object::Remote::Proxy'); + bless({ remote => $_[0] }, 'Object::Remote::Proxy'); } sub BUILD { @@ -36,7 +36,7 @@ sub current_loop { } sub call { - my ($self, $id, $method, @args) = @_; + my ($self, $method, @args) = @_; $self->_await($self->connection->send(call => $self->id, $method, @args)); } diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index 7a078b5..4e467c9 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -1,6 +1,7 @@ package Object::Remote::Connection; use CPS::Future; +use Object::Remote::Null; use Object::Remote; use IO::Handle; use Module::Runtime qw(use_module); @@ -30,7 +31,9 @@ has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x }); has local_objects_by_id => (is => 'ro', default => sub { {} }); -has remote_objects_by_id => (is => 'ro', default => sub { {} }); +has remote_objects_by_id => ( + is => 'ro', default => sub { { NULL => bless({}, 'Object::Remote::Null') } } +); has _json => ( is => 'lazy', @@ -76,20 +79,31 @@ sub send { return $future; } +sub send_discard { + my ($self, $type, @call) = @_; + + unshift @call, $type => { __remote_object => 'NULL' }; + + $self->_send(\@call); +} + sub _send { my ($self, $to_send) = @_; - print { $self->send_to_fh } $self->_serialize($to_send); + print { $self->send_to_fh } $self->_serialize($to_send)."\n"; } sub _serialize { my ($self, $data) = @_; local our @New_Ids; - eval { return $self->_encode($self->_deobjectify($data)) }; - my $err = $@; # won't get here if the eval doesn't die - # don't keep refs to new things - delete @{$self->local_objects_by_id}{@New_Ids}; - die "Error serializing: $err"; + return eval { + $self->_encode($self->_deobjectify($data)) + } or do { + my $err = $@; # won't get here if the eval doesn't die + # don't keep refs to new things + delete @{$self->local_objects_by_id}{@New_Ids}; + die "Error serializing: $err"; + }; } sub _deobjectify { @@ -116,7 +130,7 @@ sub _deobjectify { sub _receive_data_from { my ($self, $fh) = @_; my $rb = $self->_receive_data_buffer; - if (sysread($self->read_fh, $$rb, 1024, length($$rb)) > 0) { + if (sysread($fh, $$rb, 1024, length($$rb)) > 0) { while ($$rb =~ s/^(.*)\n//) { $self->_receive($1); } @@ -134,7 +148,8 @@ sub _receive { sub receive_free { my ($self, $id) = @_; - delete $self->local_objects_by_id->{$id}; + delete $self->local_objects_by_id->{$id} + or warn "Free: no such object $id"; return; } diff --git a/lib/Object/Remote/Connector/STDIO.pm b/lib/Object/Remote/Connector/STDIO.pm index 4ea49af..49cb0f9 100644 --- a/lib/Object/Remote/Connector/STDIO.pm +++ b/lib/Object/Remote/Connector/STDIO.pm @@ -12,9 +12,9 @@ sub connect { # only potentially bloody confusing but results in warnings like: # "Filehandle STDOUT reopened as STDIN only for input" close STDIN or die "Closing stdin: $!"; - open STDIN, '<', File::Spec->dev_null or die "Re-opening stdin: $!"; + open STDIN, '<', File::Spec->devnull or die "Re-opening stdin: $!"; close STDOUT or die "Closing stdout: $!"; - open STDOUT, '>', File::Spec->dev_null or die "Re-opening stdout: $!"; + open STDOUT, '>', File::Spec->devnull or die "Re-opening stdout: $!"; Object::Remote::Connection->new( send_to_fh => $stdout, receive_from_fh => $stdin diff --git a/lib/Object/Remote/Null.pm b/lib/Object/Remote/Null.pm new file mode 100644 index 0000000..d04d56e --- /dev/null +++ b/lib/Object/Remote/Null.pm @@ -0,0 +1,7 @@ +package Object::Remote::Null; + +sub AUTOLOAD { } + +sub DESTROY { } + +1; diff --git a/lib/Object/Remote/Proxy.pm b/lib/Object/Remote/Proxy.pm index b2ae3c6..a3e2a80 100644 --- a/lib/Object/Remote/Proxy.pm +++ b/lib/Object/Remote/Proxy.pm @@ -5,7 +5,7 @@ use strictures 1; sub AUTOLOAD { my $self = shift; (my $method) = (our $AUTOLOAD =~ /([^:]+)$/); - $self->{handle}->call($method => @_); + $self->{remote}->call($method => @_); } sub DESTROY { }