X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FObject%2FRemote%2FRole%2FConnector.pm;h=6da4a9c5331a2f1497f8bea7088c00d8534bb138;hb=a63cd862186adf328e26dd1294e7a3b1adc42ed6;hp=0669a9d7ba0c5dcf86f2b27116a8dd0cd4f2c1c7;hpb=84b04178bd25e342d7f522e8f60c6d695b09576a;p=scpubgit%2FObject-Remote.git diff --git a/lib/Object/Remote/Role/Connector.pm b/lib/Object/Remote/Role/Connector.pm index 0669a9d..6da4a9c 100644 --- a/lib/Object/Remote/Role/Connector.pm +++ b/lib/Object/Remote/Role/Connector.pm @@ -1,19 +1,49 @@ package Object::Remote::Role::Connector; use Module::Runtime qw(use_module); +use Object::Remote::Future; use Moo::Role; requires '_open2_for'; +has timeout => (is => 'ro', default => sub { { after => 10 } }); + 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}"; + my ($send_to_fh, $receive_from_fh, $child_pid) = $self->_open2_for(@_); + my $channel = use_module('Object::Remote::ReadChannel')->new( + fh => $receive_from_fh + ); + return future { + my $f = shift; + $channel->on_line_call(sub { + if ($_[0] eq "Shere") { + $f->done( + use_module('Object::Remote::Connection')->new( + send_to_fh => $send_to_fh, + read_channel => $channel, + child_pid => $child_pid, + ) + ); + } else { + $f->fail("Expected Shere from remote but received: $_[0]"); + } + undef($channel); + }); + $channel->on_close_call(sub { + $f->fail("Channel closed without seeing Shere: $_[0]"); + undef($channel); + }); + Object::Remote->current_loop + ->watch_time( + %{$self->timeout}, + code => sub { + $f->fail("Connection timed out") unless $f->is_ready; + undef($channel); + } + ); + $f; } - return use_module('Object::Remote::Connection')->new(\%args); } 1;