From: Matt S Trout Date: Fri, 18 May 2012 01:12:55 +0000 (+0000) Subject: defer node setup X-Git-Tag: v0.001001~54 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=commitdiff_plain;h=8ba4f2e3adb9a1fda64b463b34a4306c9034359a;hp=676438a11cbf6bd49102369824c9d87f70964fd3 defer node setup --- diff --git a/bin/object-remote-node b/bin/object-remote-node index acdf165..4f9cac4 100755 --- a/bin/object-remote-node +++ b/bin/object-remote-node @@ -9,6 +9,8 @@ use CPS::Future; my $c = Object::Remote::Connector::STDIO->new->connect; +$c->_set_is_ready(1); + my $loop = Object::Remote->current_loop; my $f = CPS::Future->new; diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index eb3c6cb..70c1409 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -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 { {} }); @@ -83,6 +92,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 +125,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"; } @@ -152,9 +169,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(); diff --git a/lib/Object/Remote/Connector/SSH.pm b/lib/Object/Remote/Connector/SSH.pm index 5d5797c..7d6142c 100644 --- a/lib/Object/Remote/Connector/SSH.pm +++ b/lib/Object/Remote/Connector/SSH.pm @@ -2,6 +2,7 @@ package Object::Remote::Connector::SSH; use Object::Remote::FatNode; use Object::Remote::ModuleSender; +use Object::Remote::Handle; use IPC::Open2; use Moo; @@ -18,7 +19,7 @@ sub _open2_for { around connect => sub { my ($orig, $self) = (shift, shift); my $conn = $self->$orig(@_); - Object::Remote->new( + Object::Remote::Handle->new( connection => $conn, class => 'Object::Remote::ModuleLoader', args => { module_sender => Object::Remote::ModuleSender->new } diff --git a/lib/Object/Remote/Future.pm b/lib/Object/Remote/Future.pm index 11850d6..3a2643b 100644 --- a/lib/Object/Remote/Future.pm +++ b/lib/Object/Remote/Future.pm @@ -33,7 +33,7 @@ package start; sub AUTOLOAD { my $invocant = shift; - my ($method) = our $AUTOLOAD =~ /([^:]+)$/; + my ($method) = our $AUTOLOAD =~ /^start::(.+)$/; if (ref($invocant) eq 'ARRAY') { return [ map $_->${\"start::${method}"}, @$invocant ]; } diff --git a/lib/Object/Remote/Role/Connector.pm b/lib/Object/Remote/Role/Connector.pm index 0669a9d..16f0d52 100644 --- a/lib/Object/Remote/Role/Connector.pm +++ b/lib/Object/Remote/Role/Connector.pm @@ -9,10 +9,6 @@ sub connect { my $self = shift; my %args; @args{qw(send_to_fh receive_from_fh child_pid)} = $self->_open2_for(@_); - my $line = readline($args{receive_from_fh}); - unless ($line eq "Shere\n") { - die "New remote container did not send Shere - got ${line}"; - } return use_module('Object::Remote::Connection')->new(\%args); }