From: Matt S Trout Date: Mon, 14 May 2012 09:29:08 +0000 (+0000) Subject: make it actually exit when dead X-Git-Tag: v0.001001~62 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=commitdiff_plain;h=ad4f54b29c5f84aa010b456303b24647c0e28ae7 make it actually exit when dead --- diff --git a/bin/object-remote-node b/bin/object-remote-node index d938ebc..638b6bc 100755 --- a/bin/object-remote-node +++ b/bin/object-remote-node @@ -4,6 +4,12 @@ use strictures 1; 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; diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index cd3a364..7bb33c6 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -9,6 +9,10 @@ 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 proxy { bless({ remote => $_[0], method => 'call' }, 'Object::Remote::Proxy'); } @@ -23,7 +27,7 @@ sub BUILD { class_call => $args->{class}, $args->{constructor}||'new', @{$args->{args}||[]} ) - ) + )->{remote}->disarm_free->id ); } $self->connection->register_remote($self); @@ -48,12 +52,12 @@ sub _await { 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); } diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index 6ff0b03..7ab6b1f 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -9,6 +9,8 @@ use Scalar::Util qw(weaken blessed refaddr); use JSON::PP qw(encode_json); use Moo; +our $DEBUG; + has send_to_fh => ( is => 'ro', required => 1, trigger => sub { $_[1]->autoflush(1) }, @@ -27,6 +29,8 @@ has receive_from_fh => ( }, ); +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 { {} }); @@ -96,7 +100,9 @@ sub _serialize { 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 @@ -133,15 +139,18 @@ sub _receive_data_from { 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; } @@ -170,7 +179,7 @@ sub receive_class_call { 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; } diff --git a/lib/Object/Remote/Connector/Local.pm b/lib/Object/Remote/Connector/Local.pm index 33f599f..4900037 100644 --- a/lib/Object/Remote/Connector/Local.pm +++ b/lib/Object/Remote/Connector/Local.pm @@ -6,7 +6,7 @@ use Moo; 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,