… | |
… | |
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, tagged message 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 }; |
… | |
… | |
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> |
… | |
… | |
349 | The default callback received all messages not matched by a more specific |
348 | The default callback received all messages not matched by a more specific |
350 | C<tag> match. |
349 | C<tag> match. |
351 | |
350 | |
352 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
351 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
353 | |
352 | |
354 | Register callbacks to be called on messages starting with the given tag on |
353 | Register (or replace) callbacks to be called on messages starting with the |
355 | the given port (and return the port), or unregister it (when C<$callback> |
354 | given tag on the given port (and return the port), or unregister it (when |
356 | is C<$undef>). |
355 | C<$callback> is C<$undef> or missing). There can only be one callback |
|
|
356 | registered for each tag. |
357 | |
357 | |
358 | The original message will be passed to the callback, after the first |
358 | The original message will be passed to the callback, after the first |
359 | element (the tag) has been removed. The callback will use the same |
359 | element (the tag) has been removed. The callback will use the same |
360 | environment as the default callback (see above). |
360 | environment as the default callback (see above). |
361 | |
361 | |
… | |
… | |
372 | snd $otherport, reply => |
372 | snd $otherport, reply => |
373 | rcv port, |
373 | rcv port, |
374 | msg1 => sub { ... }, |
374 | msg1 => sub { ... }, |
375 | ... |
375 | ... |
376 | ; |
376 | ; |
|
|
377 | |
|
|
378 | Example: temporarily register a rcv callback for a tag matching some port |
|
|
379 | (e.g. for a rpc reply) and unregister it after a message was received. |
|
|
380 | |
|
|
381 | rcv $port, $otherport => sub { |
|
|
382 | my @reply = @_; |
|
|
383 | |
|
|
384 | rcv $SELF, $otherport; |
|
|
385 | }; |
377 | |
386 | |
378 | =cut |
387 | =cut |
379 | |
388 | |
380 | sub rcv($@) { |
389 | sub rcv($@) { |
381 | my $port = shift; |
390 | my $port = shift; |
… | |
… | |
657 | my $id = "$RUNIQ." . $ID++; |
666 | my $id = "$RUNIQ." . $ID++; |
658 | |
667 | |
659 | $_[0] =~ /::/ |
668 | $_[0] =~ /::/ |
660 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
669 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
661 | |
670 | |
662 | ($NODE{$noderef} || add_node $noderef) |
671 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
663 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
|
|
664 | |
672 | |
665 | "$noderef#$id" |
673 | "$noderef#$id" |
666 | } |
674 | } |
667 | |
675 | |
668 | =back |
676 | =back |
… | |
… | |
736 | convenience functionality. |
744 | convenience functionality. |
737 | |
745 | |
738 | This means that AEMP requires a less tightly controlled environment at the |
746 | This means that AEMP requires a less tightly controlled environment at the |
739 | cost of longer node references and a slightly higher management overhead. |
747 | cost of longer node references and a slightly higher management overhead. |
740 | |
748 | |
741 | =item Erlang has a "remote ports are like local ports" philosophy, AEMP |
749 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
742 | uses "local ports are like remote ports". |
750 | uses "local ports are like remote ports". |
743 | |
751 | |
744 | The failure modes for local ports are quite different (runtime errors |
752 | 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, |
753 | 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 |
754 | when a connection to another node dies, you know nothing about the other |