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.6 by root, Mon Aug 3 21:35:03 2009 UTC vs.
Revision 1.27 by root, Thu Aug 27 07:12:48 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->init;
36 my %seen; 29 $self->transport_reset;
37 my @refs; 30
38 for (sort { $a->[0] <=> $b->[0] } @res) { 31 $self
39 push @refs, $_->[1] unless $seen{$_->[1]}++ 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 {
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
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 $self->transport_error (transport_error => "switched connections")
89 if $self->{transport};
90
91 delete $self->{connect_w};
92 delete $self->{connect_to};
93
94 $self->{transport} = $transport;
95
96 my $transport_send = $transport->can ("send");
97
98 $self->{send} = sub {
99 $transport_send->($transport, $_[0]);
100 };
101
102 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1);
103
104 $transport->send ($_)
105 for @{ delete $self->{queue} || [] };
106}
107
108sub connect {
109 my ($self) = @_;
110
111 return if $self->{transport};
112
113 # just ignore connect requests for slave nodes - let's hope it connects to us instead
114 return if $self->{noderef} =~ /^slave\//;
115
116 Scalar::Util::weaken $self;
117
118 $self->{connect_to} ||= AE::timer
119 $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT,
120 0,
121 sub {
122 $self->transport_error (transport_error => $self->{noderef}, "unable to connect");
40 } 123 };
41 shift->send (join ",", @refs);
42 });
43 124
44 $noderef = $DEFAULT_PORT unless length $noderef; 125 unless ($self->{connect_w}) {
126 my @endpoints;
127 my %trial;
45 128
46 my $idx; 129 $self->{connect_w} = AE::timer
47 for my $t (split /,/, $noderef) { 130 rand,
48 my $pri = ++$idx; 131 $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL,
49 132 sub {
50 #TODO: this should be outside normalise_noderef and in become_public 133 @endpoints = split /,/, $self->{noderef}
51 if ($t =~ /^\d*$/) { 134 unless @endpoints;
52 require POSIX;
53 my $nodename = (POSIX::uname ())[1];
54 135
55 $cv->begin; 136 my $endpoint = shift @endpoints;
56 AnyEvent::Socket::resolve_sockaddr $nodename, $t || "aemp=$DEFAULT_PORT", "tcp", 0, undef, sub { 137
57 for (@_) { 138 $trial{$endpoint} ||= do {
58 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3]; 139 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
59 push @res, [ 140 or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{noderef}: not a resolved node reference.");
60 $pri += 1e-5, 141
61 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service 142 AnyEvent::MP::Transport::mp_connect
143 $host, $port,
144 sub { delete $trial{$endpoint} }
62 ]; 145 ;
63 } 146 };
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 } 147 }
90 } 148 ;
91 } 149 }
92
93 $cv->end;
94
95 $cv
96} 150}
97 151
98sub new { 152sub kill {
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) = @_; 153 my ($self, $port, @reason) = @_;
110 154
111 if ($self->{transport}) { 155 $self->send (["", kil => $port, @reason]);
112 $self->{transport}->send ($msg);
113 } elsif ($self->{queue}) {
114 push @{ $self->{queue} }, $msg;
115 } else {
116 $self->{queue} = [$msg];
117 $self->connect;
118 }
119} 156}
120 157
121sub monitor { 158sub monitor {
122 my ($self, $portid, $cb) = @_; 159 my ($self, $portid, $cb) = @_;
123 160
124 return $cb->()
125 if $self->{failed};
126
127 my $list = $self->{lmon}{$portid} ||= []; 161 my $list = $self->{lmon}{$portid} ||= [];
128 162
129 $self->send (["", mon1 => $portid]) 163 $self->send (["", mon1 => $portid])
130 unless @$list; 164 unless @$list || !length $portid;
131 165
132 push @$list, $cb; 166 push @$list, $cb;
133} 167}
134 168
135sub unmonitor { 169sub unmonitor {
144 $self->send (["", mon0 => $portid]); 178 $self->send (["", mon0 => $portid]);
145 delete $self->{monitor}{$portid}; 179 delete $self->{monitor}{$portid};
146 } 180 }
147} 181}
148 182
183# used for direct slave connections as well
184package AnyEvent::MP::Node::Direct;
185
186use base "AnyEvent::MP::Node::External";
187
188package AnyEvent::MP::Node::Self;
189
190use base "AnyEvent::MP::Node";
191
192sub connect {
193 # we are trivially connected
194}
195
149sub set_transport { 196sub transport_reset {
150 my ($self, $transport) = @_;
151
152 $self->clr_transport
153 if $self->{transport};
154
155 delete $self->{trial};
156 delete $self->{next_connect};
157 delete $self->{failed};
158
159 if (
160 exists $self->{remote_uniq}
161 && $self->{remote_uniq} ne $transport->{remote_uniq}
162 ) {
163 # uniq changed, drop queue
164 delete $self->{queue};
165 #TODO: "DOWN"
166 }
167
168 $self->{remote_uniq} = $transport->{remote_uniq};
169 $self->{transport} = $transport;
170
171 $transport->send ($_)
172 for @{ delete $self->{queue} || [] };
173}
174
175sub clr_transport {
176 my ($self) = @_; 197 my ($self) = @_;
177 198
178 delete $self->{transport};
179 $self->{failed} = 1;
180
181 if (my $mon = delete $self->{monitor}) {
182 $_->() for map @$_, values %$mon;
183 }
184
185 $self->connect;
186}
187
188sub connect {
189 my ($self) = @_;
190
191 Scalar::Util::weaken $self; 199 Scalar::Util::weaken $self;
192 200
193 unless (exists $self->{n_noderef}) { 201 $self->{send} = sub {
194 return if $self->{n_noderef_}++;
195 (AnyEvent::MP::Node::normalise_noderef ($self->{noderef}))->cb (sub {
196 $self or return;
197 delete $self->{n_noderef_};
198 my $noderef = shift->recv;
199
200 $self->{n_noderef} = $noderef;
201
202 $AnyEvent::MP::Base::NODE{$_} = $self 202 local $AnyEvent::MP::Kernel::SRCNODE = $self;
203 for split /,/, $noderef; 203 AnyEvent::MP::Kernel::_inject (@{ $_[0] });
204
205 $self->connect;
206 });
207 return;
208 }
209
210 $self->{retry} ||= [split /,/, $self->{n_noderef}];
211
212 my $endpoint = shift @{ $self->{retry} };
213
214 if (defined $endpoint) {
215 $self->{trial}{$endpoint} ||= do {
216 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
217 or return;
218
219 my ($w, $g);
220
221 $w = AE::timer $AnyEvent::MP::Base::CONNECT_TIMEOUT, 0, sub {
222 delete $self->{trial}{$endpoint};
223 };
224 $g = AnyEvent::MP::Transport::mp_connect
225 $host, $port,
226 sub {
227 delete $self->{trial}{$endpoint}
228 unless @_;
229 $g = shift;
230 };
231 ;
232
233 [$w, \$g]
234 };
235 } else {
236 delete $self->{retry};
237 }
238
239 $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub {
240 $self->connect;
241 }; 204 };
242} 205}
243 206
244package AnyEvent::MP::Node::Self; 207sub kill {
208 my ($self, $port, @reason) = @_;
245 209
246use base "AnyEvent::MP::Node"; 210 delete $AnyEvent::MP::Kernel::PORT{$port};
211 delete $AnyEvent::MP::Kernel::PORT_DATA{$port};
247 212
248sub set_transport { 213 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port}
249 die "FATAL error, set_transport was called"; 214 or !@reason
250} 215 or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason");
251 216
252sub send { 217 $_->(@reason) for values %$mon;
253 local $AnyEvent::MP::Base::SRCNODE = $_[0];
254 AnyEvent::MP::Base::_inject (@{ $_[1] });
255} 218}
256 219
257sub monitor { 220sub monitor {
258 my ($self, $portid, $cb) = @_; 221 my ($self, $portid, $cb) = @_;
259 222
260 return $cb->() 223 return $cb->(no_such_port => "cannot monitor nonexistent port")
261 unless exists $AnyEvent::MP::Base::PORT{$portid}; 224 unless exists $AnyEvent::MP::Kernel::PORT{$portid};
262 225
263 $AnyEvent::MP::Base::LMON{$portid}{$cb+0} = $cb; 226 $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb;
264} 227}
265 228
266sub unmonitor { 229sub unmonitor {
267 my ($self, $portid, $cb) = @_; 230 my ($self, $portid, $cb) = @_;
268 231
269 delete $AnyEvent::MP::Base::LMON{$portid}{$cb+0}; 232 delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0};
270} 233}
271 234
272=head1 SEE ALSO 235=head1 SEE ALSO
273 236
274L<AnyEvent>. 237L<AnyEvent::MP>.
275 238
276=head1 AUTHOR 239=head1 AUTHOR
277 240
278 Marc Lehmann <schmorp@schmorp.de> 241 Marc Lehmann <schmorp@schmorp.de>
279 http://home.schmorp.de/ 242 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines