… | |
… | |
11 | NODE $port # returns the noderef of the port |
11 | NODE $port # returns the noderef of the port |
12 | |
12 | |
13 | $SELF # receiving/own port id in rcv callbacks |
13 | $SELF # receiving/own port id in rcv callbacks |
14 | |
14 | |
15 | # initialise the node so it can send/receive messages |
15 | # initialise the node so it can send/receive messages |
16 | initialise_node; # -OR- |
16 | initialise_node; |
17 | initialise_node "localhost:4040"; # -OR- |
|
|
18 | initialise_node "slave/", "localhost:4040" |
|
|
19 | |
17 | |
20 | # ports are message endpoints |
18 | # ports are message endpoints |
21 | |
19 | |
22 | # sending messages |
20 | # sending messages |
23 | snd $port, type => data...; |
21 | snd $port, type => data...; |
24 | snd $port, @msg; |
22 | snd $port, @msg; |
25 | snd @msg_with_first_element_being_a_port; |
23 | snd @msg_with_first_element_being_a_port; |
26 | |
24 | |
27 | # creating/using ports, the simple way |
25 | # creating/using ports, the simple way |
28 | my $somple_port = port { my @msg = @_; 0 }; |
26 | my $simple_port = port { my @msg = @_; 0 }; |
29 | |
27 | |
30 | # creating/using ports, tagged message matching |
28 | # creating/using ports, tagged message matching |
31 | my $port = port; |
29 | my $port = port; |
32 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
30 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
33 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
31 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
… | |
… | |
69 | |
67 | |
70 | =item port |
68 | =item port |
71 | |
69 | |
72 | A port is something you can send messages to (with the C<snd> function). |
70 | A port is something you can send messages to (with the C<snd> function). |
73 | |
71 | |
74 | Some ports allow you to register C<rcv> handlers that can match specific |
72 | 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 |
73 | some messages. Messages will not be queued. |
76 | will not be queued. |
|
|
77 | |
74 | |
78 | =item port id - C<noderef#portname> |
75 | =item port ID - C<noderef#portname> |
79 | |
76 | |
80 | A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as |
77 | 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 |
78 | 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 |
79 | exception is the the node port, whose ID is identical to its node |
83 | reference. |
80 | reference. |
84 | |
81 | |
85 | =item node |
82 | =item node |
86 | |
83 | |
87 | A node is a single process containing at least one port - the node |
84 | 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 |
85 | which provides nodes to manage each other remotely, and to create new |
89 | create new ports, among other things. |
86 | ports. |
90 | |
87 | |
91 | Nodes are either private (single-process only), slaves (connected to a |
88 | Nodes are either private (single-process only), slaves (can only talk to |
92 | master node only) or public nodes (connectable from unrelated nodes). |
89 | public nodes, but do not need an open port) or public nodes (connectable |
|
|
90 | from any other node). |
93 | |
91 | |
94 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
92 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
95 | |
93 | |
96 | A node reference is a string that either simply identifies the node (for |
94 | A node ID 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 |
95 | private and slave nodes), or contains a recipe on how to reach a given |
98 | node (for public nodes). |
96 | node (for public nodes). |
99 | |
97 | |
100 | This recipe is simply a comma-separated list of C<address:port> pairs (for |
98 | This recipe is simply a comma-separated list of C<address:port> pairs (for |
101 | TCP/IP, other protocols might look different). |
99 | TCP/IP, other protocols might look different). |
… | |
… | |
127 | use base "Exporter"; |
125 | use base "Exporter"; |
128 | |
126 | |
129 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
127 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
130 | |
128 | |
131 | our @EXPORT = qw( |
129 | our @EXPORT = qw( |
132 | NODE $NODE *SELF node_of _any_ |
130 | NODE $NODE *SELF node_of after |
133 | resolve_node initialise_node |
131 | resolve_node initialise_node |
134 | snd rcv mon kil reg psub spawn |
132 | snd rcv mon mon_guard kil reg psub spawn |
135 | port |
133 | port |
136 | ); |
134 | ); |
137 | |
135 | |
138 | our $SELF; |
136 | our $SELF; |
139 | |
137 | |
… | |
… | |
144 | } |
142 | } |
145 | |
143 | |
146 | =item $thisnode = NODE / $NODE |
144 | =item $thisnode = NODE / $NODE |
147 | |
145 | |
148 | The C<NODE> function returns, and the C<$NODE> variable contains the |
146 | The C<NODE> function returns, and the C<$NODE> variable contains the |
149 | noderef of the local node. The value is initialised by a call to |
147 | node id of the local node. The value is initialised by a call to |
150 | C<initialise_node>. |
148 | C<initialise_node>. |
151 | |
149 | |
152 | =item $noderef = node_of $port |
150 | =item $nodeid = node_of $port |
153 | |
151 | |
154 | Extracts and returns the noderef from a port ID or a noderef. |
152 | Extracts and returns the noderef from a port ID or a node ID. |
155 | |
153 | |
156 | =item initialise_node $noderef, $seednode, $seednode... |
154 | =item initialise_node $profile_name |
157 | |
|
|
158 | =item initialise_node "slave/", $master, $master... |
|
|
159 | |
155 | |
160 | Before a node can talk to other nodes on the network it has to initialise |
156 | Before a node can talk to other nodes on the network it has to initialise |
161 | itself - the minimum a node needs to know is it's own name, and optionally |
157 | itself - the minimum a node needs to know is it's own name, and optionally |
162 | it should know the noderefs of some other nodes in the network. |
158 | it should know the noderefs of some other nodes in the network. |
163 | |
159 | |
… | |
… | |
181 | |
177 | |
182 | For public nodes, C<$noderef> (supplied either directly to |
178 | For public nodes, C<$noderef> (supplied either directly to |
183 | C<initialise_node> or indirectly via a profile or the nodename) must be a |
179 | C<initialise_node> or indirectly via a profile or the nodename) must be a |
184 | noderef (possibly unresolved, in which case it will be resolved). |
180 | noderef (possibly unresolved, in which case it will be resolved). |
185 | |
181 | |
186 | After resolving, the node will bind itself on all endpoints and try to |
182 | After resolving, the node will bind itself on all endpoints. |
187 | connect to all additional C<$seednodes> that are specified. Seednodes are |
183 | |
|
|
184 | =item slave nodes |
|
|
185 | |
|
|
186 | When the C<$noderef> (either as given or overriden by the config file) |
|
|
187 | is the special string C<slave/>, then the node will become a slave |
|
|
188 | node. Slave nodes cannot be contacted from outside, and cannot talk to |
|
|
189 | each other (at least in this version of AnyEvent::MP). |
|
|
190 | |
|
|
191 | Slave nodes work by creating connections to all public nodes, using the |
|
|
192 | L<AnyEvent::MP::Global> service. |
|
|
193 | |
|
|
194 | =back |
|
|
195 | |
|
|
196 | After initialising itself, the node will connect to all additional |
|
|
197 | C<$seednodes> that are specified diretcly or via a profile. Seednodes are |
188 | optional and can be used to quickly bootstrap the node into an existing |
198 | optional and can be used to quickly bootstrap the node into an existing |
189 | network. |
199 | network. |
190 | |
200 | |
191 | =item slave nodes |
201 | All the seednodes will also be specially marked to automatically retry |
192 | |
202 | connecting to them indefinitely, so make sure that seednodes are really |
193 | When the C<$noderef> (either as given or overriden by the config file) |
203 | reliable and up (this might also change in the future). |
194 | is the special string C<slave/>, then the node will become a slave |
|
|
195 | node. Slave nodes cannot be contacted from outside and will route most of |
|
|
196 | their traffic to the master node that they attach to. |
|
|
197 | |
|
|
198 | At least one additional noderef is required (either by specifying it |
|
|
199 | directly or because it is part of the configuration profile): The node |
|
|
200 | will try to connect to all of them and will become a slave attached to the |
|
|
201 | first node it can successfully connect to. |
|
|
202 | |
|
|
203 | =back |
|
|
204 | |
|
|
205 | This function will block until all nodes have been resolved and, for slave |
|
|
206 | nodes, until it has successfully established a connection to a master |
|
|
207 | server. |
|
|
208 | |
204 | |
209 | Example: become a public node listening on the guessed noderef, or the one |
205 | Example: become a public node listening on the guessed noderef, or the one |
210 | specified via C<aemp> for the current node. This should be the most common |
206 | specified via C<aemp> for the current node. This should be the most common |
211 | form of invocation for "daemon"-type nodes. |
207 | form of invocation for "daemon"-type nodes. |
212 | |
208 | |
… | |
… | |
214 | |
210 | |
215 | Example: become a slave node to any of the the seednodes specified via |
211 | Example: become a slave node to any of the the seednodes specified via |
216 | C<aemp>. This form is often used for commandline clients. |
212 | C<aemp>. This form is often used for commandline clients. |
217 | |
213 | |
218 | initialise_node "slave/"; |
214 | initialise_node "slave/"; |
219 | |
|
|
220 | Example: become a slave node to any of the specified master servers. This |
|
|
221 | form is also often used for commandline clients. |
|
|
222 | |
|
|
223 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
|
|
224 | |
215 | |
225 | Example: become a public node, and try to contact some well-known master |
216 | Example: become a public node, and try to contact some well-known master |
226 | servers to become part of the network. |
217 | servers to become part of the network. |
227 | |
218 | |
228 | initialise_node undef, "master1", "master2"; |
219 | initialise_node undef, "master1", "master2"; |
… | |
… | |
349 | The default callback received all messages not matched by a more specific |
340 | The default callback received all messages not matched by a more specific |
350 | C<tag> match. |
341 | C<tag> match. |
351 | |
342 | |
352 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
343 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
353 | |
344 | |
354 | Register callbacks to be called on messages starting with the given tag on |
345 | 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> |
346 | given tag on the given port (and return the port), or unregister it (when |
356 | is C<$undef>). |
347 | C<$callback> is C<$undef> or missing). There can only be one callback |
|
|
348 | registered for each tag. |
357 | |
349 | |
358 | The original message will be passed to the callback, after the first |
350 | 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 |
351 | element (the tag) has been removed. The callback will use the same |
360 | environment as the default callback (see above). |
352 | environment as the default callback (see above). |
361 | |
353 | |
… | |
… | |
373 | rcv port, |
365 | rcv port, |
374 | msg1 => sub { ... }, |
366 | msg1 => sub { ... }, |
375 | ... |
367 | ... |
376 | ; |
368 | ; |
377 | |
369 | |
|
|
370 | Example: temporarily register a rcv callback for a tag matching some port |
|
|
371 | (e.g. for a rpc reply) and unregister it after a message was received. |
|
|
372 | |
|
|
373 | rcv $port, $otherport => sub { |
|
|
374 | my @reply = @_; |
|
|
375 | |
|
|
376 | rcv $SELF, $otherport; |
|
|
377 | }; |
|
|
378 | |
378 | =cut |
379 | =cut |
379 | |
380 | |
380 | sub rcv($@) { |
381 | sub rcv($@) { |
381 | my $port = shift; |
382 | my $port = shift; |
382 | my ($noderef, $portid) = split /#/, $port, 2; |
383 | my ($noderef, $portid) = split /#/, $port, 2; |
383 | |
384 | |
384 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
385 | $NODE{$noderef} == $NODE{""} |
385 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
386 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
386 | |
387 | |
387 | while (@_) { |
388 | while (@_) { |
388 | if (ref $_[0]) { |
389 | if (ref $_[0]) { |
389 | if (my $self = $PORT_DATA{$portid}) { |
390 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
488 | message loss has been detected. No messages will be lost "in between" |
489 | message loss has been detected. No messages will be lost "in between" |
489 | (after the first lost message no further messages will be received by the |
490 | (after the first lost message no further messages will be received by the |
490 | port). After the monitoring action was invoked, further messages might get |
491 | port). After the monitoring action was invoked, further messages might get |
491 | delivered again. |
492 | delivered again. |
492 | |
493 | |
|
|
494 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
495 | and will not trigger again. |
|
|
496 | |
493 | In the first form (callback), the callback is simply called with any |
497 | In the first form (callback), the callback is simply called with any |
494 | number of C<@reason> elements (no @reason means that the port was deleted |
498 | number of C<@reason> elements (no @reason means that the port was deleted |
495 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
499 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
496 | C<eval> if unsure. |
500 | C<eval> if unsure. |
497 | |
501 | |
… | |
… | |
657 | my $id = "$RUNIQ." . $ID++; |
661 | my $id = "$RUNIQ." . $ID++; |
658 | |
662 | |
659 | $_[0] =~ /::/ |
663 | $_[0] =~ /::/ |
660 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
664 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
661 | |
665 | |
662 | ($NODE{$noderef} || add_node $noderef) |
666 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
663 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
|
|
664 | |
667 | |
665 | "$noderef#$id" |
668 | "$noderef#$id" |
666 | } |
669 | } |
667 | |
670 | |
668 | =back |
671 | =item after $timeout, @msg |
669 | |
672 | |
670 | =head1 NODE MESSAGES |
673 | =item after $timeout, $callback |
671 | |
674 | |
672 | Nodes understand the following messages sent to them. Many of them take |
675 | Either sends the given message, or call the given callback, after the |
673 | arguments called C<@reply>, which will simply be used to compose a reply |
676 | specified number of seconds. |
674 | message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and |
|
|
675 | the remaining arguments are simply the message data. |
|
|
676 | |
677 | |
677 | While other messages exist, they are not public and subject to change. |
678 | This is simply a utility function that come sin handy at times. |
678 | |
679 | |
679 | =over 4 |
|
|
680 | |
|
|
681 | =cut |
680 | =cut |
682 | |
681 | |
683 | =item lookup => $name, @reply |
682 | sub after($@) { |
|
|
683 | my ($timeout, @action) = @_; |
684 | |
684 | |
685 | Replies with the port ID of the specified well-known port, or C<undef>. |
685 | my $t; $t = AE::timer $timeout, 0, sub { |
686 | |
686 | undef $t; |
687 | =item devnull => ... |
687 | ref $action[0] |
688 | |
688 | ? $action[0]() |
689 | Generic data sink/CPU heat conversion. |
689 | : snd @action; |
690 | |
690 | }; |
691 | =item relay => $port, @msg |
691 | } |
692 | |
|
|
693 | Simply forwards the message to the given port. |
|
|
694 | |
|
|
695 | =item eval => $string[ @reply] |
|
|
696 | |
|
|
697 | Evaluates the given string. If C<@reply> is given, then a message of the |
|
|
698 | form C<@reply, $@, @evalres> is sent. |
|
|
699 | |
|
|
700 | Example: crash another node. |
|
|
701 | |
|
|
702 | snd $othernode, eval => "exit"; |
|
|
703 | |
|
|
704 | =item time => @reply |
|
|
705 | |
|
|
706 | Replies the the current node time to C<@reply>. |
|
|
707 | |
|
|
708 | Example: tell the current node to send the current time to C<$myport> in a |
|
|
709 | C<timereply> message. |
|
|
710 | |
|
|
711 | snd $NODE, time => $myport, timereply => 1, 2; |
|
|
712 | # => snd $myport, timereply => 1, 2, <time> |
|
|
713 | |
692 | |
714 | =back |
693 | =back |
715 | |
694 | |
716 | =head1 AnyEvent::MP vs. Distributed Erlang |
695 | =head1 AnyEvent::MP vs. Distributed Erlang |
717 | |
696 | |
… | |
… | |
736 | convenience functionality. |
715 | convenience functionality. |
737 | |
716 | |
738 | This means that AEMP requires a less tightly controlled environment at the |
717 | This means that AEMP requires a less tightly controlled environment at the |
739 | cost of longer node references and a slightly higher management overhead. |
718 | cost of longer node references and a slightly higher management overhead. |
740 | |
719 | |
741 | =item Erlang has a "remote ports are like local ports" philosophy, AEMP |
720 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
742 | uses "local ports are like remote ports". |
721 | uses "local ports are like remote ports". |
743 | |
722 | |
744 | The failure modes for local ports are quite different (runtime errors |
723 | 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, |
724 | 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 |
725 | when a connection to another node dies, you know nothing about the other |