… | |
… | |
23 | snd $port, type => data...; |
23 | snd $port, type => data...; |
24 | snd $port, @msg; |
24 | snd $port, @msg; |
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 $simple_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 |
… | |
… | |
69 | |
69 | |
70 | =item port |
70 | =item port |
71 | |
71 | |
72 | A port is something you can send messages to (with the C<snd> function). |
72 | A port is something you can send messages to (with the C<snd> function). |
73 | |
73 | |
74 | Some ports allow you to register C<rcv> handlers that can match specific |
74 | Ports allow you to register C<rcv> handlers that can match all or just |
75 | messages. All C<rcv> handlers will receive messages they match, messages |
75 | some messages. Messages will not be queued. |
76 | will not be queued. |
|
|
77 | |
76 | |
78 | =item port id - C<noderef#portname> |
77 | =item port id - C<noderef#portname> |
79 | |
78 | |
80 | A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as |
79 | A port ID is the concatenation of a noderef, a hash-mark (C<#>) as |
81 | separator, and a port name (a printable string of unspecified format). An |
80 | separator, and a port name (a printable string of unspecified format). An |
82 | exception is the the node port, whose ID is identical to its node |
81 | exception is the the node port, whose ID is identical to its node |
83 | reference. |
82 | reference. |
84 | |
83 | |
85 | =item node |
84 | =item node |
86 | |
85 | |
87 | A node is a single process containing at least one port - the node |
86 | A node is a single process containing at least one port - the node port, |
88 | port. You can send messages to node ports to find existing ports or to |
87 | which provides nodes to manage each other remotely, and to create new |
89 | create new ports, among other things. |
88 | ports. |
90 | |
89 | |
91 | Nodes are either private (single-process only), slaves (connected to a |
90 | Nodes are either private (single-process only), slaves (connected to a |
92 | master node only) or public nodes (connectable from unrelated nodes). |
91 | master node only) or public nodes (connectable from unrelated nodes). |
93 | |
92 | |
94 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
93 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
… | |
… | |
143 | kil $SELF, die => $msg; |
142 | kil $SELF, die => $msg; |
144 | } |
143 | } |
145 | |
144 | |
146 | =item $thisnode = NODE / $NODE |
145 | =item $thisnode = NODE / $NODE |
147 | |
146 | |
148 | The C<NODE> function returns, and the C<$NODE> variable contains |
147 | 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 |
148 | 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 |
149 | C<initialise_node>. |
151 | identifiers become invalid. |
|
|
152 | |
150 | |
153 | =item $noderef = node_of $port |
151 | =item $noderef = node_of $port |
154 | |
152 | |
155 | Extracts and returns the noderef from a portid or a noderef. |
153 | Extracts and returns the noderef from a port ID or a noderef. |
156 | |
154 | |
157 | =item initialise_node $noderef, $seednode, $seednode... |
155 | =item initialise_node $noderef, $seednode, $seednode... |
158 | |
156 | |
159 | =item initialise_node "slave/", $master, $master... |
157 | =item initialise_node "slave/", $master, $master... |
160 | |
158 | |
… | |
… | |
199 | At least one additional noderef is required (either by specifying it |
197 | At least one additional noderef is required (either by specifying it |
200 | directly or because it is part of the configuration profile): The node |
198 | directly or because it is part of the configuration profile): The node |
201 | will try to connect to all of them and will become a slave attached to the |
199 | will try to connect to all of them and will become a slave attached to the |
202 | first node it can successfully connect to. |
200 | first node it can successfully connect to. |
203 | |
201 | |
|
|
202 | Note that slave nodes cannot change their name, and consequently, their |
|
|
203 | master, so if the master goes down, the slave node will not function well |
|
|
204 | anymore until it can re-establish conenciton to its master. This makes |
|
|
205 | slave nodes unsuitable for long-term nodes or fault-tolerant networks. |
|
|
206 | |
204 | =back |
207 | =back |
205 | |
208 | |
206 | This function will block until all nodes have been resolved and, for slave |
209 | This function will block until all nodes have been resolved and, for slave |
207 | nodes, until it has successfully established a connection to a master |
210 | nodes, until it has successfully established a connection to a master |
208 | server. |
211 | server. |
209 | |
212 | |
|
|
213 | All the seednodes will also be specially marked to automatically retry |
|
|
214 | connecting to them infinitely. |
|
|
215 | |
210 | Example: become a public node listening on the guessed noderef, or the one |
216 | Example: become a public node listening on the guessed noderef, or the one |
211 | specified via C<aemp> for the current node. This should be the most common |
217 | specified via C<aemp> for the current node. This should be the most common |
212 | form of invocation for "daemon"-type nodes. |
218 | form of invocation for "daemon"-type nodes. |
213 | |
219 | |
214 | initialise_node; |
220 | initialise_node; |
… | |
… | |
279 | =item snd $port, type => @data |
285 | =item snd $port, type => @data |
280 | |
286 | |
281 | =item snd $port, @msg |
287 | =item snd $port, @msg |
282 | |
288 | |
283 | Send the given message to the given port ID, which can identify either |
289 | 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 |
290 | a local or a remote port, and must be a port ID. |
285 | stringifies a sa port ID (such as a port object :). |
|
|
286 | |
291 | |
287 | While the message can be about anything, it is highly recommended to use a |
292 | 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 |
293 | string as first element (a port ID, or some word that indicates a request |
289 | type etc.). |
294 | type etc.). |
290 | |
295 | |
291 | The message data effectively becomes read-only after a call to this |
296 | The message data effectively becomes read-only after a call to this |
292 | function: modifying any argument is not allowed and can cause many |
297 | function: modifying any argument is not allowed and can cause many |
293 | problems. |
298 | problems. |
… | |
… | |
351 | The default callback received all messages not matched by a more specific |
356 | The default callback received all messages not matched by a more specific |
352 | C<tag> match. |
357 | C<tag> match. |
353 | |
358 | |
354 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
359 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
355 | |
360 | |
356 | Register callbacks to be called on messages starting with the given tag on |
361 | Register (or replace) callbacks to be called on messages starting with the |
357 | the given port (and return the port), or unregister it (when C<$callback> |
362 | given tag on the given port (and return the port), or unregister it (when |
358 | is C<$undef>). |
363 | C<$callback> is C<$undef> or missing). There can only be one callback |
|
|
364 | registered for each tag. |
359 | |
365 | |
360 | The original message will be passed to the callback, after the first |
366 | The original message will be passed to the callback, after the first |
361 | element (the tag) has been removed. The callback will use the same |
367 | element (the tag) has been removed. The callback will use the same |
362 | environment as the default callback (see above). |
368 | environment as the default callback (see above). |
363 | |
369 | |
… | |
… | |
375 | rcv port, |
381 | rcv port, |
376 | msg1 => sub { ... }, |
382 | msg1 => sub { ... }, |
377 | ... |
383 | ... |
378 | ; |
384 | ; |
379 | |
385 | |
|
|
386 | Example: temporarily register a rcv callback for a tag matching some port |
|
|
387 | (e.g. for a rpc reply) and unregister it after a message was received. |
|
|
388 | |
|
|
389 | rcv $port, $otherport => sub { |
|
|
390 | my @reply = @_; |
|
|
391 | |
|
|
392 | rcv $SELF, $otherport; |
|
|
393 | }; |
|
|
394 | |
380 | =cut |
395 | =cut |
381 | |
396 | |
382 | sub rcv($@) { |
397 | sub rcv($@) { |
383 | my $port = shift; |
398 | my $port = shift; |
384 | my ($noderef, $portid) = split /#/, $port, 2; |
399 | my ($noderef, $portid) = split /#/, $port, 2; |
385 | |
400 | |
386 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
401 | $NODE{$noderef} == $NODE{""} |
387 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
402 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
388 | |
403 | |
389 | while (@_) { |
404 | while (@_) { |
390 | if (ref $_[0]) { |
405 | if (ref $_[0]) { |
391 | if (my $self = $PORT_DATA{$portid}) { |
406 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
490 | message loss has been detected. No messages will be lost "in between" |
505 | message loss has been detected. No messages will be lost "in between" |
491 | (after the first lost message no further messages will be received by the |
506 | (after the first lost message no further messages will be received by the |
492 | port). After the monitoring action was invoked, further messages might get |
507 | port). After the monitoring action was invoked, further messages might get |
493 | delivered again. |
508 | delivered again. |
494 | |
509 | |
|
|
510 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
511 | and will not trigger again. |
|
|
512 | |
495 | In the first form (callback), the callback is simply called with any |
513 | In the first form (callback), the callback is simply called with any |
496 | number of C<@reason> elements (no @reason means that the port was deleted |
514 | number of C<@reason> elements (no @reason means that the port was deleted |
497 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
515 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
498 | C<eval> if unsure. |
516 | C<eval> if unsure. |
499 | |
517 | |
… | |
… | |
659 | my $id = "$RUNIQ." . $ID++; |
677 | my $id = "$RUNIQ." . $ID++; |
660 | |
678 | |
661 | $_[0] =~ /::/ |
679 | $_[0] =~ /::/ |
662 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
680 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
663 | |
681 | |
664 | ($NODE{$noderef} || add_node $noderef) |
682 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
665 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
|
|
666 | |
683 | |
667 | "$noderef#$id" |
684 | "$noderef#$id" |
668 | } |
685 | } |
669 | |
|
|
670 | =back |
|
|
671 | |
|
|
672 | =head1 NODE MESSAGES |
|
|
673 | |
|
|
674 | Nodes understand the following messages sent to them. Many of them take |
|
|
675 | arguments called C<@reply>, which will simply be used to compose a reply |
|
|
676 | message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and |
|
|
677 | the remaining arguments are simply the message data. |
|
|
678 | |
|
|
679 | While other messages exist, they are not public and subject to change. |
|
|
680 | |
|
|
681 | =over 4 |
|
|
682 | |
|
|
683 | =cut |
|
|
684 | |
|
|
685 | =item lookup => $name, @reply |
|
|
686 | |
|
|
687 | Replies with the port ID of the specified well-known port, or C<undef>. |
|
|
688 | |
|
|
689 | =item devnull => ... |
|
|
690 | |
|
|
691 | Generic data sink/CPU heat conversion. |
|
|
692 | |
|
|
693 | =item relay => $port, @msg |
|
|
694 | |
|
|
695 | Simply forwards the message to the given port. |
|
|
696 | |
|
|
697 | =item eval => $string[ @reply] |
|
|
698 | |
|
|
699 | Evaluates the given string. If C<@reply> is given, then a message of the |
|
|
700 | form C<@reply, $@, @evalres> is sent. |
|
|
701 | |
|
|
702 | Example: crash another node. |
|
|
703 | |
|
|
704 | snd $othernode, eval => "exit"; |
|
|
705 | |
|
|
706 | =item time => @reply |
|
|
707 | |
|
|
708 | Replies the the current node time to C<@reply>. |
|
|
709 | |
|
|
710 | Example: tell the current node to send the current time to C<$myport> in a |
|
|
711 | C<timereply> message. |
|
|
712 | |
|
|
713 | snd $NODE, time => $myport, timereply => 1, 2; |
|
|
714 | # => snd $myport, timereply => 1, 2, <time> |
|
|
715 | |
686 | |
716 | =back |
687 | =back |
717 | |
688 | |
718 | =head1 AnyEvent::MP vs. Distributed Erlang |
689 | =head1 AnyEvent::MP vs. Distributed Erlang |
719 | |
690 | |
… | |
… | |
738 | convenience functionality. |
709 | convenience functionality. |
739 | |
710 | |
740 | This means that AEMP requires a less tightly controlled environment at the |
711 | This means that AEMP requires a less tightly controlled environment at the |
741 | cost of longer node references and a slightly higher management overhead. |
712 | cost of longer node references and a slightly higher management overhead. |
742 | |
713 | |
|
|
714 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
|
|
715 | uses "local ports are like remote ports". |
|
|
716 | |
|
|
717 | The failure modes for local ports are quite different (runtime errors |
|
|
718 | only) then for remote ports - when a local port dies, you I<know> it dies, |
|
|
719 | when a connection to another node dies, you know nothing about the other |
|
|
720 | port. |
|
|
721 | |
|
|
722 | Erlang pretends remote ports are as reliable as local ports, even when |
|
|
723 | they are not. |
|
|
724 | |
|
|
725 | AEMP encourages a "treat remote ports differently" philosophy, with local |
|
|
726 | ports being the special case/exception, where transport errors cannot |
|
|
727 | occur. |
|
|
728 | |
743 | =item * Erlang uses processes and a mailbox, AEMP does not queue. |
729 | =item * Erlang uses processes and a mailbox, AEMP does not queue. |
744 | |
730 | |
745 | Erlang uses processes that selctively receive messages, and therefore |
731 | Erlang uses processes that selectively receive messages, and therefore |
746 | needs a queue. AEMP is event based, queuing messages would serve no useful |
732 | needs a queue. AEMP is event based, queuing messages would serve no |
747 | purpose. |
733 | useful purpose. For the same reason the pattern-matching abilities of |
|
|
734 | AnyEvent::MP are more limited, as there is little need to be able to |
|
|
735 | filter messages without dequeing them. |
748 | |
736 | |
749 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
737 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
750 | |
738 | |
751 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
739 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
752 | |
740 | |
753 | Sending messages in Erlang is synchronous and blocks the process. AEMP |
741 | Sending messages in Erlang is synchronous and blocks the process (and |
754 | sends are immediate, connection establishment is handled in the |
742 | so does not need a queue that can overflow). AEMP sends are immediate, |
755 | background. |
743 | connection establishment is handled in the background. |
756 | |
744 | |
757 | =item * Erlang can silently lose messages, AEMP cannot. |
745 | =item * Erlang suffers from silent message loss, AEMP does not. |
758 | |
746 | |
759 | Erlang makes few guarantees on messages delivery - messages can get lost |
747 | 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, |
748 | 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). |
749 | and c, and the other side only receives messages a and c). |
762 | |
750 | |
… | |
… | |
774 | eventually be killed - it cannot happen that a node detects a port as dead |
762 | 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. |
763 | and then later sends messages to it, finding it is still alive. |
776 | |
764 | |
777 | =item * Erlang can send messages to the wrong port, AEMP does not. |
765 | =item * Erlang can send messages to the wrong port, AEMP does not. |
778 | |
766 | |
779 | In Erlang it is quite possible that a node that restarts reuses a process |
767 | 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 |
768 | known to other nodes for a completely different process, causing messages |
781 | messages destined for that process to end up in an unrelated process. |
769 | destined for that process to end up in an unrelated process. |
782 | |
770 | |
783 | AEMP never reuses port IDs, so old messages or old port IDs floating |
771 | 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. |
772 | around in the network will not be sent to an unrelated port. |
785 | |
773 | |
786 | =item * Erlang uses unprotected connections, AEMP uses secure |
774 | =item * Erlang uses unprotected connections, AEMP uses secure |