… | |
… | |
25 | snd @msg_with_first_element_being_a_port; |
25 | snd @msg_with_first_element_being_a_port; |
26 | |
26 | |
27 | # creating/using ports, the simple way |
27 | # creating/using ports, the simple way |
28 | my $somple_port = port { my @msg = @_; 0 }; |
28 | my $somple_port = port { my @msg = @_; 0 }; |
29 | |
29 | |
30 | # creating/using ports, type matching |
30 | # creating/using ports, tagged message matching |
31 | my $port = port; |
31 | my $port = port; |
32 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
32 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
33 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
33 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
34 | |
34 | |
35 | # create a port on another node |
35 | # create a port on another node |
… | |
… | |
143 | kil $SELF, die => $msg; |
143 | kil $SELF, die => $msg; |
144 | } |
144 | } |
145 | |
145 | |
146 | =item $thisnode = NODE / $NODE |
146 | =item $thisnode = NODE / $NODE |
147 | |
147 | |
148 | The C<NODE> function returns, and the C<$NODE> variable contains |
148 | The C<NODE> function returns, and the C<$NODE> variable contains the |
149 | the noderef of the local node. The value is initialised by a call |
149 | noderef of the local node. The value is initialised by a call to |
150 | to C<become_public> or C<become_slave>, after which all local port |
150 | C<initialise_node>. |
151 | identifiers become invalid. |
|
|
152 | |
151 | |
153 | =item $noderef = node_of $port |
152 | =item $noderef = node_of $port |
154 | |
153 | |
155 | Extracts and returns the noderef from a portid or a noderef. |
154 | Extracts and returns the noderef from a port ID or a noderef. |
156 | |
155 | |
157 | =item initialise_node $noderef, $seednode, $seednode... |
156 | =item initialise_node $noderef, $seednode, $seednode... |
158 | |
157 | |
159 | =item initialise_node "slave/", $master, $master... |
158 | =item initialise_node "slave/", $master, $master... |
160 | |
159 | |
… | |
… | |
279 | =item snd $port, type => @data |
278 | =item snd $port, type => @data |
280 | |
279 | |
281 | =item snd $port, @msg |
280 | =item snd $port, @msg |
282 | |
281 | |
283 | Send the given message to the given port ID, which can identify either |
282 | Send the given message to the given port ID, which can identify either |
284 | a local or a remote port, and can be either a string or soemthignt hat |
283 | a local or a remote port, and must be a port ID. |
285 | stringifies a sa port ID (such as a port object :). |
|
|
286 | |
284 | |
287 | While the message can be about anything, it is highly recommended to use a |
285 | While the message can be about anything, it is highly recommended to use a |
288 | string as first element (a portid, or some word that indicates a request |
286 | string as first element (a port ID, or some word that indicates a request |
289 | type etc.). |
287 | type etc.). |
290 | |
288 | |
291 | The message data effectively becomes read-only after a call to this |
289 | The message data effectively becomes read-only after a call to this |
292 | function: modifying any argument is not allowed and can cause many |
290 | function: modifying any argument is not allowed and can cause many |
293 | problems. |
291 | problems. |
… | |
… | |
738 | convenience functionality. |
736 | convenience functionality. |
739 | |
737 | |
740 | This means that AEMP requires a less tightly controlled environment at the |
738 | This means that AEMP requires a less tightly controlled environment at the |
741 | cost of longer node references and a slightly higher management overhead. |
739 | cost of longer node references and a slightly higher management overhead. |
742 | |
740 | |
|
|
741 | =item Erlang has a "remote ports are like local ports" philosophy, AEMP |
|
|
742 | uses "local ports are like remote ports". |
|
|
743 | |
|
|
744 | The failure modes for local ports are quite different (runtime errors |
|
|
745 | only) then for remote ports - when a local port dies, you I<know> it dies, |
|
|
746 | when a connection to another node dies, you know nothing about the other |
|
|
747 | port. |
|
|
748 | |
|
|
749 | Erlang pretends remote ports are as reliable as local ports, even when |
|
|
750 | they are not. |
|
|
751 | |
|
|
752 | AEMP encourages a "treat remote ports differently" philosophy, with local |
|
|
753 | ports being the special case/exception, where transport errors cannot |
|
|
754 | occur. |
|
|
755 | |
743 | =item * Erlang uses processes and a mailbox, AEMP does not queue. |
756 | =item * Erlang uses processes and a mailbox, AEMP does not queue. |
744 | |
757 | |
745 | Erlang uses processes that selctively receive messages, and therefore |
758 | Erlang uses processes that selectively receive messages, and therefore |
746 | needs a queue. AEMP is event based, queuing messages would serve no useful |
759 | needs a queue. AEMP is event based, queuing messages would serve no |
747 | purpose. |
760 | useful purpose. For the same reason the pattern-matching abilities of |
|
|
761 | AnyEvent::MP are more limited, as there is little need to be able to |
|
|
762 | filter messages without dequeing them. |
748 | |
763 | |
749 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
764 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
750 | |
765 | |
751 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
766 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
752 | |
767 | |
753 | Sending messages in Erlang is synchronous and blocks the process. AEMP |
768 | Sending messages in Erlang is synchronous and blocks the process (and |
754 | sends are immediate, connection establishment is handled in the |
769 | so does not need a queue that can overflow). AEMP sends are immediate, |
755 | background. |
770 | connection establishment is handled in the background. |
756 | |
771 | |
757 | =item * Erlang can silently lose messages, AEMP cannot. |
772 | =item * Erlang suffers from silent message loss, AEMP does not. |
758 | |
773 | |
759 | Erlang makes few guarantees on messages delivery - messages can get lost |
774 | Erlang makes few guarantees on messages delivery - messages can get lost |
760 | without any of the processes realising it (i.e. you send messages a, b, |
775 | without any of the processes realising it (i.e. you send messages a, b, |
761 | and c, and the other side only receives messages a and c). |
776 | and c, and the other side only receives messages a and c). |
762 | |
777 | |
… | |
… | |
774 | eventually be killed - it cannot happen that a node detects a port as dead |
789 | eventually be killed - it cannot happen that a node detects a port as dead |
775 | and then later sends messages to it, finding it is still alive. |
790 | and then later sends messages to it, finding it is still alive. |
776 | |
791 | |
777 | =item * Erlang can send messages to the wrong port, AEMP does not. |
792 | =item * Erlang can send messages to the wrong port, AEMP does not. |
778 | |
793 | |
779 | In Erlang it is quite possible that a node that restarts reuses a process |
794 | In Erlang it is quite likely that a node that restarts reuses a process ID |
780 | ID known to other nodes for a completely different process, causing |
795 | known to other nodes for a completely different process, causing messages |
781 | messages destined for that process to end up in an unrelated process. |
796 | destined for that process to end up in an unrelated process. |
782 | |
797 | |
783 | AEMP never reuses port IDs, so old messages or old port IDs floating |
798 | AEMP never reuses port IDs, so old messages or old port IDs floating |
784 | around in the network will not be sent to an unrelated port. |
799 | around in the network will not be sent to an unrelated port. |
785 | |
800 | |
786 | =item * Erlang uses unprotected connections, AEMP uses secure |
801 | =item * Erlang uses unprotected connections, AEMP uses secure |