… | |
… | |
40 | |
40 | |
41 | =head1 CURRENT STATUS |
41 | =head1 CURRENT STATUS |
42 | |
42 | |
43 | AnyEvent::MP - stable API, should work |
43 | AnyEvent::MP - stable API, should work |
44 | AnyEvent::MP::Intro - outdated |
44 | AnyEvent::MP::Intro - outdated |
45 | AnyEvent::MP::Kernel - WIP |
|
|
46 | AnyEvent::MP::Transport - mostly stable |
45 | AnyEvent::MP::Kernel - mostly stable |
|
|
46 | AnyEvent::MP::Global - mostly stable |
|
|
47 | AnyEvent::MP::Node - mostly stable, but internal anyways |
|
|
48 | AnyEvent::MP::Transport - mostly stable, but internal anyways |
47 | |
49 | |
48 | stay tuned. |
50 | stay tuned. |
49 | |
51 | |
50 | =head1 DESCRIPTION |
52 | =head1 DESCRIPTION |
51 | |
53 | |
52 | This module (-family) implements a simple message passing framework. |
54 | This module (-family) implements a simple message passing framework. |
53 | |
55 | |
54 | Despite its simplicity, you can securely message other processes running |
56 | Despite its simplicity, you can securely message other processes running |
55 | on the same or other hosts. |
57 | on the same or other hosts, and you can supervise entities remotely. |
56 | |
58 | |
57 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
59 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
58 | manual page. |
60 | manual page and the examples under F<eg/>. |
59 | |
61 | |
60 | At the moment, this module family is severly broken and underdocumented, |
62 | 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 | |
63 | |
64 | =head1 CONCEPTS |
64 | =head1 CONCEPTS |
65 | |
65 | |
66 | =over 4 |
66 | =over 4 |
67 | |
67 | |
… | |
… | |
71 | |
71 | |
72 | Ports allow you to register C<rcv> handlers that can match all or just |
72 | 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 |
73 | some messages. Messages send to ports will not be queued, regardless of |
74 | anything was listening for them or not. |
74 | anything was listening for them or not. |
75 | |
75 | |
76 | =item port ID - C<noderef#portname> |
76 | =item port ID - C<nodeid#portname> |
77 | |
77 | |
78 | A port ID is the concatenation of a noderef, a hash-mark (C<#>) as |
78 | 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 |
79 | 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 | |
80 | |
83 | =item node |
81 | =item node |
84 | |
82 | |
85 | A node is a single process containing at least one port - the node port, |
83 | 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 |
84 | which enables nodes to manage each other remotely, and to create new |
87 | ports. |
85 | ports. |
88 | |
86 | |
89 | Nodes are either private (single-process only), slaves (can only talk to |
87 | 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 |
88 | (no listening ports). Private nodes cannot talk to other private nodes |
91 | from any other node). |
89 | currently. |
92 | |
90 | |
93 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
91 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
94 | |
92 | |
95 | A node ID is a string that uniquely identifies the node within a |
93 | 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 |
94 | 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 |
112 | 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 |
113 | 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 |
114 | error), they try to re-establish connections to some seednodes again to |
117 | join the network. |
115 | join the network. |
118 | |
116 | |
|
|
117 | Apart from being sued for seeding, seednodes are not special in any way - |
|
|
118 | every public node can be a seednode. |
|
|
119 | |
119 | =back |
120 | =back |
120 | |
121 | |
121 | =head1 VARIABLES/FUNCTIONS |
122 | =head1 VARIABLES/FUNCTIONS |
122 | |
123 | |
123 | =over 4 |
124 | =over 4 |
… | |
… | |
138 | |
139 | |
139 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
140 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
140 | |
141 | |
141 | our @EXPORT = qw( |
142 | our @EXPORT = qw( |
142 | NODE $NODE *SELF node_of after |
143 | NODE $NODE *SELF node_of after |
143 | resolve_node initialise_node |
144 | initialise_node |
144 | snd rcv mon mon_guard kil reg psub spawn |
145 | snd rcv mon mon_guard kil reg psub spawn |
145 | port |
146 | port |
146 | ); |
147 | ); |
147 | |
148 | |
148 | our $SELF; |
149 | our $SELF; |
… | |
… | |
153 | kil $SELF, die => $msg; |
154 | kil $SELF, die => $msg; |
154 | } |
155 | } |
155 | |
156 | |
156 | =item $thisnode = NODE / $NODE |
157 | =item $thisnode = NODE / $NODE |
157 | |
158 | |
158 | The C<NODE> function returns, and the C<$NODE> variable contains the node |
159 | 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 |
160 | ID of the node running in the current process. This value is initialised by |
160 | a call to C<initialise_node>. |
161 | a call to C<initialise_node>. |
161 | |
162 | |
162 | =item $nodeid = node_of $port |
163 | =item $nodeid = node_of $port |
163 | |
164 | |
164 | Extracts and returns the node ID part from a port ID or a node ID. |
165 | Extracts and returns the node ID from a port ID or a node ID. |
165 | |
166 | |
166 | =item initialise_node $profile_name |
167 | =item initialise_node $profile_name, key => value... |
167 | |
168 | |
168 | Before a node can talk to other nodes on the network (i.e. enter |
169 | 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 |
170 | "distributed mode") it has to initialise itself - the minimum a node needs |
170 | to know is its own name, and optionally it should know the addresses of |
171 | 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. |
172 | some other nodes in the network to discover other nodes. |
… | |
… | |
174 | never) before calling other AnyEvent::MP functions. |
175 | never) before calling other AnyEvent::MP functions. |
175 | |
176 | |
176 | The first argument is a profile name. If it is C<undef> or missing, then |
177 | The first argument is a profile name. If it is C<undef> or missing, then |
177 | the current nodename will be used instead (i.e. F<uname -n>). |
178 | the current nodename will be used instead (i.e. F<uname -n>). |
178 | |
179 | |
179 | The function then looks up the profile in the aemp configuration (see the |
180 | The function first looks up the profile in the aemp configuration (see the |
180 | L<aemp> commandline utility). |
181 | L<aemp> commandline utility). the profile is calculated as follows: |
|
|
182 | |
|
|
183 | First, all remaining key => value pairs will be used. Then they will be |
|
|
184 | overwritten by any values specified in the global default configuration |
|
|
185 | (see the F<aemp> utility), then the chain of profiles selected, if |
|
|
186 | any. That means that the values specified in the profile have highest |
|
|
187 | priority and the values specified via C<initialise_node> have lowest |
|
|
188 | priority. |
181 | |
189 | |
182 | If the profile specifies a node ID, then this will become the node ID of |
190 | 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 |
191 | 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. |
192 | special node ID of C<anon/> will be replaced by a random node ID. |
185 | |
193 | |
… | |
… | |
217 | =item $SELF |
225 | =item $SELF |
218 | |
226 | |
219 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
227 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
220 | blocks. |
228 | blocks. |
221 | |
229 | |
222 | =item SELF, %SELF, @SELF... |
230 | =item *SELF, SELF, %SELF, @SELF... |
223 | |
231 | |
224 | Due to some quirks in how perl exports variables, it is impossible to |
232 | 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 |
233 | just export C<$SELF>, all the symbols named C<SELF> are exported by this |
226 | module, but only C<$SELF> is currently used. |
234 | module, but only C<$SELF> is currently used. |
227 | |
235 | |
228 | =item snd $port, type => @data |
236 | =item snd $port, type => @data |
229 | |
237 | |
230 | =item snd $port, @msg |
238 | =item snd $port, @msg |
231 | |
239 | |
232 | Send the given message to the given port ID, which can identify either |
240 | 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. |
241 | local or a remote port, and must be a port ID. |
234 | |
242 | |
235 | While the message can be about anything, it is highly recommended to use a |
243 | 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 |
244 | use a string as first element (a port ID, or some word that indicates a |
237 | type etc.). |
245 | request type etc.) and to consist if only simple perl values (scalars, |
|
|
246 | arrays, hashes) - if you think you need to pass an object, think again. |
238 | |
247 | |
239 | The message data effectively becomes read-only after a call to this |
248 | The message data logically becomes read-only after a call to this |
240 | function: modifying any argument is not allowed and can cause many |
249 | function: modifying any argument (or values referenced by them) is |
241 | problems. |
250 | forbidden, as there can be considerable time between the call to C<snd> |
|
|
251 | and the time the message is actually being serialised - in fact, it might |
|
|
252 | never be copied as within the same process it is simply handed to the |
|
|
253 | receiving port. |
242 | |
254 | |
243 | The type of data you can transfer depends on the transport protocol: when |
255 | 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 |
256 | 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 |
257 | 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 |
258 | that Storable can serialise and deserialise is allowed, and for the local |
247 | node, anything can be passed. |
259 | node, anything can be passed. Best rely only on the common denominator of |
|
|
260 | these. |
248 | |
261 | |
249 | =item $local_port = port |
262 | =item $local_port = port |
250 | |
263 | |
251 | Create a new local port object and returns its port ID. Initially it has |
264 | 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. |
265 | no callbacks set and will throw an error when it receives messages. |
… | |
… | |
428 | $res |
441 | $res |
429 | } |
442 | } |
430 | } |
443 | } |
431 | } |
444 | } |
432 | |
445 | |
433 | =item $guard = mon $port, $cb->(@reason) |
446 | =item $guard = mon $port, $cb->(@reason) # call $cb when $port dies |
434 | |
447 | |
435 | =item $guard = mon $port, $rcvport |
448 | =item $guard = mon $port, $rcvport # kill $rcvport when $port dies |
436 | |
449 | |
437 | =item $guard = mon $port |
450 | =item $guard = mon $port # kill $SELF when $port dies |
438 | |
451 | |
439 | =item $guard = mon $port, $rcvport, @msg |
452 | =item $guard = mon $port, $rcvport, @msg # send a message when $port dies |
440 | |
453 | |
441 | Monitor the given port and do something when the port is killed or |
454 | 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 |
455 | messages to it were lost, and optionally return a guard that can be used |
443 | to stop monitoring again. |
456 | to stop monitoring again. |
444 | |
457 | |
445 | C<mon> effectively guarantees that, in the absence of hardware failures, |
458 | C<mon> effectively guarantees that, in the absence of hardware failures, |
446 | that after starting the monitor, either all messages sent to the port |
459 | after starting the monitor, either all messages sent to the port will |
447 | will arrive, or the monitoring action will be invoked after possible |
460 | arrive, or the monitoring action will be invoked after possible message |
448 | message loss has been detected. No messages will be lost "in between" |
461 | 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 |
462 | the first lost message no further messages will be received by the |
450 | port). After the monitoring action was invoked, further messages might get |
463 | port). After the monitoring action was invoked, further messages might get |
451 | delivered again. |
464 | delivered again. |
452 | |
465 | |
453 | Note that monitoring-actions are one-shot: once released, they are removed |
466 | Note that monitoring-actions are one-shot: once messages are lost (and a |
454 | and will not trigger again. |
467 | monitoring alert was raised), they are removed and will not trigger again. |
455 | |
468 | |
456 | In the first form (callback), the callback is simply called with any |
469 | 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 |
470 | 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 |
471 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
459 | C<eval> if unsure. |
472 | C<eval> if unsure. |
… | |
… | |
521 | is killed, the references will be freed. |
534 | is killed, the references will be freed. |
522 | |
535 | |
523 | Optionally returns a guard that will stop the monitoring. |
536 | Optionally returns a guard that will stop the monitoring. |
524 | |
537 | |
525 | This function is useful when you create e.g. timers or other watchers and |
538 | This function is useful when you create e.g. timers or other watchers and |
526 | want to free them when the port gets killed: |
539 | want to free them when the port gets killed (note the use of C<psub>): |
527 | |
540 | |
528 | $port->rcv (start => sub { |
541 | $port->rcv (start => sub { |
529 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { |
542 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, psub { |
530 | undef $timer if 0.9 < rand; |
543 | undef $timer if 0.9 < rand; |
531 | }); |
544 | }); |
532 | }); |
545 | }); |
533 | |
546 | |
534 | =cut |
547 | =cut |
… | |
… | |
543 | |
556 | |
544 | =item kil $port[, @reason] |
557 | =item kil $port[, @reason] |
545 | |
558 | |
546 | Kill the specified port with the given C<@reason>. |
559 | Kill the specified port with the given C<@reason>. |
547 | |
560 | |
548 | If no C<@reason> is specified, then the port is killed "normally" (linked |
561 | If no C<@reason> is specified, then the port is killed "normally" (ports |
549 | ports will not be kileld, or even notified). |
562 | monitoring other ports will not necessarily die because a port dies |
|
|
563 | "normally"). |
550 | |
564 | |
551 | Otherwise, linked ports get killed with the same reason (second form of |
565 | Otherwise, linked ports get killed with the same reason (second form of |
552 | C<mon>, see below). |
566 | C<mon>, see above). |
553 | |
567 | |
554 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
568 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
555 | will be reported as reason C<< die => $@ >>. |
569 | will be reported as reason C<< die => $@ >>. |
556 | |
570 | |
557 | Transport/communication errors are reported as C<< transport_error => |
571 | Transport/communication errors are reported as C<< transport_error => |
… | |
… | |
562 | =item $port = spawn $node, $initfunc[, @initdata] |
576 | =item $port = spawn $node, $initfunc[, @initdata] |
563 | |
577 | |
564 | Creates a port on the node C<$node> (which can also be a port ID, in which |
578 | 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). |
579 | case it's the node where that port resides). |
566 | |
580 | |
567 | The port ID of the newly created port is return immediately, and it is |
581 | 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. |
582 | possible to immediately start sending messages or to monitor the port. |
569 | |
583 | |
570 | After the port has been created, the init function is |
584 | 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 |
585 | 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 |
586 | fully-qualified function name (e.g. C<MyApp::Chat::Server::init>). To |
573 | program, use C<::name>. |
587 | specify a function in the main program, use C<::name>. |
574 | |
588 | |
575 | If the function doesn't exist, then the node tries to C<require> |
589 | 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. |
590 | 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 |
591 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
578 | exists or it runs out of package names. |
592 | exists or it runs out of package names. |
579 | |
593 | |
580 | The init function is then called with the newly-created port as context |
594 | The init function is then called with the newly-created port as context |
581 | object (C<$SELF>) and the C<@initdata> values as arguments. |
595 | object (C<$SELF>) and the C<@initdata> values as arguments. |
582 | |
596 | |
583 | A common idiom is to pass your own port, monitor the spawned port, and |
597 | 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 |
598 | port, and in the remote init function, immediately monitor the passed |
585 | ensures that both ports get cleaned up when there is a problem. |
599 | local port. This two-way monitoring ensures that both ports get cleaned up |
|
|
600 | when there is a problem. |
586 | |
601 | |
587 | Example: spawn a chat server port on C<$othernode>. |
602 | Example: spawn a chat server port on C<$othernode>. |
588 | |
603 | |
589 | # this node, executed from within a port context: |
604 | # this node, executed from within a port context: |
590 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
605 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
… | |
… | |
632 | =item after $timeout, $callback |
647 | =item after $timeout, $callback |
633 | |
648 | |
634 | Either sends the given message, or call the given callback, after the |
649 | Either sends the given message, or call the given callback, after the |
635 | specified number of seconds. |
650 | specified number of seconds. |
636 | |
651 | |
637 | This is simply a utility function that come sin handy at times. |
652 | This is simply a utility function that comes in handy at times - the |
|
|
653 | AnyEvent::MP author is not convinced of the wisdom of having it, though, |
|
|
654 | so it may go away in the future. |
638 | |
655 | |
639 | =cut |
656 | =cut |
640 | |
657 | |
641 | sub after($@) { |
658 | sub after($@) { |
642 | my ($timeout, @action) = @_; |
659 | my ($timeout, @action) = @_; |
… | |
… | |
665 | |
682 | |
666 | Despite the similarities, there are also some important differences: |
683 | Despite the similarities, there are also some important differences: |
667 | |
684 | |
668 | =over 4 |
685 | =over 4 |
669 | |
686 | |
670 | =item * Node references contain the recipe on how to contact them. |
687 | =item * Node IDs are arbitrary strings in AEMP. |
671 | |
688 | |
672 | Erlang relies on special naming and DNS to work everywhere in the |
689 | Erlang relies on special naming and DNS to work everywhere in the same |
673 | same way. AEMP relies on each node knowing it's own address(es), with |
690 | way. AEMP relies on each node somehow knowing its own address(es) (e.g. by |
674 | convenience functionality. |
691 | configuraiton or DNS), but will otherwise discover other odes itself. |
675 | |
|
|
676 | This means that AEMP requires a less tightly controlled environment at the |
|
|
677 | cost of longer node references and a slightly higher management overhead. |
|
|
678 | |
692 | |
679 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
693 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
680 | uses "local ports are like remote ports". |
694 | uses "local ports are like remote ports". |
681 | |
695 | |
682 | The failure modes for local ports are quite different (runtime errors |
696 | The failure modes for local ports are quite different (runtime errors |
… | |
… | |
711 | |
725 | |
712 | Erlang makes few guarantees on messages delivery - messages can get lost |
726 | Erlang makes few guarantees on messages delivery - messages can get lost |
713 | without any of the processes realising it (i.e. you send messages a, b, |
727 | without any of the processes realising it (i.e. you send messages a, b, |
714 | and c, and the other side only receives messages a and c). |
728 | and c, and the other side only receives messages a and c). |
715 | |
729 | |
716 | AEMP guarantees correct ordering, and the guarantee that there are no |
730 | AEMP guarantees correct ordering, and the guarantee that after one message |
717 | holes in the message sequence. |
731 | is lost, all following ones sent to the same port are lost as well, until |
718 | |
732 | monitoring raises an error, so there are no silent "holes" in the message |
719 | =item * In Erlang, processes can be declared dead and later be found to be |
733 | sequence. |
720 | alive. |
|
|
721 | |
|
|
722 | In Erlang it can happen that a monitored process is declared dead and |
|
|
723 | linked processes get killed, but later it turns out that the process is |
|
|
724 | still alive - and can receive messages. |
|
|
725 | |
|
|
726 | In AEMP, when port monitoring detects a port as dead, then that port will |
|
|
727 | eventually be killed - it cannot happen that a node detects a port as dead |
|
|
728 | and then later sends messages to it, finding it is still alive. |
|
|
729 | |
734 | |
730 | =item * Erlang can send messages to the wrong port, AEMP does not. |
735 | =item * Erlang can send messages to the wrong port, AEMP does not. |
731 | |
736 | |
732 | In Erlang it is quite likely that a node that restarts reuses a process ID |
737 | In Erlang it is quite likely that a node that restarts reuses a process ID |
733 | known to other nodes for a completely different process, causing messages |
738 | known to other nodes for a completely different process, causing messages |
… | |
… | |
737 | around in the network will not be sent to an unrelated port. |
742 | around in the network will not be sent to an unrelated port. |
738 | |
743 | |
739 | =item * Erlang uses unprotected connections, AEMP uses secure |
744 | =item * Erlang uses unprotected connections, AEMP uses secure |
740 | authentication and can use TLS. |
745 | authentication and can use TLS. |
741 | |
746 | |
742 | AEMP can use a proven protocol - SSL/TLS - to protect connections and |
747 | AEMP can use a proven protocol - TLS - to protect connections and |
743 | securely authenticate nodes. |
748 | securely authenticate nodes. |
744 | |
749 | |
745 | =item * The AEMP protocol is optimised for both text-based and binary |
750 | =item * The AEMP protocol is optimised for both text-based and binary |
746 | communications. |
751 | communications. |
747 | |
752 | |
748 | The AEMP protocol, unlike the Erlang protocol, supports both |
753 | The AEMP protocol, unlike the Erlang protocol, supports both programming |
749 | language-independent text-only protocols (good for debugging) and binary, |
754 | language independent text-only protocols (good for debugging) and binary, |
750 | language-specific serialisers (e.g. Storable). |
755 | language-specific serialisers (e.g. Storable). By default, unless TLS is |
|
|
756 | used, the protocol is actually completely text-based. |
751 | |
757 | |
752 | It has also been carefully designed to be implementable in other languages |
758 | It has also been carefully designed to be implementable in other languages |
753 | with a minimum of work while gracefully degrading fucntionality to make the |
759 | with a minimum of work while gracefully degrading functionality to make the |
754 | protocol simple. |
760 | protocol simple. |
755 | |
761 | |
756 | =item * AEMP has more flexible monitoring options than Erlang. |
762 | =item * AEMP has more flexible monitoring options than Erlang. |
757 | |
763 | |
758 | In Erlang, you can chose to receive I<all> exit signals as messages |
764 | In Erlang, you can chose to receive I<all> exit signals as messages |
… | |
… | |
761 | Erlang, as one can choose between automatic kill, exit message or callback |
767 | Erlang, as one can choose between automatic kill, exit message or callback |
762 | on a per-process basis. |
768 | on a per-process basis. |
763 | |
769 | |
764 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
770 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
765 | |
771 | |
766 | Monitoring in Erlang is not an indicator of process death/crashes, |
772 | Monitoring in Erlang is not an indicator of process death/crashes, in the |
767 | as linking is (except linking is unreliable in Erlang). |
773 | same way as linking is (except linking is unreliable in Erlang). |
768 | |
774 | |
769 | In AEMP, you don't "look up" registered port names or send to named ports |
775 | In AEMP, you don't "look up" registered port names or send to named ports |
770 | that might or might not be persistent. Instead, you normally spawn a port |
776 | that might or might not be persistent. Instead, you normally spawn a port |
771 | on the remote node. The init function monitors the you, and you monitor |
777 | on the remote node. The init function monitors you, and you monitor the |
772 | the remote port. Since both monitors are local to the node, they are much |
778 | remote port. Since both monitors are local to the node, they are much more |
773 | more reliable. |
779 | reliable (no need for C<spawn_link>). |
774 | |
780 | |
775 | This also saves round-trips and avoids sending messages to the wrong port |
781 | This also saves round-trips and avoids sending messages to the wrong port |
776 | (hard to do in Erlang). |
782 | (hard to do in Erlang). |
777 | |
783 | |
778 | =back |
784 | =back |
779 | |
785 | |
780 | =head1 RATIONALE |
786 | =head1 RATIONALE |
781 | |
787 | |
782 | =over 4 |
788 | =over 4 |
783 | |
789 | |
784 | =item Why strings for ports and noderefs, why not objects? |
790 | =item Why strings for port and node IDs, why not objects? |
785 | |
791 | |
786 | We considered "objects", but found that the actual number of methods |
792 | We considered "objects", but found that the actual number of methods |
787 | thatc an be called are very low. Since port IDs and noderefs travel over |
793 | that can be called are quite low. Since port and node IDs travel over |
788 | the network frequently, the serialising/deserialising would add lots of |
794 | the network frequently, the serialising/deserialising would add lots of |
789 | overhead, as well as having to keep a proxy object. |
795 | overhead, as well as having to keep a proxy object everywhere. |
790 | |
796 | |
791 | Strings can easily be printed, easily serialised etc. and need no special |
797 | Strings can easily be printed, easily serialised etc. and need no special |
792 | procedures to be "valid". |
798 | procedures to be "valid". |
793 | |
799 | |
794 | And a a miniport consists of a single closure stored in a global hash - it |
800 | And as a result, a miniport consists of a single closure stored in a |
795 | can't become much cheaper. |
801 | global hash - it can't become much cheaper. |
796 | |
802 | |
797 | =item Why favour JSON, why not real serialising format such as Storable? |
803 | =item Why favour JSON, why not a real serialising format such as Storable? |
798 | |
804 | |
799 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
805 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
800 | format, but currently there is no way to make a node use Storable by |
806 | format, but currently there is no way to make a node use Storable by |
801 | default. |
807 | default (although all nodes will accept it). |
802 | |
808 | |
803 | The default framing protocol is JSON because a) JSON::XS is many times |
809 | The default framing protocol is JSON because a) JSON::XS is many times |
804 | faster for small messages and b) most importantly, after years of |
810 | faster for small messages and b) most importantly, after years of |
805 | experience we found that object serialisation is causing more problems |
811 | experience we found that object serialisation is causing more problems |
806 | than it gains: Just like function calls, objects simply do not travel |
812 | than it solves: Just like function calls, objects simply do not travel |
807 | easily over the network, mostly because they will always be a copy, so you |
813 | easily over the network, mostly because they will always be a copy, so you |
808 | always have to re-think your design. |
814 | always have to re-think your design. |
809 | |
815 | |
810 | Keeping your messages simple, concentrating on data structures rather than |
816 | Keeping your messages simple, concentrating on data structures rather than |
811 | objects, will keep your messages clean, tidy and efficient. |
817 | objects, will keep your messages clean, tidy and efficient. |
812 | |
818 | |
813 | =back |
819 | =back |
814 | |
820 | |
815 | =head1 SEE ALSO |
821 | =head1 SEE ALSO |
816 | |
822 | |
|
|
823 | L<AnyEvent::MP::Intro> - a gentle introduction. |
|
|
824 | |
|
|
825 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
|
|
826 | |
|
|
827 | L<AnyEvent::MP::Global> - network maintainance and port groups, to find |
|
|
828 | your applications. |
|
|
829 | |
817 | L<AnyEvent>. |
830 | L<AnyEvent>. |
818 | |
831 | |
819 | =head1 AUTHOR |
832 | =head1 AUTHOR |
820 | |
833 | |
821 | Marc Lehmann <schmorp@schmorp.de> |
834 | Marc Lehmann <schmorp@schmorp.de> |