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