… | |
… | |
105 | |
105 | |
106 | =cut |
106 | =cut |
107 | |
107 | |
108 | package AnyEvent::MP; |
108 | package AnyEvent::MP; |
109 | |
109 | |
110 | use AnyEvent::MP::Base; |
110 | use AnyEvent::MP::Kernel; |
111 | |
111 | |
112 | use common::sense; |
112 | use common::sense; |
113 | |
113 | |
114 | use Carp (); |
114 | use Carp (); |
115 | |
115 | |
116 | use AE (); |
116 | use AE (); |
117 | |
117 | |
118 | use base "Exporter"; |
118 | use base "Exporter"; |
119 | |
119 | |
120 | our $VERSION = '0.1'; |
120 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
|
|
121 | |
121 | our @EXPORT = qw( |
122 | our @EXPORT = qw( |
122 | NODE $NODE *SELF node_of _any_ |
123 | NODE $NODE *SELF node_of _any_ |
123 | resolve_node initialise_node |
124 | resolve_node initialise_node |
124 | snd rcv mon kil reg psub spawn |
125 | snd rcv mon kil reg psub spawn |
125 | port |
126 | port |
… | |
… | |
501 | |
502 | |
502 | =item $guard = mon $port |
503 | =item $guard = mon $port |
503 | |
504 | |
504 | =item $guard = mon $port, $rcvport, @msg |
505 | =item $guard = mon $port, $rcvport, @msg |
505 | |
506 | |
506 | Monitor the given port and do something when the port is killed, and |
507 | 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. |
508 | messages to it were lost, and optionally return a guard that can be used |
|
|
509 | to stop monitoring again. |
|
|
510 | |
|
|
511 | C<mon> effectively guarantees that, in the absence of hardware failures, |
|
|
512 | that after starting the monitor, either all messages sent to the port |
|
|
513 | will arrive, or the monitoring action will be invoked after possible |
|
|
514 | message loss has been detected. No messages will be lost "in between" |
|
|
515 | (after the first lost message no further messages will be received by the |
|
|
516 | port). After the monitoring action was invoked, further messages might get |
|
|
517 | delivered again. |
508 | |
518 | |
509 | In the first form (callback), the callback is simply called with any |
519 | 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 |
520 | 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 |
521 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
512 | C<eval> if unsure. |
522 | C<eval> if unsure. |
513 | |
523 | |
514 | In the second form (another port given), the other port (C<$rcvport) |
524 | In the second form (another port given), the other port (C<$rcvport>) |
515 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
525 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
516 | "normal" kils nothing happens, while under all other conditions, the other |
526 | "normal" kils nothing happens, while under all other conditions, the other |
517 | port is killed with the same reason. |
527 | port is killed with the same reason. |
518 | |
528 | |
519 | The third form (kill self) is the same as the second form, except that |
529 | The third form (kill self) is the same as the second form, except that |
… | |
… | |
546 | sub mon { |
556 | sub mon { |
547 | my ($noderef, $port) = split /#/, shift, 2; |
557 | my ($noderef, $port) = split /#/, shift, 2; |
548 | |
558 | |
549 | my $node = $NODE{$noderef} || add_node $noderef; |
559 | my $node = $NODE{$noderef} || add_node $noderef; |
550 | |
560 | |
551 | my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
561 | my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
552 | |
562 | |
553 | unless (ref $cb) { |
563 | unless (ref $cb) { |
554 | if (@_) { |
564 | if (@_) { |
555 | # send a kill info message |
565 | # send a kill info message |
556 | my (@msg) = @_; |
566 | my (@msg) = ($cb, @_); |
557 | $cb = sub { snd @msg, @_ }; |
567 | $cb = sub { snd @msg, @_ }; |
558 | } else { |
568 | } else { |
559 | # simply kill other port |
569 | # simply kill other port |
560 | my $port = $cb; |
570 | my $port = $cb; |
561 | $cb = sub { kil $port, @_ if @_ }; |
571 | $cb = sub { kil $port, @_ if @_ }; |
… | |
… | |
619 | |
629 | |
620 | The port ID of the newly created port is return immediately, and it is |
630 | 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. |
631 | permissible to immediately start sending messages or monitor the port. |
622 | |
632 | |
623 | After the port has been created, the init function is |
633 | After the port has been created, the init function is |
624 | called. This fucntion must be a fully-qualified function name |
634 | called. This function must be a fully-qualified function name |
625 | (e.g. C<MyApp::Chat::Server::init>). |
635 | (e.g. C<MyApp::Chat::Server::init>). To specify a function in the main |
|
|
636 | program, use C<::name>. |
626 | |
637 | |
627 | If the function doesn't exist, then the node tries to C<require> |
638 | 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. |
639 | 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 |
640 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
630 | exists or it runs out of package names. |
641 | exists or it runs out of package names. |
… | |
… | |
669 | sub spawn(@) { |
680 | sub spawn(@) { |
670 | my ($noderef, undef) = split /#/, shift, 2; |
681 | my ($noderef, undef) = split /#/, shift, 2; |
671 | |
682 | |
672 | my $id = "$RUNIQ." . $ID++; |
683 | my $id = "$RUNIQ." . $ID++; |
673 | |
684 | |
|
|
685 | $_[0] =~ /::/ |
|
|
686 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
|
|
687 | |
674 | ($NODE{$noderef} || add_node $noderef) |
688 | ($NODE{$noderef} || add_node $noderef) |
675 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
689 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
676 | |
690 | |
677 | "$noderef#$id" |
691 | "$noderef#$id" |
678 | } |
692 | } |