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.38 by root, Fri Aug 7 22:55:18 2009 UTC vs.
Revision 1.42 by root, Sun Aug 9 00:41:49 2009 UTC

501 501
502=item $guard = mon $port 502=item $guard = mon $port
503 503
504=item $guard = mon $port, $rcvport, @msg 504=item $guard = mon $port, $rcvport, @msg
505 505
506Monitor the given port and do something when the port is killed, and 506Monitor the given port and do something when the port is killed or
507optionally return a guard that can be used to stop monitoring again. 507messages to it were lost, and optionally return a guard that can be used
508to stop monitoring again.
509
510C<mon> effectively guarantees that, in the absence of hardware failures,
511that after starting the monitor, either all messages sent to the port
512will arrive, or the monitoring action will be invoked after possible
513message loss has been detected. No messages will be lost "in between"
514(after the first lost message no further messages will be received by the
515port). After the monitoring action was invoked, further messages might get
516delivered again.
508 517
509In the first form (callback), the callback is simply called with any 518In the first form (callback), the callback is simply called with any
510number of C<@reason> elements (no @reason means that the port was deleted 519number of C<@reason> elements (no @reason means that the port was deleted
511"normally"). Note also that I<< the callback B<must> never die >>, so use 520"normally"). Note also that I<< the callback B<must> never die >>, so use
512C<eval> if unsure. 521C<eval> if unsure.
546sub mon { 555sub mon {
547 my ($noderef, $port) = split /#/, shift, 2; 556 my ($noderef, $port) = split /#/, shift, 2;
548 557
549 my $node = $NODE{$noderef} || add_node $noderef; 558 my $node = $NODE{$noderef} || add_node $noderef;
550 559
551 my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; 560 my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,';
552 561
553 unless (ref $cb) { 562 unless (ref $cb) {
554 if (@_) { 563 if (@_) {
555 # send a kill info message 564 # send a kill info message
556 my (@msg) = @_; 565 my (@msg) = ($cb, @_);
557 $cb = sub { snd @msg, @_ }; 566 $cb = sub { snd @msg, @_ };
558 } else { 567 } else {
559 # simply kill other port 568 # simply kill other port
560 my $port = $cb; 569 my $port = $cb;
561 $cb = sub { kil $port, @_ if @_ }; 570 $cb = sub { kil $port, @_ if @_ };
619 628
620The port ID of the newly created port is return immediately, and it is 629The port ID of the newly created port is return immediately, and it is
621permissible to immediately start sending messages or monitor the port. 630permissible to immediately start sending messages or monitor the port.
622 631
623After the port has been created, the init function is 632After the port has been created, the init function is
624called. This fucntion must be a fully-qualified function name 633called. This function must be a fully-qualified function name
625(e.g. C<MyApp::Chat::Server::init>). 634(e.g. C<MyApp::Chat::Server::init>). To specify a function in the main
635program, use C<::name>.
626 636
627If the function doesn't exist, then the node tries to C<require> 637If the function doesn't exist, then the node tries to C<require>
628the package, then the package above the package and so on (e.g. 638the package, then the package above the package and so on (e.g.
629C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function 639C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function
630exists or it runs out of package names. 640exists or it runs out of package names.
669sub spawn(@) { 679sub spawn(@) {
670 my ($noderef, undef) = split /#/, shift, 2; 680 my ($noderef, undef) = split /#/, shift, 2;
671 681
672 my $id = "$RUNIQ." . $ID++; 682 my $id = "$RUNIQ." . $ID++;
673 683
684 $_[0] =~ /::/
685 or Carp::croak "spawn init function must be a fully-qualified name, caught";
686
674 ($NODE{$noderef} || add_node $noderef) 687 ($NODE{$noderef} || add_node $noderef)
675 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); 688 ->send (["", "AnyEvent::MP::_spawn" => $id, @_]);
676 689
677 "$noderef#$id" 690 "$noderef#$id"
678} 691}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines