ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Node.pm
(Generate patch)

Comparing AnyEvent-MP/MP/Node.pm (file contents):
Revision 1.8 by root, Tue Aug 4 18:33:30 2009 UTC vs.
Revision 1.28 by root, Thu Aug 27 21:29:37 2009 UTC

18use AnyEvent::Util (); 18use AnyEvent::Util ();
19use AnyEvent::Socket (); 19use AnyEvent::Socket ();
20 20
21use AnyEvent::MP::Transport (); 21use AnyEvent::MP::Transport ();
22 22
23use base Exporter::; 23sub new {
24 my ($self, $id) = @_;
24 25
25our $VERSION = '0.0'; 26 $self = bless { id => $id }, $self;
26 27
27our $DEFAULT_PORT = "4040"; 28 $self->init;
29 $self->transport_reset;
28 30
29sub normalise_noderef($) { 31 $self
32}
33
34sub init {
35 #
36}
37
38sub send {
39 &{ shift->{send} }
40}
41
42# nodes reachable via the network
43package AnyEvent::MP::Node::External;
44
45use base "AnyEvent::MP::Node";
46
47# called at init time, mostly sets {send}
48sub transport_reset {
30 my ($noderef) = @_; 49 my ($self) = @_;
31 50
32 my $cv = AE::cv; 51 delete $self->{transport};
33 my @res;
34 52
35 $cv->begin (sub { 53 Scalar::Util::weaken $self;
36 my %seen; 54
37 my @refs; 55 $self->{send} = sub {
38 for (sort { $a->[0] <=> $b->[0] } @res) { 56 push @{$self->{queue}}, shift;
39 push @refs, $_->[1] unless $seen{$_->[1]}++ 57 $self->connect;
58 };
59
60 $self->connect
61 if $self->{autoconnect};
62}
63
64# called only after successful handshake
65sub 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
85sub 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
110sub 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");
40 } 122 };
41 shift->send (join ",", @refs);
42 });
43 123
44 $noderef = $DEFAULT_PORT unless length $noderef; 124 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]");
125 return unless @addresses;
45 126
46 my $idx; 127 unless ($self->{connect_w}) {
47 for my $t (split /,/, $noderef) { 128 my @endpoints;
48 my $pri = ++$idx;
49
50 #TODO: this should be outside normalise_noderef and in become_public
51 if ($t =~ /^\d*$/) {
52 require POSIX;
53 my $nodename = (POSIX::uname ())[1];
54 129
55 $cv->begin; 130 $self->{connect_w} = AE::timer
56 AnyEvent::Socket::resolve_sockaddr $nodename, $t || "aemp=$DEFAULT_PORT", "tcp", 0, undef, sub { 131 rand,
57 for (@_) { 132 $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL,
58 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3]; 133 sub {
59 push @res, [ 134 @endpoints = @addresses
60 $pri += 1e-5, 135 unless @endpoints;
61 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service 136
62 ]; 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} },
63 } 148 };
64 $cv->end;
65 };
66
67# my (undef, undef, undef, undef, @ipv4) = gethostbyname $nodename;
68#
69# for (@ipv4) {
70# push @res, [
71# $pri,
72# AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $_, $t || $DEFAULT_PORT,
73# ];
74# }
75 } else {
76 my ($host, $port) = AnyEvent::Socket::parse_hostport $t, "aemp=$DEFAULT_PORT"
77 or Carp::croak "$t: unparsable transport descriptor";
78
79 $cv->begin;
80 AnyEvent::Socket::resolve_sockaddr $host, $port, "tcp", 0, undef, sub {
81 for (@_) {
82 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
83 push @res, [
84 $pri += 1e-5,
85 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
86 ];
87 }
88 $cv->end;
89 } 149 }
90 } 150 ;
91 }
92
93 $cv->end;
94
95 $cv
96}
97
98sub new {
99 my ($class, $noderef) = @_;
100
101 bless { noderef => $noderef }, $class
102}
103
104package AnyEvent::MP::Node::Direct;
105
106use base "AnyEvent::MP::Node";
107
108sub send {
109 my ($self, $msg) = @_;
110
111 if ($self->{transport}) {
112 $self->{transport}->send ($msg);
113 } elsif ($self->{queue}) {
114 push @{ $self->{queue} }, $msg;
115 } else {
116 $self->{queue} = [$msg];
117 $self->connect;
118 } 151 }
119} 152}
120 153
121sub kill { 154sub kill {
122 my ($self, $port, @reason) = @_; 155 my ($self, $port, @reason) = @_;
125} 158}
126 159
127sub monitor { 160sub monitor {
128 my ($self, $portid, $cb) = @_; 161 my ($self, $portid, $cb) = @_;
129 162
130 return $cb->("node failed conenction")
131 if $self->{failed};
132
133 my $list = $self->{lmon}{$portid} ||= []; 163 my $list = $self->{lmon}{$portid} ||= [];
134 164
135 $self->send (["", mon1 => $portid]) 165 $self->send (["", mon1 => $portid])
136 unless @$list; 166 unless @$list || !length $portid;
137 167
138 push @$list, $cb; 168 push @$list, $cb;
139} 169}
140 170
141sub unmonitor { 171sub unmonitor {
150 $self->send (["", mon0 => $portid]); 180 $self->send (["", mon0 => $portid]);
151 delete $self->{monitor}{$portid}; 181 delete $self->{monitor}{$portid};
152 } 182 }
153} 183}
154 184
155sub set_transport { 185# used for direct slave connections as well
156 my ($self, $transport) = @_; 186package AnyEvent::MP::Node::Direct;
157 187
158 $self->clr_transport 188use base "AnyEvent::MP::Node::External";
159 if $self->{transport};
160 189
161 if ( 190package AnyEvent::MP::Node::Self;
162 exists $self->{remote_uniq}
163 && $self->{remote_uniq} ne $transport->{remote_uniq}
164 ) {
165 # uniq changed, different node
166 $self->fail ("node restart detected");
167 }
168 191
169 delete $self->{trial}; 192use base "AnyEvent::MP::Node";
170 delete $self->{next_connect};
171 delete $self->{failed};
172
173 $self->{remote_uniq} = $transport->{remote_uniq};
174 $self->{transport} = $transport;
175
176 $transport->send ($_)
177 for @{ delete $self->{queue} || [] };
178}
179
180sub fail {
181 my ($self, @reason) = @_;
182
183 delete $self->{queue};
184
185 $self->{failed} = 1;
186
187 if (my $mon = delete $self->{lmon}) {
188 $_->(@reason) for map @$_, values %$mon;
189 }
190}
191
192sub clr_transport {
193 my ($self, @reason) = @_;
194
195 delete $self->{transport};
196 $self->connect;
197}
198 193
199sub connect { 194sub connect {
195 # we are trivially connected
196}
197
198sub transport_reset {
200 my ($self) = @_; 199 my ($self) = @_;
201 200
202 Scalar::Util::weaken $self; 201 Scalar::Util::weaken $self;
203 202
204 unless (exists $self->{n_noderef}) { 203 $self->{send} = sub {
205 return if $self->{n_noderef_}++;
206 (AnyEvent::MP::Node::normalise_noderef ($self->{noderef}))->cb (sub {
207 $self or return;
208 delete $self->{n_noderef_};
209 my $noderef = shift->recv;
210
211 $self->{n_noderef} = $noderef;
212
213 $AnyEvent::MP::Base::NODE{$_} = $self 204 local $AnyEvent::MP::Kernel::SRCNODE = $self;
214 for split /,/, $noderef; 205 AnyEvent::MP::Kernel::_inject (@{ $_[0] });
215
216 $self->connect;
217 });
218 return;
219 }
220
221 $self->{retry} ||= [split /,/, $self->{n_noderef}];
222
223 my $endpoint = shift @{ $self->{retry} };
224
225 if (defined $endpoint) {
226 $self->{trial}{$endpoint} ||= do {
227 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
228 or return;
229
230 my ($w, $g);
231
232 $w = AE::timer $AnyEvent::MP::Base::CONNECT_TIMEOUT, 0, sub {
233 delete $self->{trial}{$endpoint};
234 };
235 $g = AnyEvent::MP::Transport::mp_connect
236 $host, $port,
237 sub {
238 delete $self->{trial}{$endpoint}
239 unless @_;
240 $g = shift;
241 };
242 ;
243
244 [$w, \$g]
245 };
246 } else {
247 delete $self->{retry};
248 }
249
250 $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub {
251 $self->connect;
252 }; 206 };
253} 207}
254 208
255package AnyEvent::MP::Node::Self; 209sub transport_connect {
210 my ($self, $tp) = @_;
256 211
257use base "AnyEvent::MP::Node"; 212 $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})");
258
259sub set_transport {
260 die "FATAL error, set_transport was called";
261}
262
263sub send {
264 local $AnyEvent::MP::Base::SRCNODE = $_[0];
265 AnyEvent::MP::Base::_inject (@{ $_[1] });
266} 213}
267 214
268sub kill { 215sub kill {
269 my ($self, $port, @reason) = @_; 216 my ($self, $port, @reason) = @_;
270 217
271 delete $AnyEvent::MP::Base::PORT{$port}; 218 delete $AnyEvent::MP::Kernel::PORT{$port};
272 delete $AnyEvent::MP::Base::PORT_DATA{$port}; 219 delete $AnyEvent::MP::Kernel::PORT_DATA{$port};
273 220
274 my $mon = delete $AnyEvent::MP::Base::LMON{$port} 221 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port}
275 or !@reason 222 or !@reason
276 or $AnyEvent::MP::Base::WARN->("unmonitored local port $port died with reason: @reason"); 223 or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason");
277 224
278 $_->(@reason) for values %$mon; 225 $_->(@reason) for values %$mon;
279} 226}
280 227
281sub monitor { 228sub monitor {
282 my ($self, $portid, $cb) = @_; 229 my ($self, $portid, $cb) = @_;
283 230
284 return $cb->() 231 return $cb->(no_such_port => "cannot monitor nonexistent port")
285 unless exists $AnyEvent::MP::Base::PORT{$portid}; 232 unless exists $AnyEvent::MP::Kernel::PORT{$portid};
286 233
287 $AnyEvent::MP::Base::LMON{$portid}{$cb+0} = $cb; 234 $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb;
288} 235}
289 236
290sub unmonitor { 237sub unmonitor {
291 my ($self, $portid, $cb) = @_; 238 my ($self, $portid, $cb) = @_;
292 239
293 delete $AnyEvent::MP::Base::LMON{$portid}{$cb+0}; 240 delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0};
294} 241}
295 242
296=head1 SEE ALSO 243=head1 SEE ALSO
297 244
298L<AnyEvent>. 245L<AnyEvent::MP>.
299 246
300=head1 AUTHOR 247=head1 AUTHOR
301 248
302 Marc Lehmann <schmorp@schmorp.de> 249 Marc Lehmann <schmorp@schmorp.de>
303 http://home.schmorp.de/ 250 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines