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.21 by root, Thu Aug 13 00:49:23 2009 UTC vs.
Revision 1.32 by root, Sun Aug 30 09:24:09 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::MP::Node - a single processing node (CPU/process...) 3AnyEvent::MP::Node - represent a node
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::MP::Node; 7 use AnyEvent::MP::Node;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10
11This is an internal utility module, horrible to look at, so don't.
10 12
11=cut 13=cut
12 14
13package AnyEvent::MP::Node; 15package AnyEvent::MP::Node;
14 16
19use AnyEvent::Socket (); 21use AnyEvent::Socket ();
20 22
21use AnyEvent::MP::Transport (); 23use AnyEvent::MP::Transport ();
22 24
23sub new { 25sub new {
24 my ($self, $noderef) = @_; 26 my ($self, $id) = @_;
25 27
26 $self = bless { noderef => $noderef }, $self; 28 $self = bless { id => $id }, $self;
27 29
30 $self->init;
28 $self->transport_reset; 31 $self->transport_reset;
29 32
30 $self 33 $self
34}
35
36sub init {
37 #
31} 38}
32 39
33sub send { 40sub send {
34 &{ shift->{send} } 41 &{ shift->{send} }
35} 42}
36 43
44# nodes reachable via the network
37package AnyEvent::MP::Node::External; 45package AnyEvent::MP::Node::External;
38 46
39use base "AnyEvent::MP::Node"; 47use base "AnyEvent::MP::Node";
40 48
41# called at init time, mostly sets {send} 49# called at init time, mostly sets {send}
50 push @{$self->{queue}}, shift; 58 push @{$self->{queue}}, shift;
51 $self->connect; 59 $self->connect;
52 }; 60 };
53} 61}
54 62
55# called only after successful handshake 63# called each time we fail to establish a connection,
64# or the existing connection failed
56sub transport_error { 65sub transport_error {
57 my ($self, @reason) = @_; 66 my ($self, @reason) = @_;
58 67
59 my $no_transport = !$self->{transport}; 68 my $no_transport = !$self->{transport};
60 69
61 delete $self->{connect_w}; 70 delete $self->{connect_w};
62 delete $self->{connect_to}; 71 delete $self->{connect_to};
63 72
64 delete $self->{queue}; 73 delete $self->{queue};
65 $self->transport_reset; 74 $self->transport_reset;
66
67 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason)
68 unless $no_transport;
69 75
70 if (my $mon = delete $self->{lmon}) { 76 if (my $mon = delete $self->{lmon}) {
71 $_->(@reason) for map @$_, values %$mon; 77 $_->(@reason) for map @$_, values %$mon;
72 } 78 }
79
80 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason)
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};
73} 86}
74 87
75# called after handshake was successful 88# called after handshake was successful
76sub transport_connect { 89sub transport_connect {
77 my ($self, $transport) = @_; 90 my ($self, $transport) = @_;
78 91
92 delete $self->{trial};
93
79 $self->transport_error (transport_error => "switched connections") 94 $self->transport_error (transport_error => "switched connections")
80 if $self->{transport}; 95 if $self->{transport};
81 96
82 delete $self->{connect_w}; 97 delete $self->{connect_w};
83 delete $self->{connect_to}; 98 delete $self->{connect_to};
95 $transport->send ($_) 110 $transport->send ($_)
96 for @{ delete $self->{queue} || [] }; 111 for @{ delete $self->{queue} || [] };
97} 112}
98 113
99sub connect { 114sub connect {
100 my ($self) = @_; 115 my ($self, @addresses) = @_;
116
117 return if $self->{transport};
101 118
102 Scalar::Util::weaken $self; 119 Scalar::Util::weaken $self;
103 120
104 $self->{connect_to} ||= AE::timer 121 $self->{connect_to} ||= AE::timer
105 $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, 122 $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT,
106 0, 123 0,
107 sub { 124 sub {
108 $self->transport_error (transport_error => $self->{noderef}, "unable to connect"); 125 $self->transport_error (transport_error => $self->{id}, "unable to connect");
109 }; 126 };
127
128 return unless @addresses;
129
130 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]");
110 131
111 unless ($self->{connect_w}) { 132 unless ($self->{connect_w}) {
112 my @endpoints; 133 my @endpoints;
113 my %trial;
114 134
115 $self->{connect_w} = AE::timer 135 $self->{connect_w} = AE::timer
116 0, 136 rand,
117 $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, 137 $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL,
118 sub { 138 sub {
119 @endpoints = split /,/, $self->{noderef} 139 @endpoints = @addresses
120 unless @endpoints; 140 unless @endpoints;
121 141
122 my $endpoint = shift @endpoints; 142 my $endpoint = shift @endpoints;
123 143
144 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint");
145
124 $trial{$endpoint} ||= do { 146 $self->{trial}{$endpoint} ||= do {
125 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint 147 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
126 or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference."); 148 or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference.");
127 149
128 AnyEvent::MP::Transport::mp_connect 150 AnyEvent::MP::Transport::mp_connect
129 $host, $port, 151 $host, $port,
130 sub { delete $trial{$endpoint} } 152 sub { delete $self->{trial}{$endpoint} },
131 ;
132 }; 153 };
133 } 154 }
134 ; 155 ;
135 } 156 }
136} 157}
145 my ($self, $portid, $cb) = @_; 166 my ($self, $portid, $cb) = @_;
146 167
147 my $list = $self->{lmon}{$portid} ||= []; 168 my $list = $self->{lmon}{$portid} ||= [];
148 169
149 $self->send (["", mon1 => $portid]) 170 $self->send (["", mon1 => $portid])
150 unless @$list; 171 unless @$list || !length $portid;
151 172
152 push @$list, $cb; 173 push @$list, $cb;
153} 174}
154 175
155sub unmonitor { 176sub unmonitor {
164 $self->send (["", mon0 => $portid]); 185 $self->send (["", mon0 => $portid]);
165 delete $self->{monitor}{$portid}; 186 delete $self->{monitor}{$portid};
166 } 187 }
167} 188}
168 189
190# used for direct slave connections as well
169package AnyEvent::MP::Node::Direct; 191package AnyEvent::MP::Node::Direct;
170 192
171use base "AnyEvent::MP::Node::External"; 193use base "AnyEvent::MP::Node::External";
172
173package AnyEvent::MP::Node::Indirect;
174
175use base "AnyEvent::MP::Node::Direct";
176
177sub connect {
178 my ($self) = @_;
179
180 $self->transport_error (transport_error => $self->{noderef}, "unable to connect to indirect node");
181}
182 194
183package AnyEvent::MP::Node::Self; 195package AnyEvent::MP::Node::Self;
184 196
185use base "AnyEvent::MP::Node"; 197use base "AnyEvent::MP::Node";
186 198
197 local $AnyEvent::MP::Kernel::SRCNODE = $self; 209 local $AnyEvent::MP::Kernel::SRCNODE = $self;
198 AnyEvent::MP::Kernel::_inject (@{ $_[0] }); 210 AnyEvent::MP::Kernel::_inject (@{ $_[0] });
199 }; 211 };
200} 212}
201 213
214sub transport_connect {
215 my ($self, $tp) = @_;
216
217 $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})");
218}
219
202sub kill { 220sub kill {
203 my ($self, $port, @reason) = @_; 221 my ($self, $port, @reason) = @_;
204 222
205 delete $AnyEvent::MP::Kernel::PORT{$port}; 223 delete $AnyEvent::MP::Kernel::PORT{$port};
206 delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; 224 delete $AnyEvent::MP::Kernel::PORT_DATA{$port};
207 225
208 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} 226 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port}
209 or !@reason 227 or !@reason
210 or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason"); 228 or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason");
211 229
212 $_->(@reason) for values %$mon; 230 $_->(@reason) for values %$mon;
213} 231}
214 232
215sub monitor { 233sub monitor {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines