Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::Connection; |
2 | |
dc28afe8 |
3 | use Object::Remote::Future; |
9d804009 |
4 | use Object::Remote::Null; |
676438a1 |
5 | use Object::Remote::Handle; |
fe6c9a7f |
6 | use Object::Remote::CodeContainer; |
ed5a8a8e |
7 | use Object::Remote::GlobProxy; |
8 | use Object::Remote::GlobContainer; |
9e72f0cf |
9 | use Object::Remote; |
ed5a8a8e |
10 | use Symbol; |
9e72f0cf |
11 | use IO::Handle; |
12 | use Module::Runtime qw(use_module); |
13 | use Scalar::Util qw(weaken blessed refaddr); |
14 | use JSON::PP qw(encode_json); |
15 | use Moo; |
16 | |
6db5156c |
17 | our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG}; |
ad4f54b2 |
18 | |
9e72f0cf |
19 | has send_to_fh => ( |
20 | is => 'ro', required => 1, |
21 | trigger => sub { $_[1]->autoflush(1) }, |
22 | ); |
23 | |
12fb4a80 |
24 | has read_channel => ( |
9e72f0cf |
25 | is => 'ro', required => 1, |
26 | trigger => sub { |
12fb4a80 |
27 | my ($self, $ch) = @_; |
9e72f0cf |
28 | weaken($self); |
12fb4a80 |
29 | $ch->on_line_call(sub { $self->_receive(@_) }); |
30 | $ch->on_close_call(sub { $self->on_close->done(@_) }); |
9e72f0cf |
31 | }, |
32 | ); |
33 | |
12fb4a80 |
34 | has on_close => ( |
35 | is => 'ro', default => sub { CPS::Future->new }, |
36 | trigger => sub { |
37 | my ($self, $f) = @_; |
38 | weaken($self); |
39 | $f->on_done(sub { |
40 | $self->_fail_outstanding("Connection lost: ".($f->get)[0]); |
41 | }); |
42 | } |
43 | ); |
ad4f54b2 |
44 | |
47c83a13 |
45 | has child_pid => (is => 'ro'); |
46 | |
11dbd4a0 |
47 | has local_objects_by_id => ( |
48 | is => 'ro', default => sub { {} }, |
49 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
50 | ); |
9e72f0cf |
51 | |
11dbd4a0 |
52 | has remote_objects_by_id => ( |
53 | is => 'ro', default => sub { {} }, |
54 | coerce => sub { +{ %{$_[0]} } }, # shallow clone on the way in |
55 | ); |
9e72f0cf |
56 | |
a980b0b8 |
57 | has outstanding_futures => (is => 'ro', default => sub { {} }); |
58 | |
12fb4a80 |
59 | sub _fail_outstanding { |
60 | my ($self, $error) = @_; |
61 | my $outstanding = $self->outstanding_futures; |
62 | $_->fail($error) for values %$outstanding; |
63 | %$outstanding = (); |
64 | return; |
65 | } |
66 | |
9e72f0cf |
67 | has _json => ( |
68 | is => 'lazy', |
69 | handles => { |
70 | _deserialize => 'decode', |
71 | _encode => 'encode', |
72 | }, |
73 | ); |
74 | |
fe6c9a7f |
75 | sub _id_to_remote_object { |
76 | my ($self, $id) = @_; |
77 | return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; |
78 | ( |
79 | $self->remote_objects_by_id->{$id} |
80 | or Object::Remote::Handle->new(connection => $self, id => $id) |
81 | )->proxy; |
82 | } |
83 | |
9e72f0cf |
84 | sub _build__json { |
85 | weaken(my $self = shift); |
9e72f0cf |
86 | JSON::PP->new->filter_json_single_key_object( |
87 | __remote_object__ => sub { |
fe6c9a7f |
88 | $self->_id_to_remote_object(@_); |
89 | } |
90 | )->filter_json_single_key_object( |
91 | __remote_code__ => sub { |
92 | my $code_container = $self->_id_to_remote_object(@_); |
93 | sub { $code_container->call(@_) }; |
9e72f0cf |
94 | } |
6ed5d580 |
95 | )->filter_json_single_key_object( |
96 | __scalar_ref__ => sub { |
97 | my $value = shift; |
98 | return \$value; |
99 | } |
ed5a8a8e |
100 | )->filter_json_single_key_object( |
101 | __glob_ref__ => sub { |
102 | my $glob_container = $self->_id_to_remote_object(@_); |
103 | my $handle = Symbol::gensym; |
104 | tie *$handle, 'Object::Remote::GlobProxy', $glob_container; |
105 | return $handle; |
106 | } |
9e72f0cf |
107 | ); |
108 | } |
109 | |
84b04178 |
110 | BEGIN { |
111 | unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; |
112 | eval { require Object::Remote::Connector::Local }; |
a9fdb55e |
113 | eval { require Object::Remote::Connector::LocalSudo }; |
84b04178 |
114 | eval { require Object::Remote::Connector::SSH }; |
1d26d6f9 |
115 | eval { require Object::Remote::Connector::UNIX }; |
84b04178 |
116 | } |
117 | |
118 | sub new_from_spec { |
119 | my ($class, $spec) = @_; |
e144d525 |
120 | return $spec if blessed $spec; |
84b04178 |
121 | foreach my $poss (do { our @Guess }) { |
fbd3b8ec |
122 | if (my $conn = $poss->($spec)) { |
123 | return $conn->maybe::start::connect; |
124 | } |
84b04178 |
125 | } |
126 | die "Couldn't figure out what to do with ${spec}"; |
127 | } |
128 | |
11dbd4a0 |
129 | sub remote_object { |
e144d525 |
130 | my ($self, @args) = @_; |
131 | Object::Remote::Handle->new( |
132 | connection => $self, @args |
133 | )->proxy; |
134 | } |
135 | |
4c17fea5 |
136 | sub connect { |
137 | my ($self, $to) = @_; |
deb77aaf |
138 | return await_future( |
139 | $self->send_class_call(0, 'Object::Remote', connect => $to) |
140 | ); |
4c17fea5 |
141 | } |
142 | |
11dbd4a0 |
143 | sub remote_sub { |
c6fe6fbd |
144 | my ($self, $sub) = @_; |
145 | my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/; |
deb77aaf |
146 | return await_future($self->send_class_call(0, $pkg, can => $name)); |
147 | } |
148 | |
149 | sub send_class_call { |
150 | my ($self, $ctx, @call) = @_; |
151 | $self->send(call => class_call_handler => $ctx => call => @call); |
152 | } |
153 | |
154 | sub register_class_call_handler { |
155 | my ($self) = @_; |
3687a42d |
156 | $self->local_objects_by_id->{'class_call_handler'} ||= do { |
c5736e1c |
157 | my $o = $self->new_class_call_handler; |
3687a42d |
158 | $self->_local_object_to_id($o); |
159 | $o; |
160 | }; |
c6fe6fbd |
161 | } |
162 | |
c5736e1c |
163 | sub new_class_call_handler { |
164 | Object::Remote::CodeContainer->new( |
165 | code => sub { |
166 | my ($class, $method) = (shift, shift); |
167 | use_module($class)->$method(@_); |
168 | } |
169 | ); |
170 | } |
171 | |
9e72f0cf |
172 | sub register_remote { |
173 | my ($self, $remote) = @_; |
174 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
175 | return $remote; |
176 | } |
177 | |
178 | sub send_free { |
179 | my ($self, $id) = @_; |
180 | delete $self->remote_objects_by_id->{$id}; |
181 | $self->_send([ free => $id ]); |
182 | } |
183 | |
184 | sub send { |
185 | my ($self, $type, @call) = @_; |
186 | |
deb77aaf |
187 | my $future = CPS::Future->new; |
a2d43709 |
188 | my $remote = $self->remote_objects_by_id->{$call[0]}; |
deb77aaf |
189 | |
190 | unshift @call, $type => $self->_local_object_to_id($future); |
9e72f0cf |
191 | |
a980b0b8 |
192 | my $outstanding = $self->outstanding_futures; |
193 | $outstanding->{$future} = $future; |
a2d43709 |
194 | $future->on_ready(sub { |
195 | undef($remote); |
196 | delete $outstanding->{$future} |
197 | }); |
a980b0b8 |
198 | |
9e72f0cf |
199 | $self->_send(\@call); |
200 | |
201 | return $future; |
202 | } |
203 | |
9d804009 |
204 | sub send_discard { |
205 | my ($self, $type, @call) = @_; |
206 | |
deb77aaf |
207 | unshift @call, $type => 'NULL'; |
9d804009 |
208 | |
209 | $self->_send(\@call); |
210 | } |
211 | |
9e72f0cf |
212 | sub _send { |
213 | my ($self, $to_send) = @_; |
214 | |
9d804009 |
215 | print { $self->send_to_fh } $self->_serialize($to_send)."\n"; |
9e72f0cf |
216 | } |
217 | |
218 | sub _serialize { |
219 | my ($self, $data) = @_; |
3687a42d |
220 | local our @New_Ids = (-1); |
9d804009 |
221 | return eval { |
ad4f54b2 |
222 | my $flat = $self->_encode($self->_deobjectify($data)); |
223 | warn "$$ >>> ${flat}\n" if $DEBUG; |
224 | $flat; |
70a578ac |
225 | } || do { |
9d804009 |
226 | my $err = $@; # won't get here if the eval doesn't die |
227 | # don't keep refs to new things |
228 | delete @{$self->local_objects_by_id}{@New_Ids}; |
229 | die "Error serializing: $err"; |
230 | }; |
9e72f0cf |
231 | } |
232 | |
a76f2f60 |
233 | sub _local_object_to_id { |
234 | my ($self, $object) = @_; |
235 | my $id = refaddr($object); |
236 | $self->local_objects_by_id->{$id} ||= do { |
3687a42d |
237 | push our(@New_Ids), $id if @New_Ids; |
a76f2f60 |
238 | $object; |
239 | }; |
240 | return $id; |
241 | } |
242 | |
9e72f0cf |
243 | sub _deobjectify { |
244 | my ($self, $data) = @_; |
245 | if (blessed($data)) { |
a76f2f60 |
246 | return +{ __remote_object__ => $self->_local_object_to_id($data) }; |
9e72f0cf |
247 | } elsif (my $ref = ref($data)) { |
248 | if ($ref eq 'HASH') { |
249 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
250 | } elsif ($ref eq 'ARRAY') { |
251 | return [ map $self->_deobjectify($_), @$data ]; |
fe6c9a7f |
252 | } elsif ($ref eq 'CODE') { |
253 | my $id = $self->_local_object_to_id( |
254 | Object::Remote::CodeContainer->new(code => $data) |
255 | ); |
256 | return +{ __remote_code__ => $id }; |
6ed5d580 |
257 | } elsif ($ref eq 'SCALAR') { |
258 | return +{ __scalar_ref__ => $$data }; |
ed5a8a8e |
259 | } elsif ($ref eq 'GLOB') { |
260 | return +{ __glob_ref__ => $self->_local_object_to_id( |
261 | Object::Remote::GlobContainer->new(handle => $data) |
262 | ) }; |
9e72f0cf |
263 | } else { |
264 | die "Can't collapse reftype $ref"; |
265 | } |
266 | } |
267 | return $data; # plain scalar |
268 | } |
269 | |
9e72f0cf |
270 | sub _receive { |
ad4f54b2 |
271 | my ($self, $flat) = @_; |
272 | warn "$$ <<< $flat\n" if $DEBUG; |
273 | my ($type, @rest) = eval { @{$self->_deserialize($flat)} } |
274 | or do { warn "Deserialize failed for ${flat}: $@"; return }; |
9e72f0cf |
275 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
ad4f54b2 |
276 | or do { warn "Receive failed for ${flat}: $@"; return }; |
9e72f0cf |
277 | return; |
278 | } |
279 | |
280 | sub receive_free { |
281 | my ($self, $id) = @_; |
9d804009 |
282 | delete $self->local_objects_by_id->{$id} |
283 | or warn "Free: no such object $id"; |
9e72f0cf |
284 | return; |
285 | } |
286 | |
287 | sub receive_call { |
deb77aaf |
288 | my ($self, $future_id, $id, @rest) = @_; |
289 | my $future = $self->_id_to_remote_object($future_id); |
8131a88a |
290 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
291 | my $local = $self->local_objects_by_id->{$id} |
292 | or do { $future->fail("No such object $id"); return }; |
293 | $self->_invoke($future, $local, @rest); |
294 | } |
295 | |
8131a88a |
296 | sub receive_call_free { |
297 | my ($self, $future, $id, @rest) = @_; |
84b04178 |
298 | $self->receive_call($future, $id, undef, @rest); |
8131a88a |
299 | $self->receive_free($id); |
300 | } |
301 | |
9e72f0cf |
302 | sub _invoke { |
84b04178 |
303 | my ($self, $future, $local, $ctx, $method, @args) = @_; |
dc28afe8 |
304 | if ($method =~ /^start::/) { |
305 | my $f = $local->$method(@args); |
306 | $f->on_done(sub { undef($f); $future->done(@_) }); |
3f1f1e66 |
307 | return unless $f; |
dc28afe8 |
308 | $f->on_fail(sub { undef($f); $future->fail(@_) }); |
309 | return; |
310 | } |
84b04178 |
311 | my $do = sub { $local->$method(@args) }; |
312 | eval { |
313 | $future->done( |
314 | defined($ctx) |
315 | ? ($ctx ? $do->() : scalar($do->())) |
316 | : do { $do->(); () } |
317 | ); |
318 | 1; |
319 | } or do { $future->fail($@); return; }; |
9e72f0cf |
320 | return; |
321 | } |
322 | |
9e72f0cf |
323 | 1; |
b9a9982d |
324 | |
325 | =head1 NAME |
326 | |
327 | Object::Remote::Connection - An underlying connection for L<Object::Remote> |
328 | |
329 | =head1 LAME |
330 | |
331 | Shipping prioritised over writing this part up. Blame mst. |
332 | |
333 | =cut |