| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
root |
1.31 |
AnyEvent::MP::Node - represent a node |
| 4 |
root |
1.1 |
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
|
|
use AnyEvent::MP::Node; |
| 8 |
|
|
|
| 9 |
|
|
=head1 DESCRIPTION |
| 10 |
|
|
|
| 11 |
root |
1.31 |
This is an internal utility module, horrible to look at, so don't. |
| 12 |
|
|
|
| 13 |
root |
1.1 |
=cut |
| 14 |
|
|
|
| 15 |
|
|
package AnyEvent::MP::Node; |
| 16 |
|
|
|
| 17 |
|
|
use common::sense; |
| 18 |
|
|
|
| 19 |
|
|
use AE (); |
| 20 |
root |
1.6 |
use AnyEvent::Util (); |
| 21 |
root |
1.1 |
use AnyEvent::Socket (); |
| 22 |
|
|
|
| 23 |
|
|
use AnyEvent::MP::Transport (); |
| 24 |
|
|
|
| 25 |
|
|
sub new { |
| 26 |
root |
1.28 |
my ($self, $id) = @_; |
| 27 |
root |
1.1 |
|
| 28 |
root |
1.28 |
$self = bless { id => $id }, $self; |
| 29 |
root |
1.1 |
|
| 30 |
root |
1.27 |
$self->init; |
| 31 |
root |
1.18 |
$self->transport_reset; |
| 32 |
root |
1.1 |
|
| 33 |
root |
1.18 |
$self |
| 34 |
|
|
} |
| 35 |
root |
1.1 |
|
| 36 |
root |
1.27 |
sub init { |
| 37 |
|
|
# |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
root |
1.1 |
sub send { |
| 41 |
root |
1.19 |
&{ shift->{send} } |
| 42 |
root |
1.1 |
} |
| 43 |
|
|
|
| 44 |
root |
1.27 |
# nodes reachable via the network |
| 45 |
root |
1.18 |
package AnyEvent::MP::Node::External; |
| 46 |
root |
1.7 |
|
| 47 |
root |
1.18 |
use base "AnyEvent::MP::Node"; |
| 48 |
root |
1.7 |
|
| 49 |
root |
1.18 |
# called at init time, mostly sets {send} |
| 50 |
|
|
sub transport_reset { |
| 51 |
|
|
my ($self) = @_; |
| 52 |
root |
1.6 |
|
| 53 |
root |
1.18 |
delete $self->{transport}; |
| 54 |
root |
1.6 |
|
| 55 |
root |
1.19 |
Scalar::Util::weaken $self; |
| 56 |
|
|
|
| 57 |
root |
1.18 |
$self->{send} = sub { |
| 58 |
root |
1.19 |
push @{$self->{queue}}, shift; |
| 59 |
|
|
$self->connect; |
| 60 |
root |
1.18 |
}; |
| 61 |
root |
1.6 |
} |
| 62 |
|
|
|
| 63 |
root |
1.32 |
# called each time we fail to establish a connection, |
| 64 |
|
|
# or the existing connection failed |
| 65 |
root |
1.18 |
sub transport_error { |
| 66 |
|
|
my ($self, @reason) = @_; |
| 67 |
root |
1.6 |
|
| 68 |
root |
1.21 |
my $no_transport = !$self->{transport}; |
| 69 |
|
|
|
| 70 |
|
|
delete $self->{connect_w}; |
| 71 |
|
|
delete $self->{connect_to}; |
| 72 |
root |
1.20 |
|
| 73 |
root |
1.18 |
delete $self->{queue}; |
| 74 |
|
|
$self->transport_reset; |
| 75 |
root |
1.6 |
|
| 76 |
root |
1.18 |
if (my $mon = delete $self->{lmon}) { |
| 77 |
|
|
$_->(@reason) for map @$_, values %$mon; |
| 78 |
root |
1.6 |
} |
| 79 |
root |
1.30 |
|
| 80 |
|
|
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
| 81 |
|
|
unless $no_transport; |
| 82 |
root |
1.32 |
|
| 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}; |
| 86 |
root |
1.6 |
} |
| 87 |
|
|
|
| 88 |
root |
1.18 |
# called after handshake was successful |
| 89 |
|
|
sub transport_connect { |
| 90 |
root |
1.1 |
my ($self, $transport) = @_; |
| 91 |
|
|
|
| 92 |
root |
1.28 |
delete $self->{trial}; |
| 93 |
|
|
|
| 94 |
root |
1.20 |
$self->transport_error (transport_error => "switched connections") |
| 95 |
root |
1.6 |
if $self->{transport}; |
| 96 |
|
|
|
| 97 |
root |
1.41 |
delete $self->{connect_addr}; |
| 98 |
root |
1.18 |
delete $self->{connect_w}; |
| 99 |
|
|
delete $self->{connect_to}; |
| 100 |
root |
1.8 |
|
| 101 |
root |
1.15 |
$self->{transport} = $transport; |
| 102 |
root |
1.1 |
|
| 103 |
root |
1.19 |
my $transport_send = $transport->can ("send"); |
| 104 |
|
|
|
| 105 |
root |
1.18 |
$self->{send} = sub { |
| 106 |
root |
1.19 |
$transport_send->($transport, $_[0]); |
| 107 |
root |
1.18 |
}; |
| 108 |
|
|
|
| 109 |
root |
1.20 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
| 110 |
|
|
|
| 111 |
root |
1.1 |
$transport->send ($_) |
| 112 |
|
|
for @{ delete $self->{queue} || [] }; |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
root |
1.18 |
sub connect { |
| 116 |
root |
1.28 |
my ($self, @addresses) = @_; |
| 117 |
root |
1.18 |
|
| 118 |
root |
1.23 |
return if $self->{transport}; |
| 119 |
|
|
|
| 120 |
root |
1.18 |
Scalar::Util::weaken $self; |
| 121 |
root |
1.8 |
|
| 122 |
root |
1.36 |
my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
| 123 |
root |
1.33 |
|
| 124 |
|
|
$self->{connect_to} ||= AE::timer $monitor, 0, sub { |
| 125 |
|
|
$self->transport_error (transport_error => $self->{id}, "unable to connect"); |
| 126 |
|
|
}; |
| 127 |
root |
1.1 |
|
| 128 |
root |
1.29 |
return unless @addresses; |
| 129 |
root |
1.41 |
|
| 130 |
|
|
if ($self->{connect_w}) { |
| 131 |
|
|
# sometimes we get told about new addresses after we started to connect |
| 132 |
|
|
unshift @{$self->{connect_addr}}, @addresses; |
| 133 |
|
|
return; |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
$self->{connect_addr} = \@addresses; # a bit weird, but efficient |
| 137 |
root |
1.29 |
|
| 138 |
root |
1.28 |
$AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
| 139 |
|
|
|
| 140 |
root |
1.36 |
my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
| 141 |
root |
1.33 |
|
| 142 |
|
|
$interval = ($monitor - $interval) / @addresses |
| 143 |
|
|
if ($monitor - $interval) / @addresses < $interval; |
| 144 |
|
|
|
| 145 |
|
|
$interval = 0.4 if $interval < 0.4; |
| 146 |
|
|
|
| 147 |
|
|
my @endpoints; |
| 148 |
|
|
|
| 149 |
root |
1.34 |
$self->{connect_w} = AE::timer 0.050 * rand, $interval * (0.9 + 0.1 * rand), sub { |
| 150 |
root |
1.33 |
@endpoints = @addresses |
| 151 |
|
|
unless @endpoints; |
| 152 |
root |
1.18 |
|
| 153 |
root |
1.33 |
my $endpoint = shift @endpoints; |
| 154 |
|
|
|
| 155 |
|
|
$AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
| 156 |
|
|
|
| 157 |
|
|
$self->{trial}{$endpoint} ||= do { |
| 158 |
|
|
my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
| 159 |
|
|
or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
| 160 |
|
|
|
| 161 |
|
|
AnyEvent::MP::Transport::mp_connect |
| 162 |
|
|
$host, $port, |
| 163 |
|
|
sub { delete $self->{trial}{$endpoint} }, |
| 164 |
|
|
}; |
| 165 |
|
|
}; |
| 166 |
root |
1.8 |
} |
| 167 |
|
|
|
| 168 |
root |
1.18 |
sub kill { |
| 169 |
|
|
my ($self, $port, @reason) = @_; |
| 170 |
root |
1.4 |
|
| 171 |
root |
1.18 |
$self->send (["", kil => $port, @reason]); |
| 172 |
root |
1.1 |
} |
| 173 |
|
|
|
| 174 |
root |
1.18 |
sub monitor { |
| 175 |
|
|
my ($self, $portid, $cb) = @_; |
| 176 |
|
|
|
| 177 |
|
|
my $list = $self->{lmon}{$portid} ||= []; |
| 178 |
|
|
|
| 179 |
|
|
$self->send (["", mon1 => $portid]) |
| 180 |
root |
1.24 |
unless @$list || !length $portid; |
| 181 |
root |
1.1 |
|
| 182 |
root |
1.18 |
push @$list, $cb; |
| 183 |
|
|
} |
| 184 |
root |
1.1 |
|
| 185 |
root |
1.18 |
sub unmonitor { |
| 186 |
|
|
my ($self, $portid, $cb) = @_; |
| 187 |
root |
1.1 |
|
| 188 |
root |
1.18 |
my $list = $self->{lmon}{$portid} |
| 189 |
|
|
or return; |
| 190 |
root |
1.1 |
|
| 191 |
root |
1.18 |
@$list = grep $_ != $cb, @$list; |
| 192 |
root |
1.1 |
|
| 193 |
root |
1.18 |
unless (@$list) { |
| 194 |
|
|
$self->send (["", mon0 => $portid]); |
| 195 |
|
|
delete $self->{monitor}{$portid}; |
| 196 |
root |
1.1 |
} |
| 197 |
root |
1.18 |
} |
| 198 |
root |
1.1 |
|
| 199 |
root |
1.27 |
# used for direct slave connections as well |
| 200 |
root |
1.18 |
package AnyEvent::MP::Node::Direct; |
| 201 |
|
|
|
| 202 |
|
|
use base "AnyEvent::MP::Node::External"; |
| 203 |
root |
1.1 |
|
| 204 |
|
|
package AnyEvent::MP::Node::Self; |
| 205 |
|
|
|
| 206 |
|
|
use base "AnyEvent::MP::Node"; |
| 207 |
|
|
|
| 208 |
root |
1.20 |
sub connect { |
| 209 |
|
|
# we are trivially connected |
| 210 |
|
|
} |
| 211 |
|
|
|
| 212 |
root |
1.38 |
# delay every so often to avoid recursion, also used to delay after spawn |
| 213 |
|
|
our $DELAY; |
| 214 |
|
|
our @DELAY; |
| 215 |
|
|
our $DELAY_W; |
| 216 |
|
|
|
| 217 |
|
|
sub _send_delayed { |
| 218 |
|
|
local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE{""}; |
| 219 |
root |
1.39 |
(shift @DELAY)->() |
| 220 |
root |
1.38 |
while @DELAY; |
| 221 |
|
|
undef $DELAY_W; |
| 222 |
|
|
$DELAY = -50; |
| 223 |
|
|
} |
| 224 |
|
|
|
| 225 |
root |
1.18 |
sub transport_reset { |
| 226 |
|
|
my ($self) = @_; |
| 227 |
root |
1.1 |
|
| 228 |
root |
1.19 |
Scalar::Util::weaken $self; |
| 229 |
|
|
|
| 230 |
root |
1.18 |
$self->{send} = sub { |
| 231 |
root |
1.38 |
if ($DELAY++ >= 0) { |
| 232 |
root |
1.39 |
my $msg = $_[0]; |
| 233 |
|
|
push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
| 234 |
root |
1.38 |
$DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
| 235 |
root |
1.40 |
return; |
| 236 |
root |
1.38 |
} |
| 237 |
root |
1.40 |
|
| 238 |
|
|
local $AnyEvent::MP::Kernel::SRCNODE = $self; |
| 239 |
|
|
AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
| 240 |
root |
1.18 |
}; |
| 241 |
root |
1.6 |
} |
| 242 |
|
|
|
| 243 |
root |
1.28 |
sub transport_connect { |
| 244 |
|
|
my ($self, $tp) = @_; |
| 245 |
|
|
|
| 246 |
|
|
$AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"); |
| 247 |
|
|
} |
| 248 |
|
|
|
| 249 |
root |
1.7 |
sub kill { |
| 250 |
|
|
my ($self, $port, @reason) = @_; |
| 251 |
|
|
|
| 252 |
root |
1.39 |
my $delay_cb = sub { |
| 253 |
root |
1.42 |
delete $AnyEvent::MP::Kernel::PORT{$port} |
| 254 |
|
|
or return; # killing nonexistent ports is O.K. |
| 255 |
root |
1.39 |
delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
| 256 |
|
|
|
| 257 |
|
|
my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
| 258 |
|
|
or !@reason |
| 259 |
|
|
or $AnyEvent::MP::Kernel::WARN->(8, "unmonitored local port $port died with reason: @reason"); |
| 260 |
root |
1.7 |
|
| 261 |
root |
1.39 |
$_->(@reason) for values %$mon; |
| 262 |
|
|
}; |
| 263 |
root |
1.7 |
|
| 264 |
root |
1.39 |
$DELAY_W ? push @DELAY, $delay_cb : &$delay_cb; |
| 265 |
root |
1.7 |
} |
| 266 |
|
|
|
| 267 |
root |
1.6 |
sub monitor { |
| 268 |
|
|
my ($self, $portid, $cb) = @_; |
| 269 |
|
|
|
| 270 |
root |
1.39 |
my $delay_cb = sub { |
| 271 |
|
|
return $cb->(no_such_port => "cannot monitor nonexistent port", "$self->{id}#$portid") |
| 272 |
|
|
unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
| 273 |
|
|
|
| 274 |
|
|
$AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
| 275 |
|
|
}; |
| 276 |
root |
1.6 |
|
| 277 |
root |
1.39 |
$DELAY_W ? push @DELAY, $delay_cb : &$delay_cb; |
| 278 |
root |
1.6 |
} |
| 279 |
|
|
|
| 280 |
|
|
sub unmonitor { |
| 281 |
|
|
my ($self, $portid, $cb) = @_; |
| 282 |
|
|
|
| 283 |
root |
1.39 |
my $delay_cb = sub { |
| 284 |
|
|
delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
| 285 |
|
|
}; |
| 286 |
|
|
|
| 287 |
|
|
$DELAY_W ? push @DELAY, $delay_cb : &$delay_cb; |
| 288 |
root |
1.1 |
} |
| 289 |
|
|
|
| 290 |
|
|
=head1 SEE ALSO |
| 291 |
|
|
|
| 292 |
root |
1.17 |
L<AnyEvent::MP>. |
| 293 |
root |
1.1 |
|
| 294 |
|
|
=head1 AUTHOR |
| 295 |
|
|
|
| 296 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 297 |
|
|
http://home.schmorp.de/ |
| 298 |
|
|
|
| 299 |
|
|
=cut |
| 300 |
|
|
|
| 301 |
|
|
1 |
| 302 |
|
|
|