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;
7 use Object::Remote::GlobProxy;
8 use Object::Remote::GlobContainer;
12 use Module::Runtime qw(use_module);
13 use Scalar::Util qw(weaken blessed refaddr);
14 use JSON::PP qw(encode_json);
17 our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG};
20 is => 'ro', required => 1,
21 trigger => sub { $_[1]->autoflush(1) },
24 has receive_from_fh => (
25 is => 'ro', required => 1,
29 Object::Remote->current_loop
32 on_read_ready => sub { $self->_receive_data_from($fh) }
37 has on_close => (is => 'rw', default => sub { CPS::Future->new });
39 has child_pid => (is => 'ro');
41 has ready_future => (is => 'lazy');
43 sub _build_ready_future { CPS::Future->new }
45 has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x });
47 has local_objects_by_id => (
48 is => 'ro', default => sub { {} },
49 coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in
52 has remote_objects_by_id => (
53 is => 'ro', default => sub { {} },
54 coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in
57 has outstanding_futures => (is => 'ro', default => sub { {} });
62 _deserialize => 'decode',
67 sub _id_to_remote_object {
69 return bless({}, 'Object::Remote::Null') if $id eq 'NULL';
71 $self->remote_objects_by_id->{$id}
72 or Object::Remote::Handle->new(connection => $self, id => $id)
77 weaken(my $self = shift);
78 JSON::PP->new->filter_json_single_key_object(
79 __remote_object__ => sub {
80 $self->_id_to_remote_object(@_);
82 )->filter_json_single_key_object(
83 __remote_code__ => sub {
84 my $code_container = $self->_id_to_remote_object(@_);
85 sub { $code_container->call(@_) };
87 )->filter_json_single_key_object(
88 __scalar_ref__ => sub {
92 )->filter_json_single_key_object(
94 my $glob_container = $self->_id_to_remote_object(@_);
95 my $handle = Symbol::gensym;
96 tie *$handle, 'Object::Remote::GlobProxy', $glob_container;
103 unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef };
104 eval { require Object::Remote::Connector::Local };
105 eval { require Object::Remote::Connector::LocalSudo };
106 eval { require Object::Remote::Connector::SSH };
107 eval { require Object::Remote::Connector::UNIX };
111 my ($class, $spec) = @_;
112 return $spec if blessed $spec;
113 foreach my $poss (do { our @Guess }) {
114 if (my $obj = $poss->($spec)) { return $obj }
116 die "Couldn't figure out what to do with ${spec}";
120 my ($self, @args) = @_;
121 Object::Remote::Handle->new(
122 connection => $self, @args
127 my ($self, $to) = @_;
129 $self->send_class_call(0, 'Object::Remote', connect => $to)
134 my ($self, $sub) = @_;
135 my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/;
136 return await_future($self->send_class_call(0, $pkg, can => $name));
139 sub send_class_call {
140 my ($self, $ctx, @call) = @_;
141 $self->send(call => class_call_handler => $ctx => call => @call);
144 sub register_class_call_handler {
146 $self->local_objects_by_id->{'class_call_handler'} ||= do {
147 my $o = $self->new_class_call_handler;
148 $self->_local_object_to_id($o);
153 sub new_class_call_handler {
154 Object::Remote::CodeContainer->new(
156 my ($class, $method) = (shift, shift);
157 use_module($class)->$method(@_);
162 sub register_remote {
163 my ($self, $remote) = @_;
164 weaken($self->remote_objects_by_id->{$remote->id} = $remote);
170 await_future($self->ready_future);
174 my ($self, $id) = @_;
175 delete $self->remote_objects_by_id->{$id};
176 $self->_send([ free => $id ]);
180 my ($self, $type, @call) = @_;
182 my $future = CPS::Future->new;
183 my $remote = $self->remote_objects_by_id->{$call[0]};
185 unshift @call, $type => $self->_local_object_to_id($future);
187 my $outstanding = $self->outstanding_futures;
188 $outstanding->{$future} = $future;
189 $future->on_ready(sub {
191 delete $outstanding->{$future}
194 $self->_send(\@call);
200 my ($self, $type, @call) = @_;
202 unshift @call, $type => 'NULL';
204 $self->_send(\@call);
208 my ($self, $to_send) = @_;
212 print { $self->send_to_fh } $self->_serialize($to_send)."\n";
216 my ($self, $data) = @_;
217 local our @New_Ids = (-1);
219 my $flat = $self->_encode($self->_deobjectify($data));
220 warn "$$ >>> ${flat}\n" if $DEBUG;
223 my $err = $@; # won't get here if the eval doesn't die
224 # don't keep refs to new things
225 delete @{$self->local_objects_by_id}{@New_Ids};
226 die "Error serializing: $err";
230 sub _local_object_to_id {
231 my ($self, $object) = @_;
232 my $id = refaddr($object);
233 $self->local_objects_by_id->{$id} ||= do {
234 push our(@New_Ids), $id if @New_Ids;
241 my ($self, $data) = @_;
242 if (blessed($data)) {
243 return +{ __remote_object__ => $self->_local_object_to_id($data) };
244 } elsif (my $ref = ref($data)) {
245 if ($ref eq 'HASH') {
246 return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data };
247 } elsif ($ref eq 'ARRAY') {
248 return [ map $self->_deobjectify($_), @$data ];
249 } elsif ($ref eq 'CODE') {
250 my $id = $self->_local_object_to_id(
251 Object::Remote::CodeContainer->new(code => $data)
253 return +{ __remote_code__ => $id };
254 } elsif ($ref eq 'SCALAR') {
255 return +{ __scalar_ref__ => $$data };
256 } elsif ($ref eq 'GLOB') {
257 return +{ __glob_ref__ => $self->_local_object_to_id(
258 Object::Remote::GlobContainer->new(handle => $data)
261 die "Can't collapse reftype $ref";
264 return $data; # plain scalar
267 sub _receive_data_from {
268 my ($self, $fh) = @_;
269 my $rb = $self->_receive_data_buffer;
270 my $ready = $self->ready_future->is_ready;
271 my $len = sysread($fh, $$rb, 1024, length($$rb));
272 my $err = defined($len) ? '' : ": $!";
273 if (defined($len) and $len > 0) {
274 while ($$rb =~ s/^(.*)\n//) {
279 die "New remote container did not send Shere - got ${line}"
280 unless $line eq "Shere";
281 $self->ready_future->done;
285 Object::Remote->current_loop
287 handle => $self->receive_from_fh,
290 my $outstanding = $self->outstanding_futures;
291 $_->fail("Connection lost${err}") for values %$outstanding;
293 $self->on_close->done();
298 my ($self, $flat) = @_;
299 warn "$$ <<< $flat\n" if $DEBUG;
300 my ($type, @rest) = eval { @{$self->_deserialize($flat)} }
301 or do { warn "Deserialize failed for ${flat}: $@"; return };
302 eval { $self->${\"receive_${type}"}(@rest); 1 }
303 or do { warn "Receive failed for ${flat}: $@"; return };
308 my ($self, $id) = @_;
309 delete $self->local_objects_by_id->{$id}
310 or warn "Free: no such object $id";
315 my ($self, $future_id, $id, @rest) = @_;
316 my $future = $self->_id_to_remote_object($future_id);
317 $future->{method} = 'call_discard_free';
318 my $local = $self->local_objects_by_id->{$id}
319 or do { $future->fail("No such object $id"); return };
320 $self->_invoke($future, $local, @rest);
323 sub receive_call_free {
324 my ($self, $future, $id, @rest) = @_;
325 $self->receive_call($future, $id, undef, @rest);
326 $self->receive_free($id);
330 my ($self, $future, $local, $ctx, $method, @args) = @_;
331 if ($method =~ /^start::/) {
332 my $f = $local->$method(@args);
333 $f->on_done(sub { undef($f); $future->done(@_) });
335 $f->on_fail(sub { undef($f); $future->fail(@_) });
338 my $do = sub { $local->$method(@args) };
342 ? ($ctx ? $do->() : scalar($do->()))
346 } or do { $future->fail($@); return; };
351 my ($self, $gd) = @_;
353 Object::Remote->current_loop
355 handle => $self->receive_from_fh,
364 Object::Remote::Connection - An underlying connection for L<Object::Remote>
368 Shipping prioritised over writing this part up. Blame mst.