… | |
… | |
8 | |
8 | |
9 | $NODE # contains this node's noderef |
9 | $NODE # contains this node's noderef |
10 | NODE # returns this node's noderef |
10 | NODE # returns this node's noderef |
11 | NODE $port # returns the noderef of the port |
11 | NODE $port # returns the noderef of the port |
12 | |
12 | |
|
|
13 | $SELF # receiving/own port id in rcv callbacks |
|
|
14 | |
|
|
15 | # initialise the node so it can send/receive messages |
|
|
16 | initialise_node; # -OR- |
|
|
17 | initialise_node "localhost:4040"; # -OR- |
|
|
18 | initialise_node "slave/", "localhost:4040" |
|
|
19 | |
|
|
20 | # ports are message endpoints |
|
|
21 | |
|
|
22 | # sending messages |
13 | snd $port, type => data...; |
23 | snd $port, type => data...; |
|
|
24 | snd $port, @msg; |
|
|
25 | snd @msg_with_first_element_being_a_port; |
14 | |
26 | |
15 | $SELF # receiving/own port id in rcv callbacks |
27 | # creating/using miniports |
|
|
28 | my $miniport = port { my @msg = @_; 0 }; |
16 | |
29 | |
|
|
30 | # creating/using full ports |
|
|
31 | my $port = port; |
17 | rcv $port, smartmatch => $cb->($port, @msg); |
32 | rcv $port, smartmatch => $cb->(@msg); |
18 | |
|
|
19 | # examples: |
|
|
20 | rcv $port2, ping => sub { snd $_[0], "pong"; 0 }; |
33 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
21 | rcv $port1, pong => sub { warn "pong received\n" }; |
34 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
22 | snd $port2, ping => $port1; |
|
|
23 | |
35 | |
24 | # more, smarter, matches (_any_ is exported by this module) |
36 | # more, smarter, matches (_any_ is exported by this module) |
25 | rcv $port, [child_died => $pid] => sub { ... |
37 | rcv $port, [child_died => $pid] => sub { ... |
26 | rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 |
38 | rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 |
|
|
39 | |
|
|
40 | # create a port on another node |
|
|
41 | my $port = spawn $node, $initfunc, @initdata; |
27 | |
42 | |
28 | # monitoring |
43 | # monitoring |
29 | mon $port, $cb->(@msg) # callback is invoked on death |
44 | mon $port, $cb->(@msg) # callback is invoked on death |
30 | mon $port, $otherport # kill otherport on abnormal death |
45 | mon $port, $otherport # kill otherport on abnormal death |
31 | mon $port, $otherport, @msg # send message on death |
46 | mon $port, $otherport, @msg # send message on death |
32 | |
47 | |
|
|
48 | =head1 CURRENT STATUS |
|
|
49 | |
|
|
50 | AnyEvent::MP - stable API, should work |
|
|
51 | AnyEvent::MP::Intro - outdated |
|
|
52 | AnyEvent::MP::Kernel - WIP |
|
|
53 | AnyEvent::MP::Transport - mostly stable |
|
|
54 | |
|
|
55 | stay tuned. |
|
|
56 | |
33 | =head1 DESCRIPTION |
57 | =head1 DESCRIPTION |
34 | |
58 | |
35 | This module (-family) implements a simple message passing framework. |
59 | This module (-family) implements a simple message passing framework. |
36 | |
60 | |
37 | Despite its simplicity, you can securely message other processes running |
61 | Despite its simplicity, you can securely message other processes running |
… | |
… | |
40 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
64 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
41 | manual page. |
65 | manual page. |
42 | |
66 | |
43 | At the moment, this module family is severly broken and underdocumented, |
67 | At the moment, this module family is severly broken and underdocumented, |
44 | so do not use. This was uploaded mainly to reserve the CPAN namespace - |
68 | so do not use. This was uploaded mainly to reserve the CPAN namespace - |
45 | stay tuned! The basic API should be finished, however. |
69 | stay tuned! |
46 | |
70 | |
47 | =head1 CONCEPTS |
71 | =head1 CONCEPTS |
48 | |
72 | |
49 | =over 4 |
73 | =over 4 |
50 | |
74 | |
… | |
… | |
95 | |
119 | |
96 | =cut |
120 | =cut |
97 | |
121 | |
98 | package AnyEvent::MP; |
122 | package AnyEvent::MP; |
99 | |
123 | |
100 | use AnyEvent::MP::Base; |
124 | use AnyEvent::MP::Kernel; |
101 | |
125 | |
102 | use common::sense; |
126 | use common::sense; |
103 | |
127 | |
104 | use Carp (); |
128 | use Carp (); |
105 | |
129 | |
106 | use AE (); |
130 | use AE (); |
107 | |
131 | |
108 | use base "Exporter"; |
132 | use base "Exporter"; |
109 | |
133 | |
110 | our $VERSION = '0.1'; |
134 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
|
|
135 | |
111 | our @EXPORT = qw( |
136 | our @EXPORT = qw( |
112 | NODE $NODE *SELF node_of _any_ |
137 | NODE $NODE *SELF node_of _any_ |
113 | resolve_node initialise_node |
138 | resolve_node initialise_node |
114 | snd rcv mon kil reg psub |
139 | snd rcv mon kil reg psub spawn |
115 | port |
140 | port |
116 | ); |
141 | ); |
117 | |
142 | |
118 | our $SELF; |
143 | our $SELF; |
119 | |
144 | |
… | |
… | |
143 | it should know the noderefs of some other nodes in the network. |
168 | it should know the noderefs of some other nodes in the network. |
144 | |
169 | |
145 | This function initialises a node - it must be called exactly once (or |
170 | This function initialises a node - it must be called exactly once (or |
146 | never) before calling other AnyEvent::MP functions. |
171 | never) before calling other AnyEvent::MP functions. |
147 | |
172 | |
148 | All arguments are noderefs, which can be either resolved or unresolved. |
173 | All arguments (optionally except for the first) are noderefs, which can be |
|
|
174 | either resolved or unresolved. |
|
|
175 | |
|
|
176 | The first argument will be looked up in the configuration database first |
|
|
177 | (if it is C<undef> then the current nodename will be used instead) to find |
|
|
178 | the relevant configuration profile (see L<aemp>). If none is found then |
|
|
179 | the default configuration is used. The configuration supplies additional |
|
|
180 | seed/master nodes and can override the actual noderef. |
149 | |
181 | |
150 | There are two types of networked nodes, public nodes and slave nodes: |
182 | There are two types of networked nodes, public nodes and slave nodes: |
151 | |
183 | |
152 | =over 4 |
184 | =over 4 |
153 | |
185 | |
154 | =item public nodes |
186 | =item public nodes |
155 | |
187 | |
156 | For public nodes, C<$noderef> must either be a (possibly unresolved) |
188 | For public nodes, C<$noderef> (supplied either directly to |
157 | noderef, in which case it will be resolved, or C<undef> (or missing), in |
189 | C<initialise_node> or indirectly via a profile or the nodename) must be a |
158 | which case the noderef will be guessed. |
190 | noderef (possibly unresolved, in which case it will be resolved). |
159 | |
191 | |
160 | Afterwards, the node will bind itself on all endpoints and try to connect |
192 | After resolving, the node will bind itself on all endpoints and try to |
161 | to all additional C<$seednodes> that are specified. Seednodes are optional |
193 | connect to all additional C<$seednodes> that are specified. Seednodes are |
162 | and can be used to quickly bootstrap the node into an existing network. |
194 | optional and can be used to quickly bootstrap the node into an existing |
|
|
195 | network. |
163 | |
196 | |
164 | =item slave nodes |
197 | =item slave nodes |
165 | |
198 | |
166 | When the C<$noderef> is the special string C<slave/>, then the node will |
199 | When the C<$noderef> (either as given or overriden by the config file) |
|
|
200 | is the special string C<slave/>, then the node will become a slave |
167 | become a slave node. Slave nodes cannot be contacted from outside and will |
201 | node. Slave nodes cannot be contacted from outside and will route most of |
168 | route most of their traffic to the master node that they attach to. |
202 | their traffic to the master node that they attach to. |
169 | |
203 | |
170 | At least one additional noderef is required: The node will try to connect |
204 | At least one additional noderef is required (either by specifying it |
171 | to all of them and will become a slave attached to the first node it can |
205 | directly or because it is part of the configuration profile): The node |
172 | successfully connect to. |
206 | will try to connect to all of them and will become a slave attached to the |
|
|
207 | first node it can successfully connect to. |
173 | |
208 | |
174 | =back |
209 | =back |
175 | |
210 | |
176 | This function will block until all nodes have been resolved and, for slave |
211 | This function will block until all nodes have been resolved and, for slave |
177 | nodes, until it has successfully established a connection to a master |
212 | nodes, until it has successfully established a connection to a master |
178 | server. |
213 | server. |
179 | |
214 | |
180 | Example: become a public node listening on the default node. |
215 | Example: become a public node listening on the guessed noderef, or the one |
|
|
216 | specified via C<aemp> for the current node. This should be the most common |
|
|
217 | form of invocation for "daemon"-type nodes. |
181 | |
218 | |
182 | initialise_node; |
219 | initialise_node; |
|
|
220 | |
|
|
221 | Example: become a slave node to any of the the seednodes specified via |
|
|
222 | C<aemp>. This form is often used for commandline clients. |
|
|
223 | |
|
|
224 | initialise_node "slave/"; |
|
|
225 | |
|
|
226 | Example: become a slave node to any of the specified master servers. This |
|
|
227 | form is also often used for commandline clients. |
|
|
228 | |
|
|
229 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
183 | |
230 | |
184 | Example: become a public node, and try to contact some well-known master |
231 | Example: become a public node, and try to contact some well-known master |
185 | servers to become part of the network. |
232 | servers to become part of the network. |
186 | |
233 | |
187 | initialise_node undef, "master1", "master2"; |
234 | initialise_node undef, "master1", "master2"; |
… | |
… | |
190 | |
237 | |
191 | initialise_node 4041; |
238 | initialise_node 4041; |
192 | |
239 | |
193 | Example: become a public node, only visible on localhost port 4044. |
240 | Example: become a public node, only visible on localhost port 4044. |
194 | |
241 | |
195 | initialise_node "locahost:4044"; |
242 | initialise_node "localhost:4044"; |
196 | |
|
|
197 | Example: become a slave node to any of the specified master servers. |
|
|
198 | |
|
|
199 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
|
|
200 | |
243 | |
201 | =item $cv = resolve_node $noderef |
244 | =item $cv = resolve_node $noderef |
202 | |
245 | |
203 | Takes an unresolved node reference that may contain hostnames and |
246 | Takes an unresolved node reference that may contain hostnames and |
204 | abbreviated IDs, resolves all of them and returns a resolved node |
247 | abbreviated IDs, resolves all of them and returns a resolved node |
… | |
… | |
340 | registered. |
383 | registered. |
341 | |
384 | |
342 | The global C<$SELF> (exported by this module) contains C<$port> while |
385 | The global C<$SELF> (exported by this module) contains C<$port> while |
343 | executing the callback. |
386 | executing the callback. |
344 | |
387 | |
345 | Runtime errors wdurign callback execution will result in the port being |
388 | Runtime errors during callback execution will result in the port being |
346 | C<kil>ed. |
389 | C<kil>ed. |
347 | |
390 | |
348 | If the match is an array reference, then it will be matched against the |
391 | If the match is an array reference, then it will be matched against the |
349 | first elements of the message, otherwise only the first element is being |
392 | first elements of the message, otherwise only the first element is being |
350 | matched. |
393 | matched. |
… | |
… | |
491 | |
534 | |
492 | =item $guard = mon $port |
535 | =item $guard = mon $port |
493 | |
536 | |
494 | =item $guard = mon $port, $rcvport, @msg |
537 | =item $guard = mon $port, $rcvport, @msg |
495 | |
538 | |
496 | Monitor the given port and do something when the port is killed, and |
539 | Monitor the given port and do something when the port is killed or |
497 | optionally return a guard that can be used to stop monitoring again. |
540 | messages to it were lost, and optionally return a guard that can be used |
|
|
541 | to stop monitoring again. |
|
|
542 | |
|
|
543 | C<mon> effectively guarantees that, in the absence of hardware failures, |
|
|
544 | that after starting the monitor, either all messages sent to the port |
|
|
545 | will arrive, or the monitoring action will be invoked after possible |
|
|
546 | message loss has been detected. No messages will be lost "in between" |
|
|
547 | (after the first lost message no further messages will be received by the |
|
|
548 | port). After the monitoring action was invoked, further messages might get |
|
|
549 | delivered again. |
498 | |
550 | |
499 | In the first form (callback), the callback is simply called with any |
551 | In the first form (callback), the callback is simply called with any |
500 | number of C<@reason> elements (no @reason means that the port was deleted |
552 | number of C<@reason> elements (no @reason means that the port was deleted |
501 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
553 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
502 | C<eval> if unsure. |
554 | C<eval> if unsure. |
503 | |
555 | |
504 | In the second form (another port given), the other port (C<$rcvport) |
556 | In the second form (another port given), the other port (C<$rcvport>) |
505 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
557 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
506 | "normal" kils nothing happens, while under all other conditions, the other |
558 | "normal" kils nothing happens, while under all other conditions, the other |
507 | port is killed with the same reason. |
559 | port is killed with the same reason. |
508 | |
560 | |
509 | The third form (kill self) is the same as the second form, except that |
561 | The third form (kill self) is the same as the second form, except that |
… | |
… | |
536 | sub mon { |
588 | sub mon { |
537 | my ($noderef, $port) = split /#/, shift, 2; |
589 | my ($noderef, $port) = split /#/, shift, 2; |
538 | |
590 | |
539 | my $node = $NODE{$noderef} || add_node $noderef; |
591 | my $node = $NODE{$noderef} || add_node $noderef; |
540 | |
592 | |
541 | my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
593 | my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
542 | |
594 | |
543 | unless (ref $cb) { |
595 | unless (ref $cb) { |
544 | if (@_) { |
596 | if (@_) { |
545 | # send a kill info message |
597 | # send a kill info message |
546 | my (@msg) = @_; |
598 | my (@msg) = ($cb, @_); |
547 | $cb = sub { snd @msg, @_ }; |
599 | $cb = sub { snd @msg, @_ }; |
548 | } else { |
600 | } else { |
549 | # simply kill other port |
601 | # simply kill other port |
550 | my $port = $cb; |
602 | my $port = $cb; |
551 | $cb = sub { kil $port, @_ if @_ }; |
603 | $cb = sub { kil $port, @_ if @_ }; |
… | |
… | |
598 | will be reported as reason C<< die => $@ >>. |
650 | will be reported as reason C<< die => $@ >>. |
599 | |
651 | |
600 | Transport/communication errors are reported as C<< transport_error => |
652 | Transport/communication errors are reported as C<< transport_error => |
601 | $message >>. |
653 | $message >>. |
602 | |
654 | |
|
|
655 | =cut |
|
|
656 | |
|
|
657 | =item $port = spawn $node, $initfunc[, @initdata] |
|
|
658 | |
|
|
659 | Creates a port on the node C<$node> (which can also be a port ID, in which |
|
|
660 | case it's the node where that port resides). |
|
|
661 | |
|
|
662 | The port ID of the newly created port is return immediately, and it is |
|
|
663 | permissible to immediately start sending messages or monitor the port. |
|
|
664 | |
|
|
665 | After the port has been created, the init function is |
|
|
666 | called. This function must be a fully-qualified function name |
|
|
667 | (e.g. C<MyApp::Chat::Server::init>). To specify a function in the main |
|
|
668 | program, use C<::name>. |
|
|
669 | |
|
|
670 | If the function doesn't exist, then the node tries to C<require> |
|
|
671 | the package, then the package above the package and so on (e.g. |
|
|
672 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
|
|
673 | exists or it runs out of package names. |
|
|
674 | |
|
|
675 | The init function is then called with the newly-created port as context |
|
|
676 | object (C<$SELF>) and the C<@initdata> values as arguments. |
|
|
677 | |
|
|
678 | A common idiom is to pass your own port, monitor the spawned port, and |
|
|
679 | in the init function, monitor the original port. This two-way monitoring |
|
|
680 | ensures that both ports get cleaned up when there is a problem. |
|
|
681 | |
|
|
682 | Example: spawn a chat server port on C<$othernode>. |
|
|
683 | |
|
|
684 | # this node, executed from within a port context: |
|
|
685 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
|
|
686 | mon $server; |
|
|
687 | |
|
|
688 | # init function on C<$othernode> |
|
|
689 | sub connect { |
|
|
690 | my ($srcport) = @_; |
|
|
691 | |
|
|
692 | mon $srcport; |
|
|
693 | |
|
|
694 | rcv $SELF, sub { |
|
|
695 | ... |
|
|
696 | }; |
|
|
697 | } |
|
|
698 | |
|
|
699 | =cut |
|
|
700 | |
|
|
701 | sub _spawn { |
|
|
702 | my $port = shift; |
|
|
703 | my $init = shift; |
|
|
704 | |
|
|
705 | local $SELF = "$NODE#$port"; |
|
|
706 | eval { |
|
|
707 | &{ load_func $init } |
|
|
708 | }; |
|
|
709 | _self_die if $@; |
|
|
710 | } |
|
|
711 | |
|
|
712 | sub spawn(@) { |
|
|
713 | my ($noderef, undef) = split /#/, shift, 2; |
|
|
714 | |
|
|
715 | my $id = "$RUNIQ." . $ID++; |
|
|
716 | |
|
|
717 | $_[0] =~ /::/ |
|
|
718 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
|
|
719 | |
|
|
720 | ($NODE{$noderef} || add_node $noderef) |
|
|
721 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
|
|
722 | |
|
|
723 | "$noderef#$id" |
|
|
724 | } |
|
|
725 | |
603 | =back |
726 | =back |
604 | |
727 | |
605 | =head1 NODE MESSAGES |
728 | =head1 NODE MESSAGES |
606 | |
729 | |
607 | Nodes understand the following messages sent to them. Many of them take |
730 | Nodes understand the following messages sent to them. Many of them take |
… | |
… | |
755 | This also saves round-trips and avoids sending messages to the wrong port |
878 | This also saves round-trips and avoids sending messages to the wrong port |
756 | (hard to do in Erlang). |
879 | (hard to do in Erlang). |
757 | |
880 | |
758 | =back |
881 | =back |
759 | |
882 | |
|
|
883 | =head1 RATIONALE |
|
|
884 | |
|
|
885 | =over 4 |
|
|
886 | |
|
|
887 | =item Why strings for ports and noderefs, why not objects? |
|
|
888 | |
|
|
889 | We considered "objects", but found that the actual number of methods |
|
|
890 | thatc an be called are very low. Since port IDs and noderefs travel over |
|
|
891 | the network frequently, the serialising/deserialising would add lots of |
|
|
892 | overhead, as well as having to keep a proxy object. |
|
|
893 | |
|
|
894 | Strings can easily be printed, easily serialised etc. and need no special |
|
|
895 | procedures to be "valid". |
|
|
896 | |
|
|
897 | And a a miniport consists of a single closure stored in a global hash - it |
|
|
898 | can't become much cheaper. |
|
|
899 | |
|
|
900 | =item Why favour JSON, why not real serialising format such as Storable? |
|
|
901 | |
|
|
902 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
|
|
903 | format, but currently there is no way to make a node use Storable by |
|
|
904 | default. |
|
|
905 | |
|
|
906 | The default framing protocol is JSON because a) JSON::XS is many times |
|
|
907 | faster for small messages and b) most importantly, after years of |
|
|
908 | experience we found that object serialisation is causing more problems |
|
|
909 | than it gains: Just like function calls, objects simply do not travel |
|
|
910 | easily over the network, mostly because they will always be a copy, so you |
|
|
911 | always have to re-think your design. |
|
|
912 | |
|
|
913 | Keeping your messages simple, concentrating on data structures rather than |
|
|
914 | objects, will keep your messages clean, tidy and efficient. |
|
|
915 | |
|
|
916 | =back |
|
|
917 | |
760 | =head1 SEE ALSO |
918 | =head1 SEE ALSO |
761 | |
919 | |
762 | L<AnyEvent>. |
920 | L<AnyEvent>. |
763 | |
921 | |
764 | =head1 AUTHOR |
922 | =head1 AUTHOR |