1 |
=head1 NAME |
2 |
|
3 |
AnyEvent::MP::Node - a single processing node (CPU/process...) |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use AnyEvent::MP::Node; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
=cut |
12 |
|
13 |
package AnyEvent::MP::Node; |
14 |
|
15 |
use common::sense; |
16 |
|
17 |
use AE (); |
18 |
use AnyEvent::Util (); |
19 |
use AnyEvent::Socket (); |
20 |
|
21 |
use AnyEvent::MP::Transport (); |
22 |
|
23 |
sub new { |
24 |
my ($self, $id) = @_; |
25 |
|
26 |
$self = bless { id => $id }, $self; |
27 |
|
28 |
$self->init; |
29 |
$self->transport_reset; |
30 |
|
31 |
$self |
32 |
} |
33 |
|
34 |
sub init { |
35 |
# |
36 |
} |
37 |
|
38 |
sub send { |
39 |
&{ shift->{send} } |
40 |
} |
41 |
|
42 |
# nodes reachable via the network |
43 |
package AnyEvent::MP::Node::External; |
44 |
|
45 |
use base "AnyEvent::MP::Node"; |
46 |
|
47 |
# called at init time, mostly sets {send} |
48 |
sub transport_reset { |
49 |
my ($self) = @_; |
50 |
|
51 |
delete $self->{transport}; |
52 |
|
53 |
Scalar::Util::weaken $self; |
54 |
|
55 |
$self->{send} = sub { |
56 |
push @{$self->{queue}}, shift; |
57 |
$self->connect; |
58 |
}; |
59 |
|
60 |
$self->connect |
61 |
if $self->{autoconnect}; |
62 |
} |
63 |
|
64 |
# called only after successful handshake |
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 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
77 |
unless $no_transport; |
78 |
|
79 |
if (my $mon = delete $self->{lmon}) { |
80 |
$_->(@reason) for map @$_, values %$mon; |
81 |
} |
82 |
} |
83 |
|
84 |
# called after handshake was successful |
85 |
sub transport_connect { |
86 |
my ($self, $transport) = @_; |
87 |
|
88 |
delete $self->{trial}; |
89 |
|
90 |
$self->transport_error (transport_error => "switched connections") |
91 |
if $self->{transport}; |
92 |
|
93 |
delete $self->{connect_w}; |
94 |
delete $self->{connect_to}; |
95 |
|
96 |
$self->{transport} = $transport; |
97 |
|
98 |
my $transport_send = $transport->can ("send"); |
99 |
|
100 |
$self->{send} = sub { |
101 |
$transport_send->($transport, $_[0]); |
102 |
}; |
103 |
|
104 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
105 |
|
106 |
$transport->send ($_) |
107 |
for @{ delete $self->{queue} || [] }; |
108 |
} |
109 |
|
110 |
sub connect { |
111 |
my ($self, @addresses) = @_; |
112 |
|
113 |
return if $self->{transport}; |
114 |
|
115 |
Scalar::Util::weaken $self; |
116 |
|
117 |
$self->{connect_to} ||= AE::timer |
118 |
$AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
119 |
0, |
120 |
sub { |
121 |
$self->transport_error (transport_error => $self->{id}, "unable to connect"); |
122 |
}; |
123 |
|
124 |
$AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
125 |
return unless @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 |
} |
152 |
} |
153 |
|
154 |
sub kill { |
155 |
my ($self, $port, @reason) = @_; |
156 |
|
157 |
$self->send (["", kil => $port, @reason]); |
158 |
} |
159 |
|
160 |
sub monitor { |
161 |
my ($self, $portid, $cb) = @_; |
162 |
|
163 |
my $list = $self->{lmon}{$portid} ||= []; |
164 |
|
165 |
$self->send (["", mon1 => $portid]) |
166 |
unless @$list || !length $portid; |
167 |
|
168 |
push @$list, $cb; |
169 |
} |
170 |
|
171 |
sub unmonitor { |
172 |
my ($self, $portid, $cb) = @_; |
173 |
|
174 |
my $list = $self->{lmon}{$portid} |
175 |
or return; |
176 |
|
177 |
@$list = grep $_ != $cb, @$list; |
178 |
|
179 |
unless (@$list) { |
180 |
$self->send (["", mon0 => $portid]); |
181 |
delete $self->{monitor}{$portid}; |
182 |
} |
183 |
} |
184 |
|
185 |
# used for direct slave connections as well |
186 |
package AnyEvent::MP::Node::Direct; |
187 |
|
188 |
use base "AnyEvent::MP::Node::External"; |
189 |
|
190 |
package AnyEvent::MP::Node::Self; |
191 |
|
192 |
use base "AnyEvent::MP::Node"; |
193 |
|
194 |
sub connect { |
195 |
# we are trivially connected |
196 |
} |
197 |
|
198 |
sub transport_reset { |
199 |
my ($self) = @_; |
200 |
|
201 |
Scalar::Util::weaken $self; |
202 |
|
203 |
$self->{send} = sub { |
204 |
local $AnyEvent::MP::Kernel::SRCNODE = $self; |
205 |
AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
206 |
}; |
207 |
} |
208 |
|
209 |
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 |
sub kill { |
216 |
my ($self, $port, @reason) = @_; |
217 |
|
218 |
delete $AnyEvent::MP::Kernel::PORT{$port}; |
219 |
delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
220 |
|
221 |
my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
222 |
or !@reason |
223 |
or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
224 |
|
225 |
$_->(@reason) for values %$mon; |
226 |
} |
227 |
|
228 |
sub monitor { |
229 |
my ($self, $portid, $cb) = @_; |
230 |
|
231 |
return $cb->(no_such_port => "cannot monitor nonexistent port") |
232 |
unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
233 |
|
234 |
$AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
235 |
} |
236 |
|
237 |
sub unmonitor { |
238 |
my ($self, $portid, $cb) = @_; |
239 |
|
240 |
delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
241 |
} |
242 |
|
243 |
=head1 SEE ALSO |
244 |
|
245 |
L<AnyEvent::MP>. |
246 |
|
247 |
=head1 AUTHOR |
248 |
|
249 |
Marc Lehmann <schmorp@schmorp.de> |
250 |
http://home.schmorp.de/ |
251 |
|
252 |
=cut |
253 |
|
254 |
1 |
255 |
|