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.52 by root, Fri Aug 14 15:13:20 2009 UTC vs.
Revision 1.61 by root, Mon Aug 24 08:06:49 2009 UTC

23 snd $port, type => data...; 23 snd $port, type => data...;
24 snd $port, @msg; 24 snd $port, @msg;
25 snd @msg_with_first_element_being_a_port; 25 snd @msg_with_first_element_being_a_port;
26 26
27 # creating/using ports, the simple way 27 # creating/using ports, the simple way
28 my $somple_port = port { my @msg = @_; 0 }; 28 my $simple_port = port { my @msg = @_; 0 };
29 29
30 # creating/using ports, tagged message matching 30 # creating/using ports, tagged message matching
31 my $port = port; 31 my $port = port;
32 rcv $port, ping => sub { snd $_[0], "pong"; 0 }; 32 rcv $port, ping => sub { snd $_[0], "pong"; 0 };
33 rcv $port, pong => sub { warn "pong received\n"; 0 }; 33 rcv $port, pong => sub { warn "pong received\n"; 0 };
69 69
70=item port 70=item port
71 71
72A port is something you can send messages to (with the C<snd> function). 72A port is something you can send messages to (with the C<snd> function).
73 73
74Some ports allow you to register C<rcv> handlers that can match specific 74Ports allow you to register C<rcv> handlers that can match all or just
75messages. All C<rcv> handlers will receive messages they match, messages 75some messages. Messages will not be queued.
76will not be queued.
77 76
78=item port id - C<noderef#portname> 77=item port id - C<noderef#portname>
79 78
80A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as 79A port ID is the concatenation of a noderef, a hash-mark (C<#>) as
81separator, and a port name (a printable string of unspecified format). An 80separator, and a port name (a printable string of unspecified format). An
82exception is the the node port, whose ID is identical to its node 81exception is the the node port, whose ID is identical to its node
83reference. 82reference.
84 83
85=item node 84=item node
86 85
87A node is a single process containing at least one port - the node 86A node is a single process containing at least one port - the node port,
88port. You can send messages to node ports to find existing ports or to 87which provides nodes to manage each other remotely, and to create new
89create new ports, among other things. 88ports.
90 89
91Nodes are either private (single-process only), slaves (connected to a 90Nodes are either private (single-process only), slaves (connected to a
92master node only) or public nodes (connectable from unrelated nodes). 91master node only) or public nodes (connectable from unrelated nodes).
93 92
94=item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> 93=item noderef - C<host:port,host:port...>, C<id@noderef>, C<id>
127use base "Exporter"; 126use base "Exporter";
128 127
129our $VERSION = $AnyEvent::MP::Kernel::VERSION; 128our $VERSION = $AnyEvent::MP::Kernel::VERSION;
130 129
131our @EXPORT = qw( 130our @EXPORT = qw(
132 NODE $NODE *SELF node_of _any_ 131 NODE $NODE *SELF node_of after
133 resolve_node initialise_node 132 resolve_node initialise_node
134 snd rcv mon kil reg psub spawn 133 snd rcv mon mon_guard kil reg psub spawn
135 port 134 port
136); 135);
137 136
138our $SELF; 137our $SELF;
139 138
198At least one additional noderef is required (either by specifying it 197At least one additional noderef is required (either by specifying it
199directly or because it is part of the configuration profile): The node 198directly or because it is part of the configuration profile): The node
200will 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
201first node it can successfully connect to. 200first node it can successfully connect to.
202 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
203=back 207=back
204 208
205This 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
206nodes, until it has successfully established a connection to a master 210nodes, until it has successfully established a connection to a master
207server. 211server.
212
213All the seednodes will also be specially marked to automatically retry
214connecting to them infinitely.
208 215
209Example: 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
210specified 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
211form of invocation for "daemon"-type nodes. 218form of invocation for "daemon"-type nodes.
212 219
349The default callback received all messages not matched by a more specific 356The default callback received all messages not matched by a more specific
350C<tag> match. 357C<tag> match.
351 358
352=item rcv $local_port, tag => $callback->(@msg_without_tag), ... 359=item rcv $local_port, tag => $callback->(@msg_without_tag), ...
353 360
354Register callbacks to be called on messages starting with the given tag on 361Register (or replace) callbacks to be called on messages starting with the
355the 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
356is C<$undef>). 363C<$callback> is C<$undef> or missing). There can only be one callback
364registered for each tag.
357 365
358The original message will be passed to the callback, after the first 366The original message will be passed to the callback, after the first
359element (the tag) has been removed. The callback will use the same 367element (the tag) has been removed. The callback will use the same
360environment as the default callback (see above). 368environment as the default callback (see above).
361 369
373 rcv port, 381 rcv port,
374 msg1 => sub { ... }, 382 msg1 => sub { ... },
375 ... 383 ...
376 ; 384 ;
377 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
378=cut 395=cut
379 396
380sub rcv($@) { 397sub rcv($@) {
381 my $port = shift; 398 my $port = shift;
382 my ($noderef, $portid) = split /#/, $port, 2; 399 my ($noderef, $portid) = split /#/, $port, 2;
383 400
384 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 401 $NODE{$noderef} == $NODE{""}
385 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";
386 403
387 while (@_) { 404 while (@_) {
388 if (ref $_[0]) { 405 if (ref $_[0]) {
389 if (my $self = $PORT_DATA{$portid}) { 406 if (my $self = $PORT_DATA{$portid}) {
488message loss has been detected. No messages will be lost "in between" 505message loss has been detected. No messages will be lost "in between"
489(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
490port). After the monitoring action was invoked, further messages might get 507port). After the monitoring action was invoked, further messages might get
491delivered again. 508delivered again.
492 509
510Note that monitoring-actions are one-shot: once released, they are removed
511and will not trigger again.
512
493In the first form (callback), the callback is simply called with any 513In the first form (callback), the callback is simply called with any
494number 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
495"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
496C<eval> if unsure. 516C<eval> if unsure.
497 517
657 my $id = "$RUNIQ." . $ID++; 677 my $id = "$RUNIQ." . $ID++;
658 678
659 $_[0] =~ /::/ 679 $_[0] =~ /::/
660 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";
661 681
662 ($NODE{$noderef} || add_node $noderef) 682 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_;
663 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]);
664 683
665 "$noderef#$id" 684 "$noderef#$id"
666} 685}
667 686
668=back 687=item after $timeout, @msg
669 688
670=head1 NODE MESSAGES 689=item after $timeout, $callback
671 690
672Nodes understand the following messages sent to them. Many of them take 691Either sends the given message, or call the given callback, after the
673arguments called C<@reply>, which will simply be used to compose a reply 692specified number of seconds.
674message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
675the remaining arguments are simply the message data.
676 693
677While other messages exist, they are not public and subject to change. 694This is simply a utility function that come sin handy at times.
678 695
679=over 4
680
681=cut 696=cut
682 697
683=item lookup => $name, @reply 698sub after($@) {
699 my ($timeout, @action) = @_;
684 700
685Replies with the port ID of the specified well-known port, or C<undef>. 701 my $t; $t = AE::timer $timeout, 0, sub {
686 702 undef $t;
687=item devnull => ... 703 ref $action[0]
688 704 ? $action[0]()
689Generic data sink/CPU heat conversion. 705 : snd @action;
690 706 };
691=item relay => $port, @msg 707}
692
693Simply forwards the message to the given port.
694
695=item eval => $string[ @reply]
696
697Evaluates the given string. If C<@reply> is given, then a message of the
698form C<@reply, $@, @evalres> is sent.
699
700Example: crash another node.
701
702 snd $othernode, eval => "exit";
703
704=item time => @reply
705
706Replies the the current node time to C<@reply>.
707
708Example: tell the current node to send the current time to C<$myport> in a
709C<timereply> message.
710
711 snd $NODE, time => $myport, timereply => 1, 2;
712 # => snd $myport, timereply => 1, 2, <time>
713 708
714=back 709=back
715 710
716=head1 AnyEvent::MP vs. Distributed Erlang 711=head1 AnyEvent::MP vs. Distributed Erlang
717 712
736convenience functionality. 731convenience functionality.
737 732
738This means that AEMP requires a less tightly controlled environment at the 733This means that AEMP requires a less tightly controlled environment at the
739cost of longer node references and a slightly higher management overhead. 734cost of longer node references and a slightly higher management overhead.
740 735
741=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
742uses "local ports are like remote ports". 737uses "local ports are like remote ports".
743 738
744The failure modes for local ports are quite different (runtime errors 739The failure modes for local ports are quite different (runtime errors
745only) 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,
746when 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