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.31 by root, Fri Aug 28 23:06:33 2009 UTC vs.
Revision 1.33 by root, Sun Aug 30 21:45:51 2009 UTC

58 push @{$self->{queue}}, shift; 58 push @{$self->{queue}}, shift;
59 $self->connect; 59 $self->connect;
60 }; 60 };
61} 61}
62 62
63# called only after successful handshake 63# called each time we fail to establish a connection,
64# or the existing connection failed
64sub transport_error { 65sub transport_error {
65 my ($self, @reason) = @_; 66 my ($self, @reason) = @_;
66 67
67 my $no_transport = !$self->{transport}; 68 my $no_transport = !$self->{transport};
68 69
76 $_->(@reason) for map @$_, values %$mon; 77 $_->(@reason) for map @$_, values %$mon;
77 } 78 }
78 79
79 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) 80 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason)
80 unless $no_transport; 81 unless $no_transport;
82
83 # if we are here and are idle, we nuke ourselves
84 delete $AnyEvent::MP::Kernel::NODE{$self->{id}}
85 unless $self->{transport} || $self->{connect_to};
81} 86}
82 87
83# called after handshake was successful 88# called after handshake was successful
84sub transport_connect { 89sub transport_connect {
85 my ($self, $transport) = @_; 90 my ($self, $transport) = @_;
111 116
112 return if $self->{transport}; 117 return if $self->{transport};
113 118
114 Scalar::Util::weaken $self; 119 Scalar::Util::weaken $self;
115 120
116 $self->{connect_to} ||= AE::timer
117 $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, 121 my $monitor = $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT;
118 0, 122
119 sub { 123 $self->{connect_to} ||= AE::timer $monitor, 0, sub {
120 $self->transport_error (transport_error => $self->{id}, "unable to connect"); 124 $self->transport_error (transport_error => $self->{id}, "unable to connect");
125 };
126
127 return unless @addresses;
128 return if $self->{connect_w};
129
130 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]");
131
132 my $interval = $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL;
133
134 $interval = ($monitor - $interval) / @addresses
135 if ($monitor - $interval) / @addresses < $interval;
136
137 $interval = 0.4 if $interval < 0.4;
138
139 my @endpoints;
140
141 $self->{connect_w} = AE::timer rand, $interval, sub {
142 @endpoints = @addresses
143 unless @endpoints;
144
145 my $endpoint = shift @endpoints;
146
147 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint");
148
149 $self->{trial}{$endpoint} ||= do {
150 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
151 or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference.");
152
153 AnyEvent::MP::Transport::mp_connect
154 $host, $port,
155 sub { delete $self->{trial}{$endpoint} },
121 }; 156 };
122
123 return unless @addresses;
124
125 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]");
126
127 unless ($self->{connect_w}) {
128 my @endpoints;
129
130 $self->{connect_w} = AE::timer
131 rand,
132 $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL,
133 sub {
134 @endpoints = @addresses
135 unless @endpoints;
136
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} },
148 };
149 }
150 ;
151 } 157 };
152} 158}
153 159
154sub kill { 160sub kill {
155 my ($self, $port, @reason) = @_; 161 my ($self, $port, @reason) = @_;
156 162

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines