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.60 by root, Mon Aug 24 08:04:52 2009 UTC

126use base "Exporter"; 126use base "Exporter";
127 127
128our $VERSION = $AnyEvent::MP::Kernel::VERSION; 128our $VERSION = $AnyEvent::MP::Kernel::VERSION;
129 129
130our @EXPORT = qw( 130our @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
137our $SELF; 137our $SELF;
138 138
197At least one additional noderef is required (either by specifying it 197At least one additional noderef is required (either by specifying it
198directly or because it is part of the configuration profile): The node 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 199will try to connect to all of them and will become a slave attached to the
200first node it can successfully connect to. 200first node it can successfully connect to.
201 201
202Note that slave nodes cannot change their name, and consequently, their
203master, so if the master goes down, the slave node will not function well
204anymore until it can re-establish conenciton to its master. This makes
205slave nodes unsuitable for long-term nodes or fault-tolerant networks.
206
202=back 207=back
203 208
204This function will block until all nodes have been resolved and, for slave 209This function will block until all nodes have been resolved and, for slave
205nodes, until it has successfully established a connection to a master 210nodes, until it has successfully established a connection to a master
206server. 211server.
212
213All the seednodes will also be specially marked to automatically retry
214connecting to them infinitely.
207 215
208Example: become a public node listening on the guessed noderef, or the one 216Example: 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 217specified via C<aemp> for the current node. This should be the most common
210form of invocation for "daemon"-type nodes. 218form of invocation for "daemon"-type nodes.
211 219
348The default callback received all messages not matched by a more specific 356The default callback received all messages not matched by a more specific
349C<tag> match. 357C<tag> match.
350 358
351=item rcv $local_port, tag => $callback->(@msg_without_tag), ... 359=item rcv $local_port, tag => $callback->(@msg_without_tag), ...
352 360
353Register callbacks to be called on messages starting with the given tag on 361Register (or replace) callbacks to be called on messages starting with the
354the given port (and return the port), or unregister it (when C<$callback> 362given tag on the given port (and return the port), or unregister it (when
355is C<$undef>). 363C<$callback> is C<$undef> or missing). There can only be one callback
364registered for each tag.
356 365
357The original message will be passed to the callback, after the first 366The original message will be passed to the callback, after the first
358element (the tag) has been removed. The callback will use the same 367element (the tag) has been removed. The callback will use the same
359environment as the default callback (see above). 368environment as the default callback (see above).
360 369
372 rcv port, 381 rcv port,
373 msg1 => sub { ... }, 382 msg1 => sub { ... },
374 ... 383 ...
375 ; 384 ;
376 385
386Example: temporarily register a rcv callback for a tag matching some port
387(e.g. for a rpc reply) and unregister it after a message was received.
388
389 rcv $port, $otherport => sub {
390 my @reply = @_;
391
392 rcv $SELF, $otherport;
393 };
394
377=cut 395=cut
378 396
379sub rcv($@) { 397sub rcv($@) {
380 my $port = shift; 398 my $port = shift;
381 my ($noderef, $portid) = split /#/, $port, 2; 399 my ($noderef, $portid) = split /#/, $port, 2;
382 400
383 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 401 $NODE{$noderef} == $NODE{""}
384 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";
385 403
386 while (@_) { 404 while (@_) {
387 if (ref $_[0]) { 405 if (ref $_[0]) {
388 if (my $self = $PORT_DATA{$portid}) { 406 if (my $self = $PORT_DATA{$portid}) {
487message loss has been detected. No messages will be lost "in between" 505message 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 506(after the first lost message no further messages will be received by the
489port). After the monitoring action was invoked, further messages might get 507port). After the monitoring action was invoked, further messages might get
490delivered again. 508delivered again.
491 509
510Note that monitoring-actions are one-shot: once released, they are removed
511and will not trigger again.
512
492In the first form (callback), the callback is simply called with any 513In the first form (callback), the callback is simply called with any
493number of C<@reason> elements (no @reason means that the port was deleted 514number 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 515"normally"). Note also that I<< the callback B<must> never die >>, so use
495C<eval> if unsure. 516C<eval> if unsure.
496 517
656 my $id = "$RUNIQ." . $ID++; 677 my $id = "$RUNIQ." . $ID++;
657 678
658 $_[0] =~ /::/ 679 $_[0] =~ /::/
659 or Carp::croak "spawn init function must be a fully-qualified name, caught"; 680 or Carp::croak "spawn init function must be a fully-qualified name, caught";
660 681
661 ($NODE{$noderef} || add_node $noderef) 682 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_;
662 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]);
663 683
664 "$noderef#$id" 684 "$noderef#$id"
665} 685}
666 686
667=back 687=item after $timeout, @msg
668 688
669=head1 NODE MESSAGES 689=item after $timeout, $callback
670 690
671Nodes understand the following messages sent to them. Many of them take 691Either sends the given message, or call the given callback, after the
672arguments called C<@reply>, which will simply be used to compose a reply 692specified 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 693
676While other messages exist, they are not public and subject to change. 694This is simply a utility function that come sin handy at times.
677 695
678=over 4
679
680=cut 696=cut
681 697
682=item lookup => $name, @reply 698sub after($@) {
699 my ($timeout, @action) = @_;
683 700
684Replies with the port ID of the specified well-known port, or C<undef>. 701 my $t; $t = AE::timer $timeout, 0, sub {
685 702 undef $t;
686=item devnull => ... 703 ref $action[0]
687 704 ? $action[0]()
688Generic data sink/CPU heat conversion. 705 : snd @action;
689 706 };
690=item relay => $port, @msg 707}
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 708
713=back 709=back
714 710
715=head1 AnyEvent::MP vs. Distributed Erlang 711=head1 AnyEvent::MP vs. Distributed Erlang
716 712
735convenience functionality. 731convenience functionality.
736 732
737This means that AEMP requires a less tightly controlled environment at the 733This means that AEMP requires a less tightly controlled environment at the
738cost of longer node references and a slightly higher management overhead. 734cost of longer node references and a slightly higher management overhead.
739 735
740=item Erlang has a "remote ports are like local ports" philosophy, AEMP 736=item * Erlang has a "remote ports are like local ports" philosophy, AEMP
741uses "local ports are like remote ports". 737uses "local ports are like remote ports".
742 738
743The failure modes for local ports are quite different (runtime errors 739The 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, 740only) 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 741when a connection to another node dies, you know nothing about the other

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines