ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Node.pm
Revision: 1.35
Committed: Thu Sep 3 20:16:36 2009 UTC (14 years, 8 months ago) by root
Branch: MAIN
Changes since 1.34: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.31 AnyEvent::MP::Node - represent a node
4 root 1.1
5     =head1 SYNOPSIS
6    
7     use AnyEvent::MP::Node;
8    
9     =head1 DESCRIPTION
10    
11 root 1.31 This is an internal utility module, horrible to look at, so don't.
12    
13 root 1.1 =cut
14    
15     package AnyEvent::MP::Node;
16    
17     use common::sense;
18    
19     use AE ();
20 root 1.6 use AnyEvent::Util ();
21 root 1.1 use AnyEvent::Socket ();
22    
23     use AnyEvent::MP::Transport ();
24    
25     sub new {
26 root 1.28 my ($self, $id) = @_;
27 root 1.1
28 root 1.28 $self = bless { id => $id }, $self;
29 root 1.1
30 root 1.27 $self->init;
31 root 1.18 $self->transport_reset;
32 root 1.1
33 root 1.18 $self
34     }
35 root 1.1
36 root 1.27 sub init {
37     #
38     }
39    
40 root 1.1 sub send {
41 root 1.19 &{ shift->{send} }
42 root 1.1 }
43    
44 root 1.27 # nodes reachable via the network
45 root 1.18 package AnyEvent::MP::Node::External;
46 root 1.7
47 root 1.18 use base "AnyEvent::MP::Node";
48 root 1.7
49 root 1.18 # called at init time, mostly sets {send}
50     sub transport_reset {
51     my ($self) = @_;
52 root 1.6
53 root 1.18 delete $self->{transport};
54 root 1.6
55 root 1.19 Scalar::Util::weaken $self;
56    
57 root 1.18 $self->{send} = sub {
58 root 1.19 push @{$self->{queue}}, shift;
59     $self->connect;
60 root 1.18 };
61 root 1.6 }
62    
63 root 1.32 # called each time we fail to establish a connection,
64     # or the existing connection failed
65 root 1.18 sub transport_error {
66     my ($self, @reason) = @_;
67 root 1.6
68 root 1.21 my $no_transport = !$self->{transport};
69    
70     delete $self->{connect_w};
71     delete $self->{connect_to};
72 root 1.20
73 root 1.18 delete $self->{queue};
74     $self->transport_reset;
75 root 1.6
76 root 1.18 if (my $mon = delete $self->{lmon}) {
77     $_->(@reason) for map @$_, values %$mon;
78 root 1.6 }
79 root 1.30
80     AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason)
81     unless $no_transport;
82 root 1.32
83     # if we are here and are idle, we nuke ourselves
84     delete $AnyEvent::MP::Kernel::NODE{$self->{id}}
85     unless $self->{transport} || $self->{connect_to};
86 root 1.6 }
87    
88 root 1.18 # called after handshake was successful
89     sub transport_connect {
90 root 1.1 my ($self, $transport) = @_;
91    
92 root 1.28 delete $self->{trial};
93    
94 root 1.20 $self->transport_error (transport_error => "switched connections")
95 root 1.6 if $self->{transport};
96    
97 root 1.18 delete $self->{connect_w};
98     delete $self->{connect_to};
99 root 1.8
100 root 1.15 $self->{transport} = $transport;
101 root 1.1
102 root 1.19 my $transport_send = $transport->can ("send");
103    
104 root 1.18 $self->{send} = sub {
105 root 1.19 $transport_send->($transport, $_[0]);
106 root 1.18 };
107    
108 root 1.20 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1);
109    
110 root 1.1 $transport->send ($_)
111     for @{ delete $self->{queue} || [] };
112     }
113    
114 root 1.18 sub connect {
115 root 1.28 my ($self, @addresses) = @_;
116 root 1.18
117 root 1.23 return if $self->{transport};
118    
119 root 1.18 Scalar::Util::weaken $self;
120 root 1.8
121 root 1.33 my $monitor = $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT;
122    
123     $self->{connect_to} ||= AE::timer $monitor, 0, sub {
124     $self->transport_error (transport_error => $self->{id}, "unable to connect");
125     };
126 root 1.1
127 root 1.29 return unless @addresses;
128 root 1.33 return if $self->{connect_w};
129 root 1.29
130 root 1.28 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]");
131    
132 root 1.33 my $interval = $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL;
133    
134     $interval = ($monitor - $interval) / @addresses
135     if ($monitor - $interval) / @addresses < $interval;
136    
137     $interval = 0.4 if $interval < 0.4;
138    
139     my @endpoints;
140    
141 root 1.34 $self->{connect_w} = AE::timer 0.050 * rand, $interval * (0.9 + 0.1 * rand), sub {
142 root 1.33 @endpoints = @addresses
143     unless @endpoints;
144 root 1.18
145 root 1.33 my $endpoint = shift @endpoints;
146    
147     $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint");
148    
149     $self->{trial}{$endpoint} ||= do {
150     my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
151     or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference.");
152    
153     AnyEvent::MP::Transport::mp_connect
154     $host, $port,
155     sub { delete $self->{trial}{$endpoint} },
156     };
157     };
158 root 1.8 }
159    
160 root 1.18 sub kill {
161     my ($self, $port, @reason) = @_;
162 root 1.4
163 root 1.18 $self->send (["", kil => $port, @reason]);
164 root 1.1 }
165    
166 root 1.18 sub monitor {
167     my ($self, $portid, $cb) = @_;
168    
169     my $list = $self->{lmon}{$portid} ||= [];
170    
171     $self->send (["", mon1 => $portid])
172 root 1.24 unless @$list || !length $portid;
173 root 1.1
174 root 1.18 push @$list, $cb;
175     }
176 root 1.1
177 root 1.18 sub unmonitor {
178     my ($self, $portid, $cb) = @_;
179 root 1.1
180 root 1.18 my $list = $self->{lmon}{$portid}
181     or return;
182 root 1.1
183 root 1.18 @$list = grep $_ != $cb, @$list;
184 root 1.1
185 root 1.18 unless (@$list) {
186     $self->send (["", mon0 => $portid]);
187     delete $self->{monitor}{$portid};
188 root 1.1 }
189 root 1.18 }
190 root 1.1
191 root 1.27 # used for direct slave connections as well
192 root 1.18 package AnyEvent::MP::Node::Direct;
193    
194     use base "AnyEvent::MP::Node::External";
195 root 1.1
196     package AnyEvent::MP::Node::Self;
197    
198     use base "AnyEvent::MP::Node";
199    
200 root 1.20 sub connect {
201     # we are trivially connected
202     }
203    
204 root 1.18 sub transport_reset {
205     my ($self) = @_;
206 root 1.1
207 root 1.19 Scalar::Util::weaken $self;
208    
209 root 1.18 $self->{send} = sub {
210 root 1.19 local $AnyEvent::MP::Kernel::SRCNODE = $self;
211     AnyEvent::MP::Kernel::_inject (@{ $_[0] });
212 root 1.18 };
213 root 1.6 }
214    
215 root 1.28 sub transport_connect {
216     my ($self, $tp) = @_;
217    
218     $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})");
219     }
220    
221 root 1.7 sub kill {
222     my ($self, $port, @reason) = @_;
223    
224 root 1.18 delete $AnyEvent::MP::Kernel::PORT{$port};
225     delete $AnyEvent::MP::Kernel::PORT_DATA{$port};
226 root 1.7
227 root 1.18 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port}
228 root 1.8 or !@reason
229 root 1.35 or $AnyEvent::MP::Kernel::WARN->(8, "unmonitored local port $port died with reason: @reason");
230 root 1.7
231     $_->(@reason) for values %$mon;
232     }
233    
234 root 1.6 sub monitor {
235     my ($self, $portid, $cb) = @_;
236    
237 root 1.14 return $cb->(no_such_port => "cannot monitor nonexistent port")
238 root 1.18 unless exists $AnyEvent::MP::Kernel::PORT{$portid};
239 root 1.6
240 root 1.18 $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb;
241 root 1.6 }
242    
243     sub unmonitor {
244     my ($self, $portid, $cb) = @_;
245    
246 root 1.18 delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0};
247 root 1.1 }
248    
249     =head1 SEE ALSO
250    
251 root 1.17 L<AnyEvent::MP>.
252 root 1.1
253     =head1 AUTHOR
254    
255     Marc Lehmann <schmorp@schmorp.de>
256     http://home.schmorp.de/
257    
258     =cut
259    
260     1
261