… | |
… | |
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 (can only talk to |
92 | master node only) or public nodes (connectable from unrelated nodes). |
91 | public nodes, but do not need an open port) or public nodes (connectable |
|
|
92 | from any other node). |
93 | |
93 | |
94 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
94 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
95 | |
95 | |
96 | A node reference is a string that either simply identifies the node (for |
96 | A node reference is a string that either simply identifies the node (for |
97 | private and slave nodes), or contains a recipe on how to reach a given |
97 | private and slave nodes), or contains a recipe on how to reach a given |
… | |
… | |
127 | use base "Exporter"; |
127 | use base "Exporter"; |
128 | |
128 | |
129 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
129 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
130 | |
130 | |
131 | our @EXPORT = qw( |
131 | our @EXPORT = qw( |
132 | NODE $NODE *SELF node_of _any_ |
132 | NODE $NODE *SELF node_of after |
133 | resolve_node initialise_node |
133 | resolve_node initialise_node |
134 | snd rcv mon kil reg psub spawn |
134 | snd rcv mon mon_guard kil reg psub spawn |
135 | port |
135 | port |
136 | ); |
136 | ); |
137 | |
137 | |
138 | our $SELF; |
138 | our $SELF; |
139 | |
139 | |
… | |
… | |
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 | |
… | |
… | |
182 | |
181 | |
183 | For public nodes, C<$noderef> (supplied either directly to |
182 | For public nodes, C<$noderef> (supplied either directly to |
184 | C<initialise_node> or indirectly via a profile or the nodename) must be a |
183 | C<initialise_node> or indirectly via a profile or the nodename) must be a |
185 | noderef (possibly unresolved, in which case it will be resolved). |
184 | noderef (possibly unresolved, in which case it will be resolved). |
186 | |
185 | |
187 | After resolving, the node will bind itself on all endpoints and try to |
186 | After resolving, the node will bind itself on all endpoints. |
188 | connect to all additional C<$seednodes> that are specified. Seednodes are |
187 | |
|
|
188 | =item slave nodes |
|
|
189 | |
|
|
190 | When the C<$noderef> (either as given or overriden by the config file) |
|
|
191 | is the special string C<slave/>, then the node will become a slave |
|
|
192 | node. Slave nodes cannot be contacted from outside, and cannot talk to |
|
|
193 | each other (at least in this version of AnyEvent::MP). |
|
|
194 | |
|
|
195 | Slave nodes work by creating connections to all public nodes, using the |
|
|
196 | L<AnyEvent::MP::Global> service. |
|
|
197 | |
|
|
198 | =back |
|
|
199 | |
|
|
200 | After initialising itself, the node will connect to all additional |
|
|
201 | C<$seednodes> that are specified diretcly or via a profile. Seednodes are |
189 | optional and can be used to quickly bootstrap the node into an existing |
202 | optional and can be used to quickly bootstrap the node into an existing |
190 | network. |
203 | network. |
191 | |
204 | |
192 | =item slave nodes |
205 | All the seednodes will also be specially marked to automatically retry |
193 | |
206 | connecting to them indefinitely, so make sure that seednodes are really |
194 | When the C<$noderef> (either as given or overriden by the config file) |
207 | reliable and up (this might also change in the future). |
195 | is the special string C<slave/>, then the node will become a slave |
|
|
196 | node. Slave nodes cannot be contacted from outside and will route most of |
|
|
197 | their traffic to the master node that they attach to. |
|
|
198 | |
|
|
199 | At least one additional noderef is required (either by specifying it |
|
|
200 | 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 |
|
|
202 | first node it can successfully connect to. |
|
|
203 | |
|
|
204 | =back |
|
|
205 | |
|
|
206 | 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 |
|
|
208 | server. |
|
|
209 | |
208 | |
210 | Example: become a public node listening on the guessed noderef, or the one |
209 | 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 |
210 | specified via C<aemp> for the current node. This should be the most common |
212 | form of invocation for "daemon"-type nodes. |
211 | form of invocation for "daemon"-type nodes. |
213 | |
212 | |
… | |
… | |
216 | Example: become a slave node to any of the the seednodes specified via |
215 | Example: become a slave node to any of the the seednodes specified via |
217 | C<aemp>. This form is often used for commandline clients. |
216 | C<aemp>. This form is often used for commandline clients. |
218 | |
217 | |
219 | initialise_node "slave/"; |
218 | initialise_node "slave/"; |
220 | |
219 | |
221 | Example: become a slave node to any of the specified master servers. This |
|
|
222 | form is also often used for commandline clients. |
|
|
223 | |
|
|
224 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
|
|
225 | |
|
|
226 | Example: become a public node, and try to contact some well-known master |
220 | Example: become a public node, and try to contact some well-known master |
227 | servers to become part of the network. |
221 | servers to become part of the network. |
228 | |
222 | |
229 | initialise_node undef, "master1", "master2"; |
223 | initialise_node undef, "master1", "master2"; |
230 | |
224 | |
… | |
… | |
279 | =item snd $port, type => @data |
273 | =item snd $port, type => @data |
280 | |
274 | |
281 | =item snd $port, @msg |
275 | =item snd $port, @msg |
282 | |
276 | |
283 | Send the given message to the given port ID, which can identify either |
277 | 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 |
278 | a local or a remote port, and must be a port ID. |
285 | stringifies a sa port ID (such as a port object :). |
|
|
286 | |
279 | |
287 | While the message can be about anything, it is highly recommended to use a |
280 | 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 |
281 | string as first element (a port ID, or some word that indicates a request |
289 | type etc.). |
282 | type etc.). |
290 | |
283 | |
291 | The message data effectively becomes read-only after a call to this |
284 | The message data effectively becomes read-only after a call to this |
292 | function: modifying any argument is not allowed and can cause many |
285 | function: modifying any argument is not allowed and can cause many |
293 | problems. |
286 | problems. |
… | |
… | |
351 | The default callback received all messages not matched by a more specific |
344 | The default callback received all messages not matched by a more specific |
352 | C<tag> match. |
345 | C<tag> match. |
353 | |
346 | |
354 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
347 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
355 | |
348 | |
356 | Register callbacks to be called on messages starting with the given tag on |
349 | 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> |
350 | given tag on the given port (and return the port), or unregister it (when |
358 | is C<$undef>). |
351 | C<$callback> is C<$undef> or missing). There can only be one callback |
|
|
352 | registered for each tag. |
359 | |
353 | |
360 | The original message will be passed to the callback, after the first |
354 | 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 |
355 | element (the tag) has been removed. The callback will use the same |
362 | environment as the default callback (see above). |
356 | environment as the default callback (see above). |
363 | |
357 | |
… | |
… | |
375 | rcv port, |
369 | rcv port, |
376 | msg1 => sub { ... }, |
370 | msg1 => sub { ... }, |
377 | ... |
371 | ... |
378 | ; |
372 | ; |
379 | |
373 | |
|
|
374 | Example: temporarily register a rcv callback for a tag matching some port |
|
|
375 | (e.g. for a rpc reply) and unregister it after a message was received. |
|
|
376 | |
|
|
377 | rcv $port, $otherport => sub { |
|
|
378 | my @reply = @_; |
|
|
379 | |
|
|
380 | rcv $SELF, $otherport; |
|
|
381 | }; |
|
|
382 | |
380 | =cut |
383 | =cut |
381 | |
384 | |
382 | sub rcv($@) { |
385 | sub rcv($@) { |
383 | my $port = shift; |
386 | my $port = shift; |
384 | my ($noderef, $portid) = split /#/, $port, 2; |
387 | my ($noderef, $portid) = split /#/, $port, 2; |
385 | |
388 | |
386 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
389 | $NODE{$noderef} == $NODE{""} |
387 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
390 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
388 | |
391 | |
389 | while (@_) { |
392 | while (@_) { |
390 | if (ref $_[0]) { |
393 | if (ref $_[0]) { |
391 | if (my $self = $PORT_DATA{$portid}) { |
394 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
490 | message loss has been detected. No messages will be lost "in between" |
493 | 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 |
494 | (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 |
495 | port). After the monitoring action was invoked, further messages might get |
493 | delivered again. |
496 | delivered again. |
494 | |
497 | |
|
|
498 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
499 | and will not trigger again. |
|
|
500 | |
495 | In the first form (callback), the callback is simply called with any |
501 | 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 |
502 | 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 |
503 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
498 | C<eval> if unsure. |
504 | C<eval> if unsure. |
499 | |
505 | |
… | |
… | |
659 | my $id = "$RUNIQ." . $ID++; |
665 | my $id = "$RUNIQ." . $ID++; |
660 | |
666 | |
661 | $_[0] =~ /::/ |
667 | $_[0] =~ /::/ |
662 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
668 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
663 | |
669 | |
664 | ($NODE{$noderef} || add_node $noderef) |
670 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
665 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
|
|
666 | |
671 | |
667 | "$noderef#$id" |
672 | "$noderef#$id" |
668 | } |
673 | } |
669 | |
674 | |
670 | =back |
675 | =item after $timeout, @msg |
671 | |
676 | |
672 | =head1 NODE MESSAGES |
677 | =item after $timeout, $callback |
673 | |
678 | |
674 | Nodes understand the following messages sent to them. Many of them take |
679 | Either sends the given message, or call the given callback, after the |
675 | arguments called C<@reply>, which will simply be used to compose a reply |
680 | specified number of seconds. |
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 | |
681 | |
679 | While other messages exist, they are not public and subject to change. |
682 | This is simply a utility function that come sin handy at times. |
680 | |
683 | |
681 | =over 4 |
|
|
682 | |
|
|
683 | =cut |
684 | =cut |
684 | |
685 | |
685 | =item lookup => $name, @reply |
686 | sub after($@) { |
|
|
687 | my ($timeout, @action) = @_; |
686 | |
688 | |
687 | Replies with the port ID of the specified well-known port, or C<undef>. |
689 | my $t; $t = AE::timer $timeout, 0, sub { |
688 | |
690 | undef $t; |
689 | =item devnull => ... |
691 | ref $action[0] |
690 | |
692 | ? $action[0]() |
691 | Generic data sink/CPU heat conversion. |
693 | : snd @action; |
692 | |
694 | }; |
693 | =item relay => $port, @msg |
695 | } |
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 | |
696 | |
716 | =back |
697 | =back |
717 | |
698 | |
718 | =head1 AnyEvent::MP vs. Distributed Erlang |
699 | =head1 AnyEvent::MP vs. Distributed Erlang |
719 | |
700 | |
… | |
… | |
738 | convenience functionality. |
719 | convenience functionality. |
739 | |
720 | |
740 | This means that AEMP requires a less tightly controlled environment at the |
721 | This means that AEMP requires a less tightly controlled environment at the |
741 | cost of longer node references and a slightly higher management overhead. |
722 | cost of longer node references and a slightly higher management overhead. |
742 | |
723 | |
743 | =item Erlang has a "remote ports are like local ports" philosophy, AEMP |
724 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
744 | uses "local ports are like remote ports". |
725 | uses "local ports are like remote ports". |
745 | |
726 | |
746 | The failure modes for local ports are quite different (runtime errors |
727 | The failure modes for local ports are quite different (runtime errors |
747 | only) then for remote ports - when a local port dies, you I<know> it dies, |
728 | only) then for remote ports - when a local port dies, you I<know> it dies, |
748 | when a connection to another node dies, you know nothing about the other |
729 | when a connection to another node dies, you know nothing about the other |