1 |
=head1 NAME |
2 |
|
3 |
AnyEvent::MP::Node - represent a node |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use AnyEvent::MP::Node; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
This is an internal utility module, horrible to look at, so don't. |
12 |
|
13 |
=cut |
14 |
|
15 |
package AnyEvent::MP::Node; |
16 |
|
17 |
use common::sense; |
18 |
|
19 |
use AE (); |
20 |
use AnyEvent::Util (); |
21 |
use AnyEvent::Socket (); |
22 |
|
23 |
use AnyEvent::MP::Transport (); |
24 |
|
25 |
sub new { |
26 |
my ($self, $id) = @_; |
27 |
|
28 |
$self = bless { id => $id }, $self; |
29 |
|
30 |
$self->init; |
31 |
$self->transport_reset; |
32 |
|
33 |
$self |
34 |
} |
35 |
|
36 |
sub init { |
37 |
# |
38 |
} |
39 |
|
40 |
sub send { |
41 |
&{ shift->{send} } |
42 |
} |
43 |
|
44 |
# nodes reachable via the network |
45 |
package AnyEvent::MP::Node::Remote; |
46 |
|
47 |
use base "AnyEvent::MP::Node"; |
48 |
|
49 |
# called at init time, mostly sets {send} |
50 |
sub transport_reset { |
51 |
my ($self) = @_; |
52 |
|
53 |
delete $self->{transport}; |
54 |
|
55 |
Scalar::Util::weaken $self; |
56 |
|
57 |
$self->{send} = sub { |
58 |
push @{$self->{queue}}, shift; |
59 |
$self->connect; |
60 |
}; |
61 |
} |
62 |
|
63 |
# called each time we fail to establish a connection, |
64 |
# or the existing connection failed |
65 |
sub transport_error { |
66 |
my ($self, @reason) = @_; |
67 |
|
68 |
my $no_transport = !$self->{transport}; |
69 |
|
70 |
delete $self->{connect_w}; |
71 |
delete $self->{connect_to}; |
72 |
|
73 |
delete $self->{queue}; |
74 |
$self->transport_reset; |
75 |
|
76 |
if (my $mon = delete $self->{lmon}) { |
77 |
$_->(@reason) for map @$_, values %$mon; |
78 |
} |
79 |
|
80 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
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}; |
86 |
} |
87 |
|
88 |
# called after handshake was successful |
89 |
sub transport_connect { |
90 |
my ($self, $transport) = @_; |
91 |
|
92 |
delete $self->{trial}; |
93 |
|
94 |
$self->transport_error (transport_error => $self->{id}, "switched connections") |
95 |
if $self->{transport}; |
96 |
|
97 |
delete $self->{connect_w}; |
98 |
delete $self->{connect_to}; |
99 |
|
100 |
$self->{transport} = $transport; |
101 |
|
102 |
my $transport_send = $transport->{send}; |
103 |
|
104 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
105 |
|
106 |
$self->{send} = $transport_send; |
107 |
|
108 |
$transport_send->($_) |
109 |
for @{ delete $self->{queue} || [] }; |
110 |
} |
111 |
|
112 |
sub connect { |
113 |
my ($self) = @_; |
114 |
|
115 |
return if $self->{transport}; |
116 |
return if $self->{connect_w}; |
117 |
|
118 |
Scalar::Util::weaken $self; |
119 |
|
120 |
$self->{connect_to} ||= AE::timer $AnyEvent::MP::Kernel::CONFIG->{connect_interval}, 0, sub { |
121 |
$self->transport_error (transport_error => $self->{id}, "unable to connect"); |
122 |
}; |
123 |
|
124 |
# maybe @$addresses? |
125 |
my $addresses = $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}}; |
126 |
|
127 |
if ($addresses) { |
128 |
$self->connect_to ($addresses); |
129 |
} else { |
130 |
# on global nodes, all bets are off now - we either know the node, or we don't |
131 |
unless ($AnyEvent::MP::Kernel::GLOBAL) { |
132 |
AnyEvent::MP::Kernel::g_find ($self->{id}); |
133 |
} |
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; |
144 |
|
145 |
AE::log 9 => "connecting to $self->{id} with [@$addresses]"; |
146 |
|
147 |
my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
148 |
my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
149 |
|
150 |
$interval = ($monitor - $interval) / @$addresses |
151 |
if ($monitor - $interval) / @$addresses < $interval; |
152 |
|
153 |
$interval = 0.4 if $interval < 0.4; |
154 |
|
155 |
my @endpoints = reverse @$addresses; |
156 |
|
157 |
$self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub { |
158 |
my $endpoint = pop @endpoints; |
159 |
|
160 |
AE::log 9 => "connecting to $self->{id} at $endpoint"; |
161 |
|
162 |
$self->{trial}{$endpoint} ||= do { |
163 |
my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
164 |
or return AE::log critical => "$self->{id}: '$endpoint' is not a resolved node reference."; |
165 |
|
166 |
AnyEvent::MP::Transport::mp_connect |
167 |
$host, $port, |
168 |
sub { delete $self->{trial}{$endpoint} }, |
169 |
}; |
170 |
}; |
171 |
} |
172 |
|
173 |
sub kill { |
174 |
my ($self, $port, @reason) = @_; |
175 |
|
176 |
$self->{send} (["", kil => $port, @reason]); |
177 |
} |
178 |
|
179 |
sub monitor { |
180 |
my ($self, $portid, $cb) = @_; |
181 |
|
182 |
my $list = $self->{lmon}{$portid} ||= []; |
183 |
|
184 |
$self->send (["", mon1 => $portid]) |
185 |
unless @$list || !length $portid; |
186 |
|
187 |
push @$list, $cb; |
188 |
} |
189 |
|
190 |
sub unmonitor { |
191 |
my ($self, $portid, $cb) = @_; |
192 |
|
193 |
my $list = $self->{lmon}{$portid} |
194 |
or return; |
195 |
|
196 |
@$list = grep $_ != $cb, @$list; |
197 |
|
198 |
unless (@$list) { |
199 |
$self->send (["", mon0 => $portid]); |
200 |
delete $self->{monitor}{$portid}; |
201 |
} |
202 |
} |
203 |
|
204 |
package AnyEvent::MP::Node::Self; |
205 |
|
206 |
use base "AnyEvent::MP::Node"; |
207 |
|
208 |
sub connect { |
209 |
# we are trivially connected |
210 |
} |
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 |
our $send_delayed = sub { |
218 |
$AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
219 |
(shift @DELAY)->() |
220 |
while @DELAY; |
221 |
undef $DELAY_W; |
222 |
$DELAY = -50; |
223 |
}; |
224 |
|
225 |
sub transport_reset { |
226 |
my ($self) = @_; |
227 |
|
228 |
Scalar::Util::weaken $self; |
229 |
|
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 |
|
238 |
local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
239 |
AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
240 |
}; |
241 |
} |
242 |
|
243 |
sub transport_connect { |
244 |
my ($self, $tp) = @_; |
245 |
|
246 |
AE::log 9 => "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"; |
247 |
} |
248 |
|
249 |
sub kill { |
250 |
my (undef, @args) = @_; |
251 |
|
252 |
# we _always_ delay kil's, to avoid calling mon callbacks |
253 |
# from anything but the event loop context. |
254 |
$DELAY = 1; |
255 |
push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) }; |
256 |
$DELAY_W ||= AE::timer 0, 0, $send_delayed; |
257 |
} |
258 |
|
259 |
sub monitor { |
260 |
# maybe always delay, too? |
261 |
if ($DELAY_W) { |
262 |
my @args = @_; |
263 |
push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) }; |
264 |
return; |
265 |
} |
266 |
&AnyEvent::MP::Kernel::_monitor; |
267 |
} |
268 |
|
269 |
sub unmonitor { |
270 |
# no need to always delay |
271 |
if ($DELAY_W) { |
272 |
my @args = @_; |
273 |
push @DELAY, sub { AnyEvent::MP::Kernel::_unmonitor (@args) }; |
274 |
return; |
275 |
} |
276 |
|
277 |
&AnyEvent::MP::Kernel::_unmonitor; |
278 |
} |
279 |
|
280 |
=head1 SEE ALSO |
281 |
|
282 |
L<AnyEvent::MP>. |
283 |
|
284 |
=head1 AUTHOR |
285 |
|
286 |
Marc Lehmann <schmorp@schmorp.de> |
287 |
http://home.schmorp.de/ |
288 |
|
289 |
=cut |
290 |
|
291 |
1 |
292 |
|