1 package Tak::ConnectorService;
8 use Tak::ConnectionService;
13 with 'Tak::Role::Service';
15 has connections => (is => 'ro', default => sub { Tak::Router->new });
17 has ssh => (is => 'ro', default => sub { {} });
20 my ($self, $on, %args) = @_;
21 die [ mistake => "No target supplied to create" ] unless $on;
22 my $log_level = $args{log_level}||'info';
23 my ($kid_in, $kid_out, $kid_pid) = $self->_open($on, $log_level);
24 unless ($on =~ /^\.?\//) {
25 $kid_in->print($Tak::STDIONode::DATA, "__END__\n") unless $on eq '-';
26 # Need to get a handshake to indicate STDIOSetup has finished
27 # messing around with file descriptors, otherwise we can severely
28 # confuse things by sending before the dup.
30 die [ failure => "Garbled response from child: $up" ]
31 unless $up eq "Ssyshere\n";
33 my $connection = Tak::ConnectionService->new(
34 read_fh => $kid_out, write_fh => $kid_in,
35 listening_service => Tak::Router->new
37 my $client = Tak::Client->new(service => $connection);
38 # actually, we should register with a monotonic id and
39 # stash the pid elsewhere. but meh for now.
40 my $pid = $client->do(meta => 'pid');
41 my $name = $on.':'.$pid;
42 my $conn_router = Tak::Router->new;
43 $conn_router->register(local => $connection->receiver->service);
44 $conn_router->register(remote => $connection);
45 $self->connections->register($name, $conn_router);
50 my ($self, $on, @args) = @_;
52 my $kid_pid = IPC::Open2::open2(my $kid_out, my $kid_in, 'tak-stdio-node', '-', @args)
53 or die "Couldn't open2 child: $!";
54 return ($kid_in, $kid_out, $kid_pid);
55 } elsif ($on =~ /^\.?\//) { # ./foo or /foo
56 my $sock = IO::Socket::UNIX->new($on)
57 or die "Couldn't open socket ${on}: $!";
58 return ($sock, $sock, 'UNIX');
60 my $ssh = $self->ssh->{$on} ||= Net::OpenSSH->new($on);
62 die "Couldn't establish ssh connection: ".$ssh->error;
63 return $ssh->open2('perl','-', $on, @args);
66 sub start_connection_request {
67 my ($self, $req, @payload) = @_;;
68 $self->connections->start_request($req, @payload);
71 sub receive_connection {
72 my ($self, @payload) = @_;
73 $self->connections->receive(@payload);