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.93 by root, Tue Sep 22 14:14:29 2009 UTC vs.
Revision 1.115 by root, Fri May 7 18:14:21 2010 UTC

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.
116seed node that blocks for long periods will slow down everybody else. 128seed 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
120Seeds are transport endpoint(s) (usually a hostname/IP address and a 132Seeds are transport endpoint(s) (usually a hostname/IP address and a
121TCP port) of nodes thta should be used as seed nodes. 133TCP port) of nodes that should be used as seed nodes.
122 134
123The nodes listening on those endpoints are expected to be long-running, 135The nodes listening on those endpoints are expected to be long-running,
124and at least one of those should always be available. When nodes run out 136and at least one of those should always be available. When nodes run out
125of connections (e.g. due to a network error), they try to re-establish 137of connections (e.g. due to a network error), they try to re-establish
126connections to some seednodes again to join the network. 138connections to some seednodes again to join the network.
143 155
144use AE (); 156use AE ();
145 157
146use base "Exporter"; 158use base "Exporter";
147 159
148our $VERSION = $AnyEvent::MP::Kernel::VERSION; 160our $VERSION = 1.29;
149 161
150our @EXPORT = qw( 162our @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
157our $SELF; 169our $SELF;
158 170
178 190
179Before a node can talk to other nodes on the network (i.e. enter 191Before 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
181to know is its own name, and optionally it should know the addresses of 193to know is its own name, and optionally it should know the addresses of
182some other nodes in the network to discover other nodes. 194some other nodes in the network to discover other nodes.
195
196The key/value pairs are basically the same ones as documented for the
197F<aemp> command line utility (sans the set/del prefix).
183 198
184This function configures a node - it must be called exactly once (or 199This function configures a node - it must be called exactly once (or
185never) before calling other AnyEvent::MP functions. 200never) 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
375Example: temporarily register a rcv callback for a tag matching some port 390Example: 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
458Evaluates the given C<$codref> within the contetx of C<$port>, that is,
459when the code throews an exception the C<$port> will be killed.
460
461Any remaining args will be passed to the callback. Any return values will
462be returned to the caller.
463
464This is useful when you temporarily want to execute code in the context of
465a port.
466
467Example: 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
478sub 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
443Remembers C<$SELF> and creates a closure out of the BLOCK. When the 495Remembers C<$SELF> and creates a closure out of the BLOCK. When the
444closure is executed, sets up the environment in the same way as in C<rcv> 496closure is executed, sets up the environment in the same way as in C<rcv>
445callbacks, i.e. runtime errors will cause the port to get C<kil>ed. 497callbacks, i.e. runtime errors will cause the port to get C<kil>ed.
498
499The effect is basically as if it returned C<< sub { peval $SELF, sub {
500BLOCK }, @_ } >>.
446 501
447This is useful when you register callbacks from C<rcv> callbacks: 502This 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) = @_;
523delivered again. 578delivered again.
524 579
525Inter-host-connection timeouts and monitoring depend on the transport 580Inter-host-connection timeouts and monitoring depend on the transport
526used. The only transport currently implemented is TCP, and AnyEvent::MP 581used. The only transport currently implemented is TCP, and AnyEvent::MP
527relies on TCP to detect node-downs (this can take 10-15 minutes on a 582relies on TCP to detect node-downs (this can take 10-15 minutes on a
528non-idle connection, and usually around two hours for idle conenctions). 583non-idle connection, and usually around two hours for idle connections).
529 584
530This means that monitoring is good for program errors and cleaning up 585This means that monitoring is good for program errors and cleaning up
531stuff eventually, but they are no replacement for a timeout when you need 586stuff eventually, but they are no replacement for a timeout when you need
532to ensure some maximum latency. 587to 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 $cb += 0
571 and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) } 625 and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) })
572} 626}
573 627
574=item $guard = mon_guard $port, $ref, $ref... 628=item $guard = mon_guard $port, $ref, $ref...
575 629
576Monitors the given C<$port> and keeps the passed references. When the port 630Monitors the given C<$port> and keeps the passed references. When the port
599 653
600=item kil $port[, @reason] 654=item kil $port[, @reason]
601 655
602Kill the specified port with the given C<@reason>. 656Kill the specified port with the given C<@reason>.
603 657
604If no C<@reason> is specified, then the port is killed "normally" (ports 658If no C<@reason> is specified, then the port is killed "normally" -
605monitoring other ports will not necessarily die because a port dies 659monitor callback will be invoked, but the kil will not cause linked ports
606"normally"). 660(C<mon $mport, $lport> form) to get killed.
607 661
608Otherwise, linked ports get killed with the same reason (second form of 662If a C<@reason> is specified, then linked ports (C<mon $mport, $lport>
609C<mon>, see above). 663form) get killed with the same reason.
610 664
611Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks 665Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks
612will be reported as reason C<< die => $@ >>. 666will be reported as reason C<< die => $@ >>.
613 667
614Transport/communication errors are reported as C<< transport_error => 668Transport/communication errors are reported as C<< transport_error =>
728 782
729If an optional time-out (in seconds) is given and it is not C<undef>, 783If an optional time-out (in seconds) is given and it is not C<undef>,
730then the callback will be called without any arguments after the time-out 784then the callback will be called without any arguments after the time-out
731elapsed and the port is C<kil>ed. 785elapsed and the port is C<kil>ed.
732 786
733If no time-out is given, then the local port will monitor the remote port 787If no time-out is given (or it is C<undef>), then the local port will
734instead, so it eventually gets cleaned-up. 788monitor the remote port instead, so it eventually gets cleaned-up.
735 789
736Currently this function returns the temporary port, but this "feature" 790Currently this function returns the temporary port, but this "feature"
737might go in future versions unless you can make a convincing case that 791might go in future versions unless you can make a convincing case that
738this is indeed useful for something. 792this is indeed useful for something.
739 793
775AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node 829AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node
776== 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
777programming techniques employed by Erlang apply to AnyEvent::MP. Here is a 831programming techniques employed by Erlang apply to AnyEvent::MP. Here is a
778sample: 832sample:
779 833
780 http://www.Erlang.se/doc/programming_rules.shtml 834 http://www.erlang.se/doc/programming_rules.shtml
781 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
782 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
783 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
784 838
785Despite the similarities, there are also some important differences: 839Despite the similarities, there are also some important differences:
786 840
787=over 4 841=over 4
788 842
789=item * Node IDs are arbitrary strings in AEMP. 843=item * Node IDs are arbitrary strings in AEMP.
790 844
791Erlang relies on special naming and DNS to work everywhere in the same 845Erlang relies on special naming and DNS to work everywhere in the same
792way. AEMP relies on each node somehow knowing its own address(es) (e.g. by 846way. AEMP relies on each node somehow knowing its own address(es) (e.g. by
793configuration or DNS), but will otherwise discover other odes itself. 847configuration or DNS), and possibly the addresses of some seed nodes, but
848will otherwise discover other nodes (and their IDs) itself.
794 849
795=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
796uses "local ports are like remote ports". 851uses "local ports are like remote ports".
797 852
798The failure modes for local ports are quite different (runtime errors 853The failure modes for local ports are quite different (runtime errors
823so does not need a queue that can overflow). AEMP sends are immediate, 878so does not need a queue that can overflow). AEMP sends are immediate,
824connection establishment is handled in the background. 879connection establishment is handled in the background.
825 880
826=item * Erlang suffers from silent message loss, AEMP does not. 881=item * Erlang suffers from silent message loss, AEMP does not.
827 882
828Erlang makes few guarantees on messages delivery - messages can get lost 883Erlang implements few guarantees on messages delivery - messages can get
829without any of the processes realising it (i.e. you send messages a, b, 884lost without any of the processes realising it (i.e. you send messages a,
830and c, and the other side only receives messages a and c). 885b, and c, and the other side only receives messages a and c).
831 886
832AEMP guarantees correct ordering, and the guarantee that after one message 887AEMP guarantees correct ordering, and the guarantee that after one message
833is lost, all following ones sent to the same port are lost as well, until 888is lost, all following ones sent to the same port are lost as well, until
834monitoring raises an error, so there are no silent "holes" in the message 889monitoring raises an error, so there are no silent "holes" in the message
835sequence. 890sequence.
897overhead, as well as having to keep a proxy object everywhere. 952overhead, as well as having to keep a proxy object everywhere.
898 953
899Strings can easily be printed, easily serialised etc. and need no special 954Strings can easily be printed, easily serialised etc. and need no special
900procedures to be "valid". 955procedures to be "valid".
901 956
902And as a result, a miniport consists of a single closure stored in a 957And as a result, a port with just a default receiver consists of a single
903global hash - it can't become much cheaper. 958closure stored in a global hash - it can't become much cheaper.
904 959
905=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?
906 961
907In fact, any AnyEvent::MP node will happily accept Storable as framing 962In fact, any AnyEvent::MP node will happily accept Storable as framing
908format, but currently there is no way to make a node use Storable by 963format, but currently there is no way to make a node use Storable by
924 979
925L<AnyEvent::MP::Intro> - a gentle introduction. 980L<AnyEvent::MP::Intro> - a gentle introduction.
926 981
927L<AnyEvent::MP::Kernel> - more, lower-level, stuff. 982L<AnyEvent::MP::Kernel> - more, lower-level, stuff.
928 983
929L<AnyEvent::MP::Global> - network maintainance and port groups, to find 984L<AnyEvent::MP::Global> - network maintenance and port groups, to find
930your applications. 985your applications.
986
987L<AnyEvent::MP::DataConn> - establish data connections between nodes.
931 988
932L<AnyEvent::MP::LogCatcher> - simple service to display log messages from 989L<AnyEvent::MP::LogCatcher> - simple service to display log messages from
933all nodes. 990all nodes.
934 991
935L<AnyEvent>. 992L<AnyEvent>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines