… | |
… | |
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) = @_; |
… | |
… | |
96 | for @{ delete $self->{queue} || [] }; |
105 | for @{ delete $self->{queue} || [] }; |
97 | } |
106 | } |
98 | |
107 | |
99 | sub connect { |
108 | sub connect { |
100 | 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\//; |
101 | |
115 | |
102 | Scalar::Util::weaken $self; |
116 | Scalar::Util::weaken $self; |
103 | |
117 | |
104 | $self->{connect_to} ||= AE::timer |
118 | $self->{connect_to} ||= AE::timer |
105 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
119 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
… | |
… | |
111 | unless ($self->{connect_w}) { |
125 | unless ($self->{connect_w}) { |
112 | my @endpoints; |
126 | my @endpoints; |
113 | my %trial; |
127 | my %trial; |
114 | |
128 | |
115 | $self->{connect_w} = AE::timer |
129 | $self->{connect_w} = AE::timer |
116 | 0, |
130 | rand, |
117 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
131 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
118 | sub { |
132 | sub { |
119 | @endpoints = split /,/, $self->{noderef} |
133 | @endpoints = split /,/, $self->{noderef} |
120 | unless @endpoints; |
134 | unless @endpoints; |
121 | |
135 | |
122 | my $endpoint = shift @endpoints; |
136 | my $endpoint = shift @endpoints; |
123 | |
137 | |
124 | $trial{$endpoint} ||= do { |
138 | $trial{$endpoint} ||= do { |
125 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
139 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
126 | 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."); |
127 | |
141 | |
128 | AnyEvent::MP::Transport::mp_connect |
142 | AnyEvent::MP::Transport::mp_connect |
129 | $host, $port, |
143 | $host, $port, |
130 | sub { delete $trial{$endpoint} } |
144 | sub { delete $trial{$endpoint} } |
131 | ; |
145 | ; |
… | |
… | |
145 | my ($self, $portid, $cb) = @_; |
159 | my ($self, $portid, $cb) = @_; |
146 | |
160 | |
147 | my $list = $self->{lmon}{$portid} ||= []; |
161 | my $list = $self->{lmon}{$portid} ||= []; |
148 | |
162 | |
149 | $self->send (["", mon1 => $portid]) |
163 | $self->send (["", mon1 => $portid]) |
150 | unless @$list; |
164 | unless @$list || !length $portid; |
151 | |
165 | |
152 | push @$list, $cb; |
166 | push @$list, $cb; |
153 | } |
167 | } |
154 | |
168 | |
155 | sub unmonitor { |
169 | sub unmonitor { |
… | |
… | |
164 | $self->send (["", mon0 => $portid]); |
178 | $self->send (["", mon0 => $portid]); |
165 | delete $self->{monitor}{$portid}; |
179 | delete $self->{monitor}{$portid}; |
166 | } |
180 | } |
167 | } |
181 | } |
168 | |
182 | |
|
|
183 | # used for direct slave connections as well |
169 | package AnyEvent::MP::Node::Direct; |
184 | package AnyEvent::MP::Node::Direct; |
170 | |
185 | |
171 | use base "AnyEvent::MP::Node::External"; |
186 | 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 | |
187 | |
183 | package AnyEvent::MP::Node::Self; |
188 | package AnyEvent::MP::Node::Self; |
184 | |
189 | |
185 | use base "AnyEvent::MP::Node"; |
190 | use base "AnyEvent::MP::Node"; |
186 | |
191 | |
… | |
… | |
205 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
210 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
206 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
211 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
207 | |
212 | |
208 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
213 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
209 | or !@reason |
214 | or !@reason |
210 | 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"); |
211 | |
216 | |
212 | $_->(@reason) for values %$mon; |
217 | $_->(@reason) for values %$mon; |
213 | } |
218 | } |
214 | |
219 | |
215 | sub monitor { |
220 | sub monitor { |