… | |
… | |
85 | |
85 | |
86 | A node is a single process containing at least one port - the node port, |
86 | 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 |
87 | which provides nodes to manage each other remotely, and to create new |
88 | ports. |
88 | ports. |
89 | |
89 | |
90 | Nodes are either private (single-process only), slaves (connected to a |
90 | Nodes are either private (single-process only), slaves (can only talk to |
91 | 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). |
92 | |
93 | |
93 | =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> |
94 | |
95 | |
95 | 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 |
96 | 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 |
… | |
… | |
126 | use base "Exporter"; |
127 | use base "Exporter"; |
127 | |
128 | |
128 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
129 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
129 | |
130 | |
130 | our @EXPORT = qw( |
131 | our @EXPORT = qw( |
131 | NODE $NODE *SELF node_of _any_ |
132 | NODE $NODE *SELF node_of after |
132 | resolve_node initialise_node |
133 | resolve_node initialise_node |
133 | snd rcv mon kil reg psub spawn |
134 | snd rcv mon mon_guard kil reg psub spawn |
134 | port |
135 | port |
135 | ); |
136 | ); |
136 | |
137 | |
137 | our $SELF; |
138 | our $SELF; |
138 | |
139 | |
… | |
… | |
180 | |
181 | |
181 | For public nodes, C<$noderef> (supplied either directly to |
182 | For public nodes, C<$noderef> (supplied either directly to |
182 | 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 |
183 | noderef (possibly unresolved, in which case it will be resolved). |
184 | noderef (possibly unresolved, in which case it will be resolved). |
184 | |
185 | |
185 | After resolving, the node will bind itself on all endpoints and try to |
186 | After resolving, the node will bind itself on all endpoints. |
186 | 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 |
187 | 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 |
188 | network. |
203 | network. |
189 | |
204 | |
190 | =item slave nodes |
205 | All the seednodes will also be specially marked to automatically retry |
191 | |
206 | connecting to them indefinitely, so make sure that seednodes are really |
192 | When the C<$noderef> (either as given or overriden by the config file) |
207 | reliable and up (this might also change in the future). |
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 | =back |
|
|
203 | |
|
|
204 | This function will block until all nodes have been resolved and, for slave |
|
|
205 | nodes, until it has successfully established a connection to a master |
|
|
206 | server. |
|
|
207 | |
208 | |
208 | 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 |
209 | 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 |
210 | form of invocation for "daemon"-type nodes. |
211 | form of invocation for "daemon"-type nodes. |
211 | |
212 | |
… | |
… | |
213 | |
214 | |
214 | 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 |
215 | C<aemp>. This form is often used for commandline clients. |
216 | C<aemp>. This form is often used for commandline clients. |
216 | |
217 | |
217 | initialise_node "slave/"; |
218 | initialise_node "slave/"; |
218 | |
|
|
219 | Example: become a slave node to any of the specified master servers. This |
|
|
220 | form is also often used for commandline clients. |
|
|
221 | |
|
|
222 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
|
|
223 | |
219 | |
224 | 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 |
225 | servers to become part of the network. |
221 | servers to become part of the network. |
226 | |
222 | |
227 | initialise_node undef, "master1", "master2"; |
223 | initialise_node undef, "master1", "master2"; |
… | |
… | |
388 | |
384 | |
389 | sub rcv($@) { |
385 | sub rcv($@) { |
390 | my $port = shift; |
386 | my $port = shift; |
391 | my ($noderef, $portid) = split /#/, $port, 2; |
387 | my ($noderef, $portid) = split /#/, $port, 2; |
392 | |
388 | |
393 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
389 | $NODE{$noderef} == $NODE{""} |
394 | 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"; |
395 | |
391 | |
396 | while (@_) { |
392 | while (@_) { |
397 | if (ref $_[0]) { |
393 | if (ref $_[0]) { |
398 | if (my $self = $PORT_DATA{$portid}) { |
394 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
497 | 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" |
498 | (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 |
499 | port). After the monitoring action was invoked, further messages might get |
495 | port). After the monitoring action was invoked, further messages might get |
500 | delivered again. |
496 | delivered again. |
501 | |
497 | |
|
|
498 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
499 | and will not trigger again. |
|
|
500 | |
502 | 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 |
503 | 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 |
504 | "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 |
505 | C<eval> if unsure. |
504 | C<eval> if unsure. |
506 | |
505 | |
… | |
… | |
671 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
670 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
672 | |
671 | |
673 | "$noderef#$id" |
672 | "$noderef#$id" |
674 | } |
673 | } |
675 | |
674 | |
676 | =back |
675 | =item after $timeout, @msg |
677 | |
676 | |
678 | =head1 NODE MESSAGES |
677 | =item after $timeout, $callback |
679 | |
678 | |
680 | 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 |
681 | arguments called C<@reply>, which will simply be used to compose a reply |
680 | specified number of seconds. |
682 | message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and |
|
|
683 | the remaining arguments are simply the message data. |
|
|
684 | |
681 | |
685 | 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. |
686 | |
683 | |
687 | =over 4 |
|
|
688 | |
|
|
689 | =cut |
684 | =cut |
690 | |
685 | |
691 | =item lookup => $name, @reply |
686 | sub after($@) { |
|
|
687 | my ($timeout, @action) = @_; |
692 | |
688 | |
693 | Replies with the port ID of the specified well-known port, or C<undef>. |
689 | my $t; $t = AE::timer $timeout, 0, sub { |
694 | |
690 | undef $t; |
695 | =item devnull => ... |
691 | ref $action[0] |
696 | |
692 | ? $action[0]() |
697 | Generic data sink/CPU heat conversion. |
693 | : snd @action; |
698 | |
694 | }; |
699 | =item relay => $port, @msg |
695 | } |
700 | |
|
|
701 | Simply forwards the message to the given port. |
|
|
702 | |
|
|
703 | =item eval => $string[ @reply] |
|
|
704 | |
|
|
705 | Evaluates the given string. If C<@reply> is given, then a message of the |
|
|
706 | form C<@reply, $@, @evalres> is sent. |
|
|
707 | |
|
|
708 | Example: crash another node. |
|
|
709 | |
|
|
710 | snd $othernode, eval => "exit"; |
|
|
711 | |
|
|
712 | =item time => @reply |
|
|
713 | |
|
|
714 | Replies the the current node time to C<@reply>. |
|
|
715 | |
|
|
716 | Example: tell the current node to send the current time to C<$myport> in a |
|
|
717 | C<timereply> message. |
|
|
718 | |
|
|
719 | snd $NODE, time => $myport, timereply => 1, 2; |
|
|
720 | # => snd $myport, timereply => 1, 2, <time> |
|
|
721 | |
696 | |
722 | =back |
697 | =back |
723 | |
698 | |
724 | =head1 AnyEvent::MP vs. Distributed Erlang |
699 | =head1 AnyEvent::MP vs. Distributed Erlang |
725 | |
700 | |