… | |
… | |
30 | rcv $port, pong => sub { warn "pong received\n" }; |
30 | rcv $port, pong => sub { warn "pong received\n" }; |
31 | |
31 | |
32 | # create a port on another node |
32 | # create a port on another node |
33 | my $port = spawn $node, $initfunc, @initdata; |
33 | my $port = spawn $node, $initfunc, @initdata; |
34 | |
34 | |
|
|
35 | # destroy a prot again |
|
|
36 | kil $port; # "normal" kill |
|
|
37 | kil $port, my_error => "everything is broken"; # error kill |
|
|
38 | |
35 | # monitoring |
39 | # monitoring |
36 | mon $localport, $cb->(@msg) # callback is invoked on death |
40 | mon $localport, $cb->(@msg) # callback is invoked on death |
37 | mon $localport, $otherport # kill otherport on abnormal death |
41 | mon $localport, $otherport # kill otherport on abnormal death |
38 | mon $localport, $otherport, @msg # send message on death |
42 | mon $localport, $otherport, @msg # send message on death |
|
|
43 | |
|
|
44 | # temporarily execute code in port context |
|
|
45 | peval $port, sub { die "kill the port!" }; |
|
|
46 | |
|
|
47 | # execute callbacks in $SELF port context |
|
|
48 | my $timer = AE::timer 1, 0, psub { |
|
|
49 | die "kill the port, delayed"; |
|
|
50 | }; |
39 | |
51 | |
40 | =head1 CURRENT STATUS |
52 | =head1 CURRENT STATUS |
41 | |
53 | |
42 | bin/aemp - stable. |
54 | bin/aemp - stable. |
43 | AnyEvent::MP - stable API, should work. |
55 | AnyEvent::MP - stable API, should work. |
… | |
… | |
116 | seed node that blocks for long periods will slow down everybody else. |
128 | seed node that blocks for long periods will slow down everybody else. |
117 | |
129 | |
118 | =item seeds - C<host:port> |
130 | =item seeds - C<host:port> |
119 | |
131 | |
120 | Seeds are transport endpoint(s) (usually a hostname/IP address and a |
132 | Seeds are transport endpoint(s) (usually a hostname/IP address and a |
121 | TCP port) of nodes thta should be used as seed nodes. |
133 | TCP port) of nodes that should be used as seed nodes. |
122 | |
134 | |
123 | The nodes listening on those endpoints are expected to be long-running, |
135 | The nodes listening on those endpoints are expected to be long-running, |
124 | and at least one of those should always be available. When nodes run out |
136 | and at least one of those should always be available. When nodes run out |
125 | of connections (e.g. due to a network error), they try to re-establish |
137 | of connections (e.g. due to a network error), they try to re-establish |
126 | connections to some seednodes again to join the network. |
138 | connections to some seednodes again to join the network. |
… | |
… | |
143 | |
155 | |
144 | use AE (); |
156 | use AE (); |
145 | |
157 | |
146 | use base "Exporter"; |
158 | use base "Exporter"; |
147 | |
159 | |
148 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
160 | our $VERSION = 1.29; |
149 | |
161 | |
150 | our @EXPORT = qw( |
162 | our @EXPORT = qw( |
151 | NODE $NODE *SELF node_of after |
163 | NODE $NODE *SELF node_of after |
152 | configure |
164 | configure |
153 | snd rcv mon mon_guard kil psub spawn cal |
165 | snd rcv mon mon_guard kil psub peval spawn cal |
154 | port |
166 | port |
155 | ); |
167 | ); |
156 | |
168 | |
157 | our $SELF; |
169 | our $SELF; |
158 | |
170 | |
… | |
… | |
178 | |
190 | |
179 | Before a node can talk to other nodes on the network (i.e. enter |
191 | Before a node can talk to other nodes on the network (i.e. enter |
180 | "distributed mode") it has to configure itself - the minimum a node needs |
192 | "distributed mode") it has to configure itself - the minimum a node needs |
181 | to know is its own name, and optionally it should know the addresses of |
193 | to know is its own name, and optionally it should know the addresses of |
182 | some other nodes in the network to discover other nodes. |
194 | some other nodes in the network to discover other nodes. |
|
|
195 | |
|
|
196 | The key/value pairs are basically the same ones as documented for the |
|
|
197 | F<aemp> command line utility (sans the set/del prefix). |
183 | |
198 | |
184 | This function configures a node - it must be called exactly once (or |
199 | This function configures a node - it must be called exactly once (or |
185 | never) before calling other AnyEvent::MP functions. |
200 | never) before calling other AnyEvent::MP functions. |
186 | |
201 | |
187 | =over 4 |
202 | =over 4 |
… | |
… | |
371 | msg1 => sub { ... }, |
386 | msg1 => sub { ... }, |
372 | ... |
387 | ... |
373 | ; |
388 | ; |
374 | |
389 | |
375 | Example: temporarily register a rcv callback for a tag matching some port |
390 | Example: temporarily register a rcv callback for a tag matching some port |
376 | (e.g. for a rpc reply) and unregister it after a message was received. |
391 | (e.g. for an rpc reply) and unregister it after a message was received. |
377 | |
392 | |
378 | rcv $port, $otherport => sub { |
393 | rcv $port, $otherport => sub { |
379 | my @reply = @_; |
394 | my @reply = @_; |
380 | |
395 | |
381 | rcv $SELF, $otherport; |
396 | rcv $SELF, $otherport; |
… | |
… | |
394 | if (ref $_[0]) { |
409 | if (ref $_[0]) { |
395 | if (my $self = $PORT_DATA{$portid}) { |
410 | if (my $self = $PORT_DATA{$portid}) { |
396 | "AnyEvent::MP::Port" eq ref $self |
411 | "AnyEvent::MP::Port" eq ref $self |
397 | or Carp::croak "$port: rcv can only be called on message matching ports, caught"; |
412 | or Carp::croak "$port: rcv can only be called on message matching ports, caught"; |
398 | |
413 | |
399 | $self->[2] = shift; |
414 | $self->[0] = shift; |
400 | } else { |
415 | } else { |
401 | my $cb = shift; |
416 | my $cb = shift; |
402 | $PORT{$portid} = sub { |
417 | $PORT{$portid} = sub { |
403 | local $SELF = $port; |
418 | local $SELF = $port; |
404 | eval { &$cb }; _self_die if $@; |
419 | eval { &$cb }; _self_die if $@; |
405 | }; |
420 | }; |
406 | } |
421 | } |
407 | } elsif (defined $_[0]) { |
422 | } elsif (defined $_[0]) { |
408 | my $self = $PORT_DATA{$portid} ||= do { |
423 | my $self = $PORT_DATA{$portid} ||= do { |
409 | my $self = bless [$PORT{$port} || sub { }, { }, $port], "AnyEvent::MP::Port"; |
424 | my $self = bless [$PORT{$portid} || sub { }, { }, $port], "AnyEvent::MP::Port"; |
410 | |
425 | |
411 | $PORT{$portid} = sub { |
426 | $PORT{$portid} = sub { |
412 | local $SELF = $port; |
427 | local $SELF = $port; |
413 | |
428 | |
414 | if (my $cb = $self->[1]{$_[0]}) { |
429 | if (my $cb = $self->[1]{$_[0]}) { |
… | |
… | |
436 | } |
451 | } |
437 | |
452 | |
438 | $port |
453 | $port |
439 | } |
454 | } |
440 | |
455 | |
|
|
456 | =item peval $port, $coderef[, @args] |
|
|
457 | |
|
|
458 | Evaluates the given C<$codref> within the contetx of C<$port>, that is, |
|
|
459 | when the code throews an exception the C<$port> will be killed. |
|
|
460 | |
|
|
461 | Any remaining args will be passed to the callback. Any return values will |
|
|
462 | be returned to the caller. |
|
|
463 | |
|
|
464 | This is useful when you temporarily want to execute code in the context of |
|
|
465 | a port. |
|
|
466 | |
|
|
467 | Example: create a port and run some initialisation code in it's context. |
|
|
468 | |
|
|
469 | my $port = port { ... }; |
|
|
470 | |
|
|
471 | peval $port, sub { |
|
|
472 | init |
|
|
473 | or die "unable to init"; |
|
|
474 | }; |
|
|
475 | |
|
|
476 | =cut |
|
|
477 | |
|
|
478 | sub peval($$) { |
|
|
479 | local $SELF = shift; |
|
|
480 | my $cb = shift; |
|
|
481 | |
|
|
482 | if (wantarray) { |
|
|
483 | my @res = eval { &$cb }; |
|
|
484 | _self_die if $@; |
|
|
485 | @res |
|
|
486 | } else { |
|
|
487 | my $res = eval { &$cb }; |
|
|
488 | _self_die if $@; |
|
|
489 | $res |
|
|
490 | } |
|
|
491 | } |
|
|
492 | |
441 | =item $closure = psub { BLOCK } |
493 | =item $closure = psub { BLOCK } |
442 | |
494 | |
443 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |
495 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |
444 | closure is executed, sets up the environment in the same way as in C<rcv> |
496 | closure is executed, sets up the environment in the same way as in C<rcv> |
445 | callbacks, i.e. runtime errors will cause the port to get C<kil>ed. |
497 | callbacks, i.e. runtime errors will cause the port to get C<kil>ed. |
|
|
498 | |
|
|
499 | The effect is basically as if it returned C<< sub { peval $SELF, sub { |
|
|
500 | BLOCK }, @_ } >>. |
446 | |
501 | |
447 | This is useful when you register callbacks from C<rcv> callbacks: |
502 | This is useful when you register callbacks from C<rcv> callbacks: |
448 | |
503 | |
449 | rcv delayed_reply => sub { |
504 | rcv delayed_reply => sub { |
450 | my ($delay, @reply) = @_; |
505 | my ($delay, @reply) = @_; |
… | |
… | |
523 | delivered again. |
578 | delivered again. |
524 | |
579 | |
525 | Inter-host-connection timeouts and monitoring depend on the transport |
580 | Inter-host-connection timeouts and monitoring depend on the transport |
526 | used. The only transport currently implemented is TCP, and AnyEvent::MP |
581 | used. The only transport currently implemented is TCP, and AnyEvent::MP |
527 | relies on TCP to detect node-downs (this can take 10-15 minutes on a |
582 | relies on TCP to detect node-downs (this can take 10-15 minutes on a |
528 | non-idle connection, and usually around two hours for idle conenctions). |
583 | non-idle connection, and usually around two hours for idle connections). |
529 | |
584 | |
530 | This means that monitoring is good for program errors and cleaning up |
585 | This means that monitoring is good for program errors and cleaning up |
531 | stuff eventually, but they are no replacement for a timeout when you need |
586 | stuff eventually, but they are no replacement for a timeout when you need |
532 | to ensure some maximum latency. |
587 | to ensure some maximum latency. |
533 | |
588 | |
… | |
… | |
565 | } |
620 | } |
566 | |
621 | |
567 | $node->monitor ($port, $cb); |
622 | $node->monitor ($port, $cb); |
568 | |
623 | |
569 | defined wantarray |
624 | defined wantarray |
570 | and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) } |
625 | and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) |
571 | } |
626 | } |
572 | |
627 | |
573 | =item $guard = mon_guard $port, $ref, $ref... |
628 | =item $guard = mon_guard $port, $ref, $ref... |
574 | |
629 | |
575 | Monitors the given C<$port> and keeps the passed references. When the port |
630 | Monitors the given C<$port> and keeps the passed references. When the port |
… | |
… | |
598 | |
653 | |
599 | =item kil $port[, @reason] |
654 | =item kil $port[, @reason] |
600 | |
655 | |
601 | Kill the specified port with the given C<@reason>. |
656 | Kill the specified port with the given C<@reason>. |
602 | |
657 | |
603 | If no C<@reason> is specified, then the port is killed "normally" (ports |
658 | If no C<@reason> is specified, then the port is killed "normally" - |
604 | monitoring other ports will not necessarily die because a port dies |
659 | monitor callback will be invoked, but the kil will not cause linked ports |
605 | "normally"). |
660 | (C<mon $mport, $lport> form) to get killed. |
606 | |
661 | |
607 | Otherwise, linked ports get killed with the same reason (second form of |
662 | If a C<@reason> is specified, then linked ports (C<mon $mport, $lport> |
608 | C<mon>, see above). |
663 | form) get killed with the same reason. |
609 | |
664 | |
610 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
665 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
611 | will be reported as reason C<< die => $@ >>. |
666 | will be reported as reason C<< die => $@ >>. |
612 | |
667 | |
613 | Transport/communication errors are reported as C<< transport_error => |
668 | Transport/communication errors are reported as C<< transport_error => |
… | |
… | |
727 | |
782 | |
728 | If an optional time-out (in seconds) is given and it is not C<undef>, |
783 | If an optional time-out (in seconds) is given and it is not C<undef>, |
729 | then the callback will be called without any arguments after the time-out |
784 | then the callback will be called without any arguments after the time-out |
730 | elapsed and the port is C<kil>ed. |
785 | elapsed and the port is C<kil>ed. |
731 | |
786 | |
732 | If no time-out is given, then the local port will monitor the remote port |
787 | If no time-out is given (or it is C<undef>), then the local port will |
733 | instead, so it eventually gets cleaned-up. |
788 | monitor the remote port instead, so it eventually gets cleaned-up. |
734 | |
789 | |
735 | Currently this function returns the temporary port, but this "feature" |
790 | Currently this function returns the temporary port, but this "feature" |
736 | might go in future versions unless you can make a convincing case that |
791 | might go in future versions unless you can make a convincing case that |
737 | this is indeed useful for something. |
792 | this is indeed useful for something. |
738 | |
793 | |
… | |
… | |
774 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
829 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
775 | == aemp node, Erlang process == aemp port), so many of the documents and |
830 | == aemp node, Erlang process == aemp port), so many of the documents and |
776 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |
831 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |
777 | sample: |
832 | sample: |
778 | |
833 | |
779 | http://www.Erlang.se/doc/programming_rules.shtml |
834 | http://www.erlang.se/doc/programming_rules.shtml |
780 | http://Erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4 |
835 | http://erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4 |
781 | http://Erlang.org/download/Erlang-book-part1.pdf # chapters 5 and 6 |
836 | http://erlang.org/download/erlang-book-part1.pdf # chapters 5 and 6 |
782 | http://Erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5 |
837 | http://erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5 |
783 | |
838 | |
784 | Despite the similarities, there are also some important differences: |
839 | Despite the similarities, there are also some important differences: |
785 | |
840 | |
786 | =over 4 |
841 | =over 4 |
787 | |
842 | |
788 | =item * Node IDs are arbitrary strings in AEMP. |
843 | =item * Node IDs are arbitrary strings in AEMP. |
789 | |
844 | |
790 | Erlang relies on special naming and DNS to work everywhere in the same |
845 | Erlang relies on special naming and DNS to work everywhere in the same |
791 | way. AEMP relies on each node somehow knowing its own address(es) (e.g. by |
846 | way. AEMP relies on each node somehow knowing its own address(es) (e.g. by |
792 | configuration or DNS), but will otherwise discover other odes itself. |
847 | configuration or DNS), and possibly the addresses of some seed nodes, but |
|
|
848 | will otherwise discover other nodes (and their IDs) itself. |
793 | |
849 | |
794 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
850 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
795 | uses "local ports are like remote ports". |
851 | uses "local ports are like remote ports". |
796 | |
852 | |
797 | The failure modes for local ports are quite different (runtime errors |
853 | The failure modes for local ports are quite different (runtime errors |
… | |
… | |
822 | so does not need a queue that can overflow). AEMP sends are immediate, |
878 | so does not need a queue that can overflow). AEMP sends are immediate, |
823 | connection establishment is handled in the background. |
879 | connection establishment is handled in the background. |
824 | |
880 | |
825 | =item * Erlang suffers from silent message loss, AEMP does not. |
881 | =item * Erlang suffers from silent message loss, AEMP does not. |
826 | |
882 | |
827 | Erlang makes few guarantees on messages delivery - messages can get lost |
883 | Erlang implements few guarantees on messages delivery - messages can get |
828 | without any of the processes realising it (i.e. you send messages a, b, |
884 | lost without any of the processes realising it (i.e. you send messages a, |
829 | and c, and the other side only receives messages a and c). |
885 | b, and c, and the other side only receives messages a and c). |
830 | |
886 | |
831 | AEMP guarantees correct ordering, and the guarantee that after one message |
887 | AEMP guarantees correct ordering, and the guarantee that after one message |
832 | is lost, all following ones sent to the same port are lost as well, until |
888 | is lost, all following ones sent to the same port are lost as well, until |
833 | monitoring raises an error, so there are no silent "holes" in the message |
889 | monitoring raises an error, so there are no silent "holes" in the message |
834 | sequence. |
890 | sequence. |
… | |
… | |
896 | overhead, as well as having to keep a proxy object everywhere. |
952 | overhead, as well as having to keep a proxy object everywhere. |
897 | |
953 | |
898 | Strings can easily be printed, easily serialised etc. and need no special |
954 | Strings can easily be printed, easily serialised etc. and need no special |
899 | procedures to be "valid". |
955 | procedures to be "valid". |
900 | |
956 | |
901 | And as a result, a miniport consists of a single closure stored in a |
957 | And as a result, a port with just a default receiver consists of a single |
902 | global hash - it can't become much cheaper. |
958 | closure stored in a global hash - it can't become much cheaper. |
903 | |
959 | |
904 | =item Why favour JSON, why not a real serialising format such as Storable? |
960 | =item Why favour JSON, why not a real serialising format such as Storable? |
905 | |
961 | |
906 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
962 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
907 | format, but currently there is no way to make a node use Storable by |
963 | format, but currently there is no way to make a node use Storable by |
… | |
… | |
923 | |
979 | |
924 | L<AnyEvent::MP::Intro> - a gentle introduction. |
980 | L<AnyEvent::MP::Intro> - a gentle introduction. |
925 | |
981 | |
926 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
982 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
927 | |
983 | |
928 | L<AnyEvent::MP::Global> - network maintainance and port groups, to find |
984 | L<AnyEvent::MP::Global> - network maintenance and port groups, to find |
929 | your applications. |
985 | your applications. |
|
|
986 | |
|
|
987 | L<AnyEvent::MP::DataConn> - establish data connections between nodes. |
930 | |
988 | |
931 | L<AnyEvent::MP::LogCatcher> - simple service to display log messages from |
989 | L<AnyEvent::MP::LogCatcher> - simple service to display log messages from |
932 | all nodes. |
990 | all nodes. |
933 | |
991 | |
934 | L<AnyEvent>. |
992 | L<AnyEvent>. |