ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Node.pm
(Generate patch)

Comparing AnyEvent-MP/MP/Node.pm (file contents):
Revision 1.22 by root, Thu Aug 13 22:50:47 2009 UTC vs.
Revision 1.26 by root, Sat Aug 15 15:13:04 2009 UTC

48 48
49 $self->{send} = sub { 49 $self->{send} = sub {
50 push @{$self->{queue}}, shift; 50 push @{$self->{queue}}, shift;
51 $self->connect; 51 $self->connect;
52 }; 52 };
53
54 $self->connect
55 if $self->{autoconnect};
53} 56}
54 57
55# called only after successful handshake 58# called only after successful handshake
56sub transport_error { 59sub transport_error {
57 my ($self, @reason) = @_; 60 my ($self, @reason) = @_;
74 77
75# called after handshake was successful 78# called after handshake was successful
76sub transport_connect { 79sub transport_connect {
77 my ($self, $transport) = @_; 80 my ($self, $transport) = @_;
78 81
82 # first connect with a master node
83 $AnyEvent::MP::Kernel::SLAVE->($self)
84 if ref $AnyEvent::MP::Kernel::SLAVE;
85
79 $self->transport_error (transport_error => "switched connections") 86 $self->transport_error (transport_error => "switched connections")
80 if $self->{transport}; 87 if $self->{transport};
81 88
82 delete $self->{connect_w}; 89 delete $self->{connect_w};
83 delete $self->{connect_to}; 90 delete $self->{connect_to};
96 for @{ delete $self->{queue} || [] }; 103 for @{ delete $self->{queue} || [] };
97} 104}
98 105
99sub connect { 106sub connect {
100 my ($self) = @_; 107 my ($self) = @_;
108
109 return if $self->{transport};
101 110
102 Scalar::Util::weaken $self; 111 Scalar::Util::weaken $self;
103 112
104 $self->{connect_to} ||= AE::timer 113 $self->{connect_to} ||= AE::timer
105 $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, 114 $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT,
111 unless ($self->{connect_w}) { 120 unless ($self->{connect_w}) {
112 my @endpoints; 121 my @endpoints;
113 my %trial; 122 my %trial;
114 123
115 $self->{connect_w} = AE::timer 124 $self->{connect_w} = AE::timer
116 0, 125 rand,
117 $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, 126 $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL,
118 sub { 127 sub {
119 @endpoints = split /,/, $self->{noderef} 128 @endpoints = split /,/, $self->{noderef}
120 unless @endpoints; 129 unless @endpoints;
121 130
122 my $endpoint = shift @endpoints; 131 my $endpoint = shift @endpoints;
123 132
124 $trial{$endpoint} ||= do { 133 $trial{$endpoint} ||= do {
125 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint 134 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
126 or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference."); 135 or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{noderef}: not a resolved node reference.");
127 136
128 AnyEvent::MP::Transport::mp_connect 137 AnyEvent::MP::Transport::mp_connect
129 $host, $port, 138 $host, $port,
130 sub { delete $trial{$endpoint} } 139 sub { delete $trial{$endpoint} }
131 ; 140 ;
145 my ($self, $portid, $cb) = @_; 154 my ($self, $portid, $cb) = @_;
146 155
147 my $list = $self->{lmon}{$portid} ||= []; 156 my $list = $self->{lmon}{$portid} ||= [];
148 157
149 $self->send (["", mon1 => $portid]) 158 $self->send (["", mon1 => $portid])
150 unless @$list; 159 unless @$list || !length $portid;
151 160
152 push @$list, $cb; 161 push @$list, $cb;
153} 162}
154 163
155sub unmonitor { 164sub unmonitor {
172 181
173package AnyEvent::MP::Node::Indirect; 182package AnyEvent::MP::Node::Indirect;
174 183
175use base "AnyEvent::MP::Node::Direct"; 184use base "AnyEvent::MP::Node::Direct";
176 185
186sub master {
187 my ($self) = @_;
188
189 my (undef, $master) = split /\@/, $self->{noderef}, 2;
190 $master =~ s/!/,/g;
191 $master
192}
193
177sub transport_reset { 194sub transport_reset {
178 my ($self) = @_; 195 my ($self) = @_;
179 196
197 if ($self->{transport}) {
180 # as an optimisation, immediately nuke slave nodes 198 # as an optimisation, immediately nuke slave nodes
181 delete $AnyEvent::MP::Kernel::NODE{$self->{noderef}} 199 delete $AnyEvent::MP::Kernel::NODE{$self->{noderef}};
182 if $self->{transport}; 200 } else {
183
184 $self->SUPER::transport_reset; 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 snd $master, snd => $noderef, @_;
212 };
213 }
185} 214}
186 215
187sub connect { 216sub connect {
188 my ($self) = @_; 217 my ($self) = @_;
189 218
190 $self->transport_error (transport_error => $self->{noderef}, "unable to connect to indirect node"); 219 #TODO#
220# # ask for a connection, #TODO# rate-limit this somehow
221# snd $self->master, relay => $self->{noderef}, connect_node => $AnyEvent::MP::Kernel::NODE;
191} 222}
192 223
193package AnyEvent::MP::Node::Self; 224package AnyEvent::MP::Node::Self;
194 225
195use base "AnyEvent::MP::Node"; 226use base "AnyEvent::MP::Node";
215 delete $AnyEvent::MP::Kernel::PORT{$port}; 246 delete $AnyEvent::MP::Kernel::PORT{$port};
216 delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; 247 delete $AnyEvent::MP::Kernel::PORT_DATA{$port};
217 248
218 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} 249 my $mon = delete $AnyEvent::MP::Kernel::LMON{$port}
219 or !@reason 250 or !@reason
220 or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason"); 251 or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason");
221 252
222 $_->(@reason) for values %$mon; 253 $_->(@reason) for values %$mon;
223} 254}
224 255
225sub monitor { 256sub monitor {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines