X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FTak.git;a=blobdiff_plain;f=lib%2FTak%2FConnectorService.pm;h=2fb1eaf0e1e7cb3510190286da9e3ae36a3ab696;hp=fa3981573c9bc58a801a74e1efc018a2750e6928;hb=2791fd73b196072f047b2b3d746b64be53312ca3;hpb=986f5290d9338118f95f2293f705368fb142d270 diff --git a/lib/Tak/ConnectorService.pm b/lib/Tak/ConnectorService.pm index fa39815..2fb1eaf 100644 --- a/lib/Tak/ConnectorService.pm +++ b/lib/Tak/ConnectorService.pm @@ -5,16 +5,18 @@ use IO::All; use Tak::Router; use Tak::Client; use Tak::ConnectionService; +use Net::OpenSSH; use Moo; with 'Tak::Role::Service'; has connections => (is => 'ro', default => sub { Tak::Router->new }); +has ssh => (is => 'ro', default => sub { {} }); + sub handle_create { - my ($self) = @_; - my $kid_pid = IPC::Open2::open2(my $kid_out, my $kid_in, $^X, '-') - or die "Couldn't open2 child: $!"; + my ($self, $on) = @_; + my ($kid_in, $kid_out, $kid_pid) = $self->_open($on); $kid_in->print(io('maint/mk-fat |')->all, "__END__\n"); my $connection = Tak::ConnectionService->new( read_fh => $kid_out, write_fh => $kid_in, @@ -24,16 +26,33 @@ sub handle_create { # actually, we should register with a monotonic id and # stash the pid elsewhere. but meh for now. my $pid = $client->do(meta => 'pid'); - $self->connections->register('|'.$pid, $connection); - return ('proxy', '|'.$pid); + my $name = ($on||'|').':'.$pid; + my $conn_router = Tak::Router->new; + $conn_router->register(local => $connection->receiver->service); + $conn_router->register(remote => $connection); + $self->connections->register($name, $conn_router); + return ($name); +} + +sub _open { + my ($self, $on) = @_; + unless ($on) { + my $kid_pid = IPC::Open2::open2(my $kid_out, my $kid_in, $^X, '-') + or die "Couldn't open2 child: $!"; + return ($kid_in, $kid_out, $kid_pid); + } + my $ssh = $self->ssh->{$on} ||= Net::OpenSSH->new($on); + $ssh->error and + die "Couldn't establish ssh connection: ".$ssh->error; + return $ssh->open2('perl','-'); } -sub start_proxy_request { +sub start_connection_request { my ($self, $req, @payload) = @_;; $self->connections->start_request($req, @payload); } -sub receive_proxy { +sub receive_connection { my ($self, @payload) = @_; $self->connections->receive(@payload); }