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 | |
12 | has send_to_fh => ( |
13 | is => 'ro', required => 1, |
14 | trigger => sub { $_[1]->autoflush(1) }, |
15 | ); |
16 | |
17 | has receive_from_fh => ( |
18 | is => 'ro', required => 1, |
19 | trigger => sub { |
20 | my ($self, $fh) = @_; |
21 | weaken($self); |
22 | Object::Remote->current_loop |
23 | ->watch_io( |
24 | handle => $fh, |
25 | on_read_ready => sub { $self->_receive_data_from($fh) } |
26 | ); |
27 | }, |
28 | ); |
29 | |
30 | has _receive_data_buffer => (is => 'ro', default => sub { my $x = ''; \$x }); |
31 | |
32 | has local_objects_by_id => (is => 'ro', default => sub { {} }); |
33 | |
9d804009 |
34 | has remote_objects_by_id => ( |
35 | is => 'ro', default => sub { { NULL => bless({}, 'Object::Remote::Null') } } |
36 | ); |
9e72f0cf |
37 | |
38 | has _json => ( |
39 | is => 'lazy', |
40 | handles => { |
41 | _deserialize => 'decode', |
42 | _encode => 'encode', |
43 | }, |
44 | ); |
45 | |
46 | sub _build__json { |
47 | weaken(my $self = shift); |
48 | my $remotes = $self->remote_objects_by_id; |
49 | JSON::PP->new->filter_json_single_key_object( |
50 | __remote_object__ => sub { |
51 | my $id = shift; |
52 | ( |
53 | $remotes->{$id} |
54 | or Object::Remote->new(connection => $self, id => $id) |
55 | )->proxy; |
56 | } |
57 | ); |
58 | } |
59 | |
60 | sub register_remote { |
61 | my ($self, $remote) = @_; |
62 | weaken($self->remote_objects_by_id->{$remote->id} = $remote); |
63 | return $remote; |
64 | } |
65 | |
66 | sub send_free { |
67 | my ($self, $id) = @_; |
68 | delete $self->remote_objects_by_id->{$id}; |
69 | $self->_send([ free => $id ]); |
70 | } |
71 | |
72 | sub send { |
73 | my ($self, $type, @call) = @_; |
74 | |
75 | unshift @call, $type => my $future = CPS::Future->new; |
76 | |
77 | $self->_send(\@call); |
78 | |
79 | return $future; |
80 | } |
81 | |
9d804009 |
82 | sub send_discard { |
83 | my ($self, $type, @call) = @_; |
84 | |
85 | unshift @call, $type => { __remote_object => 'NULL' }; |
86 | |
87 | $self->_send(\@call); |
88 | } |
89 | |
9e72f0cf |
90 | sub _send { |
91 | my ($self, $to_send) = @_; |
92 | |
9d804009 |
93 | print { $self->send_to_fh } $self->_serialize($to_send)."\n"; |
9e72f0cf |
94 | } |
95 | |
96 | sub _serialize { |
97 | my ($self, $data) = @_; |
98 | local our @New_Ids; |
9d804009 |
99 | return eval { |
100 | $self->_encode($self->_deobjectify($data)) |
101 | } or do { |
102 | my $err = $@; # won't get here if the eval doesn't die |
103 | # don't keep refs to new things |
104 | delete @{$self->local_objects_by_id}{@New_Ids}; |
105 | die "Error serializing: $err"; |
106 | }; |
9e72f0cf |
107 | } |
108 | |
109 | sub _deobjectify { |
110 | my ($self, $data) = @_; |
111 | if (blessed($data)) { |
112 | my $id = refaddr($data); |
113 | $self->local_objects_by_id->{$id} ||= do { |
114 | push our(@New_Ids), $id; |
115 | $data; |
116 | }; |
117 | return +{ __remote_object__ => $id }; |
118 | } elsif (my $ref = ref($data)) { |
119 | if ($ref eq 'HASH') { |
120 | return +{ map +($_ => $self->_deobjectify($data->{$_})), keys %$data }; |
121 | } elsif ($ref eq 'ARRAY') { |
122 | return [ map $self->_deobjectify($_), @$data ]; |
123 | } else { |
124 | die "Can't collapse reftype $ref"; |
125 | } |
126 | } |
127 | return $data; # plain scalar |
128 | } |
129 | |
130 | sub _receive_data_from { |
131 | my ($self, $fh) = @_; |
132 | my $rb = $self->_receive_data_buffer; |
9d804009 |
133 | if (sysread($fh, $$rb, 1024, length($$rb)) > 0) { |
9e72f0cf |
134 | while ($$rb =~ s/^(.*)\n//) { |
135 | $self->_receive($1); |
136 | } |
137 | } |
138 | } |
139 | |
140 | sub _receive { |
141 | my ($self, $data) = @_; |
142 | my ($type, @rest) = eval { @{$self->_deserialize($data)} } |
143 | or do { warn "Deserialize failed for ${data}: $@"; return }; |
144 | eval { $self->${\"receive_${type}"}(@rest); 1 } |
145 | or do { warn "Receive failed for ${data}: $@"; return }; |
146 | return; |
147 | } |
148 | |
149 | sub receive_free { |
150 | my ($self, $id) = @_; |
9d804009 |
151 | delete $self->local_objects_by_id->{$id} |
152 | or warn "Free: no such object $id"; |
9e72f0cf |
153 | return; |
154 | } |
155 | |
156 | sub receive_call { |
157 | my ($self, $future, $id, @rest) = @_; |
158 | my $local = $self->local_objects_by_id->{$id} |
159 | or do { $future->fail("No such object $id"); return }; |
160 | $self->_invoke($future, $local, @rest); |
161 | } |
162 | |
163 | sub receive_class_call { |
164 | my ($self, $future, $class, @rest) = @_; |
165 | eval { use_module($class) } |
166 | or do { $future->fail("Error loading ${class}: $@"); return }; |
167 | $self->_invoke($future, $class, @rest); |
168 | } |
169 | |
170 | sub _invoke { |
171 | my ($self, $future, $local, $method, @args) = @_; |
172 | eval { $future->done($local->$method(@args)); 1 } |
173 | or do { $future->fail($@); return; }; |
174 | return; |
175 | } |
176 | |
177 | sub DEMOLISH { |
178 | my ($self, $gd) = @_; |
179 | return if $gd; |
180 | Object::Remote->current_loop |
181 | ->unwatch_io( |
182 | handle => $self->receive_from_fh, |
183 | on_read_ready => 1 |
184 | ); |
185 | } |
186 | |
187 | 1; |