… | |
… | |
126 | use base "Exporter"; |
126 | use base "Exporter"; |
127 | |
127 | |
128 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
128 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
129 | |
129 | |
130 | our @EXPORT = qw( |
130 | our @EXPORT = qw( |
131 | NODE $NODE *SELF node_of _any_ |
131 | NODE $NODE *SELF node_of after |
132 | resolve_node initialise_node |
132 | resolve_node initialise_node |
133 | snd rcv mon kil reg psub spawn |
133 | snd rcv mon mob_guard kil reg psub spawn |
134 | port |
134 | port |
135 | ); |
135 | ); |
136 | |
136 | |
137 | our $SELF; |
137 | our $SELF; |
138 | |
138 | |
… | |
… | |
197 | At least one additional noderef is required (either by specifying it |
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 |
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 |
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. |
200 | first node it can successfully connect to. |
201 | |
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 | |
202 | =back |
207 | =back |
203 | |
208 | |
204 | This function will block until all nodes have been resolved and, for slave |
209 | 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 |
210 | nodes, until it has successfully established a connection to a master |
206 | server. |
211 | server. |
|
|
212 | |
|
|
213 | All the seednodes will also be specially marked to automatically retry |
|
|
214 | connecting to them infinitely. |
207 | |
215 | |
208 | Example: become a public node listening on the guessed noderef, or the one |
216 | 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 |
217 | specified via C<aemp> for the current node. This should be the most common |
210 | form of invocation for "daemon"-type nodes. |
218 | form of invocation for "daemon"-type nodes. |
211 | |
219 | |
… | |
… | |
388 | |
396 | |
389 | sub rcv($@) { |
397 | sub rcv($@) { |
390 | my $port = shift; |
398 | my $port = shift; |
391 | my ($noderef, $portid) = split /#/, $port, 2; |
399 | my ($noderef, $portid) = split /#/, $port, 2; |
392 | |
400 | |
393 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
401 | $NODE{$noderef} == $NODE{""} |
394 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
402 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
395 | |
403 | |
396 | while (@_) { |
404 | while (@_) { |
397 | if (ref $_[0]) { |
405 | if (ref $_[0]) { |
398 | if (my $self = $PORT_DATA{$portid}) { |
406 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
497 | message loss has been detected. No messages will be lost "in between" |
505 | 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 |
506 | (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 |
507 | port). After the monitoring action was invoked, further messages might get |
500 | delivered again. |
508 | delivered again. |
501 | |
509 | |
|
|
510 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
511 | and will not trigger again. |
|
|
512 | |
502 | In the first form (callback), the callback is simply called with any |
513 | 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 |
514 | 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 |
515 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
505 | C<eval> if unsure. |
516 | C<eval> if unsure. |
506 | |
517 | |
… | |
… | |
671 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
682 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
672 | |
683 | |
673 | "$noderef#$id" |
684 | "$noderef#$id" |
674 | } |
685 | } |
675 | |
686 | |
676 | =back |
687 | =item after $timeout, @msg |
677 | |
688 | |
678 | =head1 NODE MESSAGES |
689 | =item after $timeout, $callback |
679 | |
690 | |
680 | Nodes understand the following messages sent to them. Many of them take |
691 | 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 |
692 | 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 | |
693 | |
685 | While other messages exist, they are not public and subject to change. |
694 | This is simply a utility function that come sin handy at times. |
686 | |
695 | |
687 | =over 4 |
|
|
688 | |
|
|
689 | =cut |
696 | =cut |
690 | |
697 | |
691 | =item lookup => $name, @reply |
698 | sub after($@) { |
|
|
699 | my ($timeout, @action) = @_; |
692 | |
700 | |
693 | Replies with the port ID of the specified well-known port, or C<undef>. |
701 | my $t; $t = AE::timer $timeout, 0, sub { |
694 | |
702 | undef $t; |
695 | =item devnull => ... |
703 | ref $action[0] |
696 | |
704 | ? $action[0]() |
697 | Generic data sink/CPU heat conversion. |
705 | : snd @action; |
698 | |
706 | }; |
699 | =item relay => $port, @msg |
707 | } |
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 | |
708 | |
722 | =back |
709 | =back |
723 | |
710 | |
724 | =head1 AnyEvent::MP vs. Distributed Erlang |
711 | =head1 AnyEvent::MP vs. Distributed Erlang |
725 | |
712 | |