Commit | Line | Data |
9e72f0cf |
1 | package Object::Remote::Connection; |
2 | |
3 | use CPS::Future; |
9d804009 |
4 | use Object::Remote::Null; |
9e72f0cf |
5 | use Object::Remote; |
6 | use IO::Handle; |
7 | use Module::Runtime qw(use_module); |
8 | use Scalar::Util qw(weaken blessed refaddr); |
9 | use JSON::PP qw(encode_json); |
10 | use Moo; |
11 | |
ad4f54b2 |
12 | our $DEBUG; |
13 | |
9e72f0cf |
14 | has send_to_fh => ( |
15 | is => 'ro', required => 1, |
16 | trigger => sub { $_[1]->autoflush(1) }, |
17 | ); |
18 | |
19 | has receive_from_fh => ( |
20 | is => 'ro', required => 1, |
21 | trigger => sub { |
22 | my ($self, $fh) = @_; |
23 | weaken($self); |
24 | Object::Remote->current_loop |
25 | ->watch_io( |
26 | handle => $fh, |
27 | on_read_ready => sub { $self->_receive_data_from($fh) } |
28 | ); |
29 | }, |
30 | ); |
31 | |
ad4f54b2 |
32 | has on_close => (is => 'rw', default => sub {}); |
33 | |
47c83a13 |
34 | has child_pid => (is => 'ro'); |
35 | |
9e72f0cf |
36 | has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x }); |
37 | |
38 | has local_objects_by_id => (is => 'ro', default => sub { {} }); |
39 | |
2065b08b |
40 | has remote_objects_by_id => (is => 'ro', default => sub { {} }); |
9e72f0cf |
41 | |
42 | has _json => ( |
43 | is => 'lazy', |
44 | handles => { |
45 | _deserialize => 'decode', |
46 | _encode => 'encode', |
47 | }, |
48 | ); |
49 | |
50 | sub _build__json { |
51 | weaken(my $self = shift); |
52 | my $remotes = $self->remote_objects_by_id; |
53 | JSON::PP->new->filter_json_single_key_object( |
54 | __remote_object__ => sub { |
55 | my $id = shift; |
2065b08b |
56 | return bless({}, 'Object::Remote::Null') if $id eq 'NULL'; |
9e72f0cf |
57 | ( |
58 | $remotes->{$id} |
59 | or Object::Remote->new(connection => $self, id => $id) |
60 | )->proxy; |
61 | } |
62 | ); |
63 | } |
64 | |
84b04178 |
65 | BEGIN { |
66 | unshift our @Guess, sub { blessed($_[0]) ? $_[0] : undef }; |
67 | eval { require Object::Remote::Connector::Local }; |
68 | eval { require Object::Remote::Connector::SSH }; |
69 | } |
70 | |
71 | sub new_from_spec { |
72 | my ($class, $spec) = @_; |
73 | foreach my $poss (do { our @Guess }) { |
74 | if (my $obj = $poss->($spec)) { return $obj } |
75 | } |
76 | die "Couldn't figure out what to do with ${spec}"; |
77 | } |
78 | |
9e72f0cf |
79 | sub register_remote { |
80 | my ($self, $remote) = @_; |
81 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
82 | return $remote; |
83 | } |
84 | |
85 | sub send_free { |
86 | my ($self, $id) = @_; |
87 | delete $self->remote_objects_by_id->{$id}; |
88 | $self->_send([ free => $id ]); |
89 | } |
90 | |
91 | sub send { |
92 | my ($self, $type, @call) = @_; |
93 | |
94 | unshift @call, $type => my $future = CPS::Future->new; |
95 | |
96 | $self->_send(\@call); |
97 | |
98 | return $future; |
99 | } |
100 | |
9d804009 |
101 | sub send_discard { |
102 | my ($self, $type, @call) = @_; |
103 | |
2065b08b |
104 | unshift @call, $type => { __remote_object__ => 'NULL' }; |
9d804009 |
105 | |
106 | $self->_send(\@call); |
107 | } |
108 | |
9e72f0cf |
109 | sub _send { |
110 | my ($self, $to_send) = @_; |
111 | |
9d804009 |
112 | print { $self->send_to_fh } $self->_serialize($to_send)."\n"; |
9e72f0cf |
113 | } |
114 | |
115 | sub _serialize { |
116 | my ($self, $data) = @_; |
117 | local our @New_Ids; |
9d804009 |
118 | return eval { |
ad4f54b2 |
119 | my $flat = $self->_encode($self->_deobjectify($data)); |
120 | warn "$$ >>> ${flat}\n" if $DEBUG; |
121 | $flat; |
9d804009 |
122 | } or do { |
123 | my $err = $@; # won't get here if the eval doesn't die |
124 | # don't keep refs to new things |
125 | delete @{$self->local_objects_by_id}{@New_Ids}; |
126 | die "Error serializing: $err"; |
127 | }; |
9e72f0cf |
128 | } |
129 | |
130 | sub _deobjectify { |
131 | my ($self, $data) = @_; |
132 | if (blessed($data)) { |
133 | my $id = refaddr($data); |
134 | $self->local_objects_by_id->{$id} ||= do { |
135 | push our(@New_Ids), $id; |
136 | $data; |
137 | }; |
138 | return +{ __remote_object__ => $id }; |
139 | } elsif (my $ref = ref($data)) { |
140 | if ($ref eq 'HASH') { |
141 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
142 | } elsif ($ref eq 'ARRAY') { |
143 | return [ map $self->_deobjectify($_), @$data ]; |
144 | } else { |
145 | die "Can't collapse reftype $ref"; |
146 | } |
147 | } |
148 | return $data; # plain scalar |
149 | } |
150 | |
151 | sub _receive_data_from { |
152 | my ($self, $fh) = @_; |
153 | my $rb = $self->_receive_data_buffer; |
9d804009 |
154 | if (sysread($fh, $$rb, 1024, length($$rb)) > 0) { |
9e72f0cf |
155 | while ($$rb =~ s/^(.*)\n//) { |
156 | $self->_receive($1); |
157 | } |
ad4f54b2 |
158 | } else { |
159 | $self->on_close->(); |
9e72f0cf |
160 | } |
161 | } |
162 | |
163 | sub _receive { |
ad4f54b2 |
164 | my ($self, $flat) = @_; |
165 | warn "$$ <<< $flat\n" if $DEBUG; |
166 | my ($type, @rest) = eval { @{$self->_deserialize($flat)} } |
167 | or do { warn "Deserialize failed for ${flat}: $@"; return }; |
9e72f0cf |
168 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
ad4f54b2 |
169 | or do { warn "Receive failed for ${flat}: $@"; return }; |
9e72f0cf |
170 | return; |
171 | } |
172 | |
173 | sub receive_free { |
174 | my ($self, $id) = @_; |
9d804009 |
175 | delete $self->local_objects_by_id->{$id} |
176 | or warn "Free: no such object $id"; |
9e72f0cf |
177 | return; |
178 | } |
179 | |
180 | sub receive_call { |
181 | my ($self, $future, $id, @rest) = @_; |
8131a88a |
182 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
183 | my $local = $self->local_objects_by_id->{$id} |
184 | or do { $future->fail("No such object $id"); return }; |
185 | $self->_invoke($future, $local, @rest); |
186 | } |
187 | |
8131a88a |
188 | sub receive_call_free { |
189 | my ($self, $future, $id, @rest) = @_; |
84b04178 |
190 | $self->receive_call($future, $id, undef, @rest); |
8131a88a |
191 | $self->receive_free($id); |
192 | } |
193 | |
9e72f0cf |
194 | sub receive_class_call { |
195 | my ($self, $future, $class, @rest) = @_; |
8131a88a |
196 | $future->{method} = 'call_discard_free'; |
9e72f0cf |
197 | eval { use_module($class) } |
198 | or do { $future->fail("Error loading ${class}: $@"); return }; |
199 | $self->_invoke($future, $class, @rest); |
200 | } |
201 | |
202 | sub _invoke { |
84b04178 |
203 | my ($self, $future, $local, $ctx, $method, @args) = @_; |
204 | my $do = sub { $local->$method(@args) }; |
205 | eval { |
206 | $future->done( |
207 | defined($ctx) |
208 | ? ($ctx ? $do->() : scalar($do->())) |
209 | : do { $do->(); () } |
210 | ); |
211 | 1; |
212 | } or do { $future->fail($@); return; }; |
9e72f0cf |
213 | return; |
214 | } |
215 | |
216 | sub DEMOLISH { |
217 | my ($self, $gd) = @_; |
218 | return if $gd; |
219 | Object::Remote->current_loop |
220 | ->unwatch_io( |
221 | handle => $self->receive_from_fh, |
222 | on_read_ready => 1 |
223 | ); |
224 | } |
225 | |
226 | 1; |