… | |
… | |
23 | sub new { |
23 | sub new { |
24 | my ($self, $noderef) = @_; |
24 | my ($self, $noderef) = @_; |
25 | |
25 | |
26 | $self = bless { noderef => $noderef }, $self; |
26 | $self = bless { noderef => $noderef }, $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) = @_; |
58 | |
67 | |
|
|
68 | my $no_transport = !$self->{transport}; |
|
|
69 | |
|
|
70 | delete $self->{connect_w}; |
|
|
71 | delete $self->{connect_to}; |
|
|
72 | |
59 | delete $self->{queue}; |
73 | delete $self->{queue}; |
60 | $self->transport_reset; |
74 | $self->transport_reset; |
|
|
75 | |
|
|
76 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
|
|
77 | unless $no_transport; |
61 | |
78 | |
62 | if (my $mon = delete $self->{lmon}) { |
79 | if (my $mon = delete $self->{lmon}) { |
63 | $_->(@reason) for map @$_, values %$mon; |
80 | $_->(@reason) for map @$_, values %$mon; |
64 | } |
81 | } |
65 | } |
82 | } |
66 | |
83 | |
67 | # called after handshake was successful |
84 | # called after handshake was successful |
68 | sub transport_connect { |
85 | sub transport_connect { |
69 | my ($self, $transport) = @_; |
86 | my ($self, $transport) = @_; |
70 | |
87 | |
71 | $self->transport_reset |
88 | $self->transport_error (transport_error => "switched connections") |
72 | if $self->{transport}; |
89 | if $self->{transport}; |
73 | |
90 | |
74 | delete $self->{connect_w}; |
91 | delete $self->{connect_w}; |
75 | delete $self->{connect_to}; |
92 | delete $self->{connect_to}; |
76 | |
93 | |
… | |
… | |
80 | |
97 | |
81 | $self->{send} = sub { |
98 | $self->{send} = sub { |
82 | $transport_send->($transport, $_[0]); |
99 | $transport_send->($transport, $_[0]); |
83 | }; |
100 | }; |
84 | |
101 | |
|
|
102 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
|
|
103 | |
85 | $transport->send ($_) |
104 | $transport->send ($_) |
86 | for @{ delete $self->{queue} || [] }; |
105 | for @{ delete $self->{queue} || [] }; |
87 | } |
106 | } |
88 | |
107 | |
89 | sub connect { |
108 | sub connect { |
90 | my ($self) = @_; |
109 | my ($self) = @_; |
|
|
110 | |
|
|
111 | return if $self->{transport}; |
|
|
112 | |
|
|
113 | # just ignore connect requests for slave nodes - let's hope it connects to us instead |
|
|
114 | return if $self->{noderef} =~ /^slave\//; |
91 | |
115 | |
92 | Scalar::Util::weaken $self; |
116 | Scalar::Util::weaken $self; |
93 | |
117 | |
94 | $self->{connect_to} ||= AE::timer |
118 | $self->{connect_to} ||= AE::timer |
95 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
119 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
… | |
… | |
101 | unless ($self->{connect_w}) { |
125 | unless ($self->{connect_w}) { |
102 | my @endpoints; |
126 | my @endpoints; |
103 | my %trial; |
127 | my %trial; |
104 | |
128 | |
105 | $self->{connect_w} = AE::timer |
129 | $self->{connect_w} = AE::timer |
106 | 0, |
130 | rand, |
107 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
131 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
108 | sub { |
132 | sub { |
109 | @endpoints = split /,/, $self->{noderef} |
133 | @endpoints = split /,/, $self->{noderef} |
110 | unless @endpoints; |
134 | unless @endpoints; |
111 | |
135 | |
112 | my $endpoint = shift @endpoints; |
136 | my $endpoint = shift @endpoints; |
113 | |
137 | |
114 | $trial{$endpoint} ||= do { |
138 | $trial{$endpoint} ||= do { |
115 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
139 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
116 | or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference."); |
140 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{noderef}: not a resolved node reference."); |
117 | |
141 | |
118 | AnyEvent::MP::Transport::mp_connect |
142 | AnyEvent::MP::Transport::mp_connect |
119 | $host, $port, |
143 | $host, $port, |
120 | sub { delete $trial{$endpoint} } |
144 | sub { delete $trial{$endpoint} } |
121 | ; |
145 | ; |
… | |
… | |
135 | my ($self, $portid, $cb) = @_; |
159 | my ($self, $portid, $cb) = @_; |
136 | |
160 | |
137 | my $list = $self->{lmon}{$portid} ||= []; |
161 | my $list = $self->{lmon}{$portid} ||= []; |
138 | |
162 | |
139 | $self->send (["", mon1 => $portid]) |
163 | $self->send (["", mon1 => $portid]) |
140 | unless @$list; |
164 | unless @$list || !length $portid; |
141 | |
165 | |
142 | push @$list, $cb; |
166 | push @$list, $cb; |
143 | } |
167 | } |
144 | |
168 | |
145 | sub unmonitor { |
169 | sub unmonitor { |
… | |
… | |
154 | $self->send (["", mon0 => $portid]); |
178 | $self->send (["", mon0 => $portid]); |
155 | delete $self->{monitor}{$portid}; |
179 | delete $self->{monitor}{$portid}; |
156 | } |
180 | } |
157 | } |
181 | } |
158 | |
182 | |
|
|
183 | # used for direct slave connections as well |
159 | package AnyEvent::MP::Node::Direct; |
184 | package AnyEvent::MP::Node::Direct; |
160 | |
185 | |
161 | use base "AnyEvent::MP::Node::External"; |
186 | use base "AnyEvent::MP::Node::External"; |
162 | |
187 | |
163 | package AnyEvent::MP::Node::Indirect; |
188 | package AnyEvent::MP::Node::Self; |
164 | |
189 | |
165 | use base "AnyEvent::MP::Node::Direct"; |
190 | use base "AnyEvent::MP::Node"; |
166 | |
191 | |
167 | sub connect { |
192 | sub connect { |
168 | my ($self) = @_; |
193 | # we are trivially connected |
169 | |
|
|
170 | $self->transport_error (transport_error => $self->{noderef}, "unable to connect to indirect node"); |
|
|
171 | } |
194 | } |
172 | |
|
|
173 | package AnyEvent::MP::Node::Self; |
|
|
174 | |
|
|
175 | use base "AnyEvent::MP::Node"; |
|
|
176 | |
195 | |
177 | sub transport_reset { |
196 | sub transport_reset { |
178 | my ($self) = @_; |
197 | my ($self) = @_; |
179 | |
198 | |
180 | Scalar::Util::weaken $self; |
199 | Scalar::Util::weaken $self; |
… | |
… | |
191 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
210 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
192 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
211 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
193 | |
212 | |
194 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
213 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
195 | or !@reason |
214 | or !@reason |
196 | or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason"); |
215 | or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
197 | |
216 | |
198 | $_->(@reason) for values %$mon; |
217 | $_->(@reason) for values %$mon; |
199 | } |
218 | } |
200 | |
219 | |
201 | sub monitor { |
220 | sub monitor { |