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.62 by root, Thu Aug 27 07:12:48 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 (can only talk to
92master 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).
93 93
94=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>
95 95
96A 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
97private 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
127use base "Exporter"; 127use base "Exporter";
128 128
129our $VERSION = $AnyEvent::MP::Kernel::VERSION; 129our $VERSION = $AnyEvent::MP::Kernel::VERSION;
130 130
131our @EXPORT = qw( 131our @EXPORT = qw(
132 NODE $NODE *SELF node_of _any_ 132 NODE $NODE *SELF node_of after
133 resolve_node initialise_node 133 resolve_node initialise_node
134 snd rcv mon kil reg psub spawn 134 snd rcv mon mon_guard kil reg psub spawn
135 port 135 port
136); 136);
137 137
138our $SELF; 138our $SELF;
139 139
181 181
182For public nodes, C<$noderef> (supplied either directly to 182For public nodes, C<$noderef> (supplied either directly to
183C<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
184noderef (possibly unresolved, in which case it will be resolved). 184noderef (possibly unresolved, in which case it will be resolved).
185 185
186After resolving, the node will bind itself on all endpoints and try to 186After resolving, the node will bind itself on all endpoints.
187connect 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
188optional 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
189network. 203network.
190 204
191=item slave nodes 205All the seednodes will also be specially marked to automatically retry
192 206connecting to them indefinitely, so make sure that seednodes are really
193When the C<$noderef> (either as given or overriden by the config file) 207reliable and up (this might also change in the future).
194is the special string C<slave/>, then the node will become a slave
195node. Slave nodes cannot be contacted from outside and will route most of
196their traffic to the master node that they attach to.
197
198At least one additional noderef is required (either by specifying it
199directly 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
201first node it can successfully connect to.
202
203=back
204
205This function will block until all nodes have been resolved and, for slave
206nodes, until it has successfully established a connection to a master
207server.
208 208
209Example: 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
210specified 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
211form of invocation for "daemon"-type nodes. 211form of invocation for "daemon"-type nodes.
212 212
214 214
215Example: 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
216C<aemp>. This form is often used for commandline clients. 216C<aemp>. This form is often used for commandline clients.
217 217
218 initialise_node "slave/"; 218 initialise_node "slave/";
219
220Example: become a slave node to any of the specified master servers. This
221form is also often used for commandline clients.
222
223 initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net";
224 219
225Example: 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
226servers to become part of the network. 221servers to become part of the network.
227 222
228 initialise_node undef, "master1", "master2"; 223 initialise_node undef, "master1", "master2";
349The default callback received all messages not matched by a more specific 344The default callback received all messages not matched by a more specific
350C<tag> match. 345C<tag> match.
351 346
352=item rcv $local_port, tag => $callback->(@msg_without_tag), ... 347=item rcv $local_port, tag => $callback->(@msg_without_tag), ...
353 348
354Register callbacks to be called on messages starting with the given tag on 349Register (or replace) callbacks to be called on messages starting with the
355the 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
356is C<$undef>). 351C<$callback> is C<$undef> or missing). There can only be one callback
352registered for each tag.
357 353
358The original message will be passed to the callback, after the first 354The original message will be passed to the callback, after the first
359element (the tag) has been removed. The callback will use the same 355element (the tag) has been removed. The callback will use the same
360environment as the default callback (see above). 356environment as the default callback (see above).
361 357
373 rcv port, 369 rcv port,
374 msg1 => sub { ... }, 370 msg1 => sub { ... },
375 ... 371 ...
376 ; 372 ;
377 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
378=cut 383=cut
379 384
380sub rcv($@) { 385sub rcv($@) {
381 my $port = shift; 386 my $port = shift;
382 my ($noderef, $portid) = split /#/, $port, 2; 387 my ($noderef, $portid) = split /#/, $port, 2;
383 388
384 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 389 $NODE{$noderef} == $NODE{""}
385 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";
386 391
387 while (@_) { 392 while (@_) {
388 if (ref $_[0]) { 393 if (ref $_[0]) {
389 if (my $self = $PORT_DATA{$portid}) { 394 if (my $self = $PORT_DATA{$portid}) {
488message loss has been detected. No messages will be lost "in between" 493message 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 494(after the first lost message no further messages will be received by the
490port). After the monitoring action was invoked, further messages might get 495port). After the monitoring action was invoked, further messages might get
491delivered again. 496delivered again.
492 497
498Note that monitoring-actions are one-shot: once released, they are removed
499and will not trigger again.
500
493In the first form (callback), the callback is simply called with any 501In the first form (callback), the callback is simply called with any
494number 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
495"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
496C<eval> if unsure. 504C<eval> if unsure.
497 505
657 my $id = "$RUNIQ." . $ID++; 665 my $id = "$RUNIQ." . $ID++;
658 666
659 $_[0] =~ /::/ 667 $_[0] =~ /::/
660 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";
661 669
662 ($NODE{$noderef} || add_node $noderef) 670 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_;
663 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]);
664 671
665 "$noderef#$id" 672 "$noderef#$id"
666} 673}
667 674
668=back 675=item after $timeout, @msg
669 676
670=head1 NODE MESSAGES 677=item after $timeout, $callback
671 678
672Nodes understand the following messages sent to them. Many of them take 679Either sends the given message, or call the given callback, after the
673arguments called C<@reply>, which will simply be used to compose a reply 680specified 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 681
677While other messages exist, they are not public and subject to change. 682This is simply a utility function that come sin handy at times.
678 683
679=over 4
680
681=cut 684=cut
682 685
683=item lookup => $name, @reply 686sub after($@) {
687 my ($timeout, @action) = @_;
684 688
685Replies with the port ID of the specified well-known port, or C<undef>. 689 my $t; $t = AE::timer $timeout, 0, sub {
686 690 undef $t;
687=item devnull => ... 691 ref $action[0]
688 692 ? $action[0]()
689Generic data sink/CPU heat conversion. 693 : snd @action;
690 694 };
691=item relay => $port, @msg 695}
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 696
714=back 697=back
715 698
716=head1 AnyEvent::MP vs. Distributed Erlang 699=head1 AnyEvent::MP vs. Distributed Erlang
717 700
736convenience functionality. 719convenience functionality.
737 720
738This means that AEMP requires a less tightly controlled environment at the 721This means that AEMP requires a less tightly controlled environment at the
739cost of longer node references and a slightly higher management overhead. 722cost of longer node references and a slightly higher management overhead.
740 723
741=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
742uses "local ports are like remote ports". 725uses "local ports are like remote ports".
743 726
744The failure modes for local ports are quite different (runtime errors 727The 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, 728only) 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 729when a connection to another node dies, you know nothing about the other

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines