… | |
… | |
82 | |
82 | |
83 | Ports are represented by (printable) strings called "port IDs". |
83 | Ports 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 | |
87 | A port ID is the concatenation of a node ID, a hash-mark (C<#>) as |
87 | A port ID is the concatenation of a node ID, a hash-mark (C<#>) |
88 | separator, and a port name (a printable string of unspecified format). |
88 | as separator, and a port name (a printable string of unspecified |
|
|
89 | format created by AnyEvent::MP). |
89 | |
90 | |
90 | =item node |
91 | =item node |
91 | |
92 | |
92 | A node is a single process containing at least one port - the node port, |
93 | A node is a single process containing at least one port - the node port, |
93 | which enables nodes to manage each other remotely, and to create new |
94 | which enables nodes to manage each other remotely, and to create new |
… | |
… | |
184 | use common::sense; |
185 | use common::sense; |
185 | |
186 | |
186 | use Carp (); |
187 | use Carp (); |
187 | |
188 | |
188 | use AE (); |
189 | use AE (); |
|
|
190 | use Guard (); |
189 | |
191 | |
190 | use base "Exporter"; |
192 | use base "Exporter"; |
191 | |
193 | |
192 | our $VERSION = $AnyEvent::MP::Config::VERSION; |
194 | our $VERSION = $AnyEvent::MP::Config::VERSION; |
193 | |
195 | |
194 | our @EXPORT = qw( |
196 | our @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 | |
201 | our $SELF; |
204 | our $SELF; |
202 | |
205 | |
203 | sub _self_die() { |
206 | sub _self_die() { |
… | |
… | |
388 | sub _kilme { |
391 | sub _kilme { |
389 | die "received message on port without callback"; |
392 | die "received message on port without callback"; |
390 | } |
393 | } |
391 | |
394 | |
392 | sub port(;&) { |
395 | sub 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 | |
681 | Monitors the given C<$port> and keeps the passed references. When the port |
684 | Monitors the given C<$port> and keeps the passed references. When the port |
… | |
… | |
785 | } |
788 | } |
786 | |
789 | |
787 | sub spawn(@) { |
790 | sub 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 | |
|
|
884 | AnyEvent::MP comes with a simple distributed database. The database will |
|
|
885 | be mirrored asynchronously at all global nodes. Other nodes bind to one of |
|
|
886 | the global nodes for their needs. |
|
|
887 | |
|
|
888 | The database consists of a two-level hash - a hash contains a hash which |
|
|
889 | contains values. |
|
|
890 | |
|
|
891 | The top level hash key is called "family", and the second-level hash key |
|
|
892 | is simply called "key". |
|
|
893 | |
|
|
894 | The family and key must be alphanumeric ASCII strings, i.e. start |
|
|
895 | with 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 | |
|
|
898 | As the family namespaceis global, it is recommended to prefix family names |
|
|
899 | with the name of the application or module using it. |
|
|
900 | |
|
|
901 | The values should preferably be strings, but other perl scalars should |
|
|
902 | work as well (such as arrays and hashes). |
|
|
903 | |
|
|
904 | Every database entry is owned by one node - adding the same family/key |
|
|
905 | combination on multiple nodes will not cause discomfort for AnyEvent::MP, |
|
|
906 | but the result might be nondeterministic, i.e. the key might have |
|
|
907 | different values on different nodes. |
|
|
908 | |
|
|
909 | =item db_set $family => $key => $value |
|
|
910 | |
|
|
911 | Sets (or replaces) a key to the database. |
|
|
912 | |
|
|
913 | =item db_del $family => $key |
|
|
914 | |
|
|
915 | Deletes a key from the database. |
|
|
916 | |
|
|
917 | =item $guard = db_reg $family => $key [=> $value] |
|
|
918 | |
|
|
919 | Sets the key on the database and returns a guard. When the guard is |
|
|
920 | destroyed, the key is deleted from the database. If C<$value> is missing, |
|
|
921 | then 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 | |
881 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
929 | AnyEvent::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 |
883 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |
931 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |