… | |
… | |
188 | The type of data you can transfer depends on the transport protocol: when |
188 | The type of data you can transfer depends on the transport protocol: when |
189 | JSON is used, then only strings, numbers and arrays and hashes consisting |
189 | JSON is used, then only strings, numbers and arrays and hashes consisting |
190 | of those are allowed (no objects). When Storable is used, then anything |
190 | of those are allowed (no objects). When Storable is used, then anything |
191 | that Storable can serialise and deserialise is allowed, and for the local |
191 | that Storable can serialise and deserialise is allowed, and for the local |
192 | node, anything can be passed. |
192 | node, anything can be passed. |
193 | |
|
|
194 | =item kil $portid[, @reason] |
|
|
195 | |
|
|
196 | Kill the specified port with the given C<@reason>. |
|
|
197 | |
|
|
198 | If no C<@reason> is specified, then the port is killed "normally" (linked |
|
|
199 | ports will not be kileld, or even notified). |
|
|
200 | |
|
|
201 | Otherwise, linked ports get killed with the same reason (second form of |
|
|
202 | C<mon>, see below). |
|
|
203 | |
|
|
204 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
|
|
205 | will be reported as reason C<< die => $@ >>. |
|
|
206 | |
|
|
207 | Transport/communication errors are reported as C<< transport_error => |
|
|
208 | $message >>. |
|
|
209 | |
|
|
210 | =item $guard = mon $portid, $cb->(@reason) |
|
|
211 | |
|
|
212 | =item $guard = mon $portid, $otherport |
|
|
213 | |
|
|
214 | =item $guard = mon $portid, $otherport, @msg |
|
|
215 | |
|
|
216 | Monitor the given port and do something when the port is killed. |
|
|
217 | |
|
|
218 | In the first form, the callback is simply called with any number |
|
|
219 | of C<@reason> elements (no @reason means that the port was deleted |
|
|
220 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
|
|
221 | C<eval> if unsure. |
|
|
222 | |
|
|
223 | In the second form, the other port will be C<kil>'ed with C<@reason>, iff |
|
|
224 | a @reason was specified, i.e. on "normal" kils nothing happens, while |
|
|
225 | under all other conditions, the other port is killed with the same reason. |
|
|
226 | |
|
|
227 | In the last form, a message of the form C<@msg, @reason> will be C<snd>. |
|
|
228 | |
|
|
229 | Example: call a given callback when C<$port> is killed. |
|
|
230 | |
|
|
231 | mon $port, sub { warn "port died because of <@_>\n" }; |
|
|
232 | |
|
|
233 | Example: kill ourselves when C<$port> is killed abnormally. |
|
|
234 | |
|
|
235 | mon $port, $self; |
|
|
236 | |
|
|
237 | Example: send us a restart message another C<$port> is killed. |
|
|
238 | |
|
|
239 | mon $port, $self => "restart"; |
|
|
240 | |
|
|
241 | =cut |
|
|
242 | |
|
|
243 | sub mon { |
|
|
244 | my ($noderef, $port) = split /#/, shift, 2; |
|
|
245 | |
|
|
246 | my $node = $NODE{$noderef} || add_node $noderef; |
|
|
247 | |
|
|
248 | my $cb = shift; |
|
|
249 | |
|
|
250 | unless (ref $cb) { |
|
|
251 | if (@_) { |
|
|
252 | # send a kill info message |
|
|
253 | my (@msg) = ($cb, @_); |
|
|
254 | $cb = sub { snd @msg, @_ }; |
|
|
255 | } else { |
|
|
256 | # simply kill other port |
|
|
257 | my $port = $cb; |
|
|
258 | $cb = sub { kil $port, @_ if @_ }; |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
|
|
262 | $node->monitor ($port, $cb); |
|
|
263 | |
|
|
264 | defined wantarray |
|
|
265 | and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) } |
|
|
266 | } |
|
|
267 | |
|
|
268 | =item $guard = mon_guard $port, $ref, $ref... |
|
|
269 | |
|
|
270 | Monitors the given C<$port> and keeps the passed references. When the port |
|
|
271 | is killed, the references will be freed. |
|
|
272 | |
|
|
273 | Optionally returns a guard that will stop the monitoring. |
|
|
274 | |
|
|
275 | This function is useful when you create e.g. timers or other watchers and |
|
|
276 | want to free them when the port gets killed: |
|
|
277 | |
|
|
278 | $port->rcv (start => sub { |
|
|
279 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { |
|
|
280 | undef $timer if 0.9 < rand; |
|
|
281 | }); |
|
|
282 | }); |
|
|
283 | |
|
|
284 | =cut |
|
|
285 | |
|
|
286 | sub mon_guard { |
|
|
287 | my ($port, @refs) = @_; |
|
|
288 | |
|
|
289 | mon $port, sub { 0 && @refs } |
|
|
290 | } |
|
|
291 | |
|
|
292 | =item lnk $port1, $port2 |
|
|
293 | |
|
|
294 | Link two ports. This is simply a shorthand for: |
|
|
295 | |
|
|
296 | mon $port1, $port2; |
|
|
297 | mon $port2, $port1; |
|
|
298 | |
|
|
299 | It means that if either one is killed abnormally, the other one gets |
|
|
300 | killed as well. |
|
|
301 | |
193 | |
302 | =item $local_port = port |
194 | =item $local_port = port |
303 | |
195 | |
304 | Create a new local port object that can be used either as a pattern |
196 | Create a new local port object that can be used either as a pattern |
305 | matching port ("full port") or a single-callback port ("miniport"), |
197 | matching port ("full port") or a single-callback port ("miniport"), |
… | |
… | |
399 | |
291 | |
400 | =item rcv $portid, $smartmatch => $callback->(@msg), ... |
292 | =item rcv $portid, $smartmatch => $callback->(@msg), ... |
401 | |
293 | |
402 | =item rcv $portid, [$smartmatch...] => $callback->(@msg), ... |
294 | =item rcv $portid, [$smartmatch...] => $callback->(@msg), ... |
403 | |
295 | |
404 | Register callbacks to be called on matching messages on the given port. |
296 | Register callbacks to be called on matching messages on the given full |
|
|
297 | port (or newly created port). |
405 | |
298 | |
406 | The callback has to return a true value when its work is done, after |
299 | The callback has to return a true value when its work is done, after |
407 | which is will be removed, or a false value in which case it will stay |
300 | which is will be removed, or a false value in which case it will stay |
408 | registered. |
301 | registered. |
409 | |
302 | |
… | |
… | |
493 | $res |
386 | $res |
494 | } |
387 | } |
495 | } |
388 | } |
496 | } |
389 | } |
497 | |
390 | |
|
|
391 | =item $guard = mon $portid, $cb->(@reason) |
|
|
392 | |
|
|
393 | =item $guard = mon $portid, $otherport |
|
|
394 | |
|
|
395 | =item $guard = mon $portid, $otherport, @msg |
|
|
396 | |
|
|
397 | Monitor the given port and do something when the port is killed. |
|
|
398 | |
|
|
399 | In the first form, the callback is simply called with any number |
|
|
400 | of C<@reason> elements (no @reason means that the port was deleted |
|
|
401 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
|
|
402 | C<eval> if unsure. |
|
|
403 | |
|
|
404 | In the second form, the other port will be C<kil>'ed with C<@reason>, iff |
|
|
405 | a @reason was specified, i.e. on "normal" kils nothing happens, while |
|
|
406 | under all other conditions, the other port is killed with the same reason. |
|
|
407 | |
|
|
408 | In the last form, a message of the form C<@msg, @reason> will be C<snd>. |
|
|
409 | |
|
|
410 | Example: call a given callback when C<$port> is killed. |
|
|
411 | |
|
|
412 | mon $port, sub { warn "port died because of <@_>\n" }; |
|
|
413 | |
|
|
414 | Example: kill ourselves when C<$port> is killed abnormally. |
|
|
415 | |
|
|
416 | mon $port, $self; |
|
|
417 | |
|
|
418 | Example: send us a restart message another C<$port> is killed. |
|
|
419 | |
|
|
420 | mon $port, $self => "restart"; |
|
|
421 | |
|
|
422 | =cut |
|
|
423 | |
|
|
424 | sub mon { |
|
|
425 | my ($noderef, $port) = split /#/, shift, 2; |
|
|
426 | |
|
|
427 | my $node = $NODE{$noderef} || add_node $noderef; |
|
|
428 | |
|
|
429 | my $cb = shift; |
|
|
430 | |
|
|
431 | unless (ref $cb) { |
|
|
432 | if (@_) { |
|
|
433 | # send a kill info message |
|
|
434 | my (@msg) = ($cb, @_); |
|
|
435 | $cb = sub { snd @msg, @_ }; |
|
|
436 | } else { |
|
|
437 | # simply kill other port |
|
|
438 | my $port = $cb; |
|
|
439 | $cb = sub { kil $port, @_ if @_ }; |
|
|
440 | } |
|
|
441 | } |
|
|
442 | |
|
|
443 | $node->monitor ($port, $cb); |
|
|
444 | |
|
|
445 | defined wantarray |
|
|
446 | and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) } |
|
|
447 | } |
|
|
448 | |
|
|
449 | =item $guard = mon_guard $port, $ref, $ref... |
|
|
450 | |
|
|
451 | Monitors the given C<$port> and keeps the passed references. When the port |
|
|
452 | is killed, the references will be freed. |
|
|
453 | |
|
|
454 | Optionally returns a guard that will stop the monitoring. |
|
|
455 | |
|
|
456 | This function is useful when you create e.g. timers or other watchers and |
|
|
457 | want to free them when the port gets killed: |
|
|
458 | |
|
|
459 | $port->rcv (start => sub { |
|
|
460 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { |
|
|
461 | undef $timer if 0.9 < rand; |
|
|
462 | }); |
|
|
463 | }); |
|
|
464 | |
|
|
465 | =cut |
|
|
466 | |
|
|
467 | sub mon_guard { |
|
|
468 | my ($port, @refs) = @_; |
|
|
469 | |
|
|
470 | mon $port, sub { 0 && @refs } |
|
|
471 | } |
|
|
472 | |
|
|
473 | =item lnk $port1, $port2 |
|
|
474 | |
|
|
475 | Link two ports. This is simply a shorthand for: |
|
|
476 | |
|
|
477 | mon $port1, $port2; |
|
|
478 | mon $port2, $port1; |
|
|
479 | |
|
|
480 | It means that if either one is killed abnormally, the other one gets |
|
|
481 | killed as well. |
|
|
482 | |
|
|
483 | =item kil $portid[, @reason] |
|
|
484 | |
|
|
485 | Kill the specified port with the given C<@reason>. |
|
|
486 | |
|
|
487 | If no C<@reason> is specified, then the port is killed "normally" (linked |
|
|
488 | ports will not be kileld, or even notified). |
|
|
489 | |
|
|
490 | Otherwise, linked ports get killed with the same reason (second form of |
|
|
491 | C<mon>, see below). |
|
|
492 | |
|
|
493 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
|
|
494 | will be reported as reason C<< die => $@ >>. |
|
|
495 | |
|
|
496 | Transport/communication errors are reported as C<< transport_error => |
|
|
497 | $message >>. |
|
|
498 | |
498 | =back |
499 | =back |
499 | |
500 | |
500 | =head1 FUNCTIONS FOR NODES |
501 | =head1 FUNCTIONS FOR NODES |
501 | |
502 | |
502 | =over 4 |
503 | =over 4 |