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