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