1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.31 |
AnyEvent::MP::Node - represent a node |
4 |
root |
1.1 |
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use AnyEvent::MP::Node; |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
root |
1.31 |
This is an internal utility module, horrible to look at, so don't. |
12 |
|
|
|
13 |
root |
1.1 |
=cut |
14 |
|
|
|
15 |
|
|
package AnyEvent::MP::Node; |
16 |
|
|
|
17 |
|
|
use common::sense; |
18 |
|
|
|
19 |
|
|
use AE (); |
20 |
root |
1.6 |
use AnyEvent::Util (); |
21 |
root |
1.1 |
use AnyEvent::Socket (); |
22 |
|
|
|
23 |
|
|
use AnyEvent::MP::Transport (); |
24 |
|
|
|
25 |
|
|
sub new { |
26 |
root |
1.28 |
my ($self, $id) = @_; |
27 |
root |
1.1 |
|
28 |
root |
1.28 |
$self = bless { id => $id }, $self; |
29 |
root |
1.1 |
|
30 |
root |
1.27 |
$self->init; |
31 |
root |
1.18 |
$self->transport_reset; |
32 |
root |
1.1 |
|
33 |
root |
1.18 |
$self |
34 |
|
|
} |
35 |
root |
1.1 |
|
36 |
root |
1.27 |
sub init { |
37 |
|
|
# |
38 |
|
|
} |
39 |
|
|
|
40 |
root |
1.1 |
sub send { |
41 |
root |
1.19 |
&{ shift->{send} } |
42 |
root |
1.1 |
} |
43 |
|
|
|
44 |
root |
1.27 |
# nodes reachable via the network |
45 |
root |
1.18 |
package AnyEvent::MP::Node::External; |
46 |
root |
1.7 |
|
47 |
root |
1.18 |
use base "AnyEvent::MP::Node"; |
48 |
root |
1.7 |
|
49 |
root |
1.18 |
# called at init time, mostly sets {send} |
50 |
|
|
sub transport_reset { |
51 |
|
|
my ($self) = @_; |
52 |
root |
1.6 |
|
53 |
root |
1.18 |
delete $self->{transport}; |
54 |
root |
1.6 |
|
55 |
root |
1.19 |
Scalar::Util::weaken $self; |
56 |
|
|
|
57 |
root |
1.18 |
$self->{send} = sub { |
58 |
root |
1.19 |
push @{$self->{queue}}, shift; |
59 |
|
|
$self->connect; |
60 |
root |
1.18 |
}; |
61 |
root |
1.6 |
} |
62 |
|
|
|
63 |
root |
1.18 |
# called only after successful handshake |
64 |
|
|
sub transport_error { |
65 |
|
|
my ($self, @reason) = @_; |
66 |
root |
1.6 |
|
67 |
root |
1.21 |
my $no_transport = !$self->{transport}; |
68 |
|
|
|
69 |
|
|
delete $self->{connect_w}; |
70 |
|
|
delete $self->{connect_to}; |
71 |
root |
1.20 |
|
72 |
root |
1.18 |
delete $self->{queue}; |
73 |
|
|
$self->transport_reset; |
74 |
root |
1.6 |
|
75 |
root |
1.18 |
if (my $mon = delete $self->{lmon}) { |
76 |
|
|
$_->(@reason) for map @$_, values %$mon; |
77 |
root |
1.6 |
} |
78 |
root |
1.30 |
|
79 |
|
|
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
80 |
|
|
unless $no_transport; |
81 |
root |
1.6 |
} |
82 |
|
|
|
83 |
root |
1.18 |
# called after handshake was successful |
84 |
|
|
sub transport_connect { |
85 |
root |
1.1 |
my ($self, $transport) = @_; |
86 |
|
|
|
87 |
root |
1.28 |
delete $self->{trial}; |
88 |
|
|
|
89 |
root |
1.20 |
$self->transport_error (transport_error => "switched connections") |
90 |
root |
1.6 |
if $self->{transport}; |
91 |
|
|
|
92 |
root |
1.18 |
delete $self->{connect_w}; |
93 |
|
|
delete $self->{connect_to}; |
94 |
root |
1.8 |
|
95 |
root |
1.15 |
$self->{transport} = $transport; |
96 |
root |
1.1 |
|
97 |
root |
1.19 |
my $transport_send = $transport->can ("send"); |
98 |
|
|
|
99 |
root |
1.18 |
$self->{send} = sub { |
100 |
root |
1.19 |
$transport_send->($transport, $_[0]); |
101 |
root |
1.18 |
}; |
102 |
|
|
|
103 |
root |
1.20 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
104 |
|
|
|
105 |
root |
1.1 |
$transport->send ($_) |
106 |
|
|
for @{ delete $self->{queue} || [] }; |
107 |
|
|
} |
108 |
|
|
|
109 |
root |
1.18 |
sub connect { |
110 |
root |
1.28 |
my ($self, @addresses) = @_; |
111 |
root |
1.18 |
|
112 |
root |
1.23 |
return if $self->{transport}; |
113 |
|
|
|
114 |
root |
1.18 |
Scalar::Util::weaken $self; |
115 |
root |
1.8 |
|
116 |
root |
1.18 |
$self->{connect_to} ||= AE::timer |
117 |
|
|
$AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
118 |
|
|
0, |
119 |
|
|
sub { |
120 |
root |
1.28 |
$self->transport_error (transport_error => $self->{id}, "unable to connect"); |
121 |
root |
1.18 |
}; |
122 |
root |
1.1 |
|
123 |
root |
1.29 |
return unless @addresses; |
124 |
|
|
|
125 |
root |
1.28 |
$AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
126 |
|
|
|
127 |
root |
1.18 |
unless ($self->{connect_w}) { |
128 |
|
|
my @endpoints; |
129 |
|
|
|
130 |
|
|
$self->{connect_w} = AE::timer |
131 |
root |
1.26 |
rand, |
132 |
root |
1.18 |
$AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
133 |
|
|
sub { |
134 |
root |
1.28 |
@endpoints = @addresses |
135 |
root |
1.18 |
unless @endpoints; |
136 |
|
|
|
137 |
|
|
my $endpoint = shift @endpoints; |
138 |
|
|
|
139 |
root |
1.28 |
$AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
140 |
|
|
|
141 |
|
|
$self->{trial}{$endpoint} ||= do { |
142 |
root |
1.18 |
my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
143 |
root |
1.28 |
or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
144 |
root |
1.18 |
|
145 |
|
|
AnyEvent::MP::Transport::mp_connect |
146 |
|
|
$host, $port, |
147 |
root |
1.28 |
sub { delete $self->{trial}{$endpoint} }, |
148 |
root |
1.18 |
}; |
149 |
|
|
} |
150 |
|
|
; |
151 |
root |
1.6 |
} |
152 |
root |
1.8 |
} |
153 |
|
|
|
154 |
root |
1.18 |
sub kill { |
155 |
|
|
my ($self, $port, @reason) = @_; |
156 |
root |
1.4 |
|
157 |
root |
1.18 |
$self->send (["", kil => $port, @reason]); |
158 |
root |
1.1 |
} |
159 |
|
|
|
160 |
root |
1.18 |
sub monitor { |
161 |
|
|
my ($self, $portid, $cb) = @_; |
162 |
|
|
|
163 |
|
|
my $list = $self->{lmon}{$portid} ||= []; |
164 |
|
|
|
165 |
|
|
$self->send (["", mon1 => $portid]) |
166 |
root |
1.24 |
unless @$list || !length $portid; |
167 |
root |
1.1 |
|
168 |
root |
1.18 |
push @$list, $cb; |
169 |
|
|
} |
170 |
root |
1.1 |
|
171 |
root |
1.18 |
sub unmonitor { |
172 |
|
|
my ($self, $portid, $cb) = @_; |
173 |
root |
1.1 |
|
174 |
root |
1.18 |
my $list = $self->{lmon}{$portid} |
175 |
|
|
or return; |
176 |
root |
1.1 |
|
177 |
root |
1.18 |
@$list = grep $_ != $cb, @$list; |
178 |
root |
1.1 |
|
179 |
root |
1.18 |
unless (@$list) { |
180 |
|
|
$self->send (["", mon0 => $portid]); |
181 |
|
|
delete $self->{monitor}{$portid}; |
182 |
root |
1.1 |
} |
183 |
root |
1.18 |
} |
184 |
root |
1.1 |
|
185 |
root |
1.27 |
# used for direct slave connections as well |
186 |
root |
1.18 |
package AnyEvent::MP::Node::Direct; |
187 |
|
|
|
188 |
|
|
use base "AnyEvent::MP::Node::External"; |
189 |
root |
1.1 |
|
190 |
|
|
package AnyEvent::MP::Node::Self; |
191 |
|
|
|
192 |
|
|
use base "AnyEvent::MP::Node"; |
193 |
|
|
|
194 |
root |
1.20 |
sub connect { |
195 |
|
|
# we are trivially connected |
196 |
|
|
} |
197 |
|
|
|
198 |
root |
1.18 |
sub transport_reset { |
199 |
|
|
my ($self) = @_; |
200 |
root |
1.1 |
|
201 |
root |
1.19 |
Scalar::Util::weaken $self; |
202 |
|
|
|
203 |
root |
1.18 |
$self->{send} = sub { |
204 |
root |
1.19 |
local $AnyEvent::MP::Kernel::SRCNODE = $self; |
205 |
|
|
AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
206 |
root |
1.18 |
}; |
207 |
root |
1.6 |
} |
208 |
|
|
|
209 |
root |
1.28 |
sub transport_connect { |
210 |
|
|
my ($self, $tp) = @_; |
211 |
|
|
|
212 |
|
|
$AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"); |
213 |
|
|
} |
214 |
|
|
|
215 |
root |
1.7 |
sub kill { |
216 |
|
|
my ($self, $port, @reason) = @_; |
217 |
|
|
|
218 |
root |
1.18 |
delete $AnyEvent::MP::Kernel::PORT{$port}; |
219 |
|
|
delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
220 |
root |
1.7 |
|
221 |
root |
1.18 |
my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
222 |
root |
1.8 |
or !@reason |
223 |
root |
1.25 |
or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
224 |
root |
1.7 |
|
225 |
|
|
$_->(@reason) for values %$mon; |
226 |
|
|
} |
227 |
|
|
|
228 |
root |
1.6 |
sub monitor { |
229 |
|
|
my ($self, $portid, $cb) = @_; |
230 |
|
|
|
231 |
root |
1.14 |
return $cb->(no_such_port => "cannot monitor nonexistent port") |
232 |
root |
1.18 |
unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
233 |
root |
1.6 |
|
234 |
root |
1.18 |
$AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
235 |
root |
1.6 |
} |
236 |
|
|
|
237 |
|
|
sub unmonitor { |
238 |
|
|
my ($self, $portid, $cb) = @_; |
239 |
|
|
|
240 |
root |
1.18 |
delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
241 |
root |
1.1 |
} |
242 |
|
|
|
243 |
|
|
=head1 SEE ALSO |
244 |
|
|
|
245 |
root |
1.17 |
L<AnyEvent::MP>. |
246 |
root |
1.1 |
|
247 |
|
|
=head1 AUTHOR |
248 |
|
|
|
249 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
250 |
|
|
http://home.schmorp.de/ |
251 |
|
|
|
252 |
|
|
=cut |
253 |
|
|
|
254 |
|
|
1 |
255 |
|
|
|