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; |
9e72f0cf |
7 | use Object::Remote; |
8 | use IO::Handle; |
9 | use Module::Runtime qw(use_module); |
10 | use Scalar::Util qw(weaken blessed refaddr); |
11 | use JSON::PP qw(encode_json); |
12 | use Moo; |
13 | |
6db5156c |
14 | our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG}; |
ad4f54b2 |
15 | |
9e72f0cf |
16 | has send_to_fh => ( |
17 | is => 'ro', required => 1, |
18 | trigger => sub { $_[1]->autoflush(1) }, |
19 | ); |
20 | |
21 | has receive_from_fh => ( |
22 | is => 'ro', required => 1, |
23 | trigger => sub { |
24 | my ($self, $fh) = @_; |
25 | weaken($self); |
26 | Object::Remote->current_loop |
27 | ->watch_io( |
28 | handle => $fh, |
29 | on_read_ready => sub { $self->_receive_data_from($fh) } |
30 | ); |
31 | }, |
32 | ); |
33 | |
a980b0b8 |
34 | has on_close => (is => 'rw', default => sub { CPS::Future->new }); |
ad4f54b2 |
35 | |
47c83a13 |
36 | has child_pid => (is => 'ro'); |
37 | |
8ba4f2e3 |
38 | has is_ready => (is => 'rwp', trigger => sub { |
39 | my ($self, $value) = @_; |
40 | $self->ready_future->done if $value; |
41 | }); |
42 | |
43 | has ready_future => (is => 'lazy'); |
44 | |
45 | sub _build_ready_future { CPS::Future->new } |
46 | |
9e72f0cf |
47 | has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x }); |
48 | |
49 | has local_objects_by_id => (is => 'ro', default => sub { {} }); |
50 | |
2065b08b |
51 | has remote_objects_by_id => (is => 'ro', default => sub { {} }); |
9e72f0cf |
52 | |
a980b0b8 |
53 | has outstanding_futures => (is => 'ro', default => sub { {} }); |
54 | |
9e72f0cf |
55 | has _json => ( |
56 | is => 'lazy', |
57 | handles => { |
58 | _deserialize => 'decode', |
59 | _encode => 'encode', |
60 | }, |
61 | ); |
62 | |
fe6c9a7f |
63 | sub _id_to_remote_object { |
64 | my ($self, $id) = @_; |
65 | return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; |
66 | ( |
67 | $self->remote_objects_by_id->{$id} |
68 | or Object::Remote::Handle->new(connection => $self, id => $id) |
69 | )->proxy; |
70 | } |
71 | |
9e72f0cf |
72 | sub _build__json { |
73 | weaken(my $self = shift); |
9e72f0cf |
74 | JSON::PP->new->filter_json_single_key_object( |
75 | __remote_object__ => sub { |
fe6c9a7f |
76 | $self->_id_to_remote_object(@_); |
77 | } |
78 | )->filter_json_single_key_object( |
79 | __remote_code__ => sub { |
80 | my $code_container = $self->_id_to_remote_object(@_); |
81 | sub { $code_container->call(@_) }; |
9e72f0cf |
82 | } |
83 | ); |
84 | } |
85 | |
84b04178 |
86 | BEGIN { |
87 | unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; |
88 | eval { require Object::Remote::Connector::Local }; |
a9fdb55e |
89 | eval { require Object::Remote::Connector::LocalSudo }; |
84b04178 |
90 | eval { require Object::Remote::Connector::SSH }; |
91 | } |
92 | |
93 | sub new_from_spec { |
94 | my ($class, $spec) = @_; |
e144d525 |
95 | return $spec if blessed $spec; |
84b04178 |
96 | foreach my $poss (do { our @Guess }) { |
97 | if (my $obj = $poss->($spec)) { return $obj } |
98 | } |
99 | die "Couldn't figure out what to do with ${spec}"; |
100 | } |
101 | |
e144d525 |
102 | sub new_remote { |
103 | my ($self, @args) = @_; |
104 | Object::Remote::Handle->new( |
105 | connection => $self, @args |
106 | )->proxy; |
107 | } |
108 | |
9e72f0cf |
109 | sub register_remote { |
110 | my ($self, $remote) = @_; |
111 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
112 | return $remote; |
113 | } |
114 | |
8ba4f2e3 |
115 | sub await_ready { |
116 | my ($self) = @_; |
117 | return if $self->is_ready; |
118 | await_future($self->ready_future); |
119 | } |
120 | |
9e72f0cf |
121 | sub send_free { |
122 | my ($self, $id) = @_; |
123 | delete $self->remote_objects_by_id->{$id}; |
124 | $self->_send([ free => $id ]); |
125 | } |
126 | |
127 | sub send { |
128 | my ($self, $type, @call) = @_; |
129 | |
130 | unshift @call, $type => my $future = CPS::Future->new; |
131 | |
a980b0b8 |
132 | my $outstanding = $self->outstanding_futures; |
133 | $outstanding->{$future} = $future; |
134 | $future->on_ready(sub { delete $outstanding->{$future} }); |
135 | |
9e72f0cf |
136 | $self->_send(\@call); |
137 | |
138 | return $future; |
139 | } |
140 | |
9d804009 |
141 | sub send_discard { |
142 | my ($self, $type, @call) = @_; |
143 | |
2065b08b |
144 | unshift @call, $type => { __remote_object__ => 'NULL' }; |
9d804009 |
145 | |
146 | $self->_send(\@call); |
147 | } |
148 | |
9e72f0cf |
149 | sub _send { |
150 | my ($self, $to_send) = @_; |
151 | |
8ba4f2e3 |
152 | $self->await_ready unless $self->is_ready; |
153 | |
9d804009 |
154 | print { $self->send_to_fh } $self->_serialize($to_send)."\n"; |
9e72f0cf |
155 | } |
156 | |
157 | sub _serialize { |
158 | my ($self, $data) = @_; |
159 | local our @New_Ids; |
9d804009 |
160 | return eval { |
ad4f54b2 |
161 | my $flat = $self->_encode($self->_deobjectify($data)); |
162 | warn "$$ >>> ${flat}\n" if $DEBUG; |
163 | $flat; |
70a578ac |
164 | } || do { |
9d804009 |
165 | my $err = $@; # won't get here if the eval doesn't die |
166 | # don't keep refs to new things |
167 | delete @{$self->local_objects_by_id}{@New_Ids}; |
168 | die "Error serializing: $err"; |
169 | }; |
9e72f0cf |
170 | } |
171 | |
a76f2f60 |
172 | sub _local_object_to_id { |
173 | my ($self, $object) = @_; |
174 | my $id = refaddr($object); |
175 | $self->local_objects_by_id->{$id} ||= do { |
176 | push our(@New_Ids), $id; |
177 | $object; |
178 | }; |
179 | return $id; |
180 | } |
181 | |
9e72f0cf |
182 | sub _deobjectify { |
183 | my ($self, $data) = @_; |
184 | if (blessed($data)) { |
a76f2f60 |
185 | return +{ __remote_object__ => $self->_local_object_to_id($data) }; |
9e72f0cf |
186 | } elsif (my $ref = ref($data)) { |
187 | if ($ref eq 'HASH') { |
188 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
189 | } elsif ($ref eq 'ARRAY') { |
190 | return [ map $self->_deobjectify($_), @$data ]; |
fe6c9a7f |
191 | } elsif ($ref eq 'CODE') { |
192 | my $id = $self->_local_object_to_id( |
193 | Object::Remote::CodeContainer->new(code => $data) |
194 | ); |
195 | return +{ __remote_code__ => $id }; |
9e72f0cf |
196 | } else { |
197 | die "Can't collapse reftype $ref"; |
198 | } |
199 | } |
200 | return $data; # plain scalar |
201 | } |
202 | |
203 | sub _receive_data_from { |
204 | my ($self, $fh) = @_; |
205 | my $rb = $self->_receive_data_buffer; |
8ba4f2e3 |
206 | my $ready = $self->is_ready; |
9d804009 |
207 | if (sysread($fh, $$rb, 1024, length($$rb)) > 0) { |
9e72f0cf |
208 | while ($$rb =~ s/^(.*)\n//) { |
8ba4f2e3 |
209 | if ($ready) { |
210 | $self->_receive($1); |
211 | } else { |
212 | my $line = $1; |
213 | die "New remote container did not send Shere - got ${line}" |
214 | unless $line eq "Shere"; |
215 | $self->_set_is_ready($ready = 1); |
216 | } |
9e72f0cf |
217 | } |
ad4f54b2 |
218 | } else { |
a980b0b8 |
219 | Object::Remote->current_loop |
220 | ->unwatch_io( |
221 | handle => $self->receive_from_fh, |
222 | on_read_ready => 1 |
223 | ); |
224 | my $outstanding = $self->outstanding_futures; |
225 | $_->fail("Connection lost") for values %$outstanding; |
226 | %$outstanding = (); |
dc28afe8 |
227 | $self->on_close->done(); |
9e72f0cf |
228 | } |
229 | } |
230 | |
231 | sub _receive { |
ad4f54b2 |
232 | my ($self, $flat) = @_; |
233 | warn "$$ <<< $flat\n" if $DEBUG; |
234 | my ($type, @rest) = eval { @{$self->_deserialize($flat)} } |
235 | or do { warn "Deserialize failed for ${flat}: $@"; return }; |
9e72f0cf |
236 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
ad4f54b2 |
237 | or do { warn "Receive failed for ${flat}: $@"; return }; |
9e72f0cf |
238 | return; |
239 | } |
240 | |
241 | sub receive_free { |
242 | my ($self, $id) = @_; |
9d804009 |
243 | delete $self->local_objects_by_id->{$id} |
244 | or warn "Free: no such object $id"; |
9e72f0cf |
245 | return; |
246 | } |
247 | |
248 | sub receive_call { |
249 | my ($self, $future, $id, @rest) = @_; |
8131a88a |
250 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
251 | my $local = $self->local_objects_by_id->{$id} |
252 | or do { $future->fail("No such object $id"); return }; |
253 | $self->_invoke($future, $local, @rest); |
254 | } |
255 | |
8131a88a |
256 | sub receive_call_free { |
257 | my ($self, $future, $id, @rest) = @_; |
84b04178 |
258 | $self->receive_call($future, $id, undef, @rest); |
8131a88a |
259 | $self->receive_free($id); |
260 | } |
261 | |
9e72f0cf |
262 | sub receive_class_call { |
263 | my ($self, $future, $class, @rest) = @_; |
8131a88a |
264 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
265 | eval { use_module($class) } |
266 | or do { $future->fail("Error loading ${class}: $@"); return }; |
267 | $self->_invoke($future, $class, @rest); |
268 | } |
269 | |
270 | sub _invoke { |
84b04178 |
271 | my ($self, $future, $local, $ctx, $method, @args) = @_; |
dc28afe8 |
272 | if ($method =~ /^start::/) { |
273 | my $f = $local->$method(@args); |
274 | $f->on_done(sub { undef($f); $future->done(@_) }); |
3f1f1e66 |
275 | return unless $f; |
dc28afe8 |
276 | $f->on_fail(sub { undef($f); $future->fail(@_) }); |
277 | return; |
278 | } |
84b04178 |
279 | my $do = sub { $local->$method(@args) }; |
280 | eval { |
281 | $future->done( |
282 | defined($ctx) |
283 | ? ($ctx ? $do->() : scalar($do->())) |
284 | : do { $do->(); () } |
285 | ); |
286 | 1; |
287 | } or do { $future->fail($@); return; }; |
9e72f0cf |
288 | return; |
289 | } |
290 | |
291 | sub DEMOLISH { |
292 | my ($self, $gd) = @_; |
293 | return if $gd; |
294 | Object::Remote->current_loop |
295 | ->unwatch_io( |
296 | handle => $self->receive_from_fh, |
297 | on_read_ready => 1 |
298 | ); |
299 | } |
300 | |
301 | 1; |