… | |
… | |
43 | |
43 | |
44 | =over 4 |
44 | =over 4 |
45 | |
45 | |
46 | =item port |
46 | =item port |
47 | |
47 | |
48 | A port is something you can send messages to with the C<snd> function, and |
48 | A port is something you can send messages to (with the C<snd> function). |
49 | you can register C<rcv> handlers with. All C<rcv> handlers will receive |
49 | |
50 | messages they match, messages will not be queued. |
50 | Some ports allow you to register C<rcv> handlers that can match specific |
|
|
51 | messages. All C<rcv> handlers will receive messages they match, messages |
|
|
52 | will not be queued. |
51 | |
53 | |
52 | =item port id - C<noderef#portname> |
54 | =item port id - C<noderef#portname> |
53 | |
55 | |
54 | A port id is always the noderef, a hash-mark (C<#>) as separator, followed |
56 | A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as |
55 | by a port name (a printable string of unspecified format). |
57 | separator, and a port name (a printable string of unspecified format). An |
|
|
58 | exception is the the node port, whose ID is identical to its node |
|
|
59 | reference. |
56 | |
60 | |
57 | =item node |
61 | =item node |
58 | |
62 | |
59 | A node is a single process containing at least one port - the node |
63 | A node is a single process containing at least one port - the node |
60 | port. You can send messages to node ports to let them create new ports, |
64 | port. You can send messages to node ports to find existing ports or to |
61 | among other things. |
65 | create new ports, among other things. |
62 | |
66 | |
63 | Initially, nodes are either private (single-process only) or hidden |
67 | Nodes are either private (single-process only), slaves (connected to a |
64 | (connected to a master node only). Only when they epxlicitly "become |
68 | master node only) or public nodes (connectable from unrelated nodes). |
65 | public" can you send them messages from unrelated other nodes. |
|
|
66 | |
69 | |
67 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
70 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
68 | |
71 | |
69 | A noderef is a string that either uniquely identifies a given node (for |
72 | A node reference is a string that either simply identifies the node (for |
70 | private and hidden nodes), or contains a recipe on how to reach a given |
73 | private and slave nodes), or contains a recipe on how to reach a given |
71 | node (for public nodes). |
74 | node (for public nodes). |
|
|
75 | |
|
|
76 | This recipe is simply a comma-separated list of C<address:port> pairs (for |
|
|
77 | TCP/IP, other protocols might look different). |
|
|
78 | |
|
|
79 | Node references come in two flavours: resolved (containing only numerical |
|
|
80 | addresses) or unresolved (where hostnames are used instead of addresses). |
|
|
81 | |
|
|
82 | Before using an unresolved node reference in a message you first have to |
|
|
83 | resolve it. |
72 | |
84 | |
73 | =back |
85 | =back |
74 | |
86 | |
75 | =head1 VARIABLES/FUNCTIONS |
87 | =head1 VARIABLES/FUNCTIONS |
76 | |
88 | |
… | |
… | |
91 | use base "Exporter"; |
103 | use base "Exporter"; |
92 | |
104 | |
93 | our $VERSION = '0.1'; |
105 | our $VERSION = '0.1'; |
94 | our @EXPORT = qw( |
106 | our @EXPORT = qw( |
95 | NODE $NODE *SELF node_of _any_ |
107 | NODE $NODE *SELF node_of _any_ |
|
|
108 | resolve_node |
96 | become_slave become_public |
109 | become_slave become_public |
97 | snd rcv mon kil reg psub |
110 | snd rcv mon kil reg psub |
98 | port |
111 | port |
99 | ); |
112 | ); |
100 | |
113 | |
… | |
… | |
115 | |
128 | |
116 | =item $noderef = node_of $portid |
129 | =item $noderef = node_of $portid |
117 | |
130 | |
118 | Extracts and returns the noderef from a portid or a noderef. |
131 | Extracts and returns the noderef from a portid or a noderef. |
119 | |
132 | |
|
|
133 | =item $cv = resolve_node $noderef |
|
|
134 | |
|
|
135 | Takes an unresolved node reference that may contain hostnames and |
|
|
136 | abbreviated IDs, resolves all of them and returns a resolved node |
|
|
137 | reference. |
|
|
138 | |
|
|
139 | In addition to C<address:port> pairs allowed in resolved noderefs, the |
|
|
140 | following forms are supported: |
|
|
141 | |
|
|
142 | =over 4 |
|
|
143 | |
|
|
144 | =item the empty string |
|
|
145 | |
|
|
146 | An empty-string component gets resolved as if the default port (4040) was |
|
|
147 | specified. |
|
|
148 | |
|
|
149 | =item naked port numbers (e.g. C<1234>) |
|
|
150 | |
|
|
151 | These are resolved by prepending the local nodename and a colon, to be |
|
|
152 | further resolved. |
|
|
153 | |
|
|
154 | =item hostnames (e.g. C<localhost:1234>, C<localhost>) |
|
|
155 | |
|
|
156 | These are resolved by using AnyEvent::DNS to resolve them, optionally |
|
|
157 | looking up SRV records for the C<aemp=4040> port, if no port was |
|
|
158 | specified. |
|
|
159 | |
|
|
160 | =back |
|
|
161 | |
120 | =item $SELF |
162 | =item $SELF |
121 | |
163 | |
122 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
164 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
123 | blocks. |
165 | blocks. |
124 | |
166 | |
… | |
… | |
198 | mon $port, $self => "restart"; |
240 | mon $port, $self => "restart"; |
199 | |
241 | |
200 | =cut |
242 | =cut |
201 | |
243 | |
202 | sub mon { |
244 | sub mon { |
203 | my ($noderef, $port, $cb) = ((split /#/, shift, 2), shift); |
245 | my ($noderef, $port) = split /#/, shift, 2; |
204 | |
246 | |
205 | my $node = $NODE{$noderef} || add_node $noderef; |
247 | my $node = $NODE{$noderef} || add_node $noderef; |
206 | |
248 | |
207 | #TODO: ports must not be references |
249 | my $cb = shift; |
208 | if (!ref $cb or "AnyEvent::MP::Port" eq ref $cb) { |
250 | |
|
|
251 | unless (ref $cb) { |
209 | if (@_) { |
252 | if (@_) { |
210 | # send a kill info message |
253 | # send a kill info message |
211 | my (@msg) = ($cb, @_); |
254 | my (@msg) = ($cb, @_); |
212 | $cb = sub { snd @msg, @_ }; |
255 | $cb = sub { snd @msg, @_ }; |
213 | } else { |
256 | } else { |
… | |
… | |
447 | |
490 | |
448 | =head1 FUNCTIONS FOR NODES |
491 | =head1 FUNCTIONS FOR NODES |
449 | |
492 | |
450 | =over 4 |
493 | =over 4 |
451 | |
494 | |
452 | =item become_public endpoint... |
495 | =item become_public $noderef |
453 | |
496 | |
454 | Tells the node to become a public node, i.e. reachable from other nodes. |
497 | Tells the node to become a public node, i.e. reachable from other nodes. |
455 | |
498 | |
456 | If no arguments are given, or the first argument is C<undef>, then |
499 | The first argument is the (unresolved) node reference of the local node |
457 | AnyEvent::MP tries to bind on port C<4040> on all IP addresses that the |
500 | (if missing then the empty string is used). |
458 | local nodename resolves to. |
|
|
459 | |
501 | |
460 | Otherwise the first argument must be an array-reference with transport |
502 | It is quite common to not specify anything, in which case the local node |
461 | endpoints ("ip:port", "hostname:port") or port numbers (in which case the |
503 | tries to listen on the default port, or to only specify a port number, in |
462 | local nodename is used as hostname). The endpoints are all resolved and |
504 | which case AnyEvent::MP tries to guess the local addresses. |
463 | will become the node reference. |
|
|
464 | |
505 | |
465 | =cut |
506 | =cut |
466 | |
507 | |
467 | =back |
508 | =back |
468 | |
509 | |
… | |
… | |
471 | Nodes understand the following messages sent to them. Many of them take |
512 | Nodes understand the following messages sent to them. Many of them take |
472 | arguments called C<@reply>, which will simply be used to compose a reply |
513 | arguments called C<@reply>, which will simply be used to compose a reply |
473 | message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and |
514 | message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and |
474 | the remaining arguments are simply the message data. |
515 | the remaining arguments are simply the message data. |
475 | |
516 | |
|
|
517 | While other messages exist, they are not public and subject to change. |
|
|
518 | |
476 | =over 4 |
519 | =over 4 |
477 | |
520 | |
478 | =cut |
521 | =cut |
479 | |
522 | |
480 | =item lookup => $name, @reply |
523 | =item lookup => $name, @reply |
… | |
… | |
582 | authentication and can use TLS. |
625 | authentication and can use TLS. |
583 | |
626 | |
584 | AEMP can use a proven protocol - SSL/TLS - to protect connections and |
627 | AEMP can use a proven protocol - SSL/TLS - to protect connections and |
585 | securely authenticate nodes. |
628 | securely authenticate nodes. |
586 | |
629 | |
|
|
630 | =item * The AEMP protocol is optimised for both text-based and binary |
|
|
631 | communications. |
|
|
632 | |
|
|
633 | The AEMP protocol, unlike the erlang protocol, supports both |
|
|
634 | language-independent text-only protocols (good for debugging) and binary, |
|
|
635 | language-specific serialisers (e.g. Storable). |
|
|
636 | |
|
|
637 | It has also been carefully designed to be implementable in other languages |
|
|
638 | with a minimum of work while gracefully degrading fucntionality to make the |
|
|
639 | protocol simple. |
|
|
640 | |
587 | =back |
641 | =back |
588 | |
642 | |
589 | =head1 SEE ALSO |
643 | =head1 SEE ALSO |
590 | |
644 | |
591 | L<AnyEvent>. |
645 | L<AnyEvent>. |