ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Node.pm
Revision: 1.24
Committed: Sat Aug 15 04:34:34 2009 UTC (14 years, 9 months ago) by root
Branch: MAIN
Changes since 1.23: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     AnyEvent::MP::Node - a single processing node (CPU/process...)
4    
5     =head1 SYNOPSIS
6    
7     use AnyEvent::MP::Node;
8    
9     =head1 DESCRIPTION
10    
11     =cut
12    
13     package AnyEvent::MP::Node;
14    
15     use common::sense;
16    
17     use AE ();
18 root 1.6 use AnyEvent::Util ();
19 root 1.1 use AnyEvent::Socket ();
20    
21     use AnyEvent::MP::Transport ();
22    
23     sub new {
24 root 1.18 my ($self, $noderef) = @_;
25 root 1.1
26 root 1.18 $self = bless { noderef => $noderef }, $self;
27 root 1.1
28 root 1.18 $self->transport_reset;
29 root 1.1
30 root 1.18 $self
31     }
32 root 1.1
33     sub send {
34 root 1.19 &{ shift->{send} }
35 root 1.1 }
36    
37 root 1.18 package AnyEvent::MP::Node::External;
38 root 1.7
39 root 1.18 use base "AnyEvent::MP::Node";
40 root 1.7
41 root 1.18 # called at init time, mostly sets {send}
42     sub transport_reset {
43     my ($self) = @_;
44 root 1.6
45 root 1.18 delete $self->{transport};
46 root 1.6
47 root 1.19 Scalar::Util::weaken $self;
48    
49 root 1.18 $self->{send} = sub {
50 root 1.19 push @{$self->{queue}}, shift;
51     $self->connect;
52 root 1.18 };
53 root 1.23
54     $self->connect
55     if $self->{autoconnect};
56 root 1.6 }
57    
58 root 1.18 # called only after successful handshake
59     sub transport_error {
60     my ($self, @reason) = @_;
61 root 1.6
62 root 1.21 my $no_transport = !$self->{transport};
63    
64     delete $self->{connect_w};
65     delete $self->{connect_to};
66 root 1.20
67 root 1.18 delete $self->{queue};
68     $self->transport_reset;
69 root 1.6
70 root 1.21 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason)
71     unless $no_transport;
72 root 1.20
73 root 1.18 if (my $mon = delete $self->{lmon}) {
74     $_->(@reason) for map @$_, values %$mon;
75 root 1.6 }
76     }
77    
78 root 1.18 # called after handshake was successful
79     sub transport_connect {
80 root 1.1 my ($self, $transport) = @_;
81    
82 root 1.23 # first connect with a master node
83     $AnyEvent::MP::Kernel::SLAVE->($self)
84     if ref $AnyEvent::MP::Kernel::SLAVE;
85    
86 root 1.20 $self->transport_error (transport_error => "switched connections")
87 root 1.6 if $self->{transport};
88    
89 root 1.18 delete $self->{connect_w};
90     delete $self->{connect_to};
91 root 1.8
92 root 1.15 $self->{transport} = $transport;
93 root 1.1
94 root 1.19 my $transport_send = $transport->can ("send");
95    
96 root 1.18 $self->{send} = sub {
97 root 1.19 $transport_send->($transport, $_[0]);
98 root 1.18 };
99    
100 root 1.20 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1);
101    
102 root 1.1 $transport->send ($_)
103     for @{ delete $self->{queue} || [] };
104     }
105    
106 root 1.18 sub connect {
107     my ($self) = @_;
108    
109 root 1.23 return if $self->{transport};
110    
111 root 1.18 Scalar::Util::weaken $self;
112 root 1.8
113 root 1.18 $self->{connect_to} ||= AE::timer
114     $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT,
115     0,
116     sub {
117     $self->transport_error (transport_error => $self->{noderef}, "unable to connect");
118     };
119 root 1.1
120 root 1.18 unless ($self->{connect_w}) {
121     my @endpoints;
122     my %trial;
123    
124     $self->{connect_w} = AE::timer
125     0,
126     $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL,
127     sub {
128     @endpoints = split /,/, $self->{noderef}
129     unless @endpoints;
130    
131     my $endpoint = shift @endpoints;
132    
133     $trial{$endpoint} ||= do {
134     my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
135     or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference.");
136    
137     AnyEvent::MP::Transport::mp_connect
138     $host, $port,
139     sub { delete $trial{$endpoint} }
140     ;
141     };
142     }
143     ;
144 root 1.6 }
145 root 1.8 }
146    
147 root 1.18 sub kill {
148     my ($self, $port, @reason) = @_;
149 root 1.4
150 root 1.18 $self->send (["", kil => $port, @reason]);
151 root 1.1 }
152    
153 root 1.18 sub monitor {
154     my ($self, $portid, $cb) = @_;
155    
156     my $list = $self->{lmon}{$portid} ||= [];
157    
158     $self->send (["", mon1 => $portid])
159 root 1.24 unless @$list || !length $portid;
160 root 1.1
161 root 1.18 push @$list, $cb;
162     }
163 root 1.1
164 root 1.18 sub unmonitor {
165     my ($self, $portid, $cb) = @_;
166 root 1.1
167 root 1.18 my $list = $self->{lmon}{$portid}
168     or return;
169 root 1.1
170 root 1.18 @$list = grep $_ != $cb, @$list;
171 root 1.1
172 root 1.18 unless (@$list) {
173     $self->send (["", mon0 => $portid]);
174     delete $self->{monitor}{$portid};
175 root 1.1 }
176 root 1.18 }
177 root 1.1
178 root 1.18 package AnyEvent::MP::Node::Direct;
179    
180     use base "AnyEvent::MP::Node::External";
181 root 1.1
182 root 1.18 package AnyEvent::MP::Node::Indirect;
183 root 1.13
184     use base "AnyEvent::MP::Node::Direct";
185    
186 root 1.23 sub master {
187     my ($self) = @_;
188    
189     my (undef, $master) = split /\@/, $self->{noderef}, 2;
190     $master =~ s/!/,/g;
191     $master
192     }
193    
194 root 1.22 sub transport_reset {
195     my ($self) = @_;
196    
197 root 1.23 if ($self->{transport}) {
198     # as an optimisation, immediately nuke slave nodes
199     delete $AnyEvent::MP::Kernel::NODE{$self->{noderef}};
200     } else {
201     $self->SUPER::transport_reset;
202     return;#d##TODO#
203    
204     my $noderef = $self->{noderef};
205     my $master = $self->master;
206    
207     # slave nodes are so cool - we can always send to them :)
208    
209     $self->{send} = sub {
210     $self->connect;
211 root 1.24 snd $master, snd => $noderef, @_;
212 root 1.23 };
213     }
214 root 1.22 }
215    
216 root 1.13 sub connect {
217     my ($self) = @_;
218    
219 root 1.23 #TODO#
220     # # ask for a connection, #TODO# rate-limit this somehow
221     # snd $self->master, relay => $self->{noderef}, connect_node => $AnyEvent::MP::Kernel::NODE;
222 root 1.13 }
223    
224 root 1.1 package AnyEvent::MP::Node::Self;
225    
226     use base "AnyEvent::MP::Node";
227    
228 root 1.20 sub connect {
229     # we are trivially connected
230     }
231    
232 root 1.18 sub transport_reset {
233     my ($self) = @_;
234 root 1.1
235 root 1.19 Scalar::Util::weaken $self;
236    
237 root 1.18 $self->{send} = sub {
238 root 1.19 local $AnyEvent::MP::Kernel::SRCNODE = $self;
239     AnyEvent::MP::Kernel::_inject (@{ $_[0] });
240 root 1.18 };
241 root 1.6 }
242    
243 root 1.7 sub kill {
244     my ($self, $port, @reason) = @_;
245    
246 root 1.18 delete $AnyEvent::MP::Kernel::PORT{$port};
247     delete $AnyEvent::MP::Kernel::PORT_DATA{$port};
248 root 1.7
249 root 1.18 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port}
250 root 1.8 or !@reason
251 root 1.18 or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason");
252 root 1.7
253     $_->(@reason) for values %$mon;
254     }
255    
256 root 1.6 sub monitor {
257     my ($self, $portid, $cb) = @_;
258    
259 root 1.14 return $cb->(no_such_port => "cannot monitor nonexistent port")
260 root 1.18 unless exists $AnyEvent::MP::Kernel::PORT{$portid};
261 root 1.6
262 root 1.18 $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb;
263 root 1.6 }
264    
265     sub unmonitor {
266     my ($self, $portid, $cb) = @_;
267    
268 root 1.18 delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0};
269 root 1.1 }
270    
271     =head1 SEE ALSO
272    
273 root 1.17 L<AnyEvent::MP>.
274 root 1.1
275     =head1 AUTHOR
276    
277     Marc Lehmann <schmorp@schmorp.de>
278     http://home.schmorp.de/
279    
280     =cut
281    
282     1
283