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::External; |
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 => "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->can ("send"); |
103 |
|
104 |
$self->{send} = sub { |
105 |
$transport_send->($transport, $_[0]); |
106 |
}; |
107 |
|
108 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
109 |
|
110 |
$transport->send ($_) |
111 |
for @{ delete $self->{queue} || [] }; |
112 |
} |
113 |
|
114 |
sub connect { |
115 |
my ($self, @addresses) = @_; |
116 |
|
117 |
return if $self->{transport}; |
118 |
|
119 |
Scalar::Util::weaken $self; |
120 |
|
121 |
my $monitor = $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT; |
122 |
|
123 |
$self->{connect_to} ||= AE::timer $monitor, 0, sub { |
124 |
$self->transport_error (transport_error => $self->{id}, "unable to connect"); |
125 |
}; |
126 |
|
127 |
return unless @addresses; |
128 |
return if $self->{connect_w}; |
129 |
|
130 |
$AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
131 |
|
132 |
my $interval = $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL; |
133 |
|
134 |
$interval = ($monitor - $interval) / @addresses |
135 |
if ($monitor - $interval) / @addresses < $interval; |
136 |
|
137 |
$interval = 0.4 if $interval < 0.4; |
138 |
|
139 |
my @endpoints; |
140 |
|
141 |
$self->{connect_w} = AE::timer rand, $interval, sub { |
142 |
@endpoints = @addresses |
143 |
unless @endpoints; |
144 |
|
145 |
my $endpoint = shift @endpoints; |
146 |
|
147 |
$AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
148 |
|
149 |
$self->{trial}{$endpoint} ||= do { |
150 |
my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
151 |
or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
152 |
|
153 |
AnyEvent::MP::Transport::mp_connect |
154 |
$host, $port, |
155 |
sub { delete $self->{trial}{$endpoint} }, |
156 |
}; |
157 |
}; |
158 |
} |
159 |
|
160 |
sub kill { |
161 |
my ($self, $port, @reason) = @_; |
162 |
|
163 |
$self->send (["", kil => $port, @reason]); |
164 |
} |
165 |
|
166 |
sub monitor { |
167 |
my ($self, $portid, $cb) = @_; |
168 |
|
169 |
my $list = $self->{lmon}{$portid} ||= []; |
170 |
|
171 |
$self->send (["", mon1 => $portid]) |
172 |
unless @$list || !length $portid; |
173 |
|
174 |
push @$list, $cb; |
175 |
} |
176 |
|
177 |
sub unmonitor { |
178 |
my ($self, $portid, $cb) = @_; |
179 |
|
180 |
my $list = $self->{lmon}{$portid} |
181 |
or return; |
182 |
|
183 |
@$list = grep $_ != $cb, @$list; |
184 |
|
185 |
unless (@$list) { |
186 |
$self->send (["", mon0 => $portid]); |
187 |
delete $self->{monitor}{$portid}; |
188 |
} |
189 |
} |
190 |
|
191 |
# used for direct slave connections as well |
192 |
package AnyEvent::MP::Node::Direct; |
193 |
|
194 |
use base "AnyEvent::MP::Node::External"; |
195 |
|
196 |
package AnyEvent::MP::Node::Self; |
197 |
|
198 |
use base "AnyEvent::MP::Node"; |
199 |
|
200 |
sub connect { |
201 |
# we are trivially connected |
202 |
} |
203 |
|
204 |
sub transport_reset { |
205 |
my ($self) = @_; |
206 |
|
207 |
Scalar::Util::weaken $self; |
208 |
|
209 |
$self->{send} = sub { |
210 |
local $AnyEvent::MP::Kernel::SRCNODE = $self; |
211 |
AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
212 |
}; |
213 |
} |
214 |
|
215 |
sub transport_connect { |
216 |
my ($self, $tp) = @_; |
217 |
|
218 |
$AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"); |
219 |
} |
220 |
|
221 |
sub kill { |
222 |
my ($self, $port, @reason) = @_; |
223 |
|
224 |
delete $AnyEvent::MP::Kernel::PORT{$port}; |
225 |
delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
226 |
|
227 |
my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
228 |
or !@reason |
229 |
or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
230 |
|
231 |
$_->(@reason) for values %$mon; |
232 |
} |
233 |
|
234 |
sub monitor { |
235 |
my ($self, $portid, $cb) = @_; |
236 |
|
237 |
return $cb->(no_such_port => "cannot monitor nonexistent port") |
238 |
unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
239 |
|
240 |
$AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
241 |
} |
242 |
|
243 |
sub unmonitor { |
244 |
my ($self, $portid, $cb) = @_; |
245 |
|
246 |
delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
247 |
} |
248 |
|
249 |
=head1 SEE ALSO |
250 |
|
251 |
L<AnyEvent::MP>. |
252 |
|
253 |
=head1 AUTHOR |
254 |
|
255 |
Marc Lehmann <schmorp@schmorp.de> |
256 |
http://home.schmorp.de/ |
257 |
|
258 |
=cut |
259 |
|
260 |
1 |
261 |
|