… | |
… | |
28 | |
28 | |
29 | Despite its simplicity, you can securely message other processes running |
29 | Despite its simplicity, you can securely message other processes running |
30 | on the same or other hosts. |
30 | on the same or other hosts. |
31 | |
31 | |
32 | At the moment, this module family is severly brokena nd underdocumented, |
32 | At the moment, this module family is severly brokena nd underdocumented, |
33 | so do not use. This was uploaded mainly to resreve the CPAN namespace - |
33 | so do not use. This was uploaded mainly to reserve the CPAN namespace - |
34 | stay tuned! |
34 | stay tuned! |
35 | |
35 | |
36 | =head1 CONCEPTS |
36 | =head1 CONCEPTS |
37 | |
37 | |
38 | =over 4 |
38 | =over 4 |
… | |
… | |
84 | |
84 | |
85 | use base "Exporter"; |
85 | use base "Exporter"; |
86 | |
86 | |
87 | our $VERSION = '0.02'; |
87 | our $VERSION = '0.02'; |
88 | our @EXPORT = qw( |
88 | our @EXPORT = qw( |
89 | NODE $NODE $PORT snd rcv mon del _any_ |
89 | NODE $NODE $PORT snd rcv mon kil _any_ |
90 | create_port create_port_on |
90 | create_port create_port_on |
91 | create_miniport |
91 | miniport |
92 | become_slave become_public |
92 | become_slave become_public |
93 | ); |
93 | ); |
94 | |
94 | |
95 | =item NODE / $NODE |
95 | =item NODE / $NODE |
96 | |
96 | |
… | |
… | |
120 | that Storable can serialise and deserialise is allowed, and for the local |
120 | that Storable can serialise and deserialise is allowed, and for the local |
121 | node, anything can be passed. |
121 | node, anything can be passed. |
122 | |
122 | |
123 | =item $guard = mon $portid, $cb->() |
123 | =item $guard = mon $portid, $cb->() |
124 | |
124 | |
|
|
125 | =item $guard = mon $portid, $otherport |
|
|
126 | |
|
|
127 | =item $guard = mon $portid, $otherport, @msg |
|
|
128 | |
125 | Monitor the given port and call the given callback when the port is |
129 | Monitor the given port and call the given callback when the port is |
126 | destroyed or connection to it's node is lost. |
130 | destroyed or connection to it's node is lost. |
127 | |
131 | |
128 | #TODO |
132 | #TODO |
129 | |
133 | |
130 | =cut |
134 | =cut |
131 | |
135 | |
132 | sub mon { |
136 | sub mon { |
133 | my ($noderef, $port) = split /#/, shift, 2; |
137 | my ($noderef, $port, $cb) = ((split /#/, shift, 2), shift); |
134 | |
138 | |
135 | my $node = AnyEvent::MP::Base::add_node $noderef; |
139 | my $node = AnyEvent::MP::Base::add_node $noderef; |
136 | |
140 | |
137 | my $cb = shift; |
141 | #TODO: ports must not be references |
|
|
142 | if (!ref $cb or "AnyEvent::MP::Port" eq ref $cb) { |
|
|
143 | if (@_) { |
|
|
144 | # send a kill info message |
|
|
145 | my (@msg) = ($cb, @_); |
|
|
146 | $cb = sub { snd @msg, @_ }; |
|
|
147 | } else { |
|
|
148 | # simply kill other port |
|
|
149 | my $port = $cb; |
|
|
150 | $cb = sub { kil $port, @_ }; |
|
|
151 | } |
|
|
152 | } |
138 | |
153 | |
139 | $node->monitor ($port, $cb); |
154 | $node->monitor ($port, $cb); |
140 | |
155 | |
141 | defined wantarray |
156 | defined wantarray |
142 | and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) } |
157 | and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) } |
143 | } |
158 | } |
144 | |
159 | |
|
|
160 | =item $guard = mon_guard $port, $ref, $ref... |
|
|
161 | |
|
|
162 | Monitors the given C<$port> and keeps the passed references. When the port |
|
|
163 | is killed, the references will be freed. |
|
|
164 | |
|
|
165 | Optionally returns a guard that will stop the monitoring. |
|
|
166 | |
|
|
167 | This function is useful when you create e.g. timers or other watchers and |
|
|
168 | want to free them when the port gets killed: |
|
|
169 | |
|
|
170 | $port->rcv (start => sub { |
|
|
171 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { |
|
|
172 | undef $timer if 0.9 < rand; |
|
|
173 | }); |
|
|
174 | }); |
|
|
175 | |
|
|
176 | =cut |
|
|
177 | |
|
|
178 | sub mon_guard { |
|
|
179 | my ($port, @refs) = @_; |
|
|
180 | |
|
|
181 | mon $port, sub { 0 && @refs } |
|
|
182 | } |
|
|
183 | |
145 | =item $local_port = create_port |
184 | =item $local_port = create_port |
146 | |
185 | |
147 | Create a new local port object. See the next section for allowed methods. |
186 | Create a new local port object. See the next section for allowed methods. |
148 | |
187 | |
149 | =cut |
188 | =cut |
… | |
… | |
151 | sub create_port { |
190 | sub create_port { |
152 | my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; |
191 | my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; |
153 | |
192 | |
154 | my $self = bless { |
193 | my $self = bless { |
155 | id => "$NODE#$id", |
194 | id => "$NODE#$id", |
156 | names => [$id], |
|
|
157 | }, "AnyEvent::MP::Port"; |
195 | }, "AnyEvent::MP::Port"; |
158 | |
196 | |
159 | $AnyEvent::MP::Base::PORT{$id} = sub { |
197 | $AnyEvent::MP::Base::PORT{$id} = sub { |
160 | unshift @_, $self; |
198 | unshift @_, $self; |
161 | |
199 | |
… | |
… | |
204 | my $cb = shift; |
242 | my $cb = shift; |
205 | my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; |
243 | my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; |
206 | |
244 | |
207 | $AnyEvent::MP::Base::PORT{$id} = sub { |
245 | $AnyEvent::MP::Base::PORT{$id} = sub { |
208 | &$cb |
246 | &$cb |
209 | and del $id; |
247 | and kil $id; |
210 | }; |
248 | }; |
211 | |
249 | |
212 | "$NODE#$id" |
250 | "$NODE#$id" |
213 | } |
251 | } |
214 | |
252 | |
… | |
… | |
291 | |
329 | |
292 | =item $port->destroy |
330 | =item $port->destroy |
293 | |
331 | |
294 | Explicitly destroy/remove/nuke/vaporise the port. |
332 | Explicitly destroy/remove/nuke/vaporise the port. |
295 | |
333 | |
296 | Ports are normally kept alive by there mere existance alone, and need to |
334 | Ports are normally kept alive by their mere existance alone, and need to |
297 | be destroyed explicitly. |
335 | be destroyed explicitly. |
298 | |
336 | |
299 | =cut |
337 | =cut |
300 | |
338 | |
301 | sub destroy { |
339 | sub destroy { |
302 | my ($self) = @_; |
340 | my ($self) = @_; |
303 | |
341 | |
304 | AnyEvent::MP::Base::del $self->{id}; |
|
|
305 | |
|
|
306 | delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; |
342 | delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; |
307 | |
343 | |
308 | delete $AnyEvent::MP::Base::PORT{$_} |
344 | AnyEvent::MP::Base::kil $self->{id}; |
309 | for @{ $self->{names} }; |
|
|
310 | } |
345 | } |
311 | |
346 | |
312 | =back |
347 | =back |
313 | |
348 | |
314 | =head1 FUNCTIONS FOR NODES |
349 | =head1 FUNCTIONS FOR NODES |