ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Node.pm
Revision: 1.22
Committed: Thu Aug 13 22:50:47 2009 UTC (14 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-0_7
Changes since 1.21: +10 -0 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.6 }
54    
55 root 1.18 # called only after successful handshake
56     sub transport_error {
57     my ($self, @reason) = @_;
58 root 1.6
59 root 1.21 my $no_transport = !$self->{transport};
60    
61     delete $self->{connect_w};
62     delete $self->{connect_to};
63 root 1.20
64 root 1.18 delete $self->{queue};
65     $self->transport_reset;
66 root 1.6
67 root 1.21 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason)
68     unless $no_transport;
69 root 1.20
70 root 1.18 if (my $mon = delete $self->{lmon}) {
71     $_->(@reason) for map @$_, values %$mon;
72 root 1.6 }
73     }
74    
75 root 1.18 # called after handshake was successful
76     sub transport_connect {
77 root 1.1 my ($self, $transport) = @_;
78    
79 root 1.20 $self->transport_error (transport_error => "switched connections")
80 root 1.6 if $self->{transport};
81    
82 root 1.18 delete $self->{connect_w};
83     delete $self->{connect_to};
84 root 1.8
85 root 1.15 $self->{transport} = $transport;
86 root 1.1
87 root 1.19 my $transport_send = $transport->can ("send");
88    
89 root 1.18 $self->{send} = sub {
90 root 1.19 $transport_send->($transport, $_[0]);
91 root 1.18 };
92    
93 root 1.20 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1);
94    
95 root 1.1 $transport->send ($_)
96     for @{ delete $self->{queue} || [] };
97     }
98    
99 root 1.18 sub connect {
100     my ($self) = @_;
101    
102     Scalar::Util::weaken $self;
103 root 1.8
104 root 1.18 $self->{connect_to} ||= AE::timer
105     $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT,
106     0,
107     sub {
108     $self->transport_error (transport_error => $self->{noderef}, "unable to connect");
109     };
110 root 1.1
111 root 1.18 unless ($self->{connect_w}) {
112     my @endpoints;
113     my %trial;
114    
115     $self->{connect_w} = AE::timer
116     0,
117     $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL,
118     sub {
119     @endpoints = split /,/, $self->{noderef}
120     unless @endpoints;
121    
122     my $endpoint = shift @endpoints;
123    
124     $trial{$endpoint} ||= do {
125     my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
126     or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference.");
127    
128     AnyEvent::MP::Transport::mp_connect
129     $host, $port,
130     sub { delete $trial{$endpoint} }
131     ;
132     };
133     }
134     ;
135 root 1.6 }
136 root 1.8 }
137    
138 root 1.18 sub kill {
139     my ($self, $port, @reason) = @_;
140 root 1.4
141 root 1.18 $self->send (["", kil => $port, @reason]);
142 root 1.1 }
143    
144 root 1.18 sub monitor {
145     my ($self, $portid, $cb) = @_;
146    
147     my $list = $self->{lmon}{$portid} ||= [];
148    
149     $self->send (["", mon1 => $portid])
150     unless @$list;
151 root 1.1
152 root 1.18 push @$list, $cb;
153     }
154 root 1.1
155 root 1.18 sub unmonitor {
156     my ($self, $portid, $cb) = @_;
157 root 1.1
158 root 1.18 my $list = $self->{lmon}{$portid}
159     or return;
160 root 1.1
161 root 1.18 @$list = grep $_ != $cb, @$list;
162 root 1.1
163 root 1.18 unless (@$list) {
164     $self->send (["", mon0 => $portid]);
165     delete $self->{monitor}{$portid};
166 root 1.1 }
167 root 1.18 }
168 root 1.1
169 root 1.18 package AnyEvent::MP::Node::Direct;
170    
171     use base "AnyEvent::MP::Node::External";
172 root 1.1
173 root 1.18 package AnyEvent::MP::Node::Indirect;
174 root 1.13
175     use base "AnyEvent::MP::Node::Direct";
176    
177 root 1.22 sub transport_reset {
178     my ($self) = @_;
179    
180     # as an optimisation, immediately nuke slave nodes
181     delete $AnyEvent::MP::Kernel::NODE{$self->{noderef}}
182     if $self->{transport};
183    
184     $self->SUPER::transport_reset;
185     }
186    
187 root 1.13 sub connect {
188     my ($self) = @_;
189    
190 root 1.18 $self->transport_error (transport_error => $self->{noderef}, "unable to connect to indirect node");
191 root 1.13 }
192    
193 root 1.1 package AnyEvent::MP::Node::Self;
194    
195     use base "AnyEvent::MP::Node";
196    
197 root 1.20 sub connect {
198     # we are trivially connected
199     }
200    
201 root 1.18 sub transport_reset {
202     my ($self) = @_;
203 root 1.1
204 root 1.19 Scalar::Util::weaken $self;
205    
206 root 1.18 $self->{send} = sub {
207 root 1.19 local $AnyEvent::MP::Kernel::SRCNODE = $self;
208     AnyEvent::MP::Kernel::_inject (@{ $_[0] });
209 root 1.18 };
210 root 1.6 }
211    
212 root 1.7 sub kill {
213     my ($self, $port, @reason) = @_;
214    
215 root 1.18 delete $AnyEvent::MP::Kernel::PORT{$port};
216     delete $AnyEvent::MP::Kernel::PORT_DATA{$port};
217 root 1.7
218 root 1.18 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port}
219 root 1.8 or !@reason
220 root 1.18 or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason");
221 root 1.7
222     $_->(@reason) for values %$mon;
223     }
224    
225 root 1.6 sub monitor {
226     my ($self, $portid, $cb) = @_;
227    
228 root 1.14 return $cb->(no_such_port => "cannot monitor nonexistent port")
229 root 1.18 unless exists $AnyEvent::MP::Kernel::PORT{$portid};
230 root 1.6
231 root 1.18 $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb;
232 root 1.6 }
233    
234     sub unmonitor {
235     my ($self, $portid, $cb) = @_;
236    
237 root 1.18 delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0};
238 root 1.1 }
239    
240     =head1 SEE ALSO
241    
242 root 1.17 L<AnyEvent::MP>.
243 root 1.1
244     =head1 AUTHOR
245    
246     Marc Lehmann <schmorp@schmorp.de>
247     http://home.schmorp.de/
248    
249     =cut
250    
251     1
252