… | |
… | |
18 | use AnyEvent::Util (); |
18 | use AnyEvent::Util (); |
19 | use AnyEvent::Socket (); |
19 | use AnyEvent::Socket (); |
20 | |
20 | |
21 | use AnyEvent::MP::Transport (); |
21 | use AnyEvent::MP::Transport (); |
22 | |
22 | |
23 | use base Exporter::; |
|
|
24 | |
|
|
25 | our $VERSION = '0.0'; |
|
|
26 | |
|
|
27 | sub new { |
23 | sub new { |
28 | my ($class, $noderef) = @_; |
24 | my ($self, $id) = @_; |
29 | |
25 | |
30 | bless { noderef => $noderef }, $class |
26 | $self = bless { id => $id }, $self; |
31 | } |
|
|
32 | |
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 |
33 | package AnyEvent::MP::Node::Direct; |
43 | package AnyEvent::MP::Node::External; |
34 | |
44 | |
35 | use base "AnyEvent::MP::Node"; |
45 | use base "AnyEvent::MP::Node"; |
36 | |
46 | |
37 | sub send { |
47 | # called at init time, mostly sets {send} |
|
|
48 | sub transport_reset { |
38 | my ($self, $msg) = @_; |
49 | my ($self) = @_; |
39 | |
50 | |
40 | if ($self->{transport}) { |
51 | delete $self->{transport}; |
41 | $self->{transport}->send ($msg); |
52 | |
42 | } elsif ($self->{queue}) { |
53 | Scalar::Util::weaken $self; |
|
|
54 | |
|
|
55 | $self->{send} = sub { |
43 | push @{ $self->{queue} }, $msg; |
56 | push @{$self->{queue}}, shift; |
44 | } else { |
|
|
45 | $self->{queue} = [$msg]; |
|
|
46 | $self->connect; |
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 | return unless @addresses; |
|
|
125 | |
|
|
126 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
|
|
127 | |
|
|
128 | unless ($self->{connect_w}) { |
|
|
129 | my @endpoints; |
|
|
130 | |
|
|
131 | $self->{connect_w} = AE::timer |
|
|
132 | rand, |
|
|
133 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
|
|
134 | sub { |
|
|
135 | @endpoints = @addresses |
|
|
136 | unless @endpoints; |
|
|
137 | |
|
|
138 | my $endpoint = shift @endpoints; |
|
|
139 | |
|
|
140 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
141 | |
|
|
142 | $self->{trial}{$endpoint} ||= do { |
|
|
143 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
144 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
|
|
145 | |
|
|
146 | AnyEvent::MP::Transport::mp_connect |
|
|
147 | $host, $port, |
|
|
148 | sub { delete $self->{trial}{$endpoint} }, |
|
|
149 | }; |
|
|
150 | } |
|
|
151 | ; |
47 | } |
152 | } |
48 | } |
153 | } |
49 | |
154 | |
50 | sub kill { |
155 | sub kill { |
51 | my ($self, $port, @reason) = @_; |
156 | my ($self, $port, @reason) = @_; |
… | |
… | |
57 | my ($self, $portid, $cb) = @_; |
162 | my ($self, $portid, $cb) = @_; |
58 | |
163 | |
59 | my $list = $self->{lmon}{$portid} ||= []; |
164 | my $list = $self->{lmon}{$portid} ||= []; |
60 | |
165 | |
61 | $self->send (["", mon1 => $portid]) |
166 | $self->send (["", mon1 => $portid]) |
62 | unless @$list; |
167 | unless @$list || !length $portid; |
63 | |
168 | |
64 | push @$list, $cb; |
169 | push @$list, $cb; |
65 | } |
170 | } |
66 | |
171 | |
67 | sub unmonitor { |
172 | sub unmonitor { |
… | |
… | |
76 | $self->send (["", mon0 => $portid]); |
181 | $self->send (["", mon0 => $portid]); |
77 | delete $self->{monitor}{$portid}; |
182 | delete $self->{monitor}{$portid}; |
78 | } |
183 | } |
79 | } |
184 | } |
80 | |
185 | |
81 | sub set_transport { |
186 | # used for direct slave connections as well |
82 | my ($self, $transport) = @_; |
187 | package AnyEvent::MP::Node::Direct; |
83 | |
188 | |
84 | $self->clr_transport |
189 | use base "AnyEvent::MP::Node::External"; |
85 | if $self->{transport}; |
|
|
86 | |
190 | |
87 | delete $self->{trial}; |
191 | package AnyEvent::MP::Node::Self; |
88 | delete $self->{next_connect}; |
|
|
89 | |
192 | |
90 | $self->{transport} = $transport; |
193 | use base "AnyEvent::MP::Node"; |
91 | |
|
|
92 | $transport->send ($_) |
|
|
93 | for @{ delete $self->{queue} || [] }; |
|
|
94 | } |
|
|
95 | |
|
|
96 | sub fail { |
|
|
97 | my ($self, @reason) = @_; |
|
|
98 | |
|
|
99 | delete $self->{queue}; |
|
|
100 | |
|
|
101 | if (my $mon = delete $self->{lmon}) { |
|
|
102 | $_->(@reason) for map @$_, values %$mon; |
|
|
103 | } |
|
|
104 | } |
|
|
105 | |
|
|
106 | sub clr_transport { |
|
|
107 | my ($self, @reason) = @_; |
|
|
108 | |
|
|
109 | delete $self->{transport}; |
|
|
110 | $self->connect; |
|
|
111 | } |
|
|
112 | |
194 | |
113 | sub connect { |
195 | sub connect { |
|
|
196 | # we are trivially connected |
|
|
197 | } |
|
|
198 | |
|
|
199 | sub transport_reset { |
114 | my ($self) = @_; |
200 | my ($self) = @_; |
115 | |
201 | |
116 | Scalar::Util::weaken $self; |
202 | Scalar::Util::weaken $self; |
117 | |
203 | |
118 | $self->{retry} ||= [split /,/, $self->{noderef}]; |
204 | $self->{send} = sub { |
119 | |
205 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
120 | my $endpoint = shift @{ $self->{retry} }; |
206 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
121 | |
|
|
122 | if (defined $endpoint) { |
|
|
123 | $self->{trial}{$endpoint} ||= do { |
|
|
124 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
125 | or return $AnyEvent::MP::Base::WARN->("$self->{noderef}: not a resolved node reference."); |
|
|
126 | |
|
|
127 | my ($w, $g); |
|
|
128 | |
|
|
129 | $w = AE::timer $AnyEvent::MP::Base::CONNECT_TIMEOUT, 0, sub { |
|
|
130 | delete $self->{trial}{$endpoint}; |
|
|
131 | }; |
|
|
132 | $g = AnyEvent::MP::Transport::mp_connect |
|
|
133 | $host, $port, |
|
|
134 | sub { |
|
|
135 | delete $self->{trial}{$endpoint} |
|
|
136 | unless @_; |
|
|
137 | $g = shift; |
|
|
138 | }; |
|
|
139 | ; |
|
|
140 | |
|
|
141 | [$w, \$g] |
|
|
142 | }; |
|
|
143 | } else { |
|
|
144 | delete $self->{retry}; |
|
|
145 | $self->fail (transport_error => $self->{noderef}, "unable to connect"); |
|
|
146 | } |
|
|
147 | |
|
|
148 | $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub { |
|
|
149 | $self->connect; |
|
|
150 | }; |
207 | }; |
151 | } |
208 | } |
152 | |
209 | |
153 | package AnyEvent::MP::Node::Slave; |
210 | sub transport_connect { |
154 | |
|
|
155 | use base "AnyEvent::MP::Node::Direct"; |
|
|
156 | |
|
|
157 | sub connect { |
|
|
158 | my ($self) = @_; |
211 | my ($self, $tp) = @_; |
159 | |
212 | |
160 | $self->fail (transport_error => $self->{noderef}, "unable to connect to slave node"); |
213 | $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"); |
161 | } |
|
|
162 | |
|
|
163 | package AnyEvent::MP::Node::Self; |
|
|
164 | |
|
|
165 | use base "AnyEvent::MP::Node"; |
|
|
166 | |
|
|
167 | sub set_transport { |
|
|
168 | Carp::confess "FATAL error, set_transport was called on local node"; |
|
|
169 | } |
|
|
170 | |
|
|
171 | sub send { |
|
|
172 | local $AnyEvent::MP::Base::SRCNODE = $_[0]; |
|
|
173 | AnyEvent::MP::Base::_inject (@{ $_[1] }); |
|
|
174 | } |
214 | } |
175 | |
215 | |
176 | sub kill { |
216 | sub kill { |
177 | my ($self, $port, @reason) = @_; |
217 | my ($self, $port, @reason) = @_; |
178 | |
218 | |
179 | delete $AnyEvent::MP::Base::PORT{$port}; |
219 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
180 | delete $AnyEvent::MP::Base::PORT_DATA{$port}; |
220 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
181 | |
221 | |
182 | my $mon = delete $AnyEvent::MP::Base::LMON{$port} |
222 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
183 | or !@reason |
223 | or !@reason |
184 | or $AnyEvent::MP::Base::WARN->("unmonitored local port $port died with reason: @reason"); |
224 | or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
185 | |
225 | |
186 | $_->(@reason) for values %$mon; |
226 | $_->(@reason) for values %$mon; |
187 | } |
227 | } |
188 | |
228 | |
189 | sub monitor { |
229 | sub monitor { |
190 | my ($self, $portid, $cb) = @_; |
230 | my ($self, $portid, $cb) = @_; |
191 | |
231 | |
192 | return $cb->(no_such_port => "cannot monitor nonexistent port") |
232 | return $cb->(no_such_port => "cannot monitor nonexistent port") |
193 | unless exists $AnyEvent::MP::Base::PORT{$portid}; |
233 | unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
194 | |
234 | |
195 | $AnyEvent::MP::Base::LMON{$portid}{$cb+0} = $cb; |
235 | $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
196 | } |
236 | } |
197 | |
237 | |
198 | sub unmonitor { |
238 | sub unmonitor { |
199 | my ($self, $portid, $cb) = @_; |
239 | my ($self, $portid, $cb) = @_; |
200 | |
240 | |
201 | delete $AnyEvent::MP::Base::LMON{$portid}{$cb+0}; |
241 | delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
202 | } |
242 | } |
203 | |
243 | |
204 | =head1 SEE ALSO |
244 | =head1 SEE ALSO |
205 | |
245 | |
206 | L<AnyEvent>. |
246 | L<AnyEvent::MP>. |
207 | |
247 | |
208 | =head1 AUTHOR |
248 | =head1 AUTHOR |
209 | |
249 | |
210 | Marc Lehmann <schmorp@schmorp.de> |
250 | Marc Lehmann <schmorp@schmorp.de> |
211 | http://home.schmorp.de/ |
251 | http://home.schmorp.de/ |