… | |
… | |
13 | package AnyEvent::MP::Node; |
13 | package AnyEvent::MP::Node; |
14 | |
14 | |
15 | use common::sense; |
15 | use common::sense; |
16 | |
16 | |
17 | use AE (); |
17 | use AE (); |
|
|
18 | use AnyEvent::Util (); |
18 | use AnyEvent::Socket (); |
19 | use AnyEvent::Socket (); |
19 | |
20 | |
20 | use AnyEvent::MP::Transport (); |
21 | use AnyEvent::MP::Transport (); |
21 | |
22 | |
22 | use base Exporter::; |
|
|
23 | |
|
|
24 | our $VERSION = '0.0'; |
|
|
25 | |
|
|
26 | sub new { |
23 | sub new { |
27 | my ($class, $noderef) = @_; |
24 | my ($self, $noderef) = @_; |
28 | |
25 | |
29 | bless { noderef => $noderef }, $class |
26 | $self = bless { noderef => $noderef }, $self; |
30 | } |
|
|
31 | |
27 | |
|
|
28 | $self->transport_reset; |
|
|
29 | |
|
|
30 | $self |
|
|
31 | } |
|
|
32 | |
|
|
33 | sub send { |
|
|
34 | &{ shift->{send} } |
|
|
35 | } |
|
|
36 | |
32 | package AnyEvent::MP::Node::Direct; |
37 | package AnyEvent::MP::Node::External; |
33 | |
38 | |
34 | use base "AnyEvent::MP::Node"; |
39 | use base "AnyEvent::MP::Node"; |
35 | |
40 | |
36 | sub send { |
41 | # called at init time, mostly sets {send} |
|
|
42 | sub transport_reset { |
37 | my ($self, $msg) = @_; |
43 | my ($self) = @_; |
38 | |
44 | |
39 | if ($self->{transport}) { |
45 | delete $self->{transport}; |
40 | $self->{transport}->send ($msg); |
46 | |
41 | } elsif ($self->{queue}) { |
47 | Scalar::Util::weaken $self; |
|
|
48 | |
|
|
49 | $self->{send} = sub { |
42 | push @{ $self->{queue} }, $msg; |
50 | push @{$self->{queue}}, shift; |
43 | } else { |
|
|
44 | $self->{queue} = [$msg]; |
|
|
45 | $self->connect; |
51 | $self->connect; |
|
|
52 | }; |
|
|
53 | } |
|
|
54 | |
|
|
55 | # called only after successful handshake |
|
|
56 | sub transport_error { |
|
|
57 | my ($self, @reason) = @_; |
|
|
58 | |
|
|
59 | delete $self->{queue}; |
|
|
60 | $self->transport_reset; |
|
|
61 | |
|
|
62 | if (my $mon = delete $self->{lmon}) { |
|
|
63 | $_->(@reason) for map @$_, values %$mon; |
46 | } |
64 | } |
47 | } |
65 | } |
48 | |
66 | |
49 | sub set_transport { |
67 | # called after handshake was successful |
|
|
68 | sub transport_connect { |
50 | my ($self, $transport) = @_; |
69 | my ($self, $transport) = @_; |
51 | |
70 | |
52 | delete $self->{trial}; |
71 | $self->transport_reset |
|
|
72 | if $self->{transport}; |
|
|
73 | |
53 | delete $self->{next_connect}; |
74 | delete $self->{connect_w}; |
|
|
75 | delete $self->{connect_to}; |
54 | |
76 | |
55 | if ( |
|
|
56 | exists $self->{remote_uniq} |
|
|
57 | && $self->{remote_uniq} ne $transport->{remote_uniq} |
|
|
58 | ) { |
|
|
59 | # uniq changed, drop queue |
|
|
60 | delete $self->{queue}; |
|
|
61 | #TODO: "DOWN" |
|
|
62 | } |
|
|
63 | |
|
|
64 | $self->{remote_uniq} = $transport->{remote_uniq}; |
|
|
65 | $self->{transport} = $transport; |
77 | $self->{transport} = $transport; |
|
|
78 | |
|
|
79 | my $transport_send = $transport->can ("send"); |
|
|
80 | |
|
|
81 | $self->{send} = sub { |
|
|
82 | $transport_send->($transport, $_[0]); |
|
|
83 | }; |
66 | |
84 | |
67 | $transport->send ($_) |
85 | $transport->send ($_) |
68 | for @{ delete $self->{queue} || [] }; |
86 | for @{ delete $self->{queue} || [] }; |
69 | } |
87 | } |
70 | |
88 | |
71 | sub clr_transport { |
|
|
72 | my ($self) = @_; |
|
|
73 | |
|
|
74 | delete $self->{transport}; |
|
|
75 | |
|
|
76 | $self->connect; |
|
|
77 | } |
|
|
78 | |
|
|
79 | sub connect { |
89 | sub connect { |
80 | my ($self) = @_; |
90 | my ($self) = @_; |
81 | |
91 | |
82 | Scalar::Util::weaken $self; |
92 | Scalar::Util::weaken $self; |
83 | |
93 | |
84 | unless (exists $self->{n_noderef}) { |
94 | $self->{connect_to} ||= AE::timer |
85 | (AnyEvent::MP::normalise_noderef ($self->{noderef}))->cb (sub { |
95 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
86 | $self or return; |
96 | 0, |
87 | my $noderef = shift->recv; |
97 | sub { |
88 | |
98 | $self->transport_error (transport_error => $self->{noderef}, "unable to connect"); |
89 | $self->{n_noderef} = $noderef; |
|
|
90 | |
|
|
91 | $AnyEvent::MP::NODE{$_} = $self |
|
|
92 | for split /,/, $noderef; |
|
|
93 | |
|
|
94 | $self->connect; |
|
|
95 | }); |
99 | }; |
96 | return; |
100 | |
|
|
101 | unless ($self->{connect_w}) { |
|
|
102 | my @endpoints; |
|
|
103 | my %trial; |
|
|
104 | |
|
|
105 | $self->{connect_w} = AE::timer |
|
|
106 | 0, |
|
|
107 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
|
|
108 | sub { |
|
|
109 | @endpoints = split /,/, $self->{noderef} |
|
|
110 | unless @endpoints; |
|
|
111 | |
|
|
112 | my $endpoint = shift @endpoints; |
|
|
113 | |
|
|
114 | $trial{$endpoint} ||= do { |
|
|
115 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
116 | or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference."); |
|
|
117 | |
|
|
118 | AnyEvent::MP::Transport::mp_connect |
|
|
119 | $host, $port, |
|
|
120 | sub { delete $trial{$endpoint} } |
|
|
121 | ; |
|
|
122 | }; |
|
|
123 | } |
|
|
124 | ; |
97 | } |
125 | } |
|
|
126 | } |
98 | |
127 | |
99 | $self->{retry} ||= [split /,/, $self->{n_noderef}]; |
128 | sub kill { |
|
|
129 | my ($self, $port, @reason) = @_; |
100 | |
130 | |
101 | my $endpoint = shift @{ $self->{retry} }; |
131 | $self->send (["", kil => $port, @reason]); |
|
|
132 | } |
102 | |
133 | |
103 | if (defined $endpoint) { |
134 | sub monitor { |
104 | $self->{trial}{$endpoint} ||= do { |
135 | my ($self, $portid, $cb) = @_; |
105 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
136 | |
|
|
137 | my $list = $self->{lmon}{$portid} ||= []; |
|
|
138 | |
|
|
139 | $self->send (["", mon1 => $portid]) |
|
|
140 | unless @$list; |
|
|
141 | |
|
|
142 | push @$list, $cb; |
|
|
143 | } |
|
|
144 | |
|
|
145 | sub unmonitor { |
|
|
146 | my ($self, $portid, $cb) = @_; |
|
|
147 | |
|
|
148 | my $list = $self->{lmon}{$portid} |
106 | or return; |
149 | or return; |
107 | |
150 | |
108 | my ($w, $g); |
151 | @$list = grep $_ != $cb, @$list; |
109 | |
152 | |
110 | $w = AE::timer $AnyEvent::MP::CONNECT_TIMEOUT, 0, sub { |
153 | unless (@$list) { |
111 | delete $self->{trial}{$endpoint}; |
154 | $self->send (["", mon0 => $portid]); |
112 | }; |
155 | delete $self->{monitor}{$portid}; |
113 | $g = AnyEvent::MP::Transport::mp_connect |
|
|
114 | $host, $port, |
|
|
115 | sub { |
|
|
116 | delete $self->{trial}{$endpoint} |
|
|
117 | unless @_; |
|
|
118 | $g = shift; |
|
|
119 | }; |
|
|
120 | ; |
|
|
121 | |
|
|
122 | [$w, \$g] |
|
|
123 | }; |
|
|
124 | } else { |
|
|
125 | delete $self->{retry}; |
|
|
126 | } |
156 | } |
|
|
157 | } |
127 | |
158 | |
128 | $self->{next_connect} = AE::timer $AnyEvent::MP::CONNECT_INTERVAL, 0, sub { |
159 | package AnyEvent::MP::Node::Direct; |
129 | $self->connect; |
160 | |
|
|
161 | use base "AnyEvent::MP::Node::External"; |
|
|
162 | |
|
|
163 | package AnyEvent::MP::Node::Indirect; |
|
|
164 | |
|
|
165 | use base "AnyEvent::MP::Node::Direct"; |
|
|
166 | |
|
|
167 | sub connect { |
|
|
168 | my ($self) = @_; |
|
|
169 | |
|
|
170 | $self->transport_error (transport_error => $self->{noderef}, "unable to connect to indirect node"); |
|
|
171 | } |
|
|
172 | |
|
|
173 | package AnyEvent::MP::Node::Self; |
|
|
174 | |
|
|
175 | use base "AnyEvent::MP::Node"; |
|
|
176 | |
|
|
177 | sub transport_reset { |
|
|
178 | my ($self) = @_; |
|
|
179 | |
|
|
180 | Scalar::Util::weaken $self; |
|
|
181 | |
|
|
182 | $self->{send} = sub { |
|
|
183 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
|
|
184 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
130 | }; |
185 | }; |
131 | } |
186 | } |
132 | |
187 | |
133 | package AnyEvent::MP::Node::Self; |
188 | sub kill { |
|
|
189 | my ($self, $port, @reason) = @_; |
134 | |
190 | |
135 | use base "AnyEvent::MP::Node"; |
191 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
|
|
192 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
136 | |
193 | |
137 | sub set_transport { |
194 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
138 | die "FATAL error, set_transport was called"; |
195 | or !@reason |
139 | } |
196 | or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason"); |
140 | |
197 | |
141 | sub send { |
198 | $_->(@reason) for values %$mon; |
142 | AnyEvent::MP::_inject ($_[1]); |
199 | } |
|
|
200 | |
|
|
201 | sub monitor { |
|
|
202 | my ($self, $portid, $cb) = @_; |
|
|
203 | |
|
|
204 | return $cb->(no_such_port => "cannot monitor nonexistent port") |
|
|
205 | unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
|
|
206 | |
|
|
207 | $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
|
|
208 | } |
|
|
209 | |
|
|
210 | sub unmonitor { |
|
|
211 | my ($self, $portid, $cb) = @_; |
|
|
212 | |
|
|
213 | delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
143 | } |
214 | } |
144 | |
215 | |
145 | =head1 SEE ALSO |
216 | =head1 SEE ALSO |
146 | |
217 | |
147 | L<AnyEvent>. |
218 | L<AnyEvent::MP>. |
148 | |
219 | |
149 | =head1 AUTHOR |
220 | =head1 AUTHOR |
150 | |
221 | |
151 | Marc Lehmann <schmorp@schmorp.de> |
222 | Marc Lehmann <schmorp@schmorp.de> |
152 | http://home.schmorp.de/ |
223 | http://home.schmorp.de/ |