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