From: Matt S Trout Date: Thu, 17 May 2012 22:02:15 +0000 (+0000) Subject: remoting works X-Git-Tag: v0.001001~58 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=84b04178bd25e342d7f522e8f60c6d695b09576a;p=scpubgit%2FObject-Remote.git remoting works --- diff --git a/bin/remoterepl b/bin/remoterepl new file mode 100755 index 0000000..f7bae7a --- /dev/null +++ b/bin/remoterepl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strictures 1; +use Object::Remote; +use Eval::WithLexicals; +use Term::ReadLine; +use Data::Dumper; + +$SIG{INT} = sub { warn "SIGINT\n" }; + +{ package Data::Dumper; no strict 'vars'; + $Terse = $Indent = $Useqq = $Deparse = $Sortkeys = 1; + $Quotekeys = 0; +} + +#{ no warnings 'once'; $Object::Remote::Connection::DEBUG = 1; } + +my $eval = Eval::WithLexicals->new::on($ARGV[0]); + +my $read = Term::ReadLine->new('Perl REPL'); +while (1) { + my $line = $read->readline('re.pl$ '); + exit unless defined $line; + my @ret; eval { + local $SIG{INT} = sub { die "Caught SIGINT" }; + @ret = $eval->eval($line); 1; + } or @ret = ("Error!", $@); + print Dumper @ret; +} diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index c68cc8e..64355af 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -2,10 +2,27 @@ package Object::Remote; use Object::Remote::MiniLoop; use Object::Remote::Proxy; -use Scalar::Util qw(weaken); +use Scalar::Util qw(weaken blessed); +use Module::Runtime qw(use_module); use Moo; -has connection => (is => 'ro', required => 1); +sub new::on { + my ($class, $on, @args) = @_; + __PACKAGE__->new( + connection => $on, + class => $class, + args => \@args + )->proxy; +} + +has connection => ( + is => 'ro', required => 1, + coerce => sub { + blessed($_[0]) + ? $_[0] + : use_module('Object::Remote::Connection')->new_from_spec($_[0]) + }, +); has id => (is => 'rwp'); @@ -25,7 +42,7 @@ sub BUILD { $self->_set_id( $self->_await( $self->connection->send( - class_call => $args->{class}, + class_call => $args->{class}, 0, $args->{constructor}||'new', @{$args->{args}||[]} ) )->{remote}->disarm_free->id @@ -40,7 +57,9 @@ sub current_loop { sub call { my ($self, $method, @args) = @_; - $self->_await($self->connection->send(call => $self->id, $method, @args)); + $self->_await( + $self->connection->send(call => $self->id, wantarray, $method, @args) + ); } sub call_discard { @@ -59,7 +78,7 @@ sub _await { my $loop = $self->current_loop; $future->on_ready(sub { $loop->stop }); $loop->run; - ($future->get)[0]; + wantarray ? $future->get : ($future->get)[0]; } sub DEMOLISH { diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index a6df92c..4e3348a 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -62,6 +62,20 @@ sub _build__json { ); } +BEGIN { + unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; + eval { require Object::Remote::Connector::Local }; + eval { require Object::Remote::Connector::SSH }; +} + +sub new_from_spec { + my ($class, $spec) = @_; + foreach my $poss (do { our @Guess }) { + if (my $obj = $poss->($spec)) { return $obj } + } + die "Couldn't figure out what to do with ${spec}"; +} + sub register_remote { my ($self, $remote) = @_; weaken($self->remote_objects_by_id->{$remote->id} = $remote); @@ -173,7 +187,7 @@ sub receive_call { sub receive_call_free { my ($self, $future, $id, @rest) = @_; - $self->receive_call($future, $id, @rest); + $self->receive_call($future, $id, undef, @rest); $self->receive_free($id); } @@ -186,9 +200,16 @@ sub receive_class_call { } sub _invoke { - my ($self, $future, $local, $method, @args) = @_; - eval { $future->done(scalar $local->$method(@args)); 1 } - or do { $future->fail($@); return; }; + my ($self, $future, $local, $ctx, $method, @args) = @_; + my $do = sub { $local->$method(@args) }; + eval { + $future->done( + defined($ctx) + ? ($ctx ? $do->() : scalar($do->())) + : do { $do->(); () } + ); + 1; + } or do { $future->fail($@); return; }; return; } diff --git a/lib/Object/Remote/Connector/Local.pm b/lib/Object/Remote/Connector/Local.pm index c57d07d..72a153f 100644 --- a/lib/Object/Remote/Connector/Local.pm +++ b/lib/Object/Remote/Connector/Local.pm @@ -12,4 +12,8 @@ sub _open2_for { return ($its_stdin, $its_stdout, $pid); } +push @Object::Remote::Connection::Guess, sub { + if (($_[0]||'') eq '-') { __PACKAGE__->new->connect } +}; + 1; diff --git a/lib/Object/Remote/Connector/SSH.pm b/lib/Object/Remote/Connector/SSH.pm index c9193d0..5d5797c 100644 --- a/lib/Object/Remote/Connector/SSH.pm +++ b/lib/Object/Remote/Connector/SSH.pm @@ -1,23 +1,44 @@ package Object::Remote::Connector::SSH; use Object::Remote::FatNode; -use Net::OpenSSH; +use Object::Remote::ModuleSender; +use IPC::Open2; use Moo; with 'Object::Remote::Role::Connector'; -has ssh_masters => (is => 'ro', default => sub { {} }); - sub _open2_for { my $self = shift; - my @res = $self->_ssh_object_for(@_)->open2('perl','-',@_); - print { $res[0] } $Object::Remote::FatNode::DATA, "__END__\n"; - return @res; + my $pid = open2(my $ssh_stdout, my $ssh_stdin, 'ssh', $_[0], 'perl', '-') + or die "Failed to start ssh connection: $!";; + print $ssh_stdin $Object::Remote::FatNode::DATA, "__END__\n"; + return ($ssh_stdin, $ssh_stdout, $pid); } +around connect => sub { + my ($orig, $self) = (shift, shift); + my $conn = $self->$orig(@_); + Object::Remote->new( + connection => $conn, + class => 'Object::Remote::ModuleLoader', + args => { module_sender => Object::Remote::ModuleSender->new } + )->disarm_free; + return $conn; +}; + sub _ssh_object_for { my ($self, $on) = @_; $self->ssh_masters->{$on} ||= Net::OpenSSH->new($on); } +push @Object::Remote::Connection::Guess, sub { + for ($_[0]) { + # 0-9 a-z _ - first char, those or . subsequent - hostnamish + if (defined and !ref and /^(?:.*?\@)?[\w\-][\w\-\.]*/) { + return __PACKAGE__->new->connect($_[0]); + } + } + return; +}; + 1; diff --git a/lib/Object/Remote/Role/Connector.pm b/lib/Object/Remote/Role/Connector.pm index 9c7ac7b..0669a9d 100644 --- a/lib/Object/Remote/Role/Connector.pm +++ b/lib/Object/Remote/Role/Connector.pm @@ -1,6 +1,6 @@ package Object::Remote::Role::Connector; -use Object::Remote::Connection; +use Module::Runtime qw(use_module); use Moo::Role; requires '_open2_for'; @@ -13,7 +13,7 @@ sub connect { unless ($line eq "Shere\n") { die "New remote container did not send Shere - got ${line}"; } - return Object::Remote::Connection->new(\%args); + return use_module('Object::Remote::Connection')->new(\%args); } 1; diff --git a/maint/Makefile.PL.include b/maint/Makefile.PL.include new file mode 100644 index 0000000..dc0d14a --- /dev/null +++ b/maint/Makefile.PL.include @@ -0,0 +1,8 @@ +BEGIN { + -e 'Distar' + or system("git clone git://git.shadowcat.co.uk/p5sagit/Distar.git") +} +use lib 'Distar/lib'; +use Distar; + +author 'mst - Matt S. Trout (cpan:MSTROUT) ';