… | |
… | |
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 |
… | |
… | |
175 | |
176 | |
176 | =cut |
177 | =cut |
177 | |
178 | |
178 | package AnyEvent::MP; |
179 | package AnyEvent::MP; |
179 | |
180 | |
|
|
181 | use AnyEvent::MP::Config (); |
180 | use AnyEvent::MP::Kernel; |
182 | use AnyEvent::MP::Kernel; |
|
|
183 | use AnyEvent::MP::Kernel qw(%NODE %PORT %PORT_DATA $UNIQ $RUNIQ $ID); |
181 | |
184 | |
182 | use common::sense; |
185 | use common::sense; |
183 | |
186 | |
184 | use Carp (); |
187 | use Carp (); |
185 | |
188 | |
186 | use AE (); |
189 | use AE (); |
|
|
190 | use Guard (); |
187 | |
191 | |
188 | use base "Exporter"; |
192 | use base "Exporter"; |
189 | |
193 | |
190 | our $VERSION = '1.30'; |
194 | our $VERSION = $AnyEvent::MP::Config::VERSION; |
191 | |
195 | |
192 | our @EXPORT = qw( |
196 | our @EXPORT = qw( |
193 | NODE $NODE *SELF node_of after |
197 | NODE $NODE *SELF node_of after |
194 | configure |
198 | configure |
195 | snd rcv mon mon_guard kil psub peval spawn cal |
199 | snd rcv mon mon_guard kil psub peval spawn cal |
196 | port |
200 | port |
|
|
201 | db_set db_del db_reg |
197 | ); |
202 | ); |
198 | |
203 | |
199 | our $SELF; |
204 | our $SELF; |
200 | |
205 | |
201 | sub _self_die() { |
206 | sub _self_die() { |
… | |
… | |
221 | Before a node can talk to other nodes on the network (i.e. enter |
226 | Before a node can talk to other nodes on the network (i.e. enter |
222 | "distributed mode") it has to configure itself - the minimum a node needs |
227 | "distributed mode") it has to configure itself - the minimum a node needs |
223 | to know is its own name, and optionally it should know the addresses of |
228 | to know is its own name, and optionally it should know the addresses of |
224 | some other nodes in the network to discover other nodes. |
229 | some other nodes in the network to discover other nodes. |
225 | |
230 | |
226 | The key/value pairs are basically the same ones as documented for the |
|
|
227 | F<aemp> command line utility (sans the set/del prefix). |
|
|
228 | |
|
|
229 | This function configures a node - it must be called exactly once (or |
231 | This function configures a node - it must be called exactly once (or |
230 | never) before calling other AnyEvent::MP functions. |
232 | never) before calling other AnyEvent::MP functions. |
|
|
233 | |
|
|
234 | The key/value pairs are basically the same ones as documented for the |
|
|
235 | F<aemp> command line utility (sans the set/del prefix), with two additions: |
|
|
236 | |
|
|
237 | =over 4 |
|
|
238 | |
|
|
239 | =item norc => $boolean (default false) |
|
|
240 | |
|
|
241 | If true, then the rc file (e.g. F<~/.perl-anyevent-mp>) will I<not> |
|
|
242 | be consulted - all configuraiton options must be specified in the |
|
|
243 | C<configure> call. |
|
|
244 | |
|
|
245 | =item force => $boolean (default false) |
|
|
246 | |
|
|
247 | IF true, then the values specified in the C<configure> will take |
|
|
248 | precedence over any values configured via the rc file. The default is for |
|
|
249 | the rc file to override any options specified in the program. |
|
|
250 | |
|
|
251 | =back |
231 | |
252 | |
232 | =over 4 |
253 | =over 4 |
233 | |
254 | |
234 | =item step 1, gathering configuration from profiles |
255 | =item step 1, gathering configuration from profiles |
235 | |
256 | |
… | |
… | |
249 | 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 |
250 | and the values specified directly via C<configure> have lowest priority, |
271 | and the values specified directly via C<configure> have lowest priority, |
251 | and can only be used to specify defaults. |
272 | and can only be used to specify defaults. |
252 | |
273 | |
253 | 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 |
254 | 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 |
255 | 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. |
256 | |
280 | |
257 | =item step 2, bind listener sockets |
281 | =item step 2, bind listener sockets |
258 | |
282 | |
259 | 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 |
260 | 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 |
… | |
… | |
287 | 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 |
288 | 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, |
289 | customary for aemp). |
313 | customary for aemp). |
290 | |
314 | |
291 | # use the aemp commandline utility |
315 | # use the aemp commandline utility |
292 | # aemp profile seed nodeid anon/ binds '*:4040' |
316 | # aemp profile seed binds '*:4040' |
293 | |
317 | |
294 | # then use it |
318 | # then use it |
295 | configure profile => "seed"; |
319 | configure profile => "seed"; |
296 | |
320 | |
297 | # or simply use aemp from the shell again: |
321 | # or simply use aemp from the shell again: |
… | |
… | |
367 | sub _kilme { |
391 | sub _kilme { |
368 | die "received message on port without callback"; |
392 | die "received message on port without callback"; |
369 | } |
393 | } |
370 | |
394 | |
371 | sub port(;&) { |
395 | sub port(;&) { |
372 | my $id = "$UNIQ." . $ID++; |
396 | my $id = $UNIQ . ++$ID; |
373 | my $port = "$NODE#$id"; |
397 | my $port = "$NODE#$id"; |
374 | |
398 | |
375 | rcv $port, shift || \&_kilme; |
399 | rcv $port, shift || \&_kilme; |
376 | |
400 | |
377 | $port |
401 | $port |
… | |
… | |
650 | } |
674 | } |
651 | |
675 | |
652 | $node->monitor ($port, $cb); |
676 | $node->monitor ($port, $cb); |
653 | |
677 | |
654 | defined wantarray |
678 | defined wantarray |
655 | and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) |
679 | and ($cb += 0, Guard::guard { $node->unmonitor ($port, $cb) }) |
656 | } |
680 | } |
657 | |
681 | |
658 | =item $guard = mon_guard $port, $ref, $ref... |
682 | =item $guard = mon_guard $port, $ref, $ref... |
659 | |
683 | |
660 | 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 |
… | |
… | |
764 | } |
788 | } |
765 | |
789 | |
766 | sub spawn(@) { |
790 | sub spawn(@) { |
767 | my ($nodeid, undef) = split /#/, shift, 2; |
791 | my ($nodeid, undef) = split /#/, shift, 2; |
768 | |
792 | |
769 | my $id = "$RUNIQ." . $ID++; |
793 | my $id = $RUNIQ . ++$ID; |
770 | |
794 | |
771 | $_[0] =~ /::/ |
795 | $_[0] =~ /::/ |
772 | 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"; |
773 | |
797 | |
774 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
798 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
775 | |
799 | |
776 | "$nodeid#$id" |
800 | "$nodeid#$id" |
777 | } |
801 | } |
|
|
802 | |
778 | |
803 | |
779 | =item after $timeout, @msg |
804 | =item after $timeout, @msg |
780 | |
805 | |
781 | =item after $timeout, $callback |
806 | =item after $timeout, $callback |
782 | |
807 | |
… | |
… | |
852 | $port |
877 | $port |
853 | } |
878 | } |
854 | |
879 | |
855 | =back |
880 | =back |
856 | |
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 | |
857 | =head1 AnyEvent::MP vs. Distributed Erlang |
929 | =head1 AnyEvent::MP vs. Distributed Erlang |
858 | |
930 | |
859 | 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 |
860 | == 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 |
861 | 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 |