X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FObject%2FRemote%2FConnection.pm;h=d0679c4474c305581f2ef21d0261b69dac820c2a;hb=a76f2f60f38f21af50f878ea9dbd7f0670edf9f3;hp=a6df92c8de7a309e4222f4983c6101e7c965c639;hpb=47c83a1379a33fc8baa4a128edc1d75d780776b0;p=scpubgit%2FObject-Remote.git diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index a6df92c..d0679c4 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -1,7 +1,8 @@ package Object::Remote::Connection; -use CPS::Future; +use Object::Remote::Future; use Object::Remote::Null; +use Object::Remote::Handle; use Object::Remote; use IO::Handle; use Module::Runtime qw(use_module); @@ -9,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, @@ -33,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 { {} }); @@ -56,18 +66,39 @@ sub _build__json { return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; ( $remotes->{$id} - or Object::Remote->new(connection => $self, id => $id) + or Object::Remote::Handle->new(connection => $self, id => $id) )->proxy; } ); } +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 }; +} + +sub new_from_spec { + my ($class, $spec) = @_; + foreach my $poss (do { our @Guess }) { + if (my $obj = $poss->($spec)) { return $obj } + } + die "Couldn't figure out what to do with ${spec}"; +} + sub register_remote { my ($self, $remote) = @_; weaken($self->remote_objects_by_id->{$remote->id} = $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}; @@ -95,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"; } @@ -105,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}; @@ -113,15 +146,20 @@ sub _serialize { }; } +sub _local_object_to_id { + my ($self, $object) = @_; + my $id = refaddr($object); + $self->local_objects_by_id->{$id} ||= do { + push our(@New_Ids), $id; + $object; + }; + return $id; +} + sub _deobjectify { my ($self, $data) = @_; if (blessed($data)) { - my $id = refaddr($data); - $self->local_objects_by_id->{$id} ||= do { - push our(@New_Ids), $id; - $data; - }; - return +{ __remote_object__ => $id }; + return +{ __remote_object__ => $self->_local_object_to_id($data) }; } elsif (my $ref = ref($data)) { if ($ref eq 'HASH') { return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; @@ -137,12 +175,20 @@ 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->(); + $self->on_close->done(); } } @@ -173,7 +219,7 @@ sub receive_call { sub receive_call_free { my ($self, $future, $id, @rest) = @_; - $self->receive_call($future, $id, @rest); + $self->receive_call($future, $id, undef, @rest); $self->receive_free($id); } @@ -186,9 +232,23 @@ sub receive_class_call { } sub _invoke { - my ($self, $future, $local, $method, @args) = @_; - eval { $future->done(scalar $local->$method(@args)); 1 } - or do { $future->fail($@); return; }; + my ($self, $future, $local, $ctx, $method, @args) = @_; + if ($method =~ /^start::/) { + my $f = $local->$method(@args); + $f->on_done(sub { undef($f); $future->done(@_) }); + return unless $f; + $f->on_fail(sub { undef($f); $future->fail(@_) }); + return; + } + my $do = sub { $local->$method(@args) }; + eval { + $future->done( + defined($ctx) + ? ($ctx ? $do->() : scalar($do->())) + : do { $do->(); () } + ); + 1; + } or do { $future->fail($@); return; }; return; }