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.34 by root, Wed Aug 5 23:50:46 2009 UTC vs.
Revision 1.37 by root, Fri Aug 7 16:47:23 2009 UTC

22 snd $port2, ping => $port1; 22 snd $port2, ping => $port1;
23 23
24 # more, smarter, matches (_any_ is exported by this module) 24 # more, smarter, matches (_any_ is exported by this module)
25 rcv $port, [child_died => $pid] => sub { ... 25 rcv $port, [child_died => $pid] => sub { ...
26 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 26 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3
27
28 # monitoring
29 mon $port, $cb->(@msg) # callback is invoked on death
30 mon $port, $otherport # kill otherport on abnormal death
31 mon $port, $otherport, @msg # send message on death
27 32
28=head1 DESCRIPTION 33=head1 DESCRIPTION
29 34
30This module (-family) implements a simple message passing framework. 35This module (-family) implements a simple message passing framework.
31 36
297 $port 302 $port
298} 303}
299 304
300=item reg $port, $name 305=item reg $port, $name
301 306
302Registers the given port under the name C<$name>. If the name already 307=item reg $name
303exists it is replaced. 308
309Registers the given port (or C<$SELF><<< if missing) under the name
310C<$name>. If the name already exists it is replaced.
304 311
305A port can only be registered under one well known name. 312A port can only be registered under one well known name.
306 313
307A port automatically becomes unregistered when it is killed. 314A port automatically becomes unregistered when it is killed.
308 315
309=cut 316=cut
310 317
311sub reg(@) { 318sub reg(@) {
312 my ($port, $name) = @_; 319 my $port = @_ > 1 ? shift : $SELF || Carp::croak 'reg: called with one argument only, but $SELF not set,';
313 320
314 $REG{$name} = $port; 321 $REG{$_[0]} = $port;
315} 322}
316 323
317=item rcv $port, $callback->(@msg) 324=item rcv $port, $callback->(@msg)
318 325
319Replaces the callback on the specified miniport (after converting it to 326Replaces the callback on the specified miniport (after converting it to
324=item rcv $port, $smartmatch => $callback->(@msg), ... 331=item rcv $port, $smartmatch => $callback->(@msg), ...
325 332
326=item rcv $port, [$smartmatch...] => $callback->(@msg), ... 333=item rcv $port, [$smartmatch...] => $callback->(@msg), ...
327 334
328Register callbacks to be called on matching messages on the given full 335Register callbacks to be called on matching messages on the given full
329port (after converting it to one if required). 336port (after converting it to one if required) and return the port.
330 337
331The callback has to return a true value when its work is done, after 338The callback has to return a true value when its work is done, after
332which is will be removed, or a false value in which case it will stay 339which is will be removed, or a false value in which case it will stay
333registered. 340registered.
334 341
346exported by this module) matches any single element of the message. 353exported by this module) matches any single element of the message.
347 354
348While not required, it is highly recommended that the first matching 355While not required, it is highly recommended that the first matching
349element is a string identifying the message. The one-string-only match is 356element is a string identifying the message. The one-string-only match is
350also the most efficient match (by far). 357also the most efficient match (by far).
358
359Example: create a port and bind receivers on it in one go.
360
361 my $port = rcv port,
362 msg1 => sub { ...; 0 },
363 msg2 => sub { ...; 0 },
364 ;
365
366Example: create a port, bind receivers and send it in a message elsewhere
367in one go:
368
369 snd $otherport, reply =>
370 rcv port,
371 msg1 => sub { ...; 0 },
372 ...
373 ;
351 374
352=cut 375=cut
353 376
354sub rcv($@) { 377sub rcv($@) {
355 my $port = shift; 378 my $port = shift;
462 } 485 }
463} 486}
464 487
465=item $guard = mon $port, $cb->(@reason) 488=item $guard = mon $port, $cb->(@reason)
466 489
467=item $guard = mon $port, $otherport 490=item $guard = mon $port, $rcvport
468 491
492=item $guard = mon $port
493
469=item $guard = mon $port, $otherport, @msg 494=item $guard = mon $port, $rcvport, @msg
470 495
471Monitor the given port and do something when the port is killed. 496Monitor the given port and do something when the port is killed, and
497optionally return a guard that can be used to stop monitoring again.
472 498
473In the first form, the callback is simply called with any number 499In the first form (callback), the callback is simply called with any
474of C<@reason> elements (no @reason means that the port was deleted 500number of C<@reason> elements (no @reason means that the port was deleted
475"normally"). Note also that I<< the callback B<must> never die >>, so use 501"normally"). Note also that I<< the callback B<must> never die >>, so use
476C<eval> if unsure. 502C<eval> if unsure.
477 503
478In the second form, the other port will be C<kil>'ed with C<@reason>, iff 504In the second form (another port given), the other port (C<$rcvport)
479a @reason was specified, i.e. on "normal" kils nothing happens, while 505will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on
480under all other conditions, the other port is killed with the same reason. 506"normal" kils nothing happens, while under all other conditions, the other
507port is killed with the same reason.
481 508
509The third form (kill self) is the same as the second form, except that
510C<$rvport> defaults to C<$SELF>.
511
482In the last form, a message of the form C<@msg, @reason> will be C<snd>. 512In the last form (message), a message of the form C<@msg, @reason> will be
513C<snd>.
514
515As a rule of thumb, monitoring requests should always monitor a port from
516a local port (or callback). The reason is that kill messages might get
517lost, just like any other message. Another less obvious reason is that
518even monitoring requests can get lost (for exmaple, when the connection
519to the other node goes down permanently). When monitoring a port locally
520these problems do not exist.
483 521
484Example: call a given callback when C<$port> is killed. 522Example: call a given callback when C<$port> is killed.
485 523
486 mon $port, sub { warn "port died because of <@_>\n" }; 524 mon $port, sub { warn "port died because of <@_>\n" };
487 525
488Example: kill ourselves when C<$port> is killed abnormally. 526Example: kill ourselves when C<$port> is killed abnormally.
489 527
490 mon $port, $self; 528 mon $port;
491 529
492Example: send us a restart message another C<$port> is killed. 530Example: send us a restart message when another C<$port> is killed.
493 531
494 mon $port, $self => "restart"; 532 mon $port, $self => "restart";
495 533
496=cut 534=cut
497 535
498sub mon { 536sub mon {
499 my ($noderef, $port) = split /#/, shift, 2; 537 my ($noderef, $port) = split /#/, shift, 2;
500 538
501 my $node = $NODE{$noderef} || add_node $noderef; 539 my $node = $NODE{$noderef} || add_node $noderef;
502 540
503 my $cb = shift; 541 my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,';
504 542
505 unless (ref $cb) { 543 unless (ref $cb) {
506 if (@_) { 544 if (@_) {
507 # send a kill info message 545 # send a kill info message
508 my (@msg) = ($cb, @_); 546 my (@msg) = @_;
509 $cb = sub { snd @msg, @_ }; 547 $cb = sub { snd @msg, @_ };
510 } else { 548 } else {
511 # simply kill other port 549 # simply kill other port
512 my $port = $cb; 550 my $port = $cb;
513 $cb = sub { kil $port, @_ if @_ }; 551 $cb = sub { kil $port, @_ if @_ };
539=cut 577=cut
540 578
541sub mon_guard { 579sub mon_guard {
542 my ($port, @refs) = @_; 580 my ($port, @refs) = @_;
543 581
582 #TODO: mon-less form?
583
544 mon $port, sub { 0 && @refs } 584 mon $port, sub { 0 && @refs }
545} 585}
546
547=item lnk $port1, $port2
548
549Link two ports. This is simply a shorthand for:
550
551 mon $port1, $port2;
552 mon $port2, $port1;
553
554It means that if either one is killed abnormally, the other one gets
555killed as well.
556 586
557=item kil $port[, @reason] 587=item kil $port[, @reason]
558 588
559Kill the specified port with the given C<@reason>. 589Kill the specified port with the given C<@reason>.
560 590
618 648
619=back 649=back
620 650
621=head1 AnyEvent::MP vs. Distributed Erlang 651=head1 AnyEvent::MP vs. Distributed Erlang
622 652
623AnyEvent::MP got lots of its ideas from distributed erlang (erlang node 653AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node
624== aemp node, erlang process == aemp port), so many of the documents and 654== aemp node, Erlang process == aemp port), so many of the documents and
625programming techniques employed by erlang apply to AnyEvent::MP. Here is a 655programming techniques employed by Erlang apply to AnyEvent::MP. Here is a
626sample: 656sample:
627 657
628 http://www.erlang.se/doc/programming_rules.shtml 658 http://www.Erlang.se/doc/programming_rules.shtml
629 http://erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4 659 http://Erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4
630 http://erlang.org/download/erlang-book-part1.pdf # chapters 5 and 6 660 http://Erlang.org/download/Erlang-book-part1.pdf # chapters 5 and 6
631 http://erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5 661 http://Erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5
632 662
633Despite the similarities, there are also some important differences: 663Despite the similarities, there are also some important differences:
634 664
635=over 4 665=over 4
636 666
647 677
648Erlang uses processes that selctively receive messages, and therefore 678Erlang uses processes that selctively receive messages, and therefore
649needs a queue. AEMP is event based, queuing messages would serve no useful 679needs a queue. AEMP is event based, queuing messages would serve no useful
650purpose. 680purpose.
651 681
652(But see L<Coro::MP> for a more erlang-like process model on top of AEMP). 682(But see L<Coro::MP> for a more Erlang-like process model on top of AEMP).
653 683
654=item * Erlang sends are synchronous, AEMP sends are asynchronous. 684=item * Erlang sends are synchronous, AEMP sends are asynchronous.
655 685
656Sending messages in erlang is synchronous and blocks the process. AEMP 686Sending messages in Erlang is synchronous and blocks the process. AEMP
657sends are immediate, connection establishment is handled in the 687sends are immediate, connection establishment is handled in the
658background. 688background.
659 689
660=item * Erlang can silently lose messages, AEMP cannot. 690=item * Erlang can silently lose messages, AEMP cannot.
661 691
664and c, and the other side only receives messages a and c). 694and c, and the other side only receives messages a and c).
665 695
666AEMP guarantees correct ordering, and the guarantee that there are no 696AEMP guarantees correct ordering, and the guarantee that there are no
667holes in the message sequence. 697holes in the message sequence.
668 698
669=item * In erlang, processes can be declared dead and later be found to be 699=item * In Erlang, processes can be declared dead and later be found to be
670alive. 700alive.
671 701
672In erlang it can happen that a monitored process is declared dead and 702In Erlang it can happen that a monitored process is declared dead and
673linked processes get killed, but later it turns out that the process is 703linked processes get killed, but later it turns out that the process is
674still alive - and can receive messages. 704still alive - and can receive messages.
675 705
676In AEMP, when port monitoring detects a port as dead, then that port will 706In AEMP, when port monitoring detects a port as dead, then that port will
677eventually be killed - it cannot happen that a node detects a port as dead 707eventually be killed - it cannot happen that a node detects a port as dead
678and then later sends messages to it, finding it is still alive. 708and then later sends messages to it, finding it is still alive.
679 709
680=item * Erlang can send messages to the wrong port, AEMP does not. 710=item * Erlang can send messages to the wrong port, AEMP does not.
681 711
682In erlang it is quite possible that a node that restarts reuses a process 712In Erlang it is quite possible that a node that restarts reuses a process
683ID known to other nodes for a completely different process, causing 713ID known to other nodes for a completely different process, causing
684messages destined for that process to end up in an unrelated process. 714messages destined for that process to end up in an unrelated process.
685 715
686AEMP never reuses port IDs, so old messages or old port IDs floating 716AEMP never reuses port IDs, so old messages or old port IDs floating
687around in the network will not be sent to an unrelated port. 717around in the network will not be sent to an unrelated port.
693securely authenticate nodes. 723securely authenticate nodes.
694 724
695=item * The AEMP protocol is optimised for both text-based and binary 725=item * The AEMP protocol is optimised for both text-based and binary
696communications. 726communications.
697 727
698The AEMP protocol, unlike the erlang protocol, supports both 728The AEMP protocol, unlike the Erlang protocol, supports both
699language-independent text-only protocols (good for debugging) and binary, 729language-independent text-only protocols (good for debugging) and binary,
700language-specific serialisers (e.g. Storable). 730language-specific serialisers (e.g. Storable).
701 731
702It has also been carefully designed to be implementable in other languages 732It has also been carefully designed to be implementable in other languages
703with a minimum of work while gracefully degrading fucntionality to make the 733with a minimum of work while gracefully degrading fucntionality to make the
704protocol simple. 734protocol simple.
705 735
736=item * AEMP has more flexible monitoring options than Erlang.
737
738In Erlang, you can chose to receive I<all> exit signals as messages
739or I<none>, there is no in-between, so monitoring single processes is
740difficult to implement. Monitoring in AEMP is more flexible than in
741Erlang, as one can choose between automatic kill, exit message or callback
742on a per-process basis.
743
744=item * Erlang tries to hide remote/local connections, AEMP does not.
745
746Monitoring in Erlang is not an indicator of process death/crashes,
747as linking is (except linking is unreliable in Erlang).
748
749In AEMP, you don't "look up" registered port names or send to named ports
750that might or might not be persistent. Instead, you normally spawn a port
751on the remote node. The init function monitors the you, and you monitor
752the remote port. Since both monitors are local to the node, they are much
753more reliable.
754
755This also saves round-trips and avoids sending messages to the wrong port
756(hard to do in Erlang).
757
706=back 758=back
707 759
708=head1 SEE ALSO 760=head1 SEE ALSO
709 761
710L<AnyEvent>. 762L<AnyEvent>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines