… | |
… | |
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...; |
… | |
… | |
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 | Ports allow you to register C<rcv> handlers that can match all or just |
72 | Ports allow you to register C<rcv> handlers that can match all or just |
75 | some messages. Messages will not be queued. |
73 | some messages. Messages will not be queued. |
76 | |
74 | |
77 | =item port id - C<noderef#portname> |
75 | =item port ID - C<noderef#portname> |
78 | |
76 | |
79 | A port ID is 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 |
80 | 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 |
81 | 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 |
82 | reference. |
80 | reference. |
… | |
… | |
85 | |
83 | |
86 | A node is a single process containing at least one port - the node port, |
84 | A node is a single process containing at least one port - the node port, |
87 | which provides nodes to manage each other remotely, and to create new |
85 | which provides nodes to manage each other remotely, and to create new |
88 | ports. |
86 | ports. |
89 | |
87 | |
90 | Nodes are either private (single-process only), slaves (connected to a |
88 | Nodes are either private (single-process only), slaves (can only talk to |
91 | 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). |
92 | |
91 | |
93 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
92 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
94 | |
93 | |
95 | 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 |
96 | 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 |
97 | node (for public nodes). |
96 | node (for public nodes). |
98 | |
97 | |
99 | 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 |
100 | TCP/IP, other protocols might look different). |
99 | TCP/IP, other protocols might look different). |
… | |
… | |
126 | use base "Exporter"; |
125 | use base "Exporter"; |
127 | |
126 | |
128 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
127 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
129 | |
128 | |
130 | our @EXPORT = qw( |
129 | our @EXPORT = qw( |
131 | NODE $NODE *SELF node_of _any_ |
130 | NODE $NODE *SELF node_of after |
132 | resolve_node initialise_node |
131 | resolve_node initialise_node |
133 | snd rcv mon kil reg psub spawn |
132 | snd rcv mon mon_guard kil reg psub spawn |
134 | port |
133 | port |
135 | ); |
134 | ); |
136 | |
135 | |
137 | our $SELF; |
136 | our $SELF; |
138 | |
137 | |
… | |
… | |
143 | } |
142 | } |
144 | |
143 | |
145 | =item $thisnode = NODE / $NODE |
144 | =item $thisnode = NODE / $NODE |
146 | |
145 | |
147 | 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 |
148 | 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 |
149 | C<initialise_node>. |
148 | C<initialise_node>. |
150 | |
149 | |
151 | =item $noderef = node_of $port |
150 | =item $nodeid = node_of $port |
152 | |
151 | |
153 | 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. |
154 | |
153 | |
155 | =item initialise_node $noderef, $seednode, $seednode... |
154 | =item initialise_node $profile_name |
156 | |
|
|
157 | =item initialise_node "slave/", $master, $master... |
|
|
158 | |
155 | |
159 | 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 |
160 | 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 |
161 | 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. |
162 | |
159 | |
… | |
… | |
180 | |
177 | |
181 | For public nodes, C<$noderef> (supplied either directly to |
178 | For public nodes, C<$noderef> (supplied either directly to |
182 | 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 |
183 | noderef (possibly unresolved, in which case it will be resolved). |
180 | noderef (possibly unresolved, in which case it will be resolved). |
184 | |
181 | |
185 | After resolving, the node will bind itself on all endpoints and try to |
182 | After resolving, the node will bind itself on all endpoints. |
186 | 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 |
187 | 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 |
188 | network. |
199 | network. |
189 | |
200 | |
190 | =item slave nodes |
|
|
191 | |
|
|
192 | When the C<$noderef> (either as given or overriden by the config file) |
|
|
193 | is the special string C<slave/>, then the node will become a slave |
|
|
194 | node. Slave nodes cannot be contacted from outside and will route most of |
|
|
195 | their traffic to the master node that they attach to. |
|
|
196 | |
|
|
197 | At least one additional noderef is required (either by specifying it |
|
|
198 | directly or because it is part of the configuration profile): The node |
|
|
199 | will try to connect to all of them and will become a slave attached to the |
|
|
200 | first node it can successfully connect to. |
|
|
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 | |
|
|
207 | =back |
|
|
208 | |
|
|
209 | This function will block until all nodes have been resolved and, for slave |
|
|
210 | nodes, until it has successfully established a connection to a master |
|
|
211 | server. |
|
|
212 | |
|
|
213 | All the seednodes will also be specially marked to automatically retry |
201 | All the seednodes will also be specially marked to automatically retry |
214 | connecting to them infinitely. |
202 | connecting to them indefinitely, so make sure that seednodes are really |
|
|
203 | reliable and up (this might also change in the future). |
215 | |
204 | |
216 | 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 |
217 | 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 |
218 | form of invocation for "daemon"-type nodes. |
207 | form of invocation for "daemon"-type nodes. |
219 | |
208 | |
… | |
… | |
221 | |
210 | |
222 | 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 |
223 | C<aemp>. This form is often used for commandline clients. |
212 | C<aemp>. This form is often used for commandline clients. |
224 | |
213 | |
225 | initialise_node "slave/"; |
214 | initialise_node "slave/"; |
226 | |
|
|
227 | Example: become a slave node to any of the specified master servers. This |
|
|
228 | form is also often used for commandline clients. |
|
|
229 | |
|
|
230 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
|
|
231 | |
215 | |
232 | 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 |
233 | servers to become part of the network. |
217 | servers to become part of the network. |
234 | |
218 | |
235 | initialise_node undef, "master1", "master2"; |
219 | initialise_node undef, "master1", "master2"; |
… | |
… | |
396 | |
380 | |
397 | sub rcv($@) { |
381 | sub rcv($@) { |
398 | my $port = shift; |
382 | my $port = shift; |
399 | my ($noderef, $portid) = split /#/, $port, 2; |
383 | my ($noderef, $portid) = split /#/, $port, 2; |
400 | |
384 | |
401 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
385 | $NODE{$noderef} == $NODE{""} |
402 | 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"; |
403 | |
387 | |
404 | while (@_) { |
388 | while (@_) { |
405 | if (ref $_[0]) { |
389 | if (ref $_[0]) { |
406 | if (my $self = $PORT_DATA{$portid}) { |
390 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
505 | 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" |
506 | (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 |
507 | port). After the monitoring action was invoked, further messages might get |
491 | port). After the monitoring action was invoked, further messages might get |
508 | delivered again. |
492 | delivered again. |
509 | |
493 | |
|
|
494 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
495 | and will not trigger again. |
|
|
496 | |
510 | 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 |
511 | 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 |
512 | "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 |
513 | C<eval> if unsure. |
500 | C<eval> if unsure. |
514 | |
501 | |
… | |
… | |
679 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
666 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
680 | |
667 | |
681 | "$noderef#$id" |
668 | "$noderef#$id" |
682 | } |
669 | } |
683 | |
670 | |
|
|
671 | =item after $timeout, @msg |
|
|
672 | |
|
|
673 | =item after $timeout, $callback |
|
|
674 | |
|
|
675 | Either sends the given message, or call the given callback, after the |
|
|
676 | specified number of seconds. |
|
|
677 | |
|
|
678 | This is simply a utility function that come sin handy at times. |
|
|
679 | |
|
|
680 | =cut |
|
|
681 | |
|
|
682 | sub after($@) { |
|
|
683 | my ($timeout, @action) = @_; |
|
|
684 | |
|
|
685 | my $t; $t = AE::timer $timeout, 0, sub { |
|
|
686 | undef $t; |
|
|
687 | ref $action[0] |
|
|
688 | ? $action[0]() |
|
|
689 | : snd @action; |
|
|
690 | }; |
|
|
691 | } |
|
|
692 | |
684 | =back |
693 | =back |
685 | |
694 | |
686 | =head1 AnyEvent::MP vs. Distributed Erlang |
695 | =head1 AnyEvent::MP vs. Distributed Erlang |
687 | |
696 | |
688 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
697 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |