has proxy => (is => 'lazy', weak_ref => 1);
sub _build_proxy {
- bless({ handle => $_[0] }, 'Object::Remote::Proxy');
+ bless({ remote => $_[0] }, 'Object::Remote::Proxy');
}
sub BUILD {
}
sub call {
- my ($self, $id, $method, @args) = @_;
+ my ($self, $method, @args) = @_;
$self->_await($self->connection->send(call => $self->id, $method, @args));
}
package Object::Remote::Connection;
use CPS::Future;
+use Object::Remote::Null;
use Object::Remote;
use IO::Handle;
use Module::Runtime qw(use_module);
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',
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 {
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);
}
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;
}
# 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