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.1 by root, Fri Jul 31 20:55:46 2009 UTC vs.
Revision 1.7 by root, Tue Aug 4 14:10:51 2009 UTC

13package AnyEvent::MP::Node; 13package AnyEvent::MP::Node;
14 14
15use common::sense; 15use common::sense;
16 16
17use AE (); 17use AE ();
18use AnyEvent::Util ();
18use AnyEvent::Socket (); 19use AnyEvent::Socket ();
19 20
20use AnyEvent::MP::Transport (); 21use AnyEvent::MP::Transport ();
21 22
22use base Exporter::; 23use base Exporter::;
23 24
24our $VERSION = '0.0'; 25our $VERSION = '0.0';
26
27our $DEFAULT_PORT = "4040";
28
29sub normalise_noderef($) {
30 my ($noderef) = @_;
31
32 my $cv = AE::cv;
33 my @res;
34
35 $cv->begin (sub {
36 my %seen;
37 my @refs;
38 for (sort { $a->[0] <=> $b->[0] } @res) {
39 push @refs, $_->[1] unless $seen{$_->[1]}++
40 }
41 shift->send (join ",", @refs);
42 });
43
44 $noderef = $DEFAULT_PORT unless length $noderef;
45
46 my $idx;
47 for my $t (split /,/, $noderef) {
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
55 $cv->begin;
56 AnyEvent::Socket::resolve_sockaddr $nodename, $t || "aemp=$DEFAULT_PORT", "tcp", 0, undef, sub {
57 for (@_) {
58 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
59 push @res, [
60 $pri += 1e-5,
61 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
62 ];
63 }
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 }
90 }
91 }
92
93 $cv->end;
94
95 $cv
96}
25 97
26sub new { 98sub new {
27 my ($class, $noderef) = @_; 99 my ($class, $noderef) = @_;
28 100
29 bless { noderef => $noderef }, $class 101 bless { noderef => $noderef }, $class
44 $self->{queue} = [$msg]; 116 $self->{queue} = [$msg];
45 $self->connect; 117 $self->connect;
46 } 118 }
47} 119}
48 120
121sub kill {
122 my ($self, $port, @reason) = @_;
123
124 $self->send (["", kil => $port, @reason]);
125
126# delete $AnyEvent::MP::Base::PORT{$port};
127
128# my $mon = delete $AnyEvent::MP::Base::LMON{$port}
129# or return;
130
131# $_->(@reason) for values %$mon;
132}
133
134sub monitor {
135 my ($self, $portid, $cb) = @_;
136
137 return $cb->()
138 if $self->{failed};
139
140 my $list = $self->{lmon}{$portid} ||= [];
141
142 $self->send (["", mon1 => $portid])
143 unless @$list;
144
145 push @$list, $cb;
146}
147
148sub unmonitor {
149 my ($self, $portid, $cb) = @_;
150
151 my $list = $self->{lmon}{$portid}
152 or return;
153
154 @$list = grep $_ != $cb, @$list;
155
156 unless (@$list) {
157 $self->send (["", mon0 => $portid]);
158 delete $self->{monitor}{$portid};
159 }
160}
161
49sub set_transport { 162sub set_transport {
50 my ($self, $transport) = @_; 163 my ($self, $transport) = @_;
51 164
165 $self->clr_transport
166 if $self->{transport};
167
52 delete $self->{trial}; 168 delete $self->{trial};
53 delete $self->{next_connect}; 169 delete $self->{next_connect};
170 delete $self->{failed};
54 171
55 if ( 172 if (
56 exists $self->{remote_uniq} 173 exists $self->{remote_uniq}
57 && $self->{remote_uniq} ne $transport->{remote_uniq} 174 && $self->{remote_uniq} ne $transport->{remote_uniq}
58 ) { 175 ) {
70 187
71sub clr_transport { 188sub clr_transport {
72 my ($self) = @_; 189 my ($self) = @_;
73 190
74 delete $self->{transport}; 191 delete $self->{transport};
75 warn "clr_transport\n"; 192 $self->{failed} = 1;
193
194 if (my $mon = delete $self->{monitor}) {
195 $_->() for map @$_, values %$mon;
196 }
197
198 $self->connect;
76} 199}
77 200
78sub connect { 201sub connect {
79 my ($self) = @_; 202 my ($self) = @_;
80 203
81 Scalar::Util::weaken $self; 204 Scalar::Util::weaken $self;
82 205
83 unless (exists $self->{n_noderef}) { 206 unless (exists $self->{n_noderef}) {
207 return if $self->{n_noderef_}++;
84 (AnyEvent::MP::normalise_noderef ($self->{noderef}))->cb (sub { 208 (AnyEvent::MP::Node::normalise_noderef ($self->{noderef}))->cb (sub {
85 $self or return; 209 $self or return;
210 delete $self->{n_noderef_};
86 my $noderef = shift->recv; 211 my $noderef = shift->recv;
87 212
88 $self->{n_noderef} = $noderef; 213 $self->{n_noderef} = $noderef;
89 214
90 $AnyEvent::MP::NODE{$_} = $self 215 $AnyEvent::MP::Base::NODE{$_} = $self
91 for split /,/, $noderef; 216 for split /,/, $noderef;
92 217
93 $self->connect; 218 $self->connect;
94 }); 219 });
95 return; 220 return;
104 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint 229 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
105 or return; 230 or return;
106 231
107 my ($w, $g); 232 my ($w, $g);
108 233
109 $w = AE::timer $AnyEvent::MP::CONNECT_TIMEOUT, 0, sub { 234 $w = AE::timer $AnyEvent::MP::Base::CONNECT_TIMEOUT, 0, sub {
110 delete $self->{trial}{$endpoint}; 235 delete $self->{trial}{$endpoint};
111 }; 236 };
112 $g = AnyEvent::MP::Transport::mp_connect 237 $g = AnyEvent::MP::Transport::mp_connect
113 $host, $port, 238 $host, $port,
114 sub { 239 sub {
122 }; 247 };
123 } else { 248 } else {
124 delete $self->{retry}; 249 delete $self->{retry};
125 } 250 }
126 251
127 $self->{next_connect} = AE::timer $AnyEvent::MP::CONNECT_INTERVAL, 0, sub { 252 $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub {
128 $self->connect; 253 $self->connect;
129 }; 254 };
130} 255}
131 256
132package AnyEvent::MP::Node::Self; 257package AnyEvent::MP::Node::Self;
136sub set_transport { 261sub set_transport {
137 die "FATAL error, set_transport was called"; 262 die "FATAL error, set_transport was called";
138} 263}
139 264
140sub send { 265sub send {
141 die "self-send not implemented yet\n";#d# 266 local $AnyEvent::MP::Base::SRCNODE = $_[0];
267 AnyEvent::MP::Base::_inject (@{ $_[1] });
268}
269
270sub kill {
271 my ($self, $port, @reason) = @_;
272
273 delete $AnyEvent::MP::Base::PORT{$port};
274
275 my $mon = delete $AnyEvent::MP::Base::LMON{$port}
276 or return;
277
278 $_->(@reason) for values %$mon;
279}
280
281sub monitor {
282 my ($self, $portid, $cb) = @_;
283
284 return $cb->()
285 unless exists $AnyEvent::MP::Base::PORT{$portid};
286
287 $AnyEvent::MP::Base::LMON{$portid}{$cb+0} = $cb;
288}
289
290sub unmonitor {
291 my ($self, $portid, $cb) = @_;
292
293 delete $AnyEvent::MP::Base::LMON{$portid}{$cb+0};
142} 294}
143 295
144=head1 SEE ALSO 296=head1 SEE ALSO
145 297
146L<AnyEvent>. 298L<AnyEvent>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines