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.53 by root, Fri Aug 14 15:31:21 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";
348The default callback received all messages not matched by a more specific 344The default callback received all messages not matched by a more specific
349C<tag> match. 345C<tag> match.
350 346
351=item rcv $local_port, tag => $callback->(@msg_without_tag), ... 347=item rcv $local_port, tag => $callback->(@msg_without_tag), ...
352 348
353Register callbacks to be called on messages starting with the given tag on 349Register (or replace) callbacks to be called on messages starting with the
354the given port (and return the port), or unregister it (when C<$callback> 350given tag on the given port (and return the port), or unregister it (when
355is C<$undef>). 351C<$callback> is C<$undef> or missing). There can only be one callback
352registered for each tag.
356 353
357The original message will be passed to the callback, after the first 354The original message will be passed to the callback, after the first
358element (the tag) has been removed. The callback will use the same 355element (the tag) has been removed. The callback will use the same
359environment as the default callback (see above). 356environment as the default callback (see above).
360 357
372 rcv port, 369 rcv port,
373 msg1 => sub { ... }, 370 msg1 => sub { ... },
374 ... 371 ...
375 ; 372 ;
376 373
374Example: temporarily register a rcv callback for a tag matching some port
375(e.g. for a rpc reply) and unregister it after a message was received.
376
377 rcv $port, $otherport => sub {
378 my @reply = @_;
379
380 rcv $SELF, $otherport;
381 };
382
377=cut 383=cut
378 384
379sub rcv($@) { 385sub rcv($@) {
380 my $port = shift; 386 my $port = shift;
381 my ($noderef, $portid) = split /#/, $port, 2; 387 my ($noderef, $portid) = split /#/, $port, 2;
382 388
383 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 389 $NODE{$noderef} == $NODE{""}
384 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";
385 391
386 while (@_) { 392 while (@_) {
387 if (ref $_[0]) { 393 if (ref $_[0]) {
388 if (my $self = $PORT_DATA{$portid}) { 394 if (my $self = $PORT_DATA{$portid}) {
487message loss has been detected. No messages will be lost "in between" 493message loss has been detected. No messages will be lost "in between"
488(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
489port). After the monitoring action was invoked, further messages might get 495port). After the monitoring action was invoked, further messages might get
490delivered again. 496delivered again.
491 497
498Note that monitoring-actions are one-shot: once released, they are removed
499and will not trigger again.
500
492In the first form (callback), the callback is simply called with any 501In the first form (callback), the callback is simply called with any
493number 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
494"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
495C<eval> if unsure. 504C<eval> if unsure.
496 505
656 my $id = "$RUNIQ." . $ID++; 665 my $id = "$RUNIQ." . $ID++;
657 666
658 $_[0] =~ /::/ 667 $_[0] =~ /::/
659 or Carp::croak "spawn init function must be a fully-qualified name, caught"; 668 or Carp::croak "spawn init function must be a fully-qualified name, caught";
660 669
661 ($NODE{$noderef} || add_node $noderef) 670 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_;
662 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]);
663 671
664 "$noderef#$id" 672 "$noderef#$id"
665} 673}
666 674
667=back 675=item after $timeout, @msg
668 676
669=head1 NODE MESSAGES 677=item after $timeout, $callback
670 678
671Nodes understand the following messages sent to them. Many of them take 679Either sends the given message, or call the given callback, after the
672arguments called C<@reply>, which will simply be used to compose a reply 680specified number of seconds.
673message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
674the remaining arguments are simply the message data.
675 681
676While other messages exist, they are not public and subject to change. 682This is simply a utility function that come sin handy at times.
677 683
678=over 4
679
680=cut 684=cut
681 685
682=item lookup => $name, @reply 686sub after($@) {
687 my ($timeout, @action) = @_;
683 688
684Replies with the port ID of the specified well-known port, or C<undef>. 689 my $t; $t = AE::timer $timeout, 0, sub {
685 690 undef $t;
686=item devnull => ... 691 ref $action[0]
687 692 ? $action[0]()
688Generic data sink/CPU heat conversion. 693 : snd @action;
689 694 };
690=item relay => $port, @msg 695}
691
692Simply forwards the message to the given port.
693
694=item eval => $string[ @reply]
695
696Evaluates the given string. If C<@reply> is given, then a message of the
697form C<@reply, $@, @evalres> is sent.
698
699Example: crash another node.
700
701 snd $othernode, eval => "exit";
702
703=item time => @reply
704
705Replies the the current node time to C<@reply>.
706
707Example: tell the current node to send the current time to C<$myport> in a
708C<timereply> message.
709
710 snd $NODE, time => $myport, timereply => 1, 2;
711 # => snd $myport, timereply => 1, 2, <time>
712 696
713=back 697=back
714 698
715=head1 AnyEvent::MP vs. Distributed Erlang 699=head1 AnyEvent::MP vs. Distributed Erlang
716 700
735convenience functionality. 719convenience functionality.
736 720
737This means that AEMP requires a less tightly controlled environment at the 721This means that AEMP requires a less tightly controlled environment at the
738cost of longer node references and a slightly higher management overhead. 722cost of longer node references and a slightly higher management overhead.
739 723
740=item Erlang has a "remote ports are like local ports" philosophy, AEMP 724=item * Erlang has a "remote ports are like local ports" philosophy, AEMP
741uses "local ports are like remote ports". 725uses "local ports are like remote ports".
742 726
743The failure modes for local ports are quite different (runtime errors 727The failure modes for local ports are quite different (runtime errors
744only) then for remote ports - when a local port dies, you I<know> it dies, 728only) then for remote ports - when a local port dies, you I<know> it dies,
745when a connection to another node dies, you know nothing about the other 729when a connection to another node dies, you know nothing about the other

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines