package Object::Remote::Connection;
-use CPS::Future;
+use Object::Remote::Future;
use Object::Remote::Null;
+use Object::Remote::Handle;
use Object::Remote;
use IO::Handle;
use Module::Runtime qw(use_module);
has on_close => (is => 'rw', default => sub {});
+has child_pid => (is => 'ro');
+
has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x });
has local_objects_by_id => (is => 'ro', default => sub { {} });
return bless({}, 'Object::Remote::Null') if $id eq 'NULL';
(
$remotes->{$id}
- or Object::Remote->new(connection => $self, id => $id)
+ or Object::Remote::Handle->new(connection => $self, id => $id)
)->proxy;
}
);
}
+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);
$self->_receive($1);
}
} else {
- $self->on_close->();
+ $self->on_close->done();
}
}
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) = @_;
+ if ($method =~ /^start::/) {
+ my $f = $local->$method(@args);
+ $f->on_done(sub { undef($f); $future->done(@_) });
+ return unless $f;
+ $f->on_fail(sub { undef($f); $future->fail(@_) });
+ return;
+ }
+ my $do = sub { $local->$method(@args) };
+ eval {
+ $future->done(
+ defined($ctx)
+ ? ($ctx ? $do->() : scalar($do->()))
+ : do { $do->(); () }
+ );
+ 1;
+ } or do { $future->fail($@); return; };
return;
}