ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP.pm
(Generate patch)

Comparing AnyEvent-MP/MP.pm (file contents):
Revision 1.55 by root, Fri Aug 14 23:17:17 2009 UTC vs.
Revision 1.62 by root, Thu Aug 27 07:12:48 2009 UTC

85 85
86A node is a single process containing at least one port - the node port, 86A node is a single process containing at least one port - the node port,
87which provides nodes to manage each other remotely, and to create new 87which provides nodes to manage each other remotely, and to create new
88ports. 88ports.
89 89
90Nodes are either private (single-process only), slaves (connected to a 90Nodes are either private (single-process only), slaves (can only talk to
91master node only) or public nodes (connectable from unrelated nodes). 91public nodes, but do not need an open port) or public nodes (connectable
92from 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
95A node reference is a string that either simply identifies the node (for 96A node reference is a string that either simply identifies the node (for
96private and slave nodes), or contains a recipe on how to reach a given 97private and slave nodes), or contains a recipe on how to reach a given
126use base "Exporter"; 127use base "Exporter";
127 128
128our $VERSION = $AnyEvent::MP::Kernel::VERSION; 129our $VERSION = $AnyEvent::MP::Kernel::VERSION;
129 130
130our @EXPORT = qw( 131our @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
137our $SELF; 138our $SELF;
138 139
180 181
181For public nodes, C<$noderef> (supplied either directly to 182For public nodes, C<$noderef> (supplied either directly to
182C<initialise_node> or indirectly via a profile or the nodename) must be a 183C<initialise_node> or indirectly via a profile or the nodename) must be a
183noderef (possibly unresolved, in which case it will be resolved). 184noderef (possibly unresolved, in which case it will be resolved).
184 185
185After resolving, the node will bind itself on all endpoints and try to 186After resolving, the node will bind itself on all endpoints.
186connect to all additional C<$seednodes> that are specified. Seednodes are 187
188=item slave nodes
189
190When the C<$noderef> (either as given or overriden by the config file)
191is the special string C<slave/>, then the node will become a slave
192node. Slave nodes cannot be contacted from outside, and cannot talk to
193each other (at least in this version of AnyEvent::MP).
194
195Slave nodes work by creating connections to all public nodes, using the
196L<AnyEvent::MP::Global> service.
197
198=back
199
200After initialising itself, the node will connect to all additional
201C<$seednodes> that are specified diretcly or via a profile. Seednodes are
187optional and can be used to quickly bootstrap the node into an existing 202optional and can be used to quickly bootstrap the node into an existing
188network. 203network.
189 204
190=item slave nodes 205All the seednodes will also be specially marked to automatically retry
191 206connecting to them indefinitely, so make sure that seednodes are really
192When the C<$noderef> (either as given or overriden by the config file) 207reliable and up (this might also change in the future).
193is the special string C<slave/>, then the node will become a slave
194node. Slave nodes cannot be contacted from outside and will route most of
195their traffic to the master node that they attach to.
196
197At least one additional noderef is required (either by specifying it
198directly or because it is part of the configuration profile): The node
199will try to connect to all of them and will become a slave attached to the
200first node it can successfully connect to.
201
202=back
203
204This function will block until all nodes have been resolved and, for slave
205nodes, until it has successfully established a connection to a master
206server.
207 208
208Example: become a public node listening on the guessed noderef, or the one 209Example: become a public node listening on the guessed noderef, or the one
209specified via C<aemp> for the current node. This should be the most common 210specified via C<aemp> for the current node. This should be the most common
210form of invocation for "daemon"-type nodes. 211form of invocation for "daemon"-type nodes.
211 212
213 214
214Example: become a slave node to any of the the seednodes specified via 215Example: become a slave node to any of the the seednodes specified via
215C<aemp>. This form is often used for commandline clients. 216C<aemp>. This form is often used for commandline clients.
216 217
217 initialise_node "slave/"; 218 initialise_node "slave/";
218
219Example: become a slave node to any of the specified master servers. This
220form is also often used for commandline clients.
221
222 initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net";
223 219
224Example: become a public node, and try to contact some well-known master 220Example: become a public node, and try to contact some well-known master
225servers to become part of the network. 221servers to become part of the network.
226 222
227 initialise_node undef, "master1", "master2"; 223 initialise_node undef, "master1", "master2";
388 384
389sub rcv($@) { 385sub 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}) {
497message loss has been detected. No messages will be lost "in between" 493message 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
499port). After the monitoring action was invoked, further messages might get 495port). After the monitoring action was invoked, further messages might get
500delivered again. 496delivered again.
501 497
498Note that monitoring-actions are one-shot: once released, they are removed
499and will not trigger again.
500
502In the first form (callback), the callback is simply called with any 501In the first form (callback), the callback is simply called with any
503number of C<@reason> elements (no @reason means that the port was deleted 502number 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
505C<eval> if unsure. 504C<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
680Nodes understand the following messages sent to them. Many of them take 679Either sends the given message, or call the given callback, after the
681arguments called C<@reply>, which will simply be used to compose a reply 680specified number of seconds.
682message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
683the remaining arguments are simply the message data.
684 681
685While other messages exist, they are not public and subject to change. 682This 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 686sub after($@) {
687 my ($timeout, @action) = @_;
692 688
693Replies 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]()
697Generic data sink/CPU heat conversion. 693 : snd @action;
698 694 };
699=item relay => $port, @msg 695}
700
701Simply forwards the message to the given port.
702
703=item eval => $string[ @reply]
704
705Evaluates the given string. If C<@reply> is given, then a message of the
706form C<@reply, $@, @evalres> is sent.
707
708Example: crash another node.
709
710 snd $othernode, eval => "exit";
711
712=item time => @reply
713
714Replies the the current node time to C<@reply>.
715
716Example: tell the current node to send the current time to C<$myport> in a
717C<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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines