… | |
… | |
33 | $self |
33 | $self |
34 | } |
34 | } |
35 | |
35 | |
36 | sub init { |
36 | sub init { |
37 | # |
37 | # |
38 | } |
|
|
39 | |
|
|
40 | #d# only needed for bad global code at the moment |
|
|
41 | sub transport_error { |
|
|
42 | } |
38 | } |
43 | |
39 | |
44 | sub send { |
40 | sub send { |
45 | &{ shift->{send} } |
41 | &{ shift->{send} } |
46 | } |
42 | } |
… | |
… | |
96 | delete $self->{trial}; |
92 | delete $self->{trial}; |
97 | |
93 | |
98 | $self->transport_error (transport_error => $self->{id}, "switched connections") |
94 | $self->transport_error (transport_error => $self->{id}, "switched connections") |
99 | if $self->{transport}; |
95 | if $self->{transport}; |
100 | |
96 | |
101 | delete $self->{connect_addr}; |
|
|
102 | delete $self->{connect_w}; |
97 | delete $self->{connect_w}; |
103 | delete $self->{connect_to}; |
98 | delete $self->{connect_to}; |
104 | |
99 | |
105 | $self->{transport} = $transport; |
100 | $self->{transport} = $transport; |
106 | |
101 | |
… | |
… | |
120 | return if $self->{transport}; |
115 | return if $self->{transport}; |
121 | return if $self->{connect_w}; |
116 | return if $self->{connect_w}; |
122 | |
117 | |
123 | Scalar::Util::weaken $self; |
118 | Scalar::Util::weaken $self; |
124 | |
119 | |
125 | my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
120 | $self->{connect_to} ||= AE::timer $AnyEvent::MP::Kernel::CONFIG->{connect_interval}, 0, sub { |
126 | |
|
|
127 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
|
|
128 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
121 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
129 | }; |
122 | }; |
130 | |
123 | |
131 | # maybe @$addresses? |
124 | # maybe @$addresses? |
132 | my @addresses = @{ $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}} }; |
125 | my $addresses = $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}}; |
133 | |
126 | |
134 | unless (@addresses) { |
127 | if ($addresses) { |
|
|
128 | $self->connect_to ($addresses); |
|
|
129 | } else { |
135 | # on global nodes, all bets are off now - we either know the node, or we don't |
130 | # on global nodes, all bets are off now - we either know the node, or we don't |
136 | unless ($AnyEvent::MP::Kernel::GLOBAL) { |
131 | unless ($AnyEvent::MP::Kernel::GLOBAL) { |
137 | $self->{connect_w} = AnyEvent::MP::Kernel::global_req (g_find => $self->{id}); |
132 | AnyEvent::MP::Kernel::g_find ($self->{id}); |
138 | } |
133 | } |
139 | return; |
134 | } |
|
|
135 | } |
|
|
136 | |
|
|
137 | sub connect_to { |
|
|
138 | my ($self, $addresses) = @_; |
|
|
139 | |
|
|
140 | return if $self->{transport}; |
|
|
141 | return if $self->{connect_w}; |
|
|
142 | |
|
|
143 | return unless @$addresses; |
140 | } |
144 | |
141 | |
|
|
142 | $self->{connect_addr} = \@addresses; # a bit weird, but efficient |
|
|
143 | |
|
|
144 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
145 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@$addresses]"); |
145 | |
146 | |
|
|
147 | my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
146 | my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
148 | my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
147 | |
149 | |
148 | $interval = ($monitor - $interval) / @addresses |
150 | $interval = ($monitor - $interval) / @$addresses |
149 | if ($monitor - $interval) / @addresses < $interval; |
151 | if ($monitor - $interval) / @$addresses < $interval; |
150 | |
152 | |
151 | $interval = 0.4 if $interval < 0.4; |
153 | $interval = 0.4 if $interval < 0.4; |
152 | |
154 | |
153 | my @endpoints; |
155 | my @endpoints = reverse @$addresses; |
154 | |
156 | |
155 | $self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub { |
157 | $self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub { |
156 | @endpoints = @addresses |
|
|
157 | unless @endpoints; |
|
|
158 | |
|
|
159 | my $endpoint = shift @endpoints; |
158 | my $endpoint = pop @endpoints; |
160 | |
159 | |
161 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
160 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
162 | |
161 | |
163 | $self->{trial}{$endpoint} ||= do { |
162 | $self->{trial}{$endpoint} ||= do { |
164 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
163 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
… | |
… | |
169 | sub { delete $self->{trial}{$endpoint} }, |
168 | sub { delete $self->{trial}{$endpoint} }, |
170 | }; |
169 | }; |
171 | }; |
170 | }; |
172 | } |
171 | } |
173 | |
172 | |
|
|
173 | sub connect { |
|
|
174 | my ($self) = @_; |
|
|
175 | |
|
|
176 | return if $self->{transport}; |
|
|
177 | return if $self->{connect_w}; |
|
|
178 | |
|
|
179 | Scalar::Util::weaken $self; |
|
|
180 | |
|
|
181 | my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
|
|
182 | |
|
|
183 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
|
|
184 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
|
|
185 | }; |
|
|
186 | |
|
|
187 | # maybe @$addresses? |
|
|
188 | my @addresses = @{ $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}} }; |
|
|
189 | |
|
|
190 | unless (@addresses) { |
|
|
191 | # on global nodes, all bets are off now - we either know the node, or we don't |
|
|
192 | unless ($AnyEvent::MP::Kernel::GLOBAL) { |
|
|
193 | AnyEvent::MP::Kernel::g_find ($self->{id}); |
|
|
194 | } |
|
|
195 | return; |
|
|
196 | } |
|
|
197 | |
|
|
198 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
|
|
199 | |
|
|
200 | my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
|
|
201 | |
|
|
202 | $interval = ($monitor - $interval) / @addresses |
|
|
203 | if ($monitor - $interval) / @addresses < $interval; |
|
|
204 | |
|
|
205 | $interval = 0.4 if $interval < 0.4; |
|
|
206 | |
|
|
207 | my @endpoints; |
|
|
208 | |
|
|
209 | $self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub { |
|
|
210 | @endpoints = @addresses |
|
|
211 | unless @endpoints; |
|
|
212 | |
|
|
213 | my $endpoint = shift @endpoints; |
|
|
214 | |
|
|
215 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
216 | |
|
|
217 | $self->{trial}{$endpoint} ||= do { |
|
|
218 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
219 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
|
|
220 | |
|
|
221 | AnyEvent::MP::Transport::mp_connect |
|
|
222 | $host, $port, |
|
|
223 | sub { delete $self->{trial}{$endpoint} }, |
|
|
224 | }; |
|
|
225 | }; |
|
|
226 | } |
|
|
227 | |
174 | sub kill { |
228 | sub kill { |
175 | my ($self, $port, @reason) = @_; |
229 | my ($self, $port, @reason) = @_; |
176 | |
230 | |
177 | $self->{send} (["", kil => $port, @reason]); |
231 | $self->{send} (["", kil => $port, @reason]); |
178 | } |
232 | } |
… | |
… | |
214 | our $DELAY = -50; |
268 | our $DELAY = -50; |
215 | our @DELAY; |
269 | our @DELAY; |
216 | our $DELAY_W; |
270 | our $DELAY_W; |
217 | |
271 | |
218 | sub _send_delayed { |
272 | sub _send_delayed { |
219 | local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE{""}; |
273 | $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
220 | (shift @DELAY)->() |
274 | (shift @DELAY)->() |
221 | while @DELAY; |
275 | while @DELAY; |
222 | undef $DELAY_W; |
276 | undef $DELAY_W; |
223 | $DELAY = -50; |
277 | $DELAY = -50; |
224 | } |
278 | } |
… | |
… | |
234 | push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
288 | push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
235 | $DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
289 | $DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
236 | return; |
290 | return; |
237 | } |
291 | } |
238 | |
292 | |
239 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
293 | local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
240 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
294 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
241 | }; |
295 | }; |
242 | } |
296 | } |
243 | |
297 | |
244 | sub transport_connect { |
298 | sub transport_connect { |