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) = @_; |
95 | foreach my $poss (do { our @Guess }) { |
96 | if (my $obj = $poss->($spec)) { return $obj } |
97 | } |
98 | die "Couldn't figure out what to do with ${spec}"; |
99 | } |
100 | |
9e72f0cf |
101 | sub register_remote { |
102 | my ($self, $remote) = @_; |
103 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
104 | return $remote; |
105 | } |
106 | |
8ba4f2e3 |
107 | sub await_ready { |
108 | my ($self) = @_; |
109 | return if $self->is_ready; |
110 | await_future($self->ready_future); |
111 | } |
112 | |
9e72f0cf |
113 | sub send_free { |
114 | my ($self, $id) = @_; |
115 | delete $self->remote_objects_by_id->{$id}; |
116 | $self->_send([ free => $id ]); |
117 | } |
118 | |
119 | sub send { |
120 | my ($self, $type, @call) = @_; |
121 | |
122 | unshift @call, $type => my $future = CPS::Future->new; |
123 | |
a980b0b8 |
124 | my $outstanding = $self->outstanding_futures; |
125 | $outstanding->{$future} = $future; |
126 | $future->on_ready(sub { delete $outstanding->{$future} }); |
127 | |
9e72f0cf |
128 | $self->_send(\@call); |
129 | |
130 | return $future; |
131 | } |
132 | |
9d804009 |
133 | sub send_discard { |
134 | my ($self, $type, @call) = @_; |
135 | |
2065b08b |
136 | unshift @call, $type => { __remote_object__ => 'NULL' }; |
9d804009 |
137 | |
138 | $self->_send(\@call); |
139 | } |
140 | |
9e72f0cf |
141 | sub _send { |
142 | my ($self, $to_send) = @_; |
143 | |
8ba4f2e3 |
144 | $self->await_ready unless $self->is_ready; |
145 | |
9d804009 |
146 | print { $self->send_to_fh } $self->_serialize($to_send)."\n"; |
9e72f0cf |
147 | } |
148 | |
149 | sub _serialize { |
150 | my ($self, $data) = @_; |
151 | local our @New_Ids; |
9d804009 |
152 | return eval { |
ad4f54b2 |
153 | my $flat = $self->_encode($self->_deobjectify($data)); |
154 | warn "$$ >>> ${flat}\n" if $DEBUG; |
155 | $flat; |
70a578ac |
156 | } || do { |
9d804009 |
157 | my $err = $@; # won't get here if the eval doesn't die |
158 | # don't keep refs to new things |
159 | delete @{$self->local_objects_by_id}{@New_Ids}; |
160 | die "Error serializing: $err"; |
161 | }; |
9e72f0cf |
162 | } |
163 | |
a76f2f60 |
164 | sub _local_object_to_id { |
165 | my ($self, $object) = @_; |
166 | my $id = refaddr($object); |
167 | $self->local_objects_by_id->{$id} ||= do { |
168 | push our(@New_Ids), $id; |
169 | $object; |
170 | }; |
171 | return $id; |
172 | } |
173 | |
9e72f0cf |
174 | sub _deobjectify { |
175 | my ($self, $data) = @_; |
176 | if (blessed($data)) { |
a76f2f60 |
177 | return +{ __remote_object__ => $self->_local_object_to_id($data) }; |
9e72f0cf |
178 | } elsif (my $ref = ref($data)) { |
179 | if ($ref eq 'HASH') { |
180 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
181 | } elsif ($ref eq 'ARRAY') { |
182 | return [ map $self->_deobjectify($_), @$data ]; |
fe6c9a7f |
183 | } elsif ($ref eq 'CODE') { |
184 | my $id = $self->_local_object_to_id( |
185 | Object::Remote::CodeContainer->new(code => $data) |
186 | ); |
187 | return +{ __remote_code__ => $id }; |
9e72f0cf |
188 | } else { |
189 | die "Can't collapse reftype $ref"; |
190 | } |
191 | } |
192 | return $data; # plain scalar |
193 | } |
194 | |
195 | sub _receive_data_from { |
196 | my ($self, $fh) = @_; |
197 | my $rb = $self->_receive_data_buffer; |
8ba4f2e3 |
198 | my $ready = $self->is_ready; |
9d804009 |
199 | if (sysread($fh, $$rb, 1024, length($$rb)) > 0) { |
9e72f0cf |
200 | while ($$rb =~ s/^(.*)\n//) { |
8ba4f2e3 |
201 | if ($ready) { |
202 | $self->_receive($1); |
203 | } else { |
204 | my $line = $1; |
205 | die "New remote container did not send Shere - got ${line}" |
206 | unless $line eq "Shere"; |
207 | $self->_set_is_ready($ready = 1); |
208 | } |
9e72f0cf |
209 | } |
ad4f54b2 |
210 | } else { |
a980b0b8 |
211 | Object::Remote->current_loop |
212 | ->unwatch_io( |
213 | handle => $self->receive_from_fh, |
214 | on_read_ready => 1 |
215 | ); |
216 | my $outstanding = $self->outstanding_futures; |
217 | $_->fail("Connection lost") for values %$outstanding; |
218 | %$outstanding = (); |
dc28afe8 |
219 | $self->on_close->done(); |
9e72f0cf |
220 | } |
221 | } |
222 | |
223 | sub _receive { |
ad4f54b2 |
224 | my ($self, $flat) = @_; |
225 | warn "$$ <<< $flat\n" if $DEBUG; |
226 | my ($type, @rest) = eval { @{$self->_deserialize($flat)} } |
227 | or do { warn "Deserialize failed for ${flat}: $@"; return }; |
9e72f0cf |
228 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
ad4f54b2 |
229 | or do { warn "Receive failed for ${flat}: $@"; return }; |
9e72f0cf |
230 | return; |
231 | } |
232 | |
233 | sub receive_free { |
234 | my ($self, $id) = @_; |
9d804009 |
235 | delete $self->local_objects_by_id->{$id} |
236 | or warn "Free: no such object $id"; |
9e72f0cf |
237 | return; |
238 | } |
239 | |
240 | sub receive_call { |
241 | my ($self, $future, $id, @rest) = @_; |
8131a88a |
242 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
243 | my $local = $self->local_objects_by_id->{$id} |
244 | or do { $future->fail("No such object $id"); return }; |
245 | $self->_invoke($future, $local, @rest); |
246 | } |
247 | |
8131a88a |
248 | sub receive_call_free { |
249 | my ($self, $future, $id, @rest) = @_; |
84b04178 |
250 | $self->receive_call($future, $id, undef, @rest); |
8131a88a |
251 | $self->receive_free($id); |
252 | } |
253 | |
9e72f0cf |
254 | sub receive_class_call { |
255 | my ($self, $future, $class, @rest) = @_; |
8131a88a |
256 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
257 | eval { use_module($class) } |
258 | or do { $future->fail("Error loading ${class}: $@"); return }; |
259 | $self->_invoke($future, $class, @rest); |
260 | } |
261 | |
262 | sub _invoke { |
84b04178 |
263 | my ($self, $future, $local, $ctx, $method, @args) = @_; |
dc28afe8 |
264 | if ($method =~ /^start::/) { |
265 | my $f = $local->$method(@args); |
266 | $f->on_done(sub { undef($f); $future->done(@_) }); |
3f1f1e66 |
267 | return unless $f; |
dc28afe8 |
268 | $f->on_fail(sub { undef($f); $future->fail(@_) }); |
269 | return; |
270 | } |
84b04178 |
271 | my $do = sub { $local->$method(@args) }; |
272 | eval { |
273 | $future->done( |
274 | defined($ctx) |
275 | ? ($ctx ? $do->() : scalar($do->())) |
276 | : do { $do->(); () } |
277 | ); |
278 | 1; |
279 | } or do { $future->fail($@); return; }; |
9e72f0cf |
280 | return; |
281 | } |
282 | |
283 | sub DEMOLISH { |
284 | my ($self, $gd) = @_; |
285 | return if $gd; |
286 | Object::Remote->current_loop |
287 | ->unwatch_io( |
288 | handle => $self->receive_from_fh, |
289 | on_read_ready => 1 |
290 | ); |
291 | } |
292 | |
293 | 1; |