| 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 |
root |
1.64 |
package AnyEvent::MP::Node; # base class for nodes |
| 16 |
root |
1.1 |
|
| 17 |
|
|
use common::sense; |
| 18 |
|
|
|
| 19 |
root |
1.63 |
use AnyEvent (); |
| 20 |
root |
1.1 |
use AnyEvent::Socket (); |
| 21 |
|
|
|
| 22 |
|
|
use AnyEvent::MP::Transport (); |
| 23 |
|
|
|
| 24 |
|
|
sub new { |
| 25 |
root |
1.28 |
my ($self, $id) = @_; |
| 26 |
root |
1.1 |
|
| 27 |
root |
1.28 |
$self = bless { id => $id }, $self; |
| 28 |
root |
1.1 |
|
| 29 |
root |
1.27 |
$self->init; |
| 30 |
root |
1.18 |
$self->transport_reset; |
| 31 |
root |
1.1 |
|
| 32 |
root |
1.18 |
$self |
| 33 |
|
|
} |
| 34 |
root |
1.1 |
|
| 35 |
root |
1.27 |
sub init { |
| 36 |
|
|
# |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
root |
1.1 |
sub send { |
| 40 |
root |
1.19 |
&{ shift->{send} } |
| 41 |
root |
1.1 |
} |
| 42 |
|
|
|
| 43 |
root |
1.27 |
# nodes reachable via the network |
| 44 |
root |
1.64 |
package AnyEvent::MP::Node::Remote; # a remote node |
| 45 |
root |
1.7 |
|
| 46 |
root |
1.18 |
use base "AnyEvent::MP::Node"; |
| 47 |
root |
1.7 |
|
| 48 |
root |
1.18 |
# called at init time, mostly sets {send} |
| 49 |
|
|
sub transport_reset { |
| 50 |
|
|
my ($self) = @_; |
| 51 |
root |
1.6 |
|
| 52 |
root |
1.18 |
delete $self->{transport}; |
| 53 |
root |
1.6 |
|
| 54 |
root |
1.19 |
Scalar::Util::weaken $self; |
| 55 |
|
|
|
| 56 |
root |
1.18 |
$self->{send} = sub { |
| 57 |
root |
1.19 |
push @{$self->{queue}}, shift; |
| 58 |
|
|
$self->connect; |
| 59 |
root |
1.18 |
}; |
| 60 |
root |
1.6 |
} |
| 61 |
|
|
|
| 62 |
root |
1.32 |
# called each time we fail to establish a connection, |
| 63 |
|
|
# or the existing connection failed |
| 64 |
root |
1.18 |
sub transport_error { |
| 65 |
|
|
my ($self, @reason) = @_; |
| 66 |
root |
1.6 |
|
| 67 |
root |
1.21 |
my $no_transport = !$self->{transport}; |
| 68 |
|
|
|
| 69 |
|
|
delete $self->{connect_w}; |
| 70 |
|
|
delete $self->{connect_to}; |
| 71 |
root |
1.20 |
|
| 72 |
root |
1.18 |
delete $self->{queue}; |
| 73 |
|
|
$self->transport_reset; |
| 74 |
root |
1.6 |
|
| 75 |
root |
1.18 |
if (my $mon = delete $self->{lmon}) { |
| 76 |
|
|
$_->(@reason) for map @$_, values %$mon; |
| 77 |
root |
1.6 |
} |
| 78 |
root |
1.30 |
|
| 79 |
|
|
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
| 80 |
|
|
unless $no_transport; |
| 81 |
root |
1.32 |
|
| 82 |
|
|
# if we are here and are idle, we nuke ourselves |
| 83 |
|
|
delete $AnyEvent::MP::Kernel::NODE{$self->{id}} |
| 84 |
|
|
unless $self->{transport} || $self->{connect_to}; |
| 85 |
root |
1.6 |
} |
| 86 |
|
|
|
| 87 |
root |
1.18 |
# called after handshake was successful |
| 88 |
|
|
sub transport_connect { |
| 89 |
root |
1.1 |
my ($self, $transport) = @_; |
| 90 |
|
|
|
| 91 |
root |
1.28 |
delete $self->{trial}; |
| 92 |
|
|
|
| 93 |
root |
1.51 |
$self->transport_error (transport_error => $self->{id}, "switched connections") |
| 94 |
root |
1.6 |
if $self->{transport}; |
| 95 |
|
|
|
| 96 |
root |
1.18 |
delete $self->{connect_w}; |
| 97 |
|
|
delete $self->{connect_to}; |
| 98 |
root |
1.8 |
|
| 99 |
root |
1.15 |
$self->{transport} = $transport; |
| 100 |
root |
1.1 |
|
| 101 |
root |
1.52 |
my $transport_send = $transport->{send}; |
| 102 |
root |
1.19 |
|
| 103 |
root |
1.46 |
AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
| 104 |
|
|
|
| 105 |
root |
1.52 |
$self->{send} = $transport_send; |
| 106 |
root |
1.18 |
|
| 107 |
root |
1.52 |
$transport_send->($_) |
| 108 |
root |
1.1 |
for @{ delete $self->{queue} || [] }; |
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
root |
1.18 |
sub connect { |
| 112 |
root |
1.53 |
my ($self) = @_; |
| 113 |
root |
1.18 |
|
| 114 |
root |
1.23 |
return if $self->{transport}; |
| 115 |
root |
1.55 |
return if $self->{connect_w}; |
| 116 |
root |
1.23 |
|
| 117 |
root |
1.18 |
Scalar::Util::weaken $self; |
| 118 |
root |
1.8 |
|
| 119 |
root |
1.59 |
$self->{connect_to} ||= AE::timer $AnyEvent::MP::Kernel::CONFIG->{connect_interval}, 0, sub { |
| 120 |
|
|
$self->transport_error (transport_error => $self->{id}, "unable to connect"); |
| 121 |
|
|
}; |
| 122 |
|
|
|
| 123 |
|
|
# maybe @$addresses? |
| 124 |
|
|
my $addresses = $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}}; |
| 125 |
|
|
|
| 126 |
|
|
if ($addresses) { |
| 127 |
|
|
$self->connect_to ($addresses); |
| 128 |
|
|
} else { |
| 129 |
|
|
# on global nodes, all bets are off now - we either know the node, or we don't |
| 130 |
|
|
unless ($AnyEvent::MP::Kernel::GLOBAL) { |
| 131 |
|
|
AnyEvent::MP::Kernel::g_find ($self->{id}); |
| 132 |
|
|
} |
| 133 |
|
|
} |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
sub connect_to { |
| 137 |
|
|
my ($self, $addresses) = @_; |
| 138 |
|
|
|
| 139 |
|
|
return if $self->{transport}; |
| 140 |
|
|
return if $self->{connect_w}; |
| 141 |
|
|
|
| 142 |
|
|
return unless @$addresses; |
| 143 |
|
|
|
| 144 |
root |
1.62 |
AE::log 9 => "connecting to $self->{id} with [@$addresses]"; |
| 145 |
root |
1.59 |
|
| 146 |
|
|
my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
| 147 |
|
|
my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
| 148 |
|
|
|
| 149 |
|
|
$interval = ($monitor - $interval) / @$addresses |
| 150 |
|
|
if ($monitor - $interval) / @$addresses < $interval; |
| 151 |
|
|
|
| 152 |
|
|
$interval = 0.4 if $interval < 0.4; |
| 153 |
|
|
|
| 154 |
|
|
my @endpoints = reverse @$addresses; |
| 155 |
|
|
|
| 156 |
|
|
$self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub { |
| 157 |
|
|
my $endpoint = pop @endpoints; |
| 158 |
|
|
|
| 159 |
root |
1.62 |
AE::log 9 => "connecting to $self->{id} at $endpoint"; |
| 160 |
root |
1.59 |
|
| 161 |
|
|
$self->{trial}{$endpoint} ||= do { |
| 162 |
|
|
my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
| 163 |
root |
1.62 |
or return AE::log critical => "$self->{id}: '$endpoint' is not a resolved node reference."; |
| 164 |
root |
1.33 |
|
| 165 |
|
|
AnyEvent::MP::Transport::mp_connect |
| 166 |
|
|
$host, $port, |
| 167 |
|
|
sub { delete $self->{trial}{$endpoint} }, |
| 168 |
|
|
}; |
| 169 |
|
|
}; |
| 170 |
root |
1.8 |
} |
| 171 |
|
|
|
| 172 |
root |
1.18 |
sub kill { |
| 173 |
|
|
my ($self, $port, @reason) = @_; |
| 174 |
root |
1.4 |
|
| 175 |
root |
1.53 |
$self->{send} (["", kil => $port, @reason]); |
| 176 |
root |
1.1 |
} |
| 177 |
|
|
|
| 178 |
root |
1.18 |
sub monitor { |
| 179 |
|
|
my ($self, $portid, $cb) = @_; |
| 180 |
|
|
|
| 181 |
|
|
my $list = $self->{lmon}{$portid} ||= []; |
| 182 |
|
|
|
| 183 |
|
|
$self->send (["", mon1 => $portid]) |
| 184 |
root |
1.24 |
unless @$list || !length $portid; |
| 185 |
root |
1.1 |
|
| 186 |
root |
1.18 |
push @$list, $cb; |
| 187 |
|
|
} |
| 188 |
root |
1.1 |
|
| 189 |
root |
1.18 |
sub unmonitor { |
| 190 |
|
|
my ($self, $portid, $cb) = @_; |
| 191 |
root |
1.1 |
|
| 192 |
root |
1.18 |
my $list = $self->{lmon}{$portid} |
| 193 |
|
|
or return; |
| 194 |
root |
1.1 |
|
| 195 |
root |
1.18 |
@$list = grep $_ != $cb, @$list; |
| 196 |
root |
1.1 |
|
| 197 |
root |
1.18 |
unless (@$list) { |
| 198 |
|
|
$self->send (["", mon0 => $portid]); |
| 199 |
|
|
delete $self->{monitor}{$portid}; |
| 200 |
root |
1.1 |
} |
| 201 |
root |
1.18 |
} |
| 202 |
root |
1.1 |
|
| 203 |
root |
1.64 |
package AnyEvent::MP::Node::Self; # the local node |
| 204 |
root |
1.1 |
|
| 205 |
|
|
use base "AnyEvent::MP::Node"; |
| 206 |
|
|
|
| 207 |
root |
1.20 |
sub connect { |
| 208 |
|
|
# we are trivially connected |
| 209 |
|
|
} |
| 210 |
|
|
|
| 211 |
root |
1.38 |
# delay every so often to avoid recursion, also used to delay after spawn |
| 212 |
root |
1.43 |
our $DELAY = -50; |
| 213 |
root |
1.38 |
our @DELAY; |
| 214 |
|
|
our $DELAY_W; |
| 215 |
|
|
|
| 216 |
root |
1.61 |
our $send_delayed = sub { |
| 217 |
root |
1.60 |
$AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
| 218 |
root |
1.39 |
(shift @DELAY)->() |
| 219 |
root |
1.38 |
while @DELAY; |
| 220 |
|
|
undef $DELAY_W; |
| 221 |
|
|
$DELAY = -50; |
| 222 |
root |
1.61 |
}; |
| 223 |
root |
1.38 |
|
| 224 |
root |
1.18 |
sub transport_reset { |
| 225 |
|
|
my ($self) = @_; |
| 226 |
root |
1.1 |
|
| 227 |
root |
1.19 |
Scalar::Util::weaken $self; |
| 228 |
|
|
|
| 229 |
root |
1.18 |
$self->{send} = sub { |
| 230 |
root |
1.61 |
if (++$DELAY > 0) { |
| 231 |
root |
1.39 |
my $msg = $_[0]; |
| 232 |
|
|
push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
| 233 |
root |
1.61 |
$DELAY_W ||= AE::timer 0, 0, $send_delayed; |
| 234 |
root |
1.40 |
return; |
| 235 |
root |
1.38 |
} |
| 236 |
root |
1.40 |
|
| 237 |
root |
1.60 |
local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
| 238 |
root |
1.40 |
AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
| 239 |
root |
1.18 |
}; |
| 240 |
root |
1.6 |
} |
| 241 |
|
|
|
| 242 |
root |
1.28 |
sub transport_connect { |
| 243 |
|
|
my ($self, $tp) = @_; |
| 244 |
|
|
|
| 245 |
root |
1.62 |
AE::log 9 => "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"; |
| 246 |
root |
1.28 |
} |
| 247 |
|
|
|
| 248 |
root |
1.7 |
sub kill { |
| 249 |
root |
1.49 |
my (undef, @args) = @_; |
| 250 |
root |
1.7 |
|
| 251 |
root |
1.44 |
# we _always_ delay kil's, to avoid calling mon callbacks |
| 252 |
|
|
# from anything but the event loop context. |
| 253 |
|
|
$DELAY = 1; |
| 254 |
root |
1.48 |
push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) }; |
| 255 |
root |
1.61 |
$DELAY_W ||= AE::timer 0, 0, $send_delayed; |
| 256 |
root |
1.7 |
} |
| 257 |
|
|
|
| 258 |
root |
1.6 |
sub monitor { |
| 259 |
root |
1.50 |
# maybe always delay, too? |
| 260 |
root |
1.49 |
if ($DELAY_W) { |
| 261 |
|
|
my @args = @_; |
| 262 |
|
|
push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) }; |
| 263 |
|
|
return; |
| 264 |
|
|
} |
| 265 |
|
|
&AnyEvent::MP::Kernel::_monitor; |
| 266 |
root |
1.6 |
} |
| 267 |
|
|
|
| 268 |
|
|
sub unmonitor { |
| 269 |
root |
1.50 |
# no need to always delay |
| 270 |
root |
1.49 |
if ($DELAY_W) { |
| 271 |
|
|
my @args = @_; |
| 272 |
|
|
push @DELAY, sub { AnyEvent::MP::Kernel::_unmonitor (@args) }; |
| 273 |
|
|
return; |
| 274 |
|
|
} |
| 275 |
root |
1.39 |
|
| 276 |
root |
1.49 |
&AnyEvent::MP::Kernel::_unmonitor; |
| 277 |
root |
1.1 |
} |
| 278 |
|
|
|
| 279 |
|
|
=head1 SEE ALSO |
| 280 |
|
|
|
| 281 |
root |
1.17 |
L<AnyEvent::MP>. |
| 282 |
root |
1.1 |
|
| 283 |
|
|
=head1 AUTHOR |
| 284 |
|
|
|
| 285 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 286 |
|
|
http://home.schmorp.de/ |
| 287 |
|
|
|
| 288 |
|
|
=cut |
| 289 |
|
|
|
| 290 |
|
|
1 |
| 291 |
|
|
|