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