… | |
… | |
22 | snd $port2, ping => $port1; |
22 | snd $port2, ping => $port1; |
23 | |
23 | |
24 | # more, smarter, matches (_any_ is exported by this module) |
24 | # more, smarter, matches (_any_ is exported by this module) |
25 | rcv $port, [child_died => $pid] => sub { ... |
25 | rcv $port, [child_died => $pid] => sub { ... |
26 | rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 |
26 | rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 |
|
|
27 | |
|
|
28 | # monitoring |
|
|
29 | mon $port, $cb->(@msg) # callback is invoked on death |
|
|
30 | mon $port, $otherport # kill otherport on abnormal death |
|
|
31 | mon $port, $otherport, @msg # send message on death |
27 | |
32 | |
28 | =head1 DESCRIPTION |
33 | =head1 DESCRIPTION |
29 | |
34 | |
30 | This module (-family) implements a simple message passing framework. |
35 | This module (-family) implements a simple message passing framework. |
31 | |
36 | |
… | |
… | |
297 | $port |
302 | $port |
298 | } |
303 | } |
299 | |
304 | |
300 | =item reg $port, $name |
305 | =item reg $port, $name |
301 | |
306 | |
302 | Registers the given port under the name C<$name>. If the name already |
307 | =item reg $name |
303 | exists it is replaced. |
308 | |
|
|
309 | Registers the given port (or C<$SELF><<< if missing) under the name |
|
|
310 | C<$name>. If the name already exists it is replaced. |
304 | |
311 | |
305 | A port can only be registered under one well known name. |
312 | A port can only be registered under one well known name. |
306 | |
313 | |
307 | A port automatically becomes unregistered when it is killed. |
314 | A port automatically becomes unregistered when it is killed. |
308 | |
315 | |
309 | =cut |
316 | =cut |
310 | |
317 | |
311 | sub reg(@) { |
318 | sub reg(@) { |
312 | my ($port, $name) = @_; |
319 | my $port = @_ > 1 ? shift : $SELF || Carp::croak 'reg: called with one argument only, but $SELF not set,'; |
313 | |
320 | |
314 | $REG{$name} = $port; |
321 | $REG{$_[0]} = $port; |
315 | } |
322 | } |
316 | |
323 | |
317 | =item rcv $port, $callback->(@msg) |
324 | =item rcv $port, $callback->(@msg) |
318 | |
325 | |
319 | Replaces the callback on the specified miniport (after converting it to |
326 | Replaces the callback on the specified miniport (after converting it to |
… | |
… | |
324 | =item rcv $port, $smartmatch => $callback->(@msg), ... |
331 | =item rcv $port, $smartmatch => $callback->(@msg), ... |
325 | |
332 | |
326 | =item rcv $port, [$smartmatch...] => $callback->(@msg), ... |
333 | =item rcv $port, [$smartmatch...] => $callback->(@msg), ... |
327 | |
334 | |
328 | Register callbacks to be called on matching messages on the given full |
335 | Register callbacks to be called on matching messages on the given full |
329 | port (after converting it to one if required). |
336 | port (after converting it to one if required) and return the port. |
330 | |
337 | |
331 | The callback has to return a true value when its work is done, after |
338 | The callback has to return a true value when its work is done, after |
332 | which is will be removed, or a false value in which case it will stay |
339 | which is will be removed, or a false value in which case it will stay |
333 | registered. |
340 | registered. |
334 | |
341 | |
… | |
… | |
346 | exported by this module) matches any single element of the message. |
353 | exported by this module) matches any single element of the message. |
347 | |
354 | |
348 | While not required, it is highly recommended that the first matching |
355 | While not required, it is highly recommended that the first matching |
349 | element is a string identifying the message. The one-string-only match is |
356 | element is a string identifying the message. The one-string-only match is |
350 | also the most efficient match (by far). |
357 | also the most efficient match (by far). |
|
|
358 | |
|
|
359 | Example: create a port and bind receivers on it in one go. |
|
|
360 | |
|
|
361 | my $port = rcv port, |
|
|
362 | msg1 => sub { ...; 0 }, |
|
|
363 | msg2 => sub { ...; 0 }, |
|
|
364 | ; |
|
|
365 | |
|
|
366 | Example: create a port, bind receivers and send it in a message elsewhere |
|
|
367 | in one go: |
|
|
368 | |
|
|
369 | snd $otherport, reply => |
|
|
370 | rcv port, |
|
|
371 | msg1 => sub { ...; 0 }, |
|
|
372 | ... |
|
|
373 | ; |
351 | |
374 | |
352 | =cut |
375 | =cut |
353 | |
376 | |
354 | sub rcv($@) { |
377 | sub rcv($@) { |
355 | my $port = shift; |
378 | my $port = shift; |
… | |
… | |
462 | } |
485 | } |
463 | } |
486 | } |
464 | |
487 | |
465 | =item $guard = mon $port, $cb->(@reason) |
488 | =item $guard = mon $port, $cb->(@reason) |
466 | |
489 | |
467 | =item $guard = mon $port, $otherport |
490 | =item $guard = mon $port, $rcvport |
468 | |
491 | |
|
|
492 | =item $guard = mon $port |
|
|
493 | |
469 | =item $guard = mon $port, $otherport, @msg |
494 | =item $guard = mon $port, $rcvport, @msg |
470 | |
495 | |
471 | Monitor the given port and do something when the port is killed. |
496 | Monitor the given port and do something when the port is killed, and |
|
|
497 | optionally return a guard that can be used to stop monitoring again. |
472 | |
498 | |
473 | In the first form, the callback is simply called with any number |
499 | In the first form (callback), the callback is simply called with any |
474 | of C<@reason> elements (no @reason means that the port was deleted |
500 | number of C<@reason> elements (no @reason means that the port was deleted |
475 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
501 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
476 | C<eval> if unsure. |
502 | C<eval> if unsure. |
477 | |
503 | |
478 | In the second form, the other port will be C<kil>'ed with C<@reason>, iff |
504 | In the second form (another port given), the other port (C<$rcvport) |
479 | a @reason was specified, i.e. on "normal" kils nothing happens, while |
505 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
480 | under all other conditions, the other port is killed with the same reason. |
506 | "normal" kils nothing happens, while under all other conditions, the other |
|
|
507 | port is killed with the same reason. |
481 | |
508 | |
|
|
509 | The third form (kill self) is the same as the second form, except that |
|
|
510 | C<$rvport> defaults to C<$SELF>. |
|
|
511 | |
482 | In the last form, a message of the form C<@msg, @reason> will be C<snd>. |
512 | In the last form (message), a message of the form C<@msg, @reason> will be |
|
|
513 | C<snd>. |
|
|
514 | |
|
|
515 | As a rule of thumb, monitoring requests should always monitor a port from |
|
|
516 | a local port (or callback). The reason is that kill messages might get |
|
|
517 | lost, just like any other message. Another less obvious reason is that |
|
|
518 | even monitoring requests can get lost (for exmaple, when the connection |
|
|
519 | to the other node goes down permanently). When monitoring a port locally |
|
|
520 | these problems do not exist. |
483 | |
521 | |
484 | Example: call a given callback when C<$port> is killed. |
522 | Example: call a given callback when C<$port> is killed. |
485 | |
523 | |
486 | mon $port, sub { warn "port died because of <@_>\n" }; |
524 | mon $port, sub { warn "port died because of <@_>\n" }; |
487 | |
525 | |
488 | Example: kill ourselves when C<$port> is killed abnormally. |
526 | Example: kill ourselves when C<$port> is killed abnormally. |
489 | |
527 | |
490 | mon $port, $self; |
528 | mon $port; |
491 | |
529 | |
492 | Example: send us a restart message another C<$port> is killed. |
530 | Example: send us a restart message when another C<$port> is killed. |
493 | |
531 | |
494 | mon $port, $self => "restart"; |
532 | mon $port, $self => "restart"; |
495 | |
533 | |
496 | =cut |
534 | =cut |
497 | |
535 | |
498 | sub mon { |
536 | sub mon { |
499 | my ($noderef, $port) = split /#/, shift, 2; |
537 | my ($noderef, $port) = split /#/, shift, 2; |
500 | |
538 | |
501 | my $node = $NODE{$noderef} || add_node $noderef; |
539 | my $node = $NODE{$noderef} || add_node $noderef; |
502 | |
540 | |
503 | my $cb = shift; |
541 | my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
504 | |
542 | |
505 | unless (ref $cb) { |
543 | unless (ref $cb) { |
506 | if (@_) { |
544 | if (@_) { |
507 | # send a kill info message |
545 | # send a kill info message |
508 | my (@msg) = ($cb, @_); |
546 | my (@msg) = @_; |
509 | $cb = sub { snd @msg, @_ }; |
547 | $cb = sub { snd @msg, @_ }; |
510 | } else { |
548 | } else { |
511 | # simply kill other port |
549 | # simply kill other port |
512 | my $port = $cb; |
550 | my $port = $cb; |
513 | $cb = sub { kil $port, @_ if @_ }; |
551 | $cb = sub { kil $port, @_ if @_ }; |
… | |
… | |
539 | =cut |
577 | =cut |
540 | |
578 | |
541 | sub mon_guard { |
579 | sub mon_guard { |
542 | my ($port, @refs) = @_; |
580 | my ($port, @refs) = @_; |
543 | |
581 | |
|
|
582 | #TODO: mon-less form? |
|
|
583 | |
544 | mon $port, sub { 0 && @refs } |
584 | mon $port, sub { 0 && @refs } |
545 | } |
585 | } |
546 | |
|
|
547 | =item lnk $port1, $port2 |
|
|
548 | |
|
|
549 | Link two ports. This is simply a shorthand for: |
|
|
550 | |
|
|
551 | mon $port1, $port2; |
|
|
552 | mon $port2, $port1; |
|
|
553 | |
|
|
554 | It means that if either one is killed abnormally, the other one gets |
|
|
555 | killed as well. |
|
|
556 | |
586 | |
557 | =item kil $port[, @reason] |
587 | =item kil $port[, @reason] |
558 | |
588 | |
559 | Kill the specified port with the given C<@reason>. |
589 | Kill the specified port with the given C<@reason>. |
560 | |
590 | |
… | |
… | |
618 | |
648 | |
619 | =back |
649 | =back |
620 | |
650 | |
621 | =head1 AnyEvent::MP vs. Distributed Erlang |
651 | =head1 AnyEvent::MP vs. Distributed Erlang |
622 | |
652 | |
623 | AnyEvent::MP got lots of its ideas from distributed erlang (erlang node |
653 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
624 | == aemp node, erlang process == aemp port), so many of the documents and |
654 | == aemp node, Erlang process == aemp port), so many of the documents and |
625 | programming techniques employed by erlang apply to AnyEvent::MP. Here is a |
655 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |
626 | sample: |
656 | sample: |
627 | |
657 | |
628 | http://www.erlang.se/doc/programming_rules.shtml |
658 | http://www.Erlang.se/doc/programming_rules.shtml |
629 | http://erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4 |
659 | http://Erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4 |
630 | http://erlang.org/download/erlang-book-part1.pdf # chapters 5 and 6 |
660 | http://Erlang.org/download/Erlang-book-part1.pdf # chapters 5 and 6 |
631 | http://erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5 |
661 | http://Erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5 |
632 | |
662 | |
633 | Despite the similarities, there are also some important differences: |
663 | Despite the similarities, there are also some important differences: |
634 | |
664 | |
635 | =over 4 |
665 | =over 4 |
636 | |
666 | |
… | |
… | |
647 | |
677 | |
648 | Erlang uses processes that selctively receive messages, and therefore |
678 | Erlang uses processes that selctively receive messages, and therefore |
649 | needs a queue. AEMP is event based, queuing messages would serve no useful |
679 | needs a queue. AEMP is event based, queuing messages would serve no useful |
650 | purpose. |
680 | purpose. |
651 | |
681 | |
652 | (But see L<Coro::MP> for a more erlang-like process model on top of AEMP). |
682 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
653 | |
683 | |
654 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
684 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
655 | |
685 | |
656 | Sending messages in erlang is synchronous and blocks the process. AEMP |
686 | Sending messages in Erlang is synchronous and blocks the process. AEMP |
657 | sends are immediate, connection establishment is handled in the |
687 | sends are immediate, connection establishment is handled in the |
658 | background. |
688 | background. |
659 | |
689 | |
660 | =item * Erlang can silently lose messages, AEMP cannot. |
690 | =item * Erlang can silently lose messages, AEMP cannot. |
661 | |
691 | |
… | |
… | |
664 | and c, and the other side only receives messages a and c). |
694 | and c, and the other side only receives messages a and c). |
665 | |
695 | |
666 | AEMP guarantees correct ordering, and the guarantee that there are no |
696 | AEMP guarantees correct ordering, and the guarantee that there are no |
667 | holes in the message sequence. |
697 | holes in the message sequence. |
668 | |
698 | |
669 | =item * In erlang, processes can be declared dead and later be found to be |
699 | =item * In Erlang, processes can be declared dead and later be found to be |
670 | alive. |
700 | alive. |
671 | |
701 | |
672 | In erlang it can happen that a monitored process is declared dead and |
702 | In Erlang it can happen that a monitored process is declared dead and |
673 | linked processes get killed, but later it turns out that the process is |
703 | linked processes get killed, but later it turns out that the process is |
674 | still alive - and can receive messages. |
704 | still alive - and can receive messages. |
675 | |
705 | |
676 | In AEMP, when port monitoring detects a port as dead, then that port will |
706 | In AEMP, when port monitoring detects a port as dead, then that port will |
677 | eventually be killed - it cannot happen that a node detects a port as dead |
707 | eventually be killed - it cannot happen that a node detects a port as dead |
678 | and then later sends messages to it, finding it is still alive. |
708 | and then later sends messages to it, finding it is still alive. |
679 | |
709 | |
680 | =item * Erlang can send messages to the wrong port, AEMP does not. |
710 | =item * Erlang can send messages to the wrong port, AEMP does not. |
681 | |
711 | |
682 | In erlang it is quite possible that a node that restarts reuses a process |
712 | In Erlang it is quite possible that a node that restarts reuses a process |
683 | ID known to other nodes for a completely different process, causing |
713 | ID known to other nodes for a completely different process, causing |
684 | messages destined for that process to end up in an unrelated process. |
714 | messages destined for that process to end up in an unrelated process. |
685 | |
715 | |
686 | AEMP never reuses port IDs, so old messages or old port IDs floating |
716 | AEMP never reuses port IDs, so old messages or old port IDs floating |
687 | around in the network will not be sent to an unrelated port. |
717 | around in the network will not be sent to an unrelated port. |
… | |
… | |
693 | securely authenticate nodes. |
723 | securely authenticate nodes. |
694 | |
724 | |
695 | =item * The AEMP protocol is optimised for both text-based and binary |
725 | =item * The AEMP protocol is optimised for both text-based and binary |
696 | communications. |
726 | communications. |
697 | |
727 | |
698 | The AEMP protocol, unlike the erlang protocol, supports both |
728 | The AEMP protocol, unlike the Erlang protocol, supports both |
699 | language-independent text-only protocols (good for debugging) and binary, |
729 | language-independent text-only protocols (good for debugging) and binary, |
700 | language-specific serialisers (e.g. Storable). |
730 | language-specific serialisers (e.g. Storable). |
701 | |
731 | |
702 | It has also been carefully designed to be implementable in other languages |
732 | It has also been carefully designed to be implementable in other languages |
703 | with a minimum of work while gracefully degrading fucntionality to make the |
733 | with a minimum of work while gracefully degrading fucntionality to make the |
704 | protocol simple. |
734 | protocol simple. |
705 | |
735 | |
|
|
736 | =item * AEMP has more flexible monitoring options than Erlang. |
|
|
737 | |
|
|
738 | In Erlang, you can chose to receive I<all> exit signals as messages |
|
|
739 | or I<none>, there is no in-between, so monitoring single processes is |
|
|
740 | difficult to implement. Monitoring in AEMP is more flexible than in |
|
|
741 | Erlang, as one can choose between automatic kill, exit message or callback |
|
|
742 | on a per-process basis. |
|
|
743 | |
|
|
744 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
|
|
745 | |
|
|
746 | Monitoring in Erlang is not an indicator of process death/crashes, |
|
|
747 | as linking is (except linking is unreliable in Erlang). |
|
|
748 | |
|
|
749 | In AEMP, you don't "look up" registered port names or send to named ports |
|
|
750 | that might or might not be persistent. Instead, you normally spawn a port |
|
|
751 | on the remote node. The init function monitors the you, and you monitor |
|
|
752 | the remote port. Since both monitors are local to the node, they are much |
|
|
753 | more reliable. |
|
|
754 | |
|
|
755 | This also saves round-trips and avoids sending messages to the wrong port |
|
|
756 | (hard to do in Erlang). |
|
|
757 | |
706 | =back |
758 | =back |
707 | |
759 | |
708 | =head1 SEE ALSO |
760 | =head1 SEE ALSO |
709 | |
761 | |
710 | L<AnyEvent>. |
762 | L<AnyEvent>. |