… | |
… | |
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 |
13 | $SELF # receiving/own port id in rcv callbacks |
14 | |
14 | |
15 | # initialise the node so it can send/receive messages |
15 | # initialise the node so it can send/receive messages |
16 | initialise_node; |
16 | configure; |
17 | |
17 | |
18 | # ports are message endpoints |
18 | # ports are message endpoints |
19 | |
19 | |
20 | # sending messages |
20 | # sending messages |
21 | snd $port, type => data...; |
21 | snd $port, type => data...; |
22 | snd $port, @msg; |
22 | snd $port, @msg; |
23 | snd @msg_with_first_element_being_a_port; |
23 | snd @msg_with_first_element_being_a_port; |
24 | |
24 | |
25 | # creating/using ports, the simple way |
25 | # creating/using ports, the simple way |
26 | my $simple_port = port { my @msg = @_; 0 }; |
26 | my $simple_port = port { my @msg = @_ }; |
27 | |
27 | |
28 | # creating/using ports, tagged message matching |
28 | # creating/using ports, tagged message matching |
29 | my $port = port; |
29 | my $port = port; |
30 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
30 | rcv $port, ping => sub { snd $_[0], "pong" }; |
31 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
31 | rcv $port, pong => sub { warn "pong received\n" }; |
32 | |
32 | |
33 | # create a port on another node |
33 | # create a port on another node |
34 | my $port = spawn $node, $initfunc, @initdata; |
34 | my $port = spawn $node, $initfunc, @initdata; |
35 | |
35 | |
36 | # monitoring |
36 | # monitoring |
… | |
… | |
38 | mon $port, $otherport # kill otherport on abnormal death |
38 | mon $port, $otherport # kill otherport on abnormal death |
39 | mon $port, $otherport, @msg # send message on death |
39 | mon $port, $otherport, @msg # send message on death |
40 | |
40 | |
41 | =head1 CURRENT STATUS |
41 | =head1 CURRENT STATUS |
42 | |
42 | |
|
|
43 | bin/aemp - stable. |
43 | AnyEvent::MP - stable API, should work |
44 | AnyEvent::MP - stable API, should work. |
44 | AnyEvent::MP::Intro - outdated |
45 | AnyEvent::MP::Intro - uptodate, but incomplete. |
45 | AnyEvent::MP::Kernel - WIP |
|
|
46 | AnyEvent::MP::Transport - mostly stable |
46 | AnyEvent::MP::Kernel - mostly stable. |
|
|
47 | AnyEvent::MP::Global - stable API, protocol not yet final. |
47 | |
48 | |
48 | stay tuned. |
49 | stay tuned. |
49 | |
50 | |
50 | =head1 DESCRIPTION |
51 | =head1 DESCRIPTION |
51 | |
52 | |
52 | This module (-family) implements a simple message passing framework. |
53 | This module (-family) implements a simple message passing framework. |
53 | |
54 | |
54 | Despite its simplicity, you can securely message other processes running |
55 | Despite its simplicity, you can securely message other processes running |
55 | on the same or other hosts. |
56 | on the same or other hosts, and you can supervise entities remotely. |
56 | |
57 | |
57 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
58 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
58 | manual page. |
59 | manual page and the examples under F<eg/>. |
59 | |
60 | |
60 | At the moment, this module family is severly broken and underdocumented, |
61 | At the moment, this module family is a bit underdocumented. |
61 | so do not use. This was uploaded mainly to reserve the CPAN namespace - |
|
|
62 | stay tuned! |
|
|
63 | |
62 | |
64 | =head1 CONCEPTS |
63 | =head1 CONCEPTS |
65 | |
64 | |
66 | =over 4 |
65 | =over 4 |
67 | |
66 | |
… | |
… | |
71 | |
70 | |
72 | Ports allow you to register C<rcv> handlers that can match all or just |
71 | Ports allow you to register C<rcv> handlers that can match all or just |
73 | some messages. Messages send to ports will not be queued, regardless of |
72 | some messages. Messages send to ports will not be queued, regardless of |
74 | anything was listening for them or not. |
73 | anything was listening for them or not. |
75 | |
74 | |
76 | =item port ID - C<noderef#portname> |
75 | =item port ID - C<nodeid#portname> |
77 | |
76 | |
78 | A port ID is the concatenation of a noderef, a hash-mark (C<#>) as |
77 | A port ID is the concatenation of a node ID, a hash-mark (C<#>) as |
79 | separator, and a port name (a printable string of unspecified format). An |
78 | separator, and a port name (a printable string of unspecified format). |
80 | exception is the the node port, whose ID is identical to its node |
|
|
81 | reference. |
|
|
82 | |
79 | |
83 | =item node |
80 | =item node |
84 | |
81 | |
85 | A node is a single process containing at least one port - the node port, |
82 | A node is a single process containing at least one port - the node port, |
86 | which provides nodes to manage each other remotely, and to create new |
83 | which enables nodes to manage each other remotely, and to create new |
87 | ports. |
84 | ports. |
88 | |
85 | |
89 | Nodes are either private (single-process only), slaves (can only talk to |
86 | Nodes are either public (have one or more listening ports) or private |
90 | public nodes, but do not need an open port) or public nodes (connectable |
87 | (no listening ports). Private nodes cannot talk to other private nodes |
91 | from any other node). |
88 | currently. |
92 | |
89 | |
93 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
90 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
94 | |
91 | |
95 | A node ID is a string that uniquely identifies the node within a |
92 | A node ID is a string that uniquely identifies the node within a |
96 | network. Depending on the configuration used, node IDs can look like a |
93 | network. Depending on the configuration used, node IDs can look like a |
… | |
… | |
114 | are expected to be long-running, and at least one of those should always |
111 | are expected to be long-running, and at least one of those should always |
115 | be available. When nodes run out of connections (e.g. due to a network |
112 | be available. When nodes run out of connections (e.g. due to a network |
116 | error), they try to re-establish connections to some seednodes again to |
113 | error), they try to re-establish connections to some seednodes again to |
117 | join the network. |
114 | join the network. |
118 | |
115 | |
|
|
116 | Apart from being sued for seeding, seednodes are not special in any way - |
|
|
117 | every public node can be a seednode. |
|
|
118 | |
119 | =back |
119 | =back |
120 | |
120 | |
121 | =head1 VARIABLES/FUNCTIONS |
121 | =head1 VARIABLES/FUNCTIONS |
122 | |
122 | |
123 | =over 4 |
123 | =over 4 |
… | |
… | |
138 | |
138 | |
139 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
139 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
140 | |
140 | |
141 | our @EXPORT = qw( |
141 | our @EXPORT = qw( |
142 | NODE $NODE *SELF node_of after |
142 | NODE $NODE *SELF node_of after |
143 | resolve_node initialise_node |
143 | configure |
144 | snd rcv mon mon_guard kil reg psub spawn |
144 | snd rcv mon mon_guard kil reg psub spawn |
145 | port |
145 | port |
146 | ); |
146 | ); |
147 | |
147 | |
148 | our $SELF; |
148 | our $SELF; |
… | |
… | |
153 | kil $SELF, die => $msg; |
153 | kil $SELF, die => $msg; |
154 | } |
154 | } |
155 | |
155 | |
156 | =item $thisnode = NODE / $NODE |
156 | =item $thisnode = NODE / $NODE |
157 | |
157 | |
158 | The C<NODE> function returns, and the C<$NODE> variable contains the node |
158 | The C<NODE> function returns, and the C<$NODE> variable contains, the node |
159 | ID of the node running in the current process. This value is initialised by |
159 | ID of the node running in the current process. This value is initialised by |
160 | a call to C<initialise_node>. |
160 | a call to C<configure>. |
161 | |
161 | |
162 | =item $nodeid = node_of $port |
162 | =item $nodeid = node_of $port |
163 | |
163 | |
164 | Extracts and returns the node ID part from a port ID or a node ID. |
164 | Extracts and returns the node ID from a port ID or a node ID. |
165 | |
165 | |
166 | =item initialise_node $profile_name |
166 | =item configure key => value... |
167 | |
167 | |
168 | Before a node can talk to other nodes on the network (i.e. enter |
168 | Before a node can talk to other nodes on the network (i.e. enter |
169 | "distributed mode") it has to initialise itself - the minimum a node needs |
169 | "distributed mode") it has to configure itself - the minimum a node needs |
170 | to know is its own name, and optionally it should know the addresses of |
170 | to know is its own name, and optionally it should know the addresses of |
171 | some other nodes in the network to discover other nodes. |
171 | some other nodes in the network to discover other nodes. |
172 | |
172 | |
173 | This function initialises a node - it must be called exactly once (or |
173 | This function configures a node - it must be called exactly once (or |
174 | never) before calling other AnyEvent::MP functions. |
174 | never) before calling other AnyEvent::MP functions. |
175 | |
175 | |
176 | The first argument is a profile name. If it is C<undef> or missing, then |
176 | =over 4 |
177 | the current nodename will be used instead (i.e. F<uname -n>). |
|
|
178 | |
177 | |
|
|
178 | =item step 1, gathering configuration from profiles |
|
|
179 | |
179 | The function then looks up the profile in the aemp configuration (see the |
180 | The function first looks up a profile in the aemp configuration (see the |
180 | L<aemp> commandline utility). |
181 | L<aemp> commandline utility). The profile name can be specified via the |
|
|
182 | named C<profile> parameter. If it is missing, then the nodename (F<uname |
|
|
183 | -n>) will be used as profile name. |
|
|
184 | |
|
|
185 | The profile data is then gathered as follows: |
|
|
186 | |
|
|
187 | First, all remaining key => value pairs (all of which are conviniently |
|
|
188 | undocumented at the moment) will be interpreted as configuration |
|
|
189 | data. Then they will be overwritten by any values specified in the global |
|
|
190 | default configuration (see the F<aemp> utility), then the chain of |
|
|
191 | profiles chosen by the profile name (and any C<parent> attributes). |
|
|
192 | |
|
|
193 | That means that the values specified in the profile have highest priority |
|
|
194 | and the values specified directly via C<configure> have lowest priority, |
|
|
195 | and can only be used to specify defaults. |
181 | |
196 | |
182 | If the profile specifies a node ID, then this will become the node ID of |
197 | If the profile specifies a node ID, then this will become the node ID of |
183 | this process. If not, then the profile name will be used as node ID. The |
198 | this process. If not, then the profile name will be used as node ID. The |
184 | special node ID of C<anon/> will be replaced by a random node ID. |
199 | special node ID of C<anon/> will be replaced by a random node ID. |
|
|
200 | |
|
|
201 | =item step 2, bind listener sockets |
185 | |
202 | |
186 | The next step is to look up the binds in the profile, followed by binding |
203 | The next step is to look up the binds in the profile, followed by binding |
187 | aemp protocol listeners on all binds specified (it is possible and valid |
204 | aemp protocol listeners on all binds specified (it is possible and valid |
188 | to have no binds, meaning that the node cannot be contacted form the |
205 | to have no binds, meaning that the node cannot be contacted form the |
189 | outside. This means the node cannot talk to other nodes that also have no |
206 | outside. This means the node cannot talk to other nodes that also have no |
190 | binds, but it can still talk to all "normal" nodes). |
207 | binds, but it can still talk to all "normal" nodes). |
191 | |
208 | |
192 | If the profile does not specify a binds list, then the node ID will be |
209 | If the profile does not specify a binds list, then a default of C<*> is |
193 | treated as if it were of the form C<host:port>, which will be resolved and |
210 | used, meaning the node will bind on a dynamically-assigned port on every |
194 | used as binds list. |
211 | local IP address it finds. |
195 | |
212 | |
|
|
213 | =item step 3, connect to seed nodes |
|
|
214 | |
196 | Lastly, the seeds list from the profile is passed to the |
215 | As the last step, the seeds list from the profile is passed to the |
197 | L<AnyEvent::MP::Global> module, which will then use it to keep |
216 | L<AnyEvent::MP::Global> module, which will then use it to keep |
198 | connectivity with at least on of those seed nodes at any point in time. |
217 | connectivity with at least one node at any point in time. |
199 | |
218 | |
200 | Example: become a distributed node listening on the guessed noderef, or |
219 | =back |
201 | the one specified via C<aemp> for the current node. This should be the |
220 | |
|
|
221 | Example: become a distributed node using the locla node name as profile. |
202 | most common form of invocation for "daemon"-type nodes. |
222 | This should be the most common form of invocation for "daemon"-type nodes. |
203 | |
223 | |
204 | initialise_node; |
224 | configure |
205 | |
225 | |
206 | Example: become an anonymous node. This form is often used for commandline |
226 | Example: become an anonymous node. This form is often used for commandline |
207 | clients. |
227 | clients. |
208 | |
228 | |
209 | initialise_node "anon/"; |
229 | configure nodeid => "anon/"; |
210 | |
230 | |
211 | Example: become a distributed node. If there is no profile of the given |
231 | Example: configure a node using a profile called seed, which si suitable |
212 | name, or no binds list was specified, resolve C<localhost:4044> and bind |
232 | for a seed node as it binds on all local addresses on a fixed port (4040, |
213 | on the resulting addresses. |
233 | customary for aemp). |
214 | |
234 | |
215 | initialise_node "localhost:4044"; |
235 | # use the aemp commandline utility |
|
|
236 | # aemp profile seed setnodeid anon/ setbinds '*:4040' |
|
|
237 | |
|
|
238 | # then use it |
|
|
239 | configure profile => "seed"; |
|
|
240 | |
|
|
241 | # or simply use aemp from the shell again: |
|
|
242 | # aemp run profile seed |
|
|
243 | |
|
|
244 | # or provide a nicer-to-remember nodeid |
|
|
245 | # aemp run profile seed nodeid "$(hostname)" |
216 | |
246 | |
217 | =item $SELF |
247 | =item $SELF |
218 | |
248 | |
219 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
249 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
220 | blocks. |
250 | blocks. |
221 | |
251 | |
222 | =item SELF, %SELF, @SELF... |
252 | =item *SELF, SELF, %SELF, @SELF... |
223 | |
253 | |
224 | Due to some quirks in how perl exports variables, it is impossible to |
254 | Due to some quirks in how perl exports variables, it is impossible to |
225 | just export C<$SELF>, all the symbols called C<SELF> are exported by this |
255 | just export C<$SELF>, all the symbols named C<SELF> are exported by this |
226 | module, but only C<$SELF> is currently used. |
256 | module, but only C<$SELF> is currently used. |
227 | |
257 | |
228 | =item snd $port, type => @data |
258 | =item snd $port, type => @data |
229 | |
259 | |
230 | =item snd $port, @msg |
260 | =item snd $port, @msg |
231 | |
261 | |
232 | Send the given message to the given port ID, which can identify either |
262 | Send the given message to the given port, which can identify either a |
233 | a local or a remote port, and must be a port ID. |
263 | local or a remote port, and must be a port ID. |
234 | |
264 | |
235 | While the message can be about anything, it is highly recommended to use a |
265 | While the message can be almost anything, it is highly recommended to |
236 | string as first element (a port ID, or some word that indicates a request |
266 | use a string as first element (a port ID, or some word that indicates a |
237 | type etc.). |
267 | request type etc.) and to consist if only simple perl values (scalars, |
|
|
268 | arrays, hashes) - if you think you need to pass an object, think again. |
238 | |
269 | |
239 | The message data effectively becomes read-only after a call to this |
270 | The message data logically becomes read-only after a call to this |
240 | function: modifying any argument is not allowed and can cause many |
271 | function: modifying any argument (or values referenced by them) is |
241 | problems. |
272 | forbidden, as there can be considerable time between the call to C<snd> |
|
|
273 | and the time the message is actually being serialised - in fact, it might |
|
|
274 | never be copied as within the same process it is simply handed to the |
|
|
275 | receiving port. |
242 | |
276 | |
243 | The type of data you can transfer depends on the transport protocol: when |
277 | The type of data you can transfer depends on the transport protocol: when |
244 | JSON is used, then only strings, numbers and arrays and hashes consisting |
278 | JSON is used, then only strings, numbers and arrays and hashes consisting |
245 | of those are allowed (no objects). When Storable is used, then anything |
279 | of those are allowed (no objects). When Storable is used, then anything |
246 | that Storable can serialise and deserialise is allowed, and for the local |
280 | that Storable can serialise and deserialise is allowed, and for the local |
247 | node, anything can be passed. |
281 | node, anything can be passed. Best rely only on the common denominator of |
|
|
282 | these. |
248 | |
283 | |
249 | =item $local_port = port |
284 | =item $local_port = port |
250 | |
285 | |
251 | Create a new local port object and returns its port ID. Initially it has |
286 | Create a new local port object and returns its port ID. Initially it has |
252 | no callbacks set and will throw an error when it receives messages. |
287 | no callbacks set and will throw an error when it receives messages. |
… | |
… | |
428 | $res |
463 | $res |
429 | } |
464 | } |
430 | } |
465 | } |
431 | } |
466 | } |
432 | |
467 | |
433 | =item $guard = mon $port, $cb->(@reason) |
468 | =item $guard = mon $port, $cb->(@reason) # call $cb when $port dies |
434 | |
469 | |
435 | =item $guard = mon $port, $rcvport |
470 | =item $guard = mon $port, $rcvport # kill $rcvport when $port dies |
436 | |
471 | |
437 | =item $guard = mon $port |
472 | =item $guard = mon $port # kill $SELF when $port dies |
438 | |
473 | |
439 | =item $guard = mon $port, $rcvport, @msg |
474 | =item $guard = mon $port, $rcvport, @msg # send a message when $port dies |
440 | |
475 | |
441 | Monitor the given port and do something when the port is killed or |
476 | Monitor the given port and do something when the port is killed or |
442 | messages to it were lost, and optionally return a guard that can be used |
477 | messages to it were lost, and optionally return a guard that can be used |
443 | to stop monitoring again. |
478 | to stop monitoring again. |
444 | |
479 | |
445 | C<mon> effectively guarantees that, in the absence of hardware failures, |
480 | C<mon> effectively guarantees that, in the absence of hardware failures, |
446 | that after starting the monitor, either all messages sent to the port |
481 | after starting the monitor, either all messages sent to the port will |
447 | will arrive, or the monitoring action will be invoked after possible |
482 | arrive, or the monitoring action will be invoked after possible message |
448 | message loss has been detected. No messages will be lost "in between" |
483 | loss has been detected. No messages will be lost "in between" (after |
449 | (after the first lost message no further messages will be received by the |
484 | the first lost message no further messages will be received by the |
450 | port). After the monitoring action was invoked, further messages might get |
485 | port). After the monitoring action was invoked, further messages might get |
451 | delivered again. |
486 | delivered again. |
452 | |
487 | |
453 | Note that monitoring-actions are one-shot: once released, they are removed |
488 | Note that monitoring-actions are one-shot: once messages are lost (and a |
454 | and will not trigger again. |
489 | monitoring alert was raised), they are removed and will not trigger again. |
455 | |
490 | |
456 | In the first form (callback), the callback is simply called with any |
491 | In the first form (callback), the callback is simply called with any |
457 | number of C<@reason> elements (no @reason means that the port was deleted |
492 | number of C<@reason> elements (no @reason means that the port was deleted |
458 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
493 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
459 | C<eval> if unsure. |
494 | C<eval> if unsure. |
… | |
… | |
521 | is killed, the references will be freed. |
556 | is killed, the references will be freed. |
522 | |
557 | |
523 | Optionally returns a guard that will stop the monitoring. |
558 | Optionally returns a guard that will stop the monitoring. |
524 | |
559 | |
525 | This function is useful when you create e.g. timers or other watchers and |
560 | This function is useful when you create e.g. timers or other watchers and |
526 | want to free them when the port gets killed: |
561 | want to free them when the port gets killed (note the use of C<psub>): |
527 | |
562 | |
528 | $port->rcv (start => sub { |
563 | $port->rcv (start => sub { |
529 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { |
564 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, psub { |
530 | undef $timer if 0.9 < rand; |
565 | undef $timer if 0.9 < rand; |
531 | }); |
566 | }); |
532 | }); |
567 | }); |
533 | |
568 | |
534 | =cut |
569 | =cut |
… | |
… | |
543 | |
578 | |
544 | =item kil $port[, @reason] |
579 | =item kil $port[, @reason] |
545 | |
580 | |
546 | Kill the specified port with the given C<@reason>. |
581 | Kill the specified port with the given C<@reason>. |
547 | |
582 | |
548 | If no C<@reason> is specified, then the port is killed "normally" (linked |
583 | If no C<@reason> is specified, then the port is killed "normally" (ports |
549 | ports will not be kileld, or even notified). |
584 | monitoring other ports will not necessarily die because a port dies |
|
|
585 | "normally"). |
550 | |
586 | |
551 | Otherwise, linked ports get killed with the same reason (second form of |
587 | Otherwise, linked ports get killed with the same reason (second form of |
552 | C<mon>, see below). |
588 | C<mon>, see above). |
553 | |
589 | |
554 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
590 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
555 | will be reported as reason C<< die => $@ >>. |
591 | will be reported as reason C<< die => $@ >>. |
556 | |
592 | |
557 | Transport/communication errors are reported as C<< transport_error => |
593 | Transport/communication errors are reported as C<< transport_error => |
… | |
… | |
562 | =item $port = spawn $node, $initfunc[, @initdata] |
598 | =item $port = spawn $node, $initfunc[, @initdata] |
563 | |
599 | |
564 | Creates a port on the node C<$node> (which can also be a port ID, in which |
600 | Creates a port on the node C<$node> (which can also be a port ID, in which |
565 | case it's the node where that port resides). |
601 | case it's the node where that port resides). |
566 | |
602 | |
567 | The port ID of the newly created port is return immediately, and it is |
603 | The port ID of the newly created port is returned immediately, and it is |
568 | permissible to immediately start sending messages or monitor the port. |
604 | possible to immediately start sending messages or to monitor the port. |
569 | |
605 | |
570 | After the port has been created, the init function is |
606 | After the port has been created, the init function is called on the remote |
571 | called. This function must be a fully-qualified function name |
607 | node, in the same context as a C<rcv> callback. This function must be a |
572 | (e.g. C<MyApp::Chat::Server::init>). To specify a function in the main |
608 | fully-qualified function name (e.g. C<MyApp::Chat::Server::init>). To |
573 | program, use C<::name>. |
609 | specify a function in the main program, use C<::name>. |
574 | |
610 | |
575 | If the function doesn't exist, then the node tries to C<require> |
611 | If the function doesn't exist, then the node tries to C<require> |
576 | the package, then the package above the package and so on (e.g. |
612 | the package, then the package above the package and so on (e.g. |
577 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
613 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
578 | exists or it runs out of package names. |
614 | exists or it runs out of package names. |
579 | |
615 | |
580 | The init function is then called with the newly-created port as context |
616 | The init function is then called with the newly-created port as context |
581 | object (C<$SELF>) and the C<@initdata> values as arguments. |
617 | object (C<$SELF>) and the C<@initdata> values as arguments. |
582 | |
618 | |
583 | A common idiom is to pass your own port, monitor the spawned port, and |
619 | A common idiom is to pass a local port, immediately monitor the spawned |
584 | in the init function, monitor the original port. This two-way monitoring |
620 | port, and in the remote init function, immediately monitor the passed |
585 | ensures that both ports get cleaned up when there is a problem. |
621 | local port. This two-way monitoring ensures that both ports get cleaned up |
|
|
622 | when there is a problem. |
586 | |
623 | |
587 | Example: spawn a chat server port on C<$othernode>. |
624 | Example: spawn a chat server port on C<$othernode>. |
588 | |
625 | |
589 | # this node, executed from within a port context: |
626 | # this node, executed from within a port context: |
590 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
627 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
… | |
… | |
632 | =item after $timeout, $callback |
669 | =item after $timeout, $callback |
633 | |
670 | |
634 | Either sends the given message, or call the given callback, after the |
671 | Either sends the given message, or call the given callback, after the |
635 | specified number of seconds. |
672 | specified number of seconds. |
636 | |
673 | |
637 | This is simply a utility function that come sin handy at times. |
674 | This is simply a utility function that comes in handy at times - the |
|
|
675 | AnyEvent::MP author is not convinced of the wisdom of having it, though, |
|
|
676 | so it may go away in the future. |
638 | |
677 | |
639 | =cut |
678 | =cut |
640 | |
679 | |
641 | sub after($@) { |
680 | sub after($@) { |
642 | my ($timeout, @action) = @_; |
681 | my ($timeout, @action) = @_; |
… | |
… | |
708 | |
747 | |
709 | Erlang makes few guarantees on messages delivery - messages can get lost |
748 | Erlang makes few guarantees on messages delivery - messages can get lost |
710 | without any of the processes realising it (i.e. you send messages a, b, |
749 | without any of the processes realising it (i.e. you send messages a, b, |
711 | and c, and the other side only receives messages a and c). |
750 | and c, and the other side only receives messages a and c). |
712 | |
751 | |
713 | AEMP guarantees correct ordering, and the guarantee that there are no |
752 | AEMP guarantees correct ordering, and the guarantee that after one message |
714 | holes in the message sequence. |
753 | is lost, all following ones sent to the same port are lost as well, until |
715 | |
754 | monitoring raises an error, so there are no silent "holes" in the message |
716 | =item * In Erlang, processes can be declared dead and later be found to be |
755 | sequence. |
717 | alive. |
|
|
718 | |
|
|
719 | In Erlang it can happen that a monitored process is declared dead and |
|
|
720 | linked processes get killed, but later it turns out that the process is |
|
|
721 | still alive - and can receive messages. |
|
|
722 | |
|
|
723 | In AEMP, when port monitoring detects a port as dead, then that port will |
|
|
724 | eventually be killed - it cannot happen that a node detects a port as dead |
|
|
725 | and then later sends messages to it, finding it is still alive. |
|
|
726 | |
756 | |
727 | =item * Erlang can send messages to the wrong port, AEMP does not. |
757 | =item * Erlang can send messages to the wrong port, AEMP does not. |
728 | |
758 | |
729 | In Erlang it is quite likely that a node that restarts reuses a process ID |
759 | In Erlang it is quite likely that a node that restarts reuses a process ID |
730 | known to other nodes for a completely different process, causing messages |
760 | known to other nodes for a completely different process, causing messages |
… | |
… | |
734 | around in the network will not be sent to an unrelated port. |
764 | around in the network will not be sent to an unrelated port. |
735 | |
765 | |
736 | =item * Erlang uses unprotected connections, AEMP uses secure |
766 | =item * Erlang uses unprotected connections, AEMP uses secure |
737 | authentication and can use TLS. |
767 | authentication and can use TLS. |
738 | |
768 | |
739 | AEMP can use a proven protocol - SSL/TLS - to protect connections and |
769 | AEMP can use a proven protocol - TLS - to protect connections and |
740 | securely authenticate nodes. |
770 | securely authenticate nodes. |
741 | |
771 | |
742 | =item * The AEMP protocol is optimised for both text-based and binary |
772 | =item * The AEMP protocol is optimised for both text-based and binary |
743 | communications. |
773 | communications. |
744 | |
774 | |
745 | The AEMP protocol, unlike the Erlang protocol, supports both |
775 | The AEMP protocol, unlike the Erlang protocol, supports both programming |
746 | language-independent text-only protocols (good for debugging) and binary, |
776 | language independent text-only protocols (good for debugging) and binary, |
747 | language-specific serialisers (e.g. Storable). |
777 | language-specific serialisers (e.g. Storable). By default, unless TLS is |
|
|
778 | used, the protocol is actually completely text-based. |
748 | |
779 | |
749 | It has also been carefully designed to be implementable in other languages |
780 | It has also been carefully designed to be implementable in other languages |
750 | with a minimum of work while gracefully degrading fucntionality to make the |
781 | with a minimum of work while gracefully degrading functionality to make the |
751 | protocol simple. |
782 | protocol simple. |
752 | |
783 | |
753 | =item * AEMP has more flexible monitoring options than Erlang. |
784 | =item * AEMP has more flexible monitoring options than Erlang. |
754 | |
785 | |
755 | In Erlang, you can chose to receive I<all> exit signals as messages |
786 | In Erlang, you can chose to receive I<all> exit signals as messages |
… | |
… | |
758 | Erlang, as one can choose between automatic kill, exit message or callback |
789 | Erlang, as one can choose between automatic kill, exit message or callback |
759 | on a per-process basis. |
790 | on a per-process basis. |
760 | |
791 | |
761 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
792 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
762 | |
793 | |
763 | Monitoring in Erlang is not an indicator of process death/crashes, |
794 | Monitoring in Erlang is not an indicator of process death/crashes, in the |
764 | as linking is (except linking is unreliable in Erlang). |
795 | same way as linking is (except linking is unreliable in Erlang). |
765 | |
796 | |
766 | In AEMP, you don't "look up" registered port names or send to named ports |
797 | In AEMP, you don't "look up" registered port names or send to named ports |
767 | that might or might not be persistent. Instead, you normally spawn a port |
798 | that might or might not be persistent. Instead, you normally spawn a port |
768 | on the remote node. The init function monitors the you, and you monitor |
799 | on the remote node. The init function monitors you, and you monitor the |
769 | the remote port. Since both monitors are local to the node, they are much |
800 | remote port. Since both monitors are local to the node, they are much more |
770 | more reliable. |
801 | reliable (no need for C<spawn_link>). |
771 | |
802 | |
772 | This also saves round-trips and avoids sending messages to the wrong port |
803 | This also saves round-trips and avoids sending messages to the wrong port |
773 | (hard to do in Erlang). |
804 | (hard to do in Erlang). |
774 | |
805 | |
775 | =back |
806 | =back |
776 | |
807 | |
777 | =head1 RATIONALE |
808 | =head1 RATIONALE |
778 | |
809 | |
779 | =over 4 |
810 | =over 4 |
780 | |
811 | |
781 | =item Why strings for ports and noderefs, why not objects? |
812 | =item Why strings for port and node IDs, why not objects? |
782 | |
813 | |
783 | We considered "objects", but found that the actual number of methods |
814 | We considered "objects", but found that the actual number of methods |
784 | thatc an be called are very low. Since port IDs and noderefs travel over |
815 | that can be called are quite low. Since port and node IDs travel over |
785 | the network frequently, the serialising/deserialising would add lots of |
816 | the network frequently, the serialising/deserialising would add lots of |
786 | overhead, as well as having to keep a proxy object. |
817 | overhead, as well as having to keep a proxy object everywhere. |
787 | |
818 | |
788 | Strings can easily be printed, easily serialised etc. and need no special |
819 | Strings can easily be printed, easily serialised etc. and need no special |
789 | procedures to be "valid". |
820 | procedures to be "valid". |
790 | |
821 | |
791 | And a a miniport consists of a single closure stored in a global hash - it |
822 | And as a result, a miniport consists of a single closure stored in a |
792 | can't become much cheaper. |
823 | global hash - it can't become much cheaper. |
793 | |
824 | |
794 | =item Why favour JSON, why not real serialising format such as Storable? |
825 | =item Why favour JSON, why not a real serialising format such as Storable? |
795 | |
826 | |
796 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
827 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
797 | format, but currently there is no way to make a node use Storable by |
828 | format, but currently there is no way to make a node use Storable by |
798 | default. |
829 | default (although all nodes will accept it). |
799 | |
830 | |
800 | The default framing protocol is JSON because a) JSON::XS is many times |
831 | The default framing protocol is JSON because a) JSON::XS is many times |
801 | faster for small messages and b) most importantly, after years of |
832 | faster for small messages and b) most importantly, after years of |
802 | experience we found that object serialisation is causing more problems |
833 | experience we found that object serialisation is causing more problems |
803 | than it gains: Just like function calls, objects simply do not travel |
834 | than it solves: Just like function calls, objects simply do not travel |
804 | easily over the network, mostly because they will always be a copy, so you |
835 | easily over the network, mostly because they will always be a copy, so you |
805 | always have to re-think your design. |
836 | always have to re-think your design. |
806 | |
837 | |
807 | Keeping your messages simple, concentrating on data structures rather than |
838 | Keeping your messages simple, concentrating on data structures rather than |
808 | objects, will keep your messages clean, tidy and efficient. |
839 | objects, will keep your messages clean, tidy and efficient. |
809 | |
840 | |
810 | =back |
841 | =back |
811 | |
842 | |
812 | =head1 SEE ALSO |
843 | =head1 SEE ALSO |
813 | |
844 | |
|
|
845 | L<AnyEvent::MP::Intro> - a gentle introduction. |
|
|
846 | |
|
|
847 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
|
|
848 | |
|
|
849 | L<AnyEvent::MP::Global> - network maintainance and port groups, to find |
|
|
850 | your applications. |
|
|
851 | |
814 | L<AnyEvent>. |
852 | L<AnyEvent>. |
815 | |
853 | |
816 | =head1 AUTHOR |
854 | =head1 AUTHOR |
817 | |
855 | |
818 | Marc Lehmann <schmorp@schmorp.de> |
856 | Marc Lehmann <schmorp@schmorp.de> |