File: Connector.pm

package info (click to toggle)
libobject-remote-perl 0.003004-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 408 kB
  • ctags: 221
  • sloc: perl: 2,365; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package Object::Remote::Role::Connector;

use Module::Runtime qw(use_module);
use Object::Remote::Future;
use Object::Remote::Logging qw(:log :dlog router);
use Moo::Role;

requires '_open2_for';

has timeout => (is => 'ro', default => sub { 10 });

BEGIN { router()->exclude_forwarding; }

sub connect {
  my $self = shift;
  Dlog_debug { "Preparing to create connection with args of: $_" } @_;
  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 {
    log_trace { "Initializing connection for child pid '$child_pid'" };
    my $f = shift;
    $channel->on_line_call(sub {
      if ($_[0] eq "Shere") {
        log_trace { "Received 'Shere' from child pid '$child_pid'; setting done handler to create connection" };
        $f->done(
          use_module('Object::Remote::Connection')->new(
            send_to_fh => $send_to_fh,
            read_channel => $channel,
            child_pid => $child_pid,
          )
        );
      } else {
        log_warn { "'Shere' was not found in connection data for child pid '$child_pid'" };
        $f->fail("Expected Shere from remote but received: $_[0]");
      }
      undef($channel);
    });
    $channel->on_close_call(sub {
      log_trace { "Connection has been closed" };
      $f->fail("Channel closed without seeing Shere: $_[0]");
      undef($channel);
    });
    log_trace { "initialized events on channel for child pid '$child_pid'; creating timeout" };
    Object::Remote->current_loop
                  ->watch_time(
                      after => $self->timeout,
                      code => sub {
                        Dlog_trace {"Connection timeout timer has fired for child pid '$child_pid'; is_ready: $_" } $f->is_ready;
                        unless($f->is_ready) {
                            log_warn { "Connection with child pid '$child_pid' has timed out" };
                            $f->fail("Connection timed out") unless $f->is_ready;
                        }
                        undef($channel);

                      }
                    );
    log_trace { "connection for child pid '$child_pid' has been initialized" };
    $f;
  }
}

1;