… | |
… | |
30 | rcv $port, pong => sub { warn "pong received\n" }; |
30 | rcv $port, pong => sub { warn "pong received\n" }; |
31 | |
31 | |
32 | # create a port on another node |
32 | # create a port on another node |
33 | my $port = spawn $node, $initfunc, @initdata; |
33 | my $port = spawn $node, $initfunc, @initdata; |
34 | |
34 | |
35 | # destroy a prot again |
35 | # destroy a port again |
36 | kil $port; # "normal" kill |
36 | kil $port; # "normal" kill |
37 | kil $port, my_error => "everything is broken"; # error kill |
37 | kil $port, my_error => "everything is broken"; # error kill |
38 | |
38 | |
39 | # monitoring |
39 | # monitoring |
40 | mon $localport, $cb->(@msg) # callback is invoked on death |
40 | mon $localport, $cb->(@msg) # callback is invoked on death |
… | |
… | |
155 | |
155 | |
156 | use AE (); |
156 | use AE (); |
157 | |
157 | |
158 | use base "Exporter"; |
158 | use base "Exporter"; |
159 | |
159 | |
160 | our $VERSION = 1.21; |
160 | our $VERSION = 1.29; |
161 | |
161 | |
162 | our @EXPORT = qw( |
162 | our @EXPORT = qw( |
163 | NODE $NODE *SELF node_of after |
163 | NODE $NODE *SELF node_of after |
164 | configure |
164 | configure |
165 | snd rcv mon mon_guard kil psub peval spawn cal |
165 | snd rcv mon mon_guard kil psub peval spawn cal |
… | |
… | |
191 | Before a node can talk to other nodes on the network (i.e. enter |
191 | Before a node can talk to other nodes on the network (i.e. enter |
192 | "distributed mode") it has to configure itself - the minimum a node needs |
192 | "distributed mode") it has to configure itself - the minimum a node needs |
193 | to know is its own name, and optionally it should know the addresses of |
193 | to know is its own name, and optionally it should know the addresses of |
194 | some other nodes in the network to discover other nodes. |
194 | some other nodes in the network to discover other nodes. |
195 | |
195 | |
|
|
196 | The key/value pairs are basically the same ones as documented for the |
|
|
197 | F<aemp> command line utility (sans the set/del prefix). |
|
|
198 | |
196 | This function configures a node - it must be called exactly once (or |
199 | This function configures a node - it must be called exactly once (or |
197 | never) before calling other AnyEvent::MP functions. |
200 | never) before calling other AnyEvent::MP functions. |
198 | |
201 | |
199 | =over 4 |
202 | =over 4 |
200 | |
203 | |
… | |
… | |
406 | if (ref $_[0]) { |
409 | if (ref $_[0]) { |
407 | if (my $self = $PORT_DATA{$portid}) { |
410 | if (my $self = $PORT_DATA{$portid}) { |
408 | "AnyEvent::MP::Port" eq ref $self |
411 | "AnyEvent::MP::Port" eq ref $self |
409 | or Carp::croak "$port: rcv can only be called on message matching ports, caught"; |
412 | or Carp::croak "$port: rcv can only be called on message matching ports, caught"; |
410 | |
413 | |
411 | $self->[2] = shift; |
414 | $self->[0] = shift; |
412 | } else { |
415 | } else { |
413 | my $cb = shift; |
416 | my $cb = shift; |
414 | $PORT{$portid} = sub { |
417 | $PORT{$portid} = sub { |
415 | local $SELF = $port; |
418 | local $SELF = $port; |
416 | eval { &$cb }; _self_die if $@; |
419 | eval { &$cb }; _self_die if $@; |
417 | }; |
420 | }; |
418 | } |
421 | } |
419 | } elsif (defined $_[0]) { |
422 | } elsif (defined $_[0]) { |
420 | my $self = $PORT_DATA{$portid} ||= do { |
423 | my $self = $PORT_DATA{$portid} ||= do { |
421 | my $self = bless [$PORT{$port} || sub { }, { }, $port], "AnyEvent::MP::Port"; |
424 | my $self = bless [$PORT{$portid} || sub { }, { }, $port], "AnyEvent::MP::Port"; |
422 | |
425 | |
423 | $PORT{$portid} = sub { |
426 | $PORT{$portid} = sub { |
424 | local $SELF = $port; |
427 | local $SELF = $port; |
425 | |
428 | |
426 | if (my $cb = $self->[1]{$_[0]}) { |
429 | if (my $cb = $self->[1]{$_[0]}) { |
… | |
… | |
492 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |
495 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |
493 | closure is executed, sets up the environment in the same way as in C<rcv> |
496 | closure is executed, sets up the environment in the same way as in C<rcv> |
494 | callbacks, i.e. runtime errors will cause the port to get C<kil>ed. |
497 | callbacks, i.e. runtime errors will cause the port to get C<kil>ed. |
495 | |
498 | |
496 | The effect is basically as if it returned C<< sub { peval $SELF, sub { |
499 | The effect is basically as if it returned C<< sub { peval $SELF, sub { |
497 | BLOCK } } >>. |
500 | BLOCK }, @_ } >>. |
498 | |
501 | |
499 | This is useful when you register callbacks from C<rcv> callbacks: |
502 | This is useful when you register callbacks from C<rcv> callbacks: |
500 | |
503 | |
501 | rcv delayed_reply => sub { |
504 | rcv delayed_reply => sub { |
502 | my ($delay, @reply) = @_; |
505 | my ($delay, @reply) = @_; |
… | |
… | |
650 | |
653 | |
651 | =item kil $port[, @reason] |
654 | =item kil $port[, @reason] |
652 | |
655 | |
653 | Kill the specified port with the given C<@reason>. |
656 | Kill the specified port with the given C<@reason>. |
654 | |
657 | |
655 | If no C<@reason> is specified, then the port is killed "normally" (ports |
658 | If no C<@reason> is specified, then the port is killed "normally" - |
656 | monitoring other ports will not necessarily die because a port dies |
659 | monitor callback will be invoked, but the kil will not cause linked ports |
657 | "normally"). |
660 | (C<mon $mport, $lport> form) to get killed. |
658 | |
661 | |
659 | Otherwise, linked ports get killed with the same reason (second form of |
662 | If a C<@reason> is specified, then linked ports (C<mon $mport, $lport> |
660 | C<mon>, see above). |
663 | form) get killed with the same reason. |
661 | |
664 | |
662 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
665 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
663 | will be reported as reason C<< die => $@ >>. |
666 | will be reported as reason C<< die => $@ >>. |
664 | |
667 | |
665 | Transport/communication errors are reported as C<< transport_error => |
668 | Transport/communication errors are reported as C<< transport_error => |
… | |
… | |
949 | overhead, as well as having to keep a proxy object everywhere. |
952 | overhead, as well as having to keep a proxy object everywhere. |
950 | |
953 | |
951 | Strings can easily be printed, easily serialised etc. and need no special |
954 | Strings can easily be printed, easily serialised etc. and need no special |
952 | procedures to be "valid". |
955 | procedures to be "valid". |
953 | |
956 | |
954 | And as a result, a miniport consists of a single closure stored in a |
957 | And as a result, a port with just a default receiver consists of a single |
955 | global hash - it can't become much cheaper. |
958 | closure stored in a global hash - it can't become much cheaper. |
956 | |
959 | |
957 | =item Why favour JSON, why not a real serialising format such as Storable? |
960 | =item Why favour JSON, why not a real serialising format such as Storable? |
958 | |
961 | |
959 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
962 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
960 | format, but currently there is no way to make a node use Storable by |
963 | format, but currently there is no way to make a node use Storable by |
… | |
… | |
976 | |
979 | |
977 | L<AnyEvent::MP::Intro> - a gentle introduction. |
980 | L<AnyEvent::MP::Intro> - a gentle introduction. |
978 | |
981 | |
979 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
982 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
980 | |
983 | |
981 | L<AnyEvent::MP::Global> - network maintainance and port groups, to find |
984 | L<AnyEvent::MP::Global> - network maintenance and port groups, to find |
982 | your applications. |
985 | your applications. |
|
|
986 | |
|
|
987 | L<AnyEvent::MP::DataConn> - establish data connections between nodes. |
983 | |
988 | |
984 | L<AnyEvent::MP::LogCatcher> - simple service to display log messages from |
989 | L<AnyEvent::MP::LogCatcher> - simple service to display log messages from |
985 | all nodes. |
990 | all nodes. |
986 | |
991 | |
987 | L<AnyEvent>. |
992 | L<AnyEvent>. |