… | |
… | |
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 |
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 |
… | |
… | |
217 | =item $SELF |
218 | =item $SELF |
218 | |
219 | |
219 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
220 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
220 | blocks. |
221 | blocks. |
221 | |
222 | |
222 | =item SELF, %SELF, @SELF... |
223 | =item *SELF, SELF, %SELF, @SELF... |
223 | |
224 | |
224 | Due to some quirks in how perl exports variables, it is impossible to |
225 | 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 |
226 | just export C<$SELF>, all the symbols named C<SELF> are exported by this |
226 | module, but only C<$SELF> is currently used. |
227 | module, but only C<$SELF> is currently used. |
227 | |
228 | |
228 | =item snd $port, type => @data |
229 | =item snd $port, type => @data |
229 | |
230 | |
230 | =item snd $port, @msg |
231 | =item snd $port, @msg |
231 | |
232 | |
232 | Send the given message to the given port ID, which can identify either |
233 | 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. |
234 | local or a remote port, and must be a port ID. |
234 | |
235 | |
235 | While the message can be about anything, it is highly recommended to use a |
236 | 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 |
237 | use a string as first element (a port ID, or some word that indicates a |
237 | type etc.). |
238 | request type etc.) and to consist if only simple perl values (scalars, |
|
|
239 | arrays, hashes) - if you think you need to pass an object, think again. |
238 | |
240 | |
239 | The message data effectively becomes read-only after a call to this |
241 | The message data logically becomes read-only after a call to this |
240 | function: modifying any argument is not allowed and can cause many |
242 | function: modifying any argument (or values referenced by them) is |
241 | problems. |
243 | forbidden, as there can be considerable time between the call to C<snd> |
|
|
244 | and the time the message is actually being serialised - in fact, it might |
|
|
245 | never be copied as within the same process it is simply handed to the |
|
|
246 | receiving port. |
242 | |
247 | |
243 | The type of data you can transfer depends on the transport protocol: when |
248 | 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 |
249 | 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 |
250 | 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 |
251 | that Storable can serialise and deserialise is allowed, and for the local |
247 | node, anything can be passed. |
252 | node, anything can be passed. Best rely only on the common denominator of |
|
|
253 | these. |
248 | |
254 | |
249 | =item $local_port = port |
255 | =item $local_port = port |
250 | |
256 | |
251 | Create a new local port object and returns its port ID. Initially it has |
257 | 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. |
258 | no callbacks set and will throw an error when it receives messages. |
… | |
… | |
428 | $res |
434 | $res |
429 | } |
435 | } |
430 | } |
436 | } |
431 | } |
437 | } |
432 | |
438 | |
433 | =item $guard = mon $port, $cb->(@reason) |
439 | =item $guard = mon $port, $cb->(@reason) # call $cb when $port dies |
434 | |
440 | |
435 | =item $guard = mon $port, $rcvport |
441 | =item $guard = mon $port, $rcvport # kill $rcvport when $port dies |
436 | |
442 | |
437 | =item $guard = mon $port |
443 | =item $guard = mon $port # kill $SELF when $port dies |
438 | |
444 | |
439 | =item $guard = mon $port, $rcvport, @msg |
445 | =item $guard = mon $port, $rcvport, @msg # send a message when $port dies |
440 | |
446 | |
441 | Monitor the given port and do something when the port is killed or |
447 | 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 |
448 | messages to it were lost, and optionally return a guard that can be used |
443 | to stop monitoring again. |
449 | to stop monitoring again. |
444 | |
450 | |
445 | C<mon> effectively guarantees that, in the absence of hardware failures, |
451 | C<mon> effectively guarantees that, in the absence of hardware failures, |
446 | that after starting the monitor, either all messages sent to the port |
452 | after starting the monitor, either all messages sent to the port will |
447 | will arrive, or the monitoring action will be invoked after possible |
453 | arrive, or the monitoring action will be invoked after possible message |
448 | message loss has been detected. No messages will be lost "in between" |
454 | 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 |
455 | the first lost message no further messages will be received by the |
450 | port). After the monitoring action was invoked, further messages might get |
456 | port). After the monitoring action was invoked, further messages might get |
451 | delivered again. |
457 | delivered again. |
452 | |
458 | |
453 | Note that monitoring-actions are one-shot: once released, they are removed |
459 | Note that monitoring-actions are one-shot: once messages are lost (and a |
454 | and will not trigger again. |
460 | monitoring alert was raised), they are removed and will not trigger again. |
455 | |
461 | |
456 | In the first form (callback), the callback is simply called with any |
462 | 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 |
463 | 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 |
464 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
459 | C<eval> if unsure. |
465 | C<eval> if unsure. |
… | |
… | |
521 | is killed, the references will be freed. |
527 | is killed, the references will be freed. |
522 | |
528 | |
523 | Optionally returns a guard that will stop the monitoring. |
529 | Optionally returns a guard that will stop the monitoring. |
524 | |
530 | |
525 | This function is useful when you create e.g. timers or other watchers and |
531 | This function is useful when you create e.g. timers or other watchers and |
526 | want to free them when the port gets killed: |
532 | want to free them when the port gets killed (note the use of C<psub>): |
527 | |
533 | |
528 | $port->rcv (start => sub { |
534 | $port->rcv (start => sub { |
529 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { |
535 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, psub { |
530 | undef $timer if 0.9 < rand; |
536 | undef $timer if 0.9 < rand; |
531 | }); |
537 | }); |
532 | }); |
538 | }); |
533 | |
539 | |
534 | =cut |
540 | =cut |
… | |
… | |
543 | |
549 | |
544 | =item kil $port[, @reason] |
550 | =item kil $port[, @reason] |
545 | |
551 | |
546 | Kill the specified port with the given C<@reason>. |
552 | Kill the specified port with the given C<@reason>. |
547 | |
553 | |
548 | If no C<@reason> is specified, then the port is killed "normally" (linked |
554 | If no C<@reason> is specified, then the port is killed "normally" (ports |
549 | ports will not be kileld, or even notified). |
555 | monitoring other ports will not necessarily die because a port dies |
|
|
556 | "normally"). |
550 | |
557 | |
551 | Otherwise, linked ports get killed with the same reason (second form of |
558 | Otherwise, linked ports get killed with the same reason (second form of |
552 | C<mon>, see below). |
559 | C<mon>, see above). |
553 | |
560 | |
554 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
561 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
555 | will be reported as reason C<< die => $@ >>. |
562 | will be reported as reason C<< die => $@ >>. |
556 | |
563 | |
557 | Transport/communication errors are reported as C<< transport_error => |
564 | Transport/communication errors are reported as C<< transport_error => |
… | |
… | |
562 | =item $port = spawn $node, $initfunc[, @initdata] |
569 | =item $port = spawn $node, $initfunc[, @initdata] |
563 | |
570 | |
564 | Creates a port on the node C<$node> (which can also be a port ID, in which |
571 | 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). |
572 | case it's the node where that port resides). |
566 | |
573 | |
567 | The port ID of the newly created port is return immediately, and it is |
574 | 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. |
575 | possible to immediately start sending messages or to monitor the port. |
569 | |
576 | |
570 | After the port has been created, the init function is |
577 | 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 |
578 | 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 |
579 | fully-qualified function name (e.g. C<MyApp::Chat::Server::init>). To |
573 | program, use C<::name>. |
580 | specify a function in the main program, use C<::name>. |
574 | |
581 | |
575 | If the function doesn't exist, then the node tries to C<require> |
582 | 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. |
583 | 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 |
584 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
578 | exists or it runs out of package names. |
585 | exists or it runs out of package names. |
579 | |
586 | |
580 | The init function is then called with the newly-created port as context |
587 | The init function is then called with the newly-created port as context |
581 | object (C<$SELF>) and the C<@initdata> values as arguments. |
588 | object (C<$SELF>) and the C<@initdata> values as arguments. |
582 | |
589 | |
583 | A common idiom is to pass your own port, monitor the spawned port, and |
590 | 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 |
591 | port, and in the remote init function, immediately monitor the passed |
585 | ensures that both ports get cleaned up when there is a problem. |
592 | local port. This two-way monitoring ensures that both ports get cleaned up |
|
|
593 | when there is a problem. |
586 | |
594 | |
587 | Example: spawn a chat server port on C<$othernode>. |
595 | Example: spawn a chat server port on C<$othernode>. |
588 | |
596 | |
589 | # this node, executed from within a port context: |
597 | # this node, executed from within a port context: |
590 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
598 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
… | |
… | |
632 | =item after $timeout, $callback |
640 | =item after $timeout, $callback |
633 | |
641 | |
634 | Either sends the given message, or call the given callback, after the |
642 | Either sends the given message, or call the given callback, after the |
635 | specified number of seconds. |
643 | specified number of seconds. |
636 | |
644 | |
637 | This is simply a utility function that come sin handy at times. |
645 | This is simply a utility function that comes in handy at times - the |
|
|
646 | AnyEvent::MP author is not convinced of the wisdom of having it, though, |
|
|
647 | so it may go away in the future. |
638 | |
648 | |
639 | =cut |
649 | =cut |
640 | |
650 | |
641 | sub after($@) { |
651 | sub after($@) { |
642 | my ($timeout, @action) = @_; |
652 | my ($timeout, @action) = @_; |
… | |
… | |
733 | =item * The AEMP protocol is optimised for both text-based and binary |
743 | =item * The AEMP protocol is optimised for both text-based and binary |
734 | communications. |
744 | communications. |
735 | |
745 | |
736 | The AEMP protocol, unlike the Erlang protocol, supports both programming |
746 | The AEMP protocol, unlike the Erlang protocol, supports both programming |
737 | language independent text-only protocols (good for debugging) and binary, |
747 | language independent text-only protocols (good for debugging) and binary, |
738 | language-specific serialisers (e.g. Storable). |
748 | language-specific serialisers (e.g. Storable). By default, unless TLS is |
|
|
749 | used, the protocol is actually completely text-based. |
739 | |
750 | |
740 | It has also been carefully designed to be implementable in other languages |
751 | It has also been carefully designed to be implementable in other languages |
741 | with a minimum of work while gracefully degrading functionality to make the |
752 | with a minimum of work while gracefully degrading functionality to make the |
742 | protocol simple. |
753 | protocol simple. |
743 | |
754 | |
… | |
… | |
749 | Erlang, as one can choose between automatic kill, exit message or callback |
760 | Erlang, as one can choose between automatic kill, exit message or callback |
750 | on a per-process basis. |
761 | on a per-process basis. |
751 | |
762 | |
752 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
763 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
753 | |
764 | |
754 | Monitoring in Erlang is not an indicator of process death/crashes, |
765 | Monitoring in Erlang is not an indicator of process death/crashes, in the |
755 | as linking is (except linking is unreliable in Erlang). |
766 | same way as linking is (except linking is unreliable in Erlang). |
756 | |
767 | |
757 | In AEMP, you don't "look up" registered port names or send to named ports |
768 | In AEMP, you don't "look up" registered port names or send to named ports |
758 | that might or might not be persistent. Instead, you normally spawn a port |
769 | that might or might not be persistent. Instead, you normally spawn a port |
759 | on the remote node. The init function monitors the you, and you monitor |
770 | on the remote node. The init function monitors you, and you monitor the |
760 | the remote port. Since both monitors are local to the node, they are much |
771 | remote port. Since both monitors are local to the node, they are much more |
761 | more reliable. |
772 | reliable (no need for C<spawn_link>). |
762 | |
773 | |
763 | This also saves round-trips and avoids sending messages to the wrong port |
774 | This also saves round-trips and avoids sending messages to the wrong port |
764 | (hard to do in Erlang). |
775 | (hard to do in Erlang). |
765 | |
776 | |
766 | =back |
777 | =back |
767 | |
778 | |
768 | =head1 RATIONALE |
779 | =head1 RATIONALE |
769 | |
780 | |
770 | =over 4 |
781 | =over 4 |
771 | |
782 | |
772 | =item Why strings for ports and noderefs, why not objects? |
783 | =item Why strings for port and node IDs, why not objects? |
773 | |
784 | |
774 | We considered "objects", but found that the actual number of methods |
785 | We considered "objects", but found that the actual number of methods |
775 | thatc an be called are very low. Since port IDs and noderefs travel over |
786 | that can be called are quite low. Since port and node IDs travel over |
776 | the network frequently, the serialising/deserialising would add lots of |
787 | the network frequently, the serialising/deserialising would add lots of |
777 | overhead, as well as having to keep a proxy object. |
788 | overhead, as well as having to keep a proxy object everywhere. |
778 | |
789 | |
779 | Strings can easily be printed, easily serialised etc. and need no special |
790 | Strings can easily be printed, easily serialised etc. and need no special |
780 | procedures to be "valid". |
791 | procedures to be "valid". |
781 | |
792 | |
782 | And a a miniport consists of a single closure stored in a global hash - it |
793 | And as a result, a miniport consists of a single closure stored in a |
783 | can't become much cheaper. |
794 | global hash - it can't become much cheaper. |
784 | |
795 | |
785 | =item Why favour JSON, why not real serialising format such as Storable? |
796 | =item Why favour JSON, why not a real serialising format such as Storable? |
786 | |
797 | |
787 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
798 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
788 | format, but currently there is no way to make a node use Storable by |
799 | format, but currently there is no way to make a node use Storable by |
789 | default. |
800 | default (although all nodes will accept it). |
790 | |
801 | |
791 | The default framing protocol is JSON because a) JSON::XS is many times |
802 | The default framing protocol is JSON because a) JSON::XS is many times |
792 | faster for small messages and b) most importantly, after years of |
803 | faster for small messages and b) most importantly, after years of |
793 | experience we found that object serialisation is causing more problems |
804 | experience we found that object serialisation is causing more problems |
794 | than it gains: Just like function calls, objects simply do not travel |
805 | than it solves: Just like function calls, objects simply do not travel |
795 | easily over the network, mostly because they will always be a copy, so you |
806 | easily over the network, mostly because they will always be a copy, so you |
796 | always have to re-think your design. |
807 | always have to re-think your design. |
797 | |
808 | |
798 | Keeping your messages simple, concentrating on data structures rather than |
809 | Keeping your messages simple, concentrating on data structures rather than |
799 | objects, will keep your messages clean, tidy and efficient. |
810 | objects, will keep your messages clean, tidy and efficient. |