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.57 by root, Sat Aug 15 04:34:34 2009 UTC vs.
Revision 1.62 by root, Thu Aug 27 07:12:48 2009 UTC

85 85
86A node is a single process containing at least one port - the node port, 86A node is a single process containing at least one port - the node port,
87which provides nodes to manage each other remotely, and to create new 87which provides nodes to manage each other remotely, and to create new
88ports. 88ports.
89 89
90Nodes are either private (single-process only), slaves (connected to a 90Nodes are either private (single-process only), slaves (can only talk to
91master 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).
92 93
93=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>
94 95
95A 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
96private 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
126use base "Exporter"; 127use base "Exporter";
127 128
128our $VERSION = $AnyEvent::MP::Kernel::VERSION; 129our $VERSION = $AnyEvent::MP::Kernel::VERSION;
129 130
130our @EXPORT = qw( 131our @EXPORT = qw(
131 NODE $NODE *SELF node_of _any_ 132 NODE $NODE *SELF node_of after
132 resolve_node initialise_node 133 resolve_node initialise_node
133 snd rcv mon kil reg psub spawn 134 snd rcv mon mon_guard kil reg psub spawn
134 port 135 port
135); 136);
136 137
137our $SELF; 138our $SELF;
138 139
180 181
181For public nodes, C<$noderef> (supplied either directly to 182For public nodes, C<$noderef> (supplied either directly to
182C<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
183noderef (possibly unresolved, in which case it will be resolved). 184noderef (possibly unresolved, in which case it will be resolved).
184 185
185After resolving, the node will bind itself on all endpoints and try to 186After resolving, the node will bind itself on all endpoints.
186connect 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
187optional 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
188network. 203network.
189 204
190=item slave nodes
191
192When the C<$noderef> (either as given or overriden by the config file)
193is the special string C<slave/>, then the node will become a slave
194node. Slave nodes cannot be contacted from outside and will route most of
195their traffic to the master node that they attach to.
196
197At least one additional noderef is required (either by specifying it
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
200first node it can successfully connect to.
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
207=back
208
209This function will block until all nodes have been resolved and, for slave
210nodes, until it has successfully established a connection to a master
211server.
212
213All the seednodes will also be specially marked to automatically retry 205All the seednodes will also be specially marked to automatically retry
214connecting to them infinitely. 206connecting to them indefinitely, so make sure that seednodes are really
207reliable and up (this might also change in the future).
215 208
216Example: 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
217specified 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
218form of invocation for "daemon"-type nodes. 211form of invocation for "daemon"-type nodes.
219 212
221 214
222Example: 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
223C<aemp>. This form is often used for commandline clients. 216C<aemp>. This form is often used for commandline clients.
224 217
225 initialise_node "slave/"; 218 initialise_node "slave/";
226
227Example: become a slave node to any of the specified master servers. This
228form is also often used for commandline clients.
229
230 initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net";
231 219
232Example: 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
233servers to become part of the network. 221servers to become part of the network.
234 222
235 initialise_node undef, "master1", "master2"; 223 initialise_node undef, "master1", "master2";
396 384
397sub rcv($@) { 385sub rcv($@) {
398 my $port = shift; 386 my $port = shift;
399 my ($noderef, $portid) = split /#/, $port, 2; 387 my ($noderef, $portid) = split /#/, $port, 2;
400 388
401 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 389 $NODE{$noderef} == $NODE{""}
402 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";
403 391
404 while (@_) { 392 while (@_) {
405 if (ref $_[0]) { 393 if (ref $_[0]) {
406 if (my $self = $PORT_DATA{$portid}) { 394 if (my $self = $PORT_DATA{$portid}) {
505message loss has been detected. No messages will be lost "in between" 493message loss has been detected. No messages will be lost "in between"
506(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
507port). After the monitoring action was invoked, further messages might get 495port). After the monitoring action was invoked, further messages might get
508delivered again. 496delivered again.
509 497
498Note that monitoring-actions are one-shot: once released, they are removed
499and will not trigger again.
500
510In the first form (callback), the callback is simply called with any 501In the first form (callback), the callback is simply called with any
511number 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
512"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
513C<eval> if unsure. 504C<eval> if unsure.
514 505
679 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; 670 snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_;
680 671
681 "$noderef#$id" 672 "$noderef#$id"
682} 673}
683 674
675=item after $timeout, @msg
676
677=item after $timeout, $callback
678
679Either sends the given message, or call the given callback, after the
680specified number of seconds.
681
682This is simply a utility function that come sin handy at times.
683
684=cut
685
686sub after($@) {
687 my ($timeout, @action) = @_;
688
689 my $t; $t = AE::timer $timeout, 0, sub {
690 undef $t;
691 ref $action[0]
692 ? $action[0]()
693 : snd @action;
694 };
695}
696
684=back 697=back
685 698
686=head1 AnyEvent::MP vs. Distributed Erlang 699=head1 AnyEvent::MP vs. Distributed Erlang
687 700
688AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node 701AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines