… | |
… | |
58 | push @{$self->{queue}}, shift; |
58 | push @{$self->{queue}}, shift; |
59 | $self->connect; |
59 | $self->connect; |
60 | }; |
60 | }; |
61 | } |
61 | } |
62 | |
62 | |
63 | # called only after successful handshake |
63 | # called each time we fail to establish a connection, |
|
|
64 | # or the existing connection failed |
64 | sub transport_error { |
65 | sub transport_error { |
65 | my ($self, @reason) = @_; |
66 | my ($self, @reason) = @_; |
66 | |
67 | |
67 | my $no_transport = !$self->{transport}; |
68 | my $no_transport = !$self->{transport}; |
68 | |
69 | |
… | |
… | |
76 | $_->(@reason) for map @$_, values %$mon; |
77 | $_->(@reason) for map @$_, values %$mon; |
77 | } |
78 | } |
78 | |
79 | |
79 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
80 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
80 | unless $no_transport; |
81 | unless $no_transport; |
|
|
82 | |
|
|
83 | # if we are here and are idle, we nuke ourselves |
|
|
84 | delete $AnyEvent::MP::Kernel::NODE{$self->{id}} |
|
|
85 | unless $self->{transport} || $self->{connect_to}; |
81 | } |
86 | } |
82 | |
87 | |
83 | # called after handshake was successful |
88 | # called after handshake was successful |
84 | sub transport_connect { |
89 | sub transport_connect { |
85 | my ($self, $transport) = @_; |
90 | my ($self, $transport) = @_; |
… | |
… | |
87 | delete $self->{trial}; |
92 | delete $self->{trial}; |
88 | |
93 | |
89 | $self->transport_error (transport_error => "switched connections") |
94 | $self->transport_error (transport_error => "switched connections") |
90 | if $self->{transport}; |
95 | if $self->{transport}; |
91 | |
96 | |
|
|
97 | delete $self->{connect_addr}; |
92 | delete $self->{connect_w}; |
98 | delete $self->{connect_w}; |
93 | delete $self->{connect_to}; |
99 | delete $self->{connect_to}; |
94 | |
100 | |
95 | $self->{transport} = $transport; |
101 | $self->{transport} = $transport; |
96 | |
102 | |
… | |
… | |
111 | |
117 | |
112 | return if $self->{transport}; |
118 | return if $self->{transport}; |
113 | |
119 | |
114 | Scalar::Util::weaken $self; |
120 | Scalar::Util::weaken $self; |
115 | |
121 | |
|
|
122 | my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
|
|
123 | |
116 | $self->{connect_to} ||= AE::timer |
124 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
117 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
|
|
118 | 0, |
|
|
119 | sub { |
|
|
120 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
125 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
|
|
126 | }; |
|
|
127 | |
|
|
128 | return unless @addresses; |
|
|
129 | |
|
|
130 | if ($self->{connect_w}) { |
|
|
131 | # sometimes we get told about new addresses after we started to connect |
|
|
132 | unshift @{$self->{connect_addr}}, @addresses; |
|
|
133 | return; |
|
|
134 | } |
|
|
135 | |
|
|
136 | $self->{connect_addr} = \@addresses; # a bit weird, but efficient |
|
|
137 | |
|
|
138 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
|
|
139 | |
|
|
140 | my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
|
|
141 | |
|
|
142 | $interval = ($monitor - $interval) / @addresses |
|
|
143 | if ($monitor - $interval) / @addresses < $interval; |
|
|
144 | |
|
|
145 | $interval = 0.4 if $interval < 0.4; |
|
|
146 | |
|
|
147 | my @endpoints; |
|
|
148 | |
|
|
149 | $self->{connect_w} = AE::timer 0.050 * rand, $interval * (0.9 + 0.1 * rand), sub { |
|
|
150 | @endpoints = @addresses |
|
|
151 | unless @endpoints; |
|
|
152 | |
|
|
153 | my $endpoint = shift @endpoints; |
|
|
154 | |
|
|
155 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
156 | |
|
|
157 | $self->{trial}{$endpoint} ||= do { |
|
|
158 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
159 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
|
|
160 | |
|
|
161 | AnyEvent::MP::Transport::mp_connect |
|
|
162 | $host, $port, |
|
|
163 | sub { delete $self->{trial}{$endpoint} }, |
121 | }; |
164 | }; |
122 | |
|
|
123 | return unless @addresses; |
|
|
124 | |
|
|
125 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
|
|
126 | |
|
|
127 | unless ($self->{connect_w}) { |
|
|
128 | my @endpoints; |
|
|
129 | |
|
|
130 | $self->{connect_w} = AE::timer |
|
|
131 | rand, |
|
|
132 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
|
|
133 | sub { |
|
|
134 | @endpoints = @addresses |
|
|
135 | unless @endpoints; |
|
|
136 | |
|
|
137 | my $endpoint = shift @endpoints; |
|
|
138 | |
|
|
139 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
140 | |
|
|
141 | $self->{trial}{$endpoint} ||= do { |
|
|
142 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
143 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
|
|
144 | |
|
|
145 | AnyEvent::MP::Transport::mp_connect |
|
|
146 | $host, $port, |
|
|
147 | sub { delete $self->{trial}{$endpoint} }, |
|
|
148 | }; |
|
|
149 | } |
|
|
150 | ; |
|
|
151 | } |
165 | }; |
152 | } |
166 | } |
153 | |
167 | |
154 | sub kill { |
168 | sub kill { |
155 | my ($self, $port, @reason) = @_; |
169 | my ($self, $port, @reason) = @_; |
156 | |
170 | |
… | |
… | |
193 | |
207 | |
194 | sub connect { |
208 | sub connect { |
195 | # we are trivially connected |
209 | # we are trivially connected |
196 | } |
210 | } |
197 | |
211 | |
|
|
212 | # delay every so often to avoid recursion, also used to delay after spawn |
|
|
213 | our $DELAY = -50; |
|
|
214 | our @DELAY; |
|
|
215 | our $DELAY_W; |
|
|
216 | |
|
|
217 | sub _send_delayed { |
|
|
218 | local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE{""}; |
|
|
219 | (shift @DELAY)->() |
|
|
220 | while @DELAY; |
|
|
221 | undef $DELAY_W; |
|
|
222 | $DELAY = -50; |
|
|
223 | } |
|
|
224 | |
198 | sub transport_reset { |
225 | sub transport_reset { |
199 | my ($self) = @_; |
226 | my ($self) = @_; |
200 | |
227 | |
201 | Scalar::Util::weaken $self; |
228 | Scalar::Util::weaken $self; |
202 | |
229 | |
203 | $self->{send} = sub { |
230 | $self->{send} = sub { |
|
|
231 | if ($DELAY++ >= 0) { |
|
|
232 | my $msg = $_[0]; |
|
|
233 | push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
|
|
234 | $DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
|
|
235 | return; |
|
|
236 | } |
|
|
237 | |
204 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
238 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
205 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
239 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
206 | }; |
240 | }; |
207 | } |
241 | } |
208 | |
242 | |
… | |
… | |
213 | } |
247 | } |
214 | |
248 | |
215 | sub kill { |
249 | sub kill { |
216 | my ($self, $port, @reason) = @_; |
250 | my ($self, $port, @reason) = @_; |
217 | |
251 | |
|
|
252 | my $delay_cb = sub { |
218 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
253 | delete $AnyEvent::MP::Kernel::PORT{$port} |
|
|
254 | or return; # killing nonexistent ports is O.K. |
219 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
255 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
220 | |
256 | |
221 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
257 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
222 | or !@reason |
258 | or !@reason |
223 | or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
259 | or $AnyEvent::MP::Kernel::WARN->(8, "unmonitored local port $port died with reason: @reason"); |
224 | |
260 | |
225 | $_->(@reason) for values %$mon; |
261 | $_->(@reason) for values %$mon; |
|
|
262 | }; |
|
|
263 | |
|
|
264 | $DELAY_W ? push @DELAY, $delay_cb : &$delay_cb; |
226 | } |
265 | } |
227 | |
266 | |
228 | sub monitor { |
267 | sub monitor { |
229 | my ($self, $portid, $cb) = @_; |
268 | my ($self, $portid, $cb) = @_; |
230 | |
269 | |
|
|
270 | my $delay_cb = sub { |
231 | return $cb->(no_such_port => "cannot monitor nonexistent port") |
271 | return $cb->(no_such_port => "cannot monitor nonexistent port", "$self->{id}#$portid") |
232 | unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
272 | unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
233 | |
273 | |
234 | $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
274 | $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
|
|
275 | }; |
|
|
276 | |
|
|
277 | $DELAY_W ? push @DELAY, $delay_cb : &$delay_cb; |
235 | } |
278 | } |
236 | |
279 | |
237 | sub unmonitor { |
280 | sub unmonitor { |
238 | my ($self, $portid, $cb) = @_; |
281 | my ($self, $portid, $cb) = @_; |
239 | |
282 | |
|
|
283 | my $delay_cb = sub { |
240 | delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
284 | delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
|
|
285 | }; |
|
|
286 | |
|
|
287 | $DELAY_W ? push @DELAY, $delay_cb : &$delay_cb; |
241 | } |
288 | } |
242 | |
289 | |
243 | =head1 SEE ALSO |
290 | =head1 SEE ALSO |
244 | |
291 | |
245 | L<AnyEvent::MP>. |
292 | L<AnyEvent::MP>. |