… | |
… | |
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 | |
506 | Monitor the given port and do something when the port is killed, and |
506 | Monitor the given port and do something when the port is killed or |
507 | optionally return a guard that can be used to stop monitoring again. |
507 | messages to it were lost, and optionally return a guard that can be used |
|
|
508 | to stop monitoring again. |
|
|
509 | |
|
|
510 | C<mon> effectively guarantees that, in the absence of hardware failures, |
|
|
511 | that after starting the monitor, either all messages sent to the port |
|
|
512 | will arrive, or the monitoring action will be invoked after possible |
|
|
513 | message 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 |
|
|
515 | port). After the monitoring action was invoked, further messages might get |
|
|
516 | delivered again. |
508 | |
517 | |
509 | In the first form (callback), the callback is simply called with any |
518 | In the first form (callback), the callback is simply called with any |
510 | number of C<@reason> elements (no @reason means that the port was deleted |
519 | number 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 |
512 | C<eval> if unsure. |
521 | C<eval> if unsure. |
… | |
… | |
546 | sub mon { |
555 | sub 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 @_ }; |