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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines