use Object::Remote::Connector::STDIO;
use Object::Remote;
+#$Object::Remote::Connection::DEBUG = 1;
+
my $c = Object::Remote::Connector::STDIO->new->connect;
-Object::Remote->current_loop->run;
+my $loop = Object::Remote->current_loop;
+
+$c->on_close(sub { $loop->stop });
+
+$loop->run;
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');
}
class_call => $args->{class},
$args->{constructor}||'new', @{$args->{args}||[]}
)
- )
+ )->{remote}->disarm_free->id
);
}
$self->connection->register_remote($self);
my $loop = $self->current_loop;
$future->on_ready(sub { $loop->stop });
$loop->run;
- $future->get;
+ ($future->get)[0];
}
sub DEMOLISH {
my ($self, $gd) = @_;
- return if $gd;
+ return if $gd or $self->disarmed_free;
$self->connection->send_free($self->id);
}
use JSON::PP qw(encode_json);
use Moo;
+our $DEBUG;
+
has send_to_fh => (
is => 'ro', required => 1,
trigger => sub { $_[1]->autoflush(1) },
},
);
+has on_close => (is => 'rw', default => sub {});
+
has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x });
has local_objects_by_id => (is => 'ro', default => sub { {} });
my ($self, $data) = @_;
local our @New_Ids;
return eval {
- $self->_encode($self->_deobjectify($data))
+ my $flat = $self->_encode($self->_deobjectify($data));
+ warn "$$ >>> ${flat}\n" if $DEBUG;
+ $flat;
} or do {
my $err = $@; # won't get here if the eval doesn't die
# don't keep refs to new things
while ($$rb =~ s/^(.*)\n//) {
$self->_receive($1);
}
+ } else {
+ $self->on_close->();
}
}
sub _receive {
- my ($self, $data) = @_;
- my ($type, @rest) = eval { @{$self->_deserialize($data)} }
- or do { warn "Deserialize failed for ${data}: $@"; return };
+ my ($self, $flat) = @_;
+ warn "$$ <<< $flat\n" if $DEBUG;
+ my ($type, @rest) = eval { @{$self->_deserialize($flat)} }
+ or do { warn "Deserialize failed for ${flat}: $@"; return };
eval { $self->${\"receive_${type}"}(@rest); 1 }
- or do { warn "Receive failed for ${data}: $@"; return };
+ or do { warn "Receive failed for ${flat}: $@"; return };
return;
}
sub _invoke {
my ($self, $future, $local, $method, @args) = @_;
- eval { $future->done($local->$method(@args)); 1 }
+ eval { $future->done(scalar $local->$method(@args)); 1 }
or do { $future->fail($@); return; };
return;
}
sub connect {
# XXX bin/ is wrong but meh, fix later
- my $pid = open2(my $its_stdin, my $its_stdout, 'bin/object-remote-node')
+ my $pid = open2(my $its_stdout, my $its_stdin, 'bin/object-remote-node')
or die "Couldn't start local node: $!";
Object::Remote::Connection->new(
send_to_fh => $its_stdin,