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