… | |
… | |
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() { |
… | |
… | |
267 | That means that the values specified in the profile have highest priority |
270 | That means that the values specified in the profile have highest priority |
268 | and the values specified directly via C<configure> have lowest priority, |
271 | and the values specified directly via C<configure> have lowest priority, |
269 | and can only be used to specify defaults. |
272 | and can only be used to specify defaults. |
270 | |
273 | |
271 | If the profile specifies a node ID, then this will become the node ID of |
274 | If the profile specifies a node ID, then this will become the node ID of |
272 | this process. If not, then the profile name will be used as node ID. The |
275 | this process. If not, then the profile name will be used as node ID, with |
273 | special node ID of C<anon/> will be replaced by a random node ID. |
276 | a slash (C</>) attached. |
|
|
277 | |
|
|
278 | If the node ID (or profile name) ends with a slash (C</>), then a random |
|
|
279 | string is appended to make it unique. |
274 | |
280 | |
275 | =item step 2, bind listener sockets |
281 | =item step 2, bind listener sockets |
276 | |
282 | |
277 | The next step is to look up the binds in the profile, followed by binding |
283 | The next step is to look up the binds in the profile, followed by binding |
278 | aemp protocol listeners on all binds specified (it is possible and valid |
284 | aemp protocol listeners on all binds specified (it is possible and valid |
… | |
… | |
305 | Example: configure a node using a profile called seed, which is suitable |
311 | Example: configure a node using a profile called seed, which is suitable |
306 | for a seed node as it binds on all local addresses on a fixed port (4040, |
312 | for a seed node as it binds on all local addresses on a fixed port (4040, |
307 | customary for aemp). |
313 | customary for aemp). |
308 | |
314 | |
309 | # use the aemp commandline utility |
315 | # use the aemp commandline utility |
310 | # aemp profile seed nodeid anon/ binds '*:4040' |
316 | # aemp profile seed binds '*:4040' |
311 | |
317 | |
312 | # then use it |
318 | # then use it |
313 | configure profile => "seed"; |
319 | configure profile => "seed"; |
314 | |
320 | |
315 | # or simply use aemp from the shell again: |
321 | # or simply use aemp from the shell again: |
… | |
… | |
385 | sub _kilme { |
391 | sub _kilme { |
386 | die "received message on port without callback"; |
392 | die "received message on port without callback"; |
387 | } |
393 | } |
388 | |
394 | |
389 | sub port(;&) { |
395 | sub port(;&) { |
390 | my $id = "$UNIQ." . ++$ID; |
396 | my $id = $UNIQ . ++$ID; |
391 | my $port = "$NODE#$id"; |
397 | my $port = "$NODE#$id"; |
392 | |
398 | |
393 | rcv $port, shift || \&_kilme; |
399 | rcv $port, shift || \&_kilme; |
394 | |
400 | |
395 | $port |
401 | $port |
… | |
… | |
668 | } |
674 | } |
669 | |
675 | |
670 | $node->monitor ($port, $cb); |
676 | $node->monitor ($port, $cb); |
671 | |
677 | |
672 | defined wantarray |
678 | defined wantarray |
673 | and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) |
679 | and ($cb += 0, Guard::guard { $node->unmonitor ($port, $cb) }) |
674 | } |
680 | } |
675 | |
681 | |
676 | =item $guard = mon_guard $port, $ref, $ref... |
682 | =item $guard = mon_guard $port, $ref, $ref... |
677 | |
683 | |
678 | 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 |
… | |
… | |
782 | } |
788 | } |
783 | |
789 | |
784 | sub spawn(@) { |
790 | sub spawn(@) { |
785 | my ($nodeid, undef) = split /#/, shift, 2; |
791 | my ($nodeid, undef) = split /#/, shift, 2; |
786 | |
792 | |
787 | my $id = "$RUNIQ." . ++$ID; |
793 | my $id = $RUNIQ . ++$ID; |
788 | |
794 | |
789 | $_[0] =~ /::/ |
795 | $_[0] =~ /::/ |
790 | 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"; |
791 | |
797 | |
792 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
798 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
… | |
… | |
871 | $port |
877 | $port |
872 | } |
878 | } |
873 | |
879 | |
874 | =back |
880 | =back |
875 | |
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 must be alphanumeric, i.e. start with a letter and consist |
|
|
895 | of letters, digits, underscores and colons (C<[A-Za-z][A-Za-z0-9_:]*>, |
|
|
896 | pretty much like Perl module names. |
|
|
897 | |
|
|
898 | As the family namespace is global, it is recommended to prefix family names |
|
|
899 | with the name of the application or module using it. |
|
|
900 | |
|
|
901 | The keys must be strings, with no other limitations. |
|
|
902 | |
|
|
903 | The values should preferably be strings, but other perl scalars should |
|
|
904 | work as well (such as undef, arrays and hashes). |
|
|
905 | |
|
|
906 | Every database entry is owned by one node - adding the same family/key |
|
|
907 | combination on multiple nodes will not cause discomfort for AnyEvent::MP, |
|
|
908 | but the result might be nondeterministic, i.e. the key might have |
|
|
909 | different values on different nodes. |
|
|
910 | |
|
|
911 | =item db_set $family => $key => $value |
|
|
912 | |
|
|
913 | Sets (or replaces) a key to the database. |
|
|
914 | |
|
|
915 | =item db_del $family => $key |
|
|
916 | |
|
|
917 | Deletes a key from the database. |
|
|
918 | |
|
|
919 | =item $guard = db_reg $family => $key [=> $value] |
|
|
920 | |
|
|
921 | Sets the key on the database and returns a guard. When the guard is |
|
|
922 | destroyed, the key is deleted from the database. If C<$value> is missing, |
|
|
923 | then C<undef> is used. |
|
|
924 | |
|
|
925 | =cut |
|
|
926 | |
|
|
927 | =back |
|
|
928 | |
876 | =head1 AnyEvent::MP vs. Distributed Erlang |
929 | =head1 AnyEvent::MP vs. Distributed Erlang |
877 | |
930 | |
878 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
931 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
879 | == aemp node, Erlang process == aemp port), so many of the documents and |
932 | == aemp node, Erlang process == aemp port), so many of the documents and |
880 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |
933 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |