--- /dev/null
+#!/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;
+}
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');
$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
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 {
my $loop = $self->current_loop;
$future->on_ready(sub { $loop->stop });
$loop->run;
- ($future->get)[0];
+ wantarray ? $future->get : ($future->get)[0];
}
sub DEMOLISH {
);
}
+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);
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);
}
}
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;
}
return ($its_stdin, $its_stdout, $pid);
}
+push @Object::Remote::Connection::Guess, sub {
+ if (($_[0]||'') eq '-') { __PACKAGE__->new->connect }
+};
+
1;
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;
package Object::Remote::Role::Connector;
-use Object::Remote::Connection;
+use Module::Runtime qw(use_module);
use Moo::Role;
requires '_open2_for';
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;
--- /dev/null
+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) <mst@shadowcat.co.uk>';