1 package Object::Remote::Connection;
3 use Object::Remote::Future;
4 use Object::Remote::Null;
5 use Object::Remote::Handle;
6 use Object::Remote::CodeContainer;
9 use Module::Runtime qw(use_module);
10 use Scalar::Util qw(weaken blessed refaddr);
11 use JSON::PP qw(encode_json);
14 our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG};
17 is => 'ro', required => 1,
18 trigger => sub { $_[1]->autoflush(1) },
21 has receive_from_fh => (
22 is => 'ro', required => 1,
26 Object::Remote->current_loop
29 on_read_ready => sub { $self->_receive_data_from($fh) }
34 has on_close => (is => 'rw', default => sub { CPS::Future->new });
36 has child_pid => (is => 'ro');
38 has ready_future => (is => 'lazy');
40 sub _build_ready_future { CPS::Future->new }
42 has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x });
44 has local_objects_by_id => (is => 'ro', default => sub { {} });
46 has remote_objects_by_id => (is => 'ro', default => sub { {} });
48 has outstanding_futures => (is => 'ro', default => sub { {} });
53 _deserialize => 'decode',
58 sub _id_to_remote_object {
60 return bless({}, 'Object::Remote::Null') if $id eq 'NULL';
62 $self->remote_objects_by_id->{$id}
63 or Object::Remote::Handle->new(connection => $self, id => $id)
68 weaken(my $self = shift);
69 JSON::PP->new->filter_json_single_key_object(
70 __remote_object__ => sub {
71 $self->_id_to_remote_object(@_);
73 )->filter_json_single_key_object(
74 __remote_code__ => sub {
75 my $code_container = $self->_id_to_remote_object(@_);
76 sub { $code_container->call(@_) };
82 unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef };
83 eval { require Object::Remote::Connector::Local };
84 eval { require Object::Remote::Connector::LocalSudo };
85 eval { require Object::Remote::Connector::SSH };
89 my ($class, $spec) = @_;
90 return $spec if blessed $spec;
91 foreach my $poss (do { our @Guess }) {
92 if (my $obj = $poss->($spec)) { return $obj }
94 die "Couldn't figure out what to do with ${spec}";
98 my ($self, @args) = @_;
99 Object::Remote::Handle->new(
100 connection => $self, @args
105 my ($self, $to) = @_;
106 return await_future($self->send(
107 class_call => 'Object::Remote', 0, connect => $to
112 my ($self, $sub) = @_;
113 my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/;
114 return await_future($self->send(class_call => $pkg, 0, can => $name));
117 sub register_remote {
118 my ($self, $remote) = @_;
119 weaken($self->remote_objects_by_id->{$remote->id} = $remote);
125 await_future($self->ready_future);
129 my ($self, $id) = @_;
130 delete $self->remote_objects_by_id->{$id};
131 $self->_send([ free => $id ]);
135 my ($self, $type, @call) = @_;
137 unshift @call, $type => my $future = CPS::Future->new;
139 my $outstanding = $self->outstanding_futures;
140 $outstanding->{$future} = $future;
141 $future->on_ready(sub { delete $outstanding->{$future} });
143 $self->_send(\@call);
149 my ($self, $type, @call) = @_;
151 unshift @call, $type => { __remote_object__ => 'NULL' };
153 $self->_send(\@call);
157 my ($self, $to_send) = @_;
161 print { $self->send_to_fh } $self->_serialize($to_send)."\n";
165 my ($self, $data) = @_;
168 my $flat = $self->_encode($self->_deobjectify($data));
169 warn "$$ >>> ${flat}\n" if $DEBUG;
172 my $err = $@; # won't get here if the eval doesn't die
173 # don't keep refs to new things
174 delete @{$self->local_objects_by_id}{@New_Ids};
175 die "Error serializing: $err";
179 sub _local_object_to_id {
180 my ($self, $object) = @_;
181 my $id = refaddr($object);
182 $self->local_objects_by_id->{$id} ||= do {
183 push our(@New_Ids), $id;
190 my ($self, $data) = @_;
191 if (blessed($data)) {
192 return +{ __remote_object__ => $self->_local_object_to_id($data) };
193 } elsif (my $ref = ref($data)) {
194 if ($ref eq 'HASH') {
195 return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data };
196 } elsif ($ref eq 'ARRAY') {
197 return [ map $self->_deobjectify($_), @$data ];
198 } elsif ($ref eq 'CODE') {
199 my $id = $self->_local_object_to_id(
200 Object::Remote::CodeContainer->new(code => $data)
202 return +{ __remote_code__ => $id };
204 die "Can't collapse reftype $ref";
207 return $data; # plain scalar
210 sub _receive_data_from {
211 my ($self, $fh) = @_;
212 my $rb = $self->_receive_data_buffer;
213 my $ready = $self->ready_future->is_ready;
214 if (sysread($fh, $$rb, 1024, length($$rb)) > 0) {
215 while ($$rb =~ s/^(.*)\n//) {
220 die "New remote container did not send Shere - got ${line}"
221 unless $line eq "Shere";
222 $self->ready_future->done;
226 Object::Remote->current_loop
228 handle => $self->receive_from_fh,
231 my $outstanding = $self->outstanding_futures;
232 $_->fail("Connection lost") for values %$outstanding;
234 $self->on_close->done();
239 my ($self, $flat) = @_;
240 warn "$$ <<< $flat\n" if $DEBUG;
241 my ($type, @rest) = eval { @{$self->_deserialize($flat)} }
242 or do { warn "Deserialize failed for ${flat}: $@"; return };
243 eval { $self->${\"receive_${type}"}(@rest); 1 }
244 or do { warn "Receive failed for ${flat}: $@"; return };
249 my ($self, $id) = @_;
250 delete $self->local_objects_by_id->{$id}
251 or warn "Free: no such object $id";
256 my ($self, $future, $id, @rest) = @_;
257 $future->{method} = 'call_discard_free';
258 my $local = $self->local_objects_by_id->{$id}
259 or do { $future->fail("No such object $id"); return };
260 $self->_invoke($future, $local, @rest);
263 sub receive_call_free {
264 my ($self, $future, $id, @rest) = @_;
265 $self->receive_call($future, $id, undef, @rest);
266 $self->receive_free($id);
269 sub receive_class_call {
270 my ($self, $future, $class, @rest) = @_;
271 $future->{method} = 'call_discard_free';
272 eval { use_module($class) }
273 or do { $future->fail("Error loading ${class}: $@"); return };
274 $self->_invoke($future, $class, @rest);
278 my ($self, $future, $local, $ctx, $method, @args) = @_;
279 if ($method =~ /^start::/) {
280 my $f = $local->$method(@args);
281 $f->on_done(sub { undef($f); $future->done(@_) });
283 $f->on_fail(sub { undef($f); $future->fail(@_) });
286 my $do = sub { $local->$method(@args) };
290 ? ($ctx ? $do->() : scalar($do->()))
294 } or do { $future->fail($@); return; };
299 my ($self, $gd) = @_;
301 Object::Remote->current_loop
303 handle => $self->receive_from_fh,