add in support for tied objects, adjust a few log levels
[scpubgit/Object-Remote.git] / lib / Object / Remote / Role / Connector.pm
CommitLineData
47c83a13 1package Object::Remote::Role::Connector;
2
84b04178 3use Module::Runtime qw(use_module);
12fb4a80 4use Object::Remote::Future;
5d59cb98 5use Object::Remote::Logging qw(:log :dlog );
47c83a13 6use Moo::Role;
7
8requires '_open2_for';
9
9031635d 10#TODO return to 10 seconds after debugging
11#has timeout => (is => 'ro', default => sub { { after => 10 } });
cc670b30 12has timeout => (is => 'ro', default => sub { { after => 10 } });
498c4ad5 13
47c83a13 14sub connect {
15 my $self = shift;
5d59cb98 16 Dlog_debug { "Perparing to create connection with args of: $_" } @_;
12fb4a80 17 my ($send_to_fh, $receive_from_fh, $child_pid) = $self->_open2_for(@_);
18 my $channel = use_module('Object::Remote::ReadChannel')->new(
19 fh => $receive_from_fh
20 );
21 return future {
5d59cb98 22 log_trace { "Initializing connection for child pid '$child_pid'" };
12fb4a80 23 my $f = shift;
24 $channel->on_line_call(sub {
25 if ($_[0] eq "Shere") {
5d59cb98 26 log_trace { "Received 'Shere' from child pid '$child_pid'; setting done handler to create connection" };
12fb4a80 27 $f->done(
28 use_module('Object::Remote::Connection')->new(
29 send_to_fh => $send_to_fh,
30 read_channel => $channel,
31 child_pid => $child_pid,
32 )
33 );
34 } else {
5d59cb98 35 log_warn { "'Shere' was not found in connection data for child pid '$child_pid'" };
12fb4a80 36 $f->fail("Expected Shere from remote but received: $_[0]");
37 }
38 undef($channel);
39 });
40 $channel->on_close_call(sub {
41 $f->fail("Channel closed without seeing Shere: $_[0]");
42 undef($channel);
43 });
5d59cb98 44 log_trace { "initialized events on channel for child pid '$child_pid'; creating timeout" };
12fb4a80 45 Object::Remote->current_loop
46 ->watch_time(
498c4ad5 47 %{$self->timeout},
12fb4a80 48 code => sub {
5d59cb98 49# log_warn { "Connection timed out for child pid '$child_pid'" };
50# $f->fail("Connection timed out") unless $f->is_ready;
51# undef($channel);
52 Dlog_trace { "Connection timeout timer has fired for child pid '$child_pid'; is_ready: $_" } $f->is_ready;
53 unless($f->is_ready) {
54 log_warn { "Connection with child pid '$child_pid' has timed out" };
9031635d 55 $f->fail("Connection timed out") unless $f->is_ready;
5d59cb98 56 }
12fb4a80 57 undef($channel);
9031635d 58
12fb4a80 59 }
60 );
5d59cb98 61 log_trace { "connection for child pid '$child_pid' has been initialized" };
12fb4a80 62 $f;
63 }
47c83a13 64}
65
661;