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

Comparing cvsroot/AnyEvent-MP/MP.pm (file contents):
Revision 1.50 by root, Fri Aug 14 14:01:05 2009 UTC vs.
Revision 1.63 by root, Thu Aug 27 21:29:37 2009 UTC

11 NODE $port # returns the noderef of the port 11 NODE $port # returns the noderef of the port
12 12
13 $SELF # receiving/own port id in rcv callbacks 13 $SELF # receiving/own port id in rcv callbacks
14 14
15 # initialise the node so it can send/receive messages 15 # initialise the node so it can send/receive messages
16 initialise_node; # -OR- 16 initialise_node;
17 initialise_node "localhost:4040"; # -OR-
18 initialise_node "slave/", "localhost:4040"
19 17
20 # ports are message endpoints 18 # ports are message endpoints
21 19
22 # sending messages 20 # sending messages
23 snd $port, type => data...; 21 snd $port, type => data...;
24 snd $port, @msg; 22 snd $port, @msg;
25 snd @msg_with_first_element_being_a_port; 23 snd @msg_with_first_element_being_a_port;
26 24
27 # creating/using ports, the simple way 25 # creating/using ports, the simple way
28 my $somple_port = port { my @msg = @_; 0 }; 26 my $simple_port = port { my @msg = @_; 0 };
29 27
30 # creating/using ports, type matching 28 # creating/using ports, tagged message matching
31 my $port = port; 29 my $port = port;
32 rcv $port, ping => sub { snd $_[0], "pong"; 0 }; 30 rcv $port, ping => sub { snd $_[0], "pong"; 0 };
33 rcv $port, pong => sub { warn "pong received\n"; 0 }; 31 rcv $port, pong => sub { warn "pong received\n"; 0 };
34 32
35 # create a port on another node 33 # create a port on another node
69 67
70=item port 68=item port
71 69
72A port is something you can send messages to (with the C<snd> function). 70A port is something you can send messages to (with the C<snd> function).
73 71
74Some ports allow you to register C<rcv> handlers that can match specific 72Ports allow you to register C<rcv> handlers that can match all or just
75messages. All C<rcv> handlers will receive messages they match, messages 73some messages. Messages will not be queued.
76will not be queued.
77 74
78=item port id - C<noderef#portname> 75=item port ID - C<noderef#portname>
79 76
80A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as 77A 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 78separator, and a port name (a printable string of unspecified format). An
82exception is the the node port, whose ID is identical to its node 79exception is the the node port, whose ID is identical to its node
83reference. 80reference.
84 81
85=item node 82=item node
86 83
87A node is a single process containing at least one port - the node 84A 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 85which provides nodes to manage each other remotely, and to create new
89create new ports, among other things. 86ports.
90 87
91Nodes are either private (single-process only), slaves (connected to a 88Nodes are either private (single-process only), slaves (can only talk to
92master node only) or public nodes (connectable from unrelated nodes). 89public nodes, but do not need an open port) or public nodes (connectable
90from any other node).
93 91
94=item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> 92=item node ID - C<[a-za-Z0-9_\-.:]+>
95 93
96A node reference is a string that either simply identifies the node (for 94A node ID is a string that either simply identifies the node (for
97private and slave nodes), or contains a recipe on how to reach a given 95private and slave nodes), or contains a recipe on how to reach a given
98node (for public nodes). 96node (for public nodes).
99 97
100This recipe is simply a comma-separated list of C<address:port> pairs (for 98This recipe is simply a comma-separated list of C<address:port> pairs (for
101TCP/IP, other protocols might look different). 99TCP/IP, other protocols might look different).
127use base "Exporter"; 125use base "Exporter";
128 126
129our $VERSION = $AnyEvent::MP::Kernel::VERSION; 127our $VERSION = $AnyEvent::MP::Kernel::VERSION;
130 128
131our @EXPORT = qw( 129our @EXPORT = qw(
132 NODE $NODE *SELF node_of _any_ 130 NODE $NODE *SELF node_of after
133 resolve_node initialise_node 131 resolve_node initialise_node
134 snd rcv mon kil reg psub spawn 132 snd rcv mon mon_guard kil reg psub spawn
135 port 133 port
136); 134);
137 135
138our $SELF; 136our $SELF;
139 137
143 kil $SELF, die => $msg; 141 kil $SELF, die => $msg;
144} 142}
145 143
146=item $thisnode = NODE / $NODE 144=item $thisnode = NODE / $NODE
147 145
148The C<NODE> function returns, and the C<$NODE> variable contains 146The C<NODE> function returns, and the C<$NODE> variable contains the
149the noderef of the local node. The value is initialised by a call 147node id of the local node. The value is initialised by a call to
150to C<become_public> or C<become_slave>, after which all local port 148C<initialise_node>.
151identifiers become invalid.
152 149
153=item $noderef = node_of $port 150=item $nodeid = node_of $port
154 151
155Extracts and returns the noderef from a portid or a noderef. 152Extracts and returns the noderef from a port ID or a node ID.
156 153
157=item initialise_node $noderef, $seednode, $seednode... 154=item initialise_node $profile_name
158
159=item initialise_node "slave/", $master, $master...
160 155
161Before a node can talk to other nodes on the network it has to initialise 156Before a node can talk to other nodes on the network it has to initialise
162itself - the minimum a node needs to know is it's own name, and optionally 157itself - the minimum a node needs to know is it's own name, and optionally
163it should know the noderefs of some other nodes in the network. 158it should know the noderefs of some other nodes in the network.
164 159
182 177
183For public nodes, C<$noderef> (supplied either directly to 178For public nodes, C<$noderef> (supplied either directly to
184C<initialise_node> or indirectly via a profile or the nodename) must be a 179C<initialise_node> or indirectly via a profile or the nodename) must be a
185noderef (possibly unresolved, in which case it will be resolved). 180noderef (possibly unresolved, in which case it will be resolved).
186 181
187After resolving, the node will bind itself on all endpoints and try to 182After resolving, the node will bind itself on all endpoints.
188connect to all additional C<$seednodes> that are specified. Seednodes are 183
184=item slave nodes
185
186When the C<$noderef> (either as given or overriden by the config file)
187is the special string C<slave/>, then the node will become a slave
188node. Slave nodes cannot be contacted from outside, and cannot talk to
189each other (at least in this version of AnyEvent::MP).
190
191Slave nodes work by creating connections to all public nodes, using the
192L<AnyEvent::MP::Global> service.
193
194=back
195
196After initialising itself, the node will connect to all additional
197C<$seednodes> that are specified diretcly or via a profile. Seednodes are
189optional and can be used to quickly bootstrap the node into an existing 198optional and can be used to quickly bootstrap the node into an existing
190network. 199network.
191 200
192=item slave nodes 201All the seednodes will also be specially marked to automatically retry
193 202connecting to them indefinitely, so make sure that seednodes are really
194When the C<$noderef> (either as given or overriden by the config file) 203reliable and up (this might also change in the future).
195is the special string C<slave/>, then the node will become a slave
196node. Slave nodes cannot be contacted from outside and will route most of
197their traffic to the master node that they attach to.
198
199At least one additional noderef is required (either by specifying it
200directly or because it is part of the configuration profile): The node
201will try to connect to all of them and will become a slave attached to the
202first node it can successfully connect to.
203
204=back
205
206This function will block until all nodes have been resolved and, for slave
207nodes, until it has successfully established a connection to a master
208server.
209 204
210Example: become a public node listening on the guessed noderef, or the one 205Example: become a public node listening on the guessed noderef, or the one
211specified via C<aemp> for the current node. This should be the most common 206specified via C<aemp> for the current node. This should be the most common
212form of invocation for "daemon"-type nodes. 207form of invocation for "daemon"-type nodes.
213 208
216Example: become a slave node to any of the the seednodes specified via 211Example: become a slave node to any of the the seednodes specified via
217C<aemp>. This form is often used for commandline clients. 212C<aemp>. This form is often used for commandline clients.
218 213
219 initialise_node "slave/"; 214 initialise_node "slave/";
220 215
221Example: become a slave node to any of the specified master servers. This
222form is also often used for commandline clients.
223
224 initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net";
225
226Example: become a public node, and try to contact some well-known master 216Example: become a public node, and try to contact some well-known master
227servers to become part of the network. 217servers to become part of the network.
228 218
229 initialise_node undef, "master1", "master2"; 219 initialise_node undef, "master1", "master2";
230 220
279=item snd $port, type => @data 269=item snd $port, type => @data
280 270
281=item snd $port, @msg 271=item snd $port, @msg
282 272
283Send the given message to the given port ID, which can identify either 273Send the given message to the given port ID, which can identify either
284a local or a remote port, and can be either a string or soemthignt hat 274a local or a remote port, and must be a port ID.
285stringifies a sa port ID (such as a port object :).
286 275
287While the message can be about anything, it is highly recommended to use a 276While the message can be about anything, it is highly recommended to use a
288string as first element (a portid, or some word that indicates a request 277string as first element (a port ID, or some word that indicates a request
289type etc.). 278type etc.).
290 279
291The message data effectively becomes read-only after a call to this 280The message data effectively becomes read-only after a call to this
292function: modifying any argument is not allowed and can cause many 281function: modifying any argument is not allowed and can cause many
293problems. 282problems.
351The default callback received all messages not matched by a more specific 340The default callback received all messages not matched by a more specific
352C<tag> match. 341C<tag> match.
353 342
354=item rcv $local_port, tag => $callback->(@msg_without_tag), ... 343=item rcv $local_port, tag => $callback->(@msg_without_tag), ...
355 344
356Register callbacks to be called on messages starting with the given tag on 345Register (or replace) callbacks to be called on messages starting with the
357the given port (and return the port), or unregister it (when C<$callback> 346given tag on the given port (and return the port), or unregister it (when
358is C<$undef>). 347C<$callback> is C<$undef> or missing). There can only be one callback
348registered for each tag.
359 349
360The original message will be passed to the callback, after the first 350The original message will be passed to the callback, after the first
361element (the tag) has been removed. The callback will use the same 351element (the tag) has been removed. The callback will use the same
362environment as the default callback (see above). 352environment as the default callback (see above).
363 353
375 rcv port, 365 rcv port,
376 msg1 => sub { ... }, 366 msg1 => sub { ... },
377 ... 367 ...
378 ; 368 ;
379 369
370Example: temporarily register a rcv callback for a tag matching some port
371(e.g. for a rpc reply) and unregister it after a message was received.
372
373 rcv $port, $otherport => sub {
374 my @reply = @_;
375
376 rcv $SELF, $otherport;
377 };
378
380=cut 379=cut
381 380
382sub rcv($@) { 381sub rcv($@) {
383 my $port = shift; 382 my $port = shift;
384 my ($noderef, $portid) = split /#/, $port, 2; 383 my ($noderef, $portid) = split /#/, $port, 2;
385 384
386 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 385 $NODE{$noderef} == $NODE{""}
387 or Carp::croak "$port: rcv can only be called on local ports, caught"; 386 or Carp::croak "$port: rcv can only be called on local ports, caught";
388 387
389 while (@_) { 388 while (@_) {
390 if (ref $_[0]) { 389 if (ref $_[0]) {
391 if (my $self = $PORT_DATA{$portid}) { 390 if (my $self = $PORT_DATA{$portid}) {
490message loss has been detected. No messages will be lost "in between" 489message loss has been detected. No messages will be lost "in between"
491(after the first lost message no further messages will be received by the 490(after the first lost message no further messages will be received by the
492port). After the monitoring action was invoked, further messages might get 491port). After the monitoring action was invoked, further messages might get
493delivered again. 492delivered again.
494 493
494Note that monitoring-actions are one-shot: once released, they are removed
495and will not trigger again.
496
495In the first form (callback), the callback is simply called with any 497In the first form (callback), the callback is simply called with any
496number of C<@reason> elements (no @reason means that the port was deleted 498number of C<@reason> elements (no @reason means that the port was deleted
497"normally"). Note also that I<< the callback B<must> never die >>, so use 499"normally"). Note also that I<< the callback B<must> never die >>, so use
498C<eval> if unsure. 500C<eval> if unsure.
499 501
659 my $id = "$RUNIQ." . $ID++; 661 my $id = "$RUNIQ." . $ID++;
660 662
661 $_[0] =~ /::/ 663 $_[0] =~ /::/
662 or Carp::croak "spawn init function must be a fully-qualified name, caught"; 664 or Carp::croak "spawn init function must be a fully-qualified name, caught";
663 665
664 ($NODE{$noderef} || add_node $noderef) 666 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_;
665 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]);
666 667
667 "$noderef#$id" 668 "$noderef#$id"
668} 669}
669 670
670=back 671=item after $timeout, @msg
671 672
672=head1 NODE MESSAGES 673=item after $timeout, $callback
673 674
674Nodes understand the following messages sent to them. Many of them take 675Either sends the given message, or call the given callback, after the
675arguments called C<@reply>, which will simply be used to compose a reply 676specified number of seconds.
676message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
677the remaining arguments are simply the message data.
678 677
679While other messages exist, they are not public and subject to change. 678This is simply a utility function that come sin handy at times.
680 679
681=over 4
682
683=cut 680=cut
684 681
685=item lookup => $name, @reply 682sub after($@) {
683 my ($timeout, @action) = @_;
686 684
687Replies with the port ID of the specified well-known port, or C<undef>. 685 my $t; $t = AE::timer $timeout, 0, sub {
688 686 undef $t;
689=item devnull => ... 687 ref $action[0]
690 688 ? $action[0]()
691Generic data sink/CPU heat conversion. 689 : snd @action;
692 690 };
693=item relay => $port, @msg 691}
694
695Simply forwards the message to the given port.
696
697=item eval => $string[ @reply]
698
699Evaluates the given string. If C<@reply> is given, then a message of the
700form C<@reply, $@, @evalres> is sent.
701
702Example: crash another node.
703
704 snd $othernode, eval => "exit";
705
706=item time => @reply
707
708Replies the the current node time to C<@reply>.
709
710Example: tell the current node to send the current time to C<$myport> in a
711C<timereply> message.
712
713 snd $NODE, time => $myport, timereply => 1, 2;
714 # => snd $myport, timereply => 1, 2, <time>
715 692
716=back 693=back
717 694
718=head1 AnyEvent::MP vs. Distributed Erlang 695=head1 AnyEvent::MP vs. Distributed Erlang
719 696
738convenience functionality. 715convenience functionality.
739 716
740This means that AEMP requires a less tightly controlled environment at the 717This means that AEMP requires a less tightly controlled environment at the
741cost of longer node references and a slightly higher management overhead. 718cost of longer node references and a slightly higher management overhead.
742 719
720=item * Erlang has a "remote ports are like local ports" philosophy, AEMP
721uses "local ports are like remote ports".
722
723The failure modes for local ports are quite different (runtime errors
724only) then for remote ports - when a local port dies, you I<know> it dies,
725when a connection to another node dies, you know nothing about the other
726port.
727
728Erlang pretends remote ports are as reliable as local ports, even when
729they are not.
730
731AEMP encourages a "treat remote ports differently" philosophy, with local
732ports being the special case/exception, where transport errors cannot
733occur.
734
743=item * Erlang uses processes and a mailbox, AEMP does not queue. 735=item * Erlang uses processes and a mailbox, AEMP does not queue.
744 736
745Erlang uses processes that selctively receive messages, and therefore 737Erlang uses processes that selectively receive messages, and therefore
746needs a queue. AEMP is event based, queuing messages would serve no useful 738needs a queue. AEMP is event based, queuing messages would serve no
747purpose. 739useful purpose. For the same reason the pattern-matching abilities of
740AnyEvent::MP are more limited, as there is little need to be able to
741filter messages without dequeing them.
748 742
749(But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). 743(But see L<Coro::MP> for a more Erlang-like process model on top of AEMP).
750 744
751=item * Erlang sends are synchronous, AEMP sends are asynchronous. 745=item * Erlang sends are synchronous, AEMP sends are asynchronous.
752 746
753Sending messages in Erlang is synchronous and blocks the process. AEMP 747Sending messages in Erlang is synchronous and blocks the process (and
754sends are immediate, connection establishment is handled in the 748so does not need a queue that can overflow). AEMP sends are immediate,
755background. 749connection establishment is handled in the background.
756 750
757=item * Erlang can silently lose messages, AEMP cannot. 751=item * Erlang suffers from silent message loss, AEMP does not.
758 752
759Erlang makes few guarantees on messages delivery - messages can get lost 753Erlang makes few guarantees on messages delivery - messages can get lost
760without any of the processes realising it (i.e. you send messages a, b, 754without any of the processes realising it (i.e. you send messages a, b,
761and c, and the other side only receives messages a and c). 755and c, and the other side only receives messages a and c).
762 756
774eventually be killed - it cannot happen that a node detects a port as dead 768eventually be killed - it cannot happen that a node detects a port as dead
775and then later sends messages to it, finding it is still alive. 769and then later sends messages to it, finding it is still alive.
776 770
777=item * Erlang can send messages to the wrong port, AEMP does not. 771=item * Erlang can send messages to the wrong port, AEMP does not.
778 772
779In Erlang it is quite possible that a node that restarts reuses a process 773In Erlang it is quite likely that a node that restarts reuses a process ID
780ID known to other nodes for a completely different process, causing 774known to other nodes for a completely different process, causing messages
781messages destined for that process to end up in an unrelated process. 775destined for that process to end up in an unrelated process.
782 776
783AEMP never reuses port IDs, so old messages or old port IDs floating 777AEMP never reuses port IDs, so old messages or old port IDs floating
784around in the network will not be sent to an unrelated port. 778around in the network will not be sent to an unrelated port.
785 779
786=item * Erlang uses unprotected connections, AEMP uses secure 780=item * Erlang uses unprotected connections, AEMP uses secure

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines