… | |
… | |
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 @_ }; |
… | |
… | |
620 | The port ID of the newly created port is return immediately, and it is |
629 | The port ID of the newly created port is return immediately, and it is |
621 | permissible to immediately start sending messages or monitor the port. |
630 | permissible to immediately start sending messages or monitor the port. |
622 | |
631 | |
623 | After the port has been created, the init function is |
632 | After the port has been created, the init function is |
624 | called. This function must be a fully-qualified function name |
633 | called. 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 |
|
|
635 | program, use C<::name>. |
626 | |
636 | |
627 | If the function doesn't exist, then the node tries to C<require> |
637 | If the function doesn't exist, then the node tries to C<require> |
628 | the package, then the package above the package and so on (e.g. |
638 | the package, then the package above the package and so on (e.g. |
629 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
639 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
630 | exists or it runs out of package names. |
640 | exists or it runs out of package names. |