X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FObject%2FRemote%2FConnection.pm;h=1d21114a5bd9c3ea7b7f0132aac47acbf8230fa5;hb=97c80c7615102e500c162597807f7eaa3905891d;hp=eb3c6cb4ab85b21fc77ec1c49453be43baf81634;hpb=676438a11cbf6bd49102369824c9d87f70964fd3;p=scpubgit%2FObject-Remote.git diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index eb3c6cb..1d21114 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -10,7 +10,7 @@ use Scalar::Util qw(weaken blessed refaddr); use JSON::PP qw(encode_json); use Moo; -our $DEBUG; +our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG}; has send_to_fh => ( is => 'ro', required => 1, @@ -34,6 +34,15 @@ has on_close => (is => 'rw', default => sub {}); has child_pid => (is => 'ro'); +has is_ready => (is => 'rwp', trigger => sub { + my ($self, $value) = @_; + $self->ready_future->done if $value; +}); + +has ready_future => (is => 'lazy'); + +sub _build_ready_future { CPS::Future->new } + has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x }); has local_objects_by_id => (is => 'ro', default => sub { {} }); @@ -66,6 +75,7 @@ sub _build__json { BEGIN { unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; eval { require Object::Remote::Connector::Local }; + eval { require Object::Remote::Connector::LocalSudo }; eval { require Object::Remote::Connector::SSH }; } @@ -83,6 +93,12 @@ sub register_remote { return $remote; } +sub await_ready { + my ($self) = @_; + return if $self->is_ready; + await_future($self->ready_future); +} + sub send_free { my ($self, $id) = @_; delete $self->remote_objects_by_id->{$id}; @@ -110,6 +126,8 @@ sub send_discard { sub _send { my ($self, $to_send) = @_; + $self->await_ready unless $self->is_ready; + print { $self->send_to_fh } $self->_serialize($to_send)."\n"; } @@ -120,7 +138,7 @@ sub _serialize { my $flat = $self->_encode($self->_deobjectify($data)); warn "$$ >>> ${flat}\n" if $DEBUG; $flat; - } or do { + } || 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}; @@ -152,9 +170,17 @@ sub _deobjectify { sub _receive_data_from { my ($self, $fh) = @_; my $rb = $self->_receive_data_buffer; + my $ready = $self->is_ready; if (sysread($fh, $$rb, 1024, length($$rb)) > 0) { while ($$rb =~ s/^(.*)\n//) { - $self->_receive($1); + if ($ready) { + $self->_receive($1); + } else { + my $line = $1; + die "New remote container did not send Shere - got ${line}" + unless $line eq "Shere"; + $self->_set_is_ready($ready = 1); + } } } else { $self->on_close->done();