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.122 by root, Wed Feb 29 18:44:59 2012 UTC vs.
Revision 1.124 by root, Sat Mar 3 11:38:43 2012 UTC

82 82
83Ports are represented by (printable) strings called "port IDs". 83Ports are represented by (printable) strings called "port IDs".
84 84
85=item port ID - C<nodeid#portname> 85=item port ID - C<nodeid#portname>
86 86
87A port ID is the concatenation of a node ID, a hash-mark (C<#>) as 87A port ID is the concatenation of a node ID, a hash-mark (C<#>)
88separator, and a port name (a printable string of unspecified format). 88as separator, and a port name (a printable string of unspecified
89format created by AnyEvent::MP).
89 90
90=item node 91=item node
91 92
92A node is a single process containing at least one port - the node port, 93A node is a single process containing at least one port - the node port,
93which enables nodes to manage each other remotely, and to create new 94which enables nodes to manage each other remotely, and to create new
184use common::sense; 185use common::sense;
185 186
186use Carp (); 187use Carp ();
187 188
188use AE (); 189use AE ();
190use Guard ();
189 191
190use base "Exporter"; 192use base "Exporter";
191 193
192our $VERSION = $AnyEvent::MP::Config::VERSION; 194our $VERSION = $AnyEvent::MP::Config::VERSION;
193 195
194our @EXPORT = qw( 196our @EXPORT = qw(
195 NODE $NODE *SELF node_of after 197 NODE $NODE *SELF node_of after
196 configure 198 configure
197 snd rcv mon mon_guard kil psub peval spawn cal 199 snd rcv mon mon_guard kil psub peval spawn cal
198 port 200 port
201 db_set db_del db_reg
199); 202);
200 203
201our $SELF; 204our $SELF;
202 205
203sub _self_die() { 206sub _self_die() {
388sub _kilme { 391sub _kilme {
389 die "received message on port without callback"; 392 die "received message on port without callback";
390} 393}
391 394
392sub port(;&) { 395sub port(;&) {
393 my $id = "$UNIQ." . ++$ID; 396 my $id = $UNIQ . ++$ID;
394 my $port = "$NODE#$id"; 397 my $port = "$NODE#$id";
395 398
396 rcv $port, shift || \&_kilme; 399 rcv $port, shift || \&_kilme;
397 400
398 $port 401 $port
671 } 674 }
672 675
673 $node->monitor ($port, $cb); 676 $node->monitor ($port, $cb);
674 677
675 defined wantarray 678 defined wantarray
676 and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) 679 and ($cb += 0, Guard::guard { $node->unmonitor ($port, $cb) })
677} 680}
678 681
679=item $guard = mon_guard $port, $ref, $ref... 682=item $guard = mon_guard $port, $ref, $ref...
680 683
681Monitors the given C<$port> and keeps the passed references. When the port 684Monitors the given C<$port> and keeps the passed references. When the port
785} 788}
786 789
787sub spawn(@) { 790sub spawn(@) {
788 my ($nodeid, undef) = split /#/, shift, 2; 791 my ($nodeid, undef) = split /#/, shift, 2;
789 792
790 my $id = "$RUNIQ." . ++$ID; 793 my $id = $RUNIQ . ++$ID;
791 794
792 $_[0] =~ /::/ 795 $_[0] =~ /::/
793 or Carp::croak "spawn init function must be a fully-qualified name, caught"; 796 or Carp::croak "spawn init function must be a fully-qualified name, caught";
794 797
795 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; 798 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_;
874 $port 877 $port
875} 878}
876 879
877=back 880=back
878 881
882=head1 DISTRIBUTED DATABASE
883
884AnyEvent::MP comes with a simple distributed database. The database will
885be mirrored asynchronously at all global nodes. Other nodes bind to one of
886the global nodes for their needs.
887
888The database consists of a two-level hash - a hash contains a hash which
889contains values.
890
891The top level hash key is called "family", and the second-level hash key
892is simply called "key".
893
894The family and key must be alphanumeric ASCII strings, i.e. start
895with a letter and consist of letters, digits, underscores and colons
896(C<[A-Za-z][A-Za-z0-9_:]*>, pretty much like Perl module names.
897
898As the family namespaceis global, it is recommended to prefix family names
899with the name of the application or module using it.
900
901The values should preferably be strings, but other perl scalars should
902work as well (such as arrays and hashes).
903
904Every database entry is owned by one node - adding the same family/key
905combination on multiple nodes will not cause discomfort for AnyEvent::MP,
906but the result might be nondeterministic, i.e. the key might have
907different values on different nodes.
908
909=item db_set $family => $key => $value
910
911Sets (or replaces) a key to the database.
912
913=item db_del $family => $key
914
915Deletes a key from the database.
916
917=item $guard = db_reg $family => $key [=> $value]
918
919Sets the key on the database and returns a guard. When the guard is
920destroyed, the key is deleted from the database. If C<$value> is missing,
921then C<undef> is used.
922
923=cut
924
925=back
926
879=head1 AnyEvent::MP vs. Distributed Erlang 927=head1 AnyEvent::MP vs. Distributed Erlang
880 928
881AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node 929AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node
882== aemp node, Erlang process == aemp port), so many of the documents and 930== aemp node, Erlang process == aemp port), so many of the documents and
883programming techniques employed by Erlang apply to AnyEvent::MP. Here is a 931programming techniques employed by Erlang apply to AnyEvent::MP. Here is a

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines