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.51 by root, Fri Aug 14 14:07:44 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, type 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 };
34 34
35 # create a port on another node 35 # create a port on another node
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
143 kil $SELF, die => $msg; 143 kil $SELF, die => $msg;
144} 144}
145 145
146=item $thisnode = NODE / $NODE 146=item $thisnode = NODE / $NODE
147 147
148The C<NODE> function returns, and the C<$NODE> variable contains 148The C<NODE> function returns, and the C<$NODE> variable contains the
149the noderef of the local node. The value is initialised by a call 149noderef 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 150C<initialise_node>.
151identifiers become invalid.
152 151
153=item $noderef = node_of $port 152=item $noderef = node_of $port
154 153
155Extracts and returns the noderef from a portid or a noderef. 154Extracts and returns the noderef from a port ID or a noderef.
156 155
157=item initialise_node $noderef, $seednode, $seednode... 156=item initialise_node $noderef, $seednode, $seednode...
158 157
159=item initialise_node "slave/", $master, $master... 158=item initialise_node "slave/", $master, $master...
160 159
182 181
183For public nodes, C<$noderef> (supplied either directly to 182For public nodes, C<$noderef> (supplied either directly to
184C<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
185noderef (possibly unresolved, in which case it will be resolved). 184noderef (possibly unresolved, in which case it will be resolved).
186 185
187After resolving, the node will bind itself on all endpoints and try to 186After resolving, the node will bind itself on all endpoints.
188connect 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
189optional 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
190network. 203network.
191 204
192=item slave nodes 205All the seednodes will also be specially marked to automatically retry
193 206connecting to them indefinitely, so make sure that seednodes are really
194When the C<$noderef> (either as given or overriden by the config file) 207reliable 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 208
210Example: 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
211specified 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
212form of invocation for "daemon"-type nodes. 211form of invocation for "daemon"-type nodes.
213 212
216Example: 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
217C<aemp>. This form is often used for commandline clients. 216C<aemp>. This form is often used for commandline clients.
218 217
219 initialise_node "slave/"; 218 initialise_node "slave/";
220 219
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 220Example: become a public node, and try to contact some well-known master
227servers to become part of the network. 221servers to become part of the network.
228 222
229 initialise_node undef, "master1", "master2"; 223 initialise_node undef, "master1", "master2";
230 224
279=item snd $port, type => @data 273=item snd $port, type => @data
280 274
281=item snd $port, @msg 275=item snd $port, @msg
282 276
283Send the given message to the given port ID, which can identify either 277Send 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 278a local or a remote port, and must be a port ID.
285stringifies a sa port ID (such as a port object :).
286 279
287While the message can be about anything, it is highly recommended to use a 280While 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 281string as first element (a port ID, or some word that indicates a request
289type etc.). 282type etc.).
290 283
291The message data effectively becomes read-only after a call to this 284The message data effectively becomes read-only after a call to this
292function: modifying any argument is not allowed and can cause many 285function: modifying any argument is not allowed and can cause many
293problems. 286problems.
351The default callback received all messages not matched by a more specific 344The default callback received all messages not matched by a more specific
352C<tag> match. 345C<tag> match.
353 346
354=item rcv $local_port, tag => $callback->(@msg_without_tag), ... 347=item rcv $local_port, tag => $callback->(@msg_without_tag), ...
355 348
356Register callbacks to be called on messages starting with the given tag on 349Register (or replace) callbacks to be called on messages starting with the
357the 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
358is C<$undef>). 351C<$callback> is C<$undef> or missing). There can only be one callback
352registered for each tag.
359 353
360The original message will be passed to the callback, after the first 354The original message will be passed to the callback, after the first
361element (the tag) has been removed. The callback will use the same 355element (the tag) has been removed. The callback will use the same
362environment as the default callback (see above). 356environment as the default callback (see above).
363 357
375 rcv port, 369 rcv port,
376 msg1 => sub { ... }, 370 msg1 => sub { ... },
377 ... 371 ...
378 ; 372 ;
379 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
380=cut 383=cut
381 384
382sub rcv($@) { 385sub rcv($@) {
383 my $port = shift; 386 my $port = shift;
384 my ($noderef, $portid) = split /#/, $port, 2; 387 my ($noderef, $portid) = split /#/, $port, 2;
385 388
386 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 389 $NODE{$noderef} == $NODE{""}
387 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";
388 391
389 while (@_) { 392 while (@_) {
390 if (ref $_[0]) { 393 if (ref $_[0]) {
391 if (my $self = $PORT_DATA{$portid}) { 394 if (my $self = $PORT_DATA{$portid}) {
490message loss has been detected. No messages will be lost "in between" 493message 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 494(after the first lost message no further messages will be received by the
492port). After the monitoring action was invoked, further messages might get 495port). After the monitoring action was invoked, further messages might get
493delivered again. 496delivered again.
494 497
498Note that monitoring-actions are one-shot: once released, they are removed
499and will not trigger again.
500
495In the first form (callback), the callback is simply called with any 501In the first form (callback), the callback is simply called with any
496number 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
497"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
498C<eval> if unsure. 504C<eval> if unsure.
499 505
659 my $id = "$RUNIQ." . $ID++; 665 my $id = "$RUNIQ." . $ID++;
660 666
661 $_[0] =~ /::/ 667 $_[0] =~ /::/
662 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";
663 669
664 ($NODE{$noderef} || add_node $noderef) 670 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_;
665 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]);
666 671
667 "$noderef#$id" 672 "$noderef#$id"
668} 673}
669 674
670=back 675=item after $timeout, @msg
671 676
672=head1 NODE MESSAGES 677=item after $timeout, $callback
673 678
674Nodes understand the following messages sent to them. Many of them take 679Either sends the given message, or call the given callback, after the
675arguments called C<@reply>, which will simply be used to compose a reply 680specified 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 681
679While other messages exist, they are not public and subject to change. 682This is simply a utility function that come sin handy at times.
680 683
681=over 4
682
683=cut 684=cut
684 685
685=item lookup => $name, @reply 686sub after($@) {
687 my ($timeout, @action) = @_;
686 688
687Replies with the port ID of the specified well-known port, or C<undef>. 689 my $t; $t = AE::timer $timeout, 0, sub {
688 690 undef $t;
689=item devnull => ... 691 ref $action[0]
690 692 ? $action[0]()
691Generic data sink/CPU heat conversion. 693 : snd @action;
692 694 };
693=item relay => $port, @msg 695}
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 696
716=back 697=back
717 698
718=head1 AnyEvent::MP vs. Distributed Erlang 699=head1 AnyEvent::MP vs. Distributed Erlang
719 700
738convenience functionality. 719convenience functionality.
739 720
740This means that AEMP requires a less tightly controlled environment at the 721This means that AEMP requires a less tightly controlled environment at the
741cost of longer node references and a slightly higher management overhead. 722cost of longer node references and a slightly higher management overhead.
742 723
743=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
744uses "local ports are like remote ports". 725uses "local ports are like remote ports".
745 726
746The failure modes for local ports are quite different (runtime errors 727The failure modes for local ports are quite different (runtime errors
747only) 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,
748when 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