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