… | |
… | |
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_ |
96 | become_slave become_public |
108 | resolve_node initialise_node |
97 | snd rcv mon kil reg psub |
109 | snd rcv mon kil reg psub |
98 | port |
110 | port |
99 | ); |
111 | ); |
100 | |
112 | |
101 | our $SELF; |
113 | our $SELF; |
… | |
… | |
115 | |
127 | |
116 | =item $noderef = node_of $portid |
128 | =item $noderef = node_of $portid |
117 | |
129 | |
118 | Extracts and returns the noderef from a portid or a noderef. |
130 | Extracts and returns the noderef from a portid or a noderef. |
119 | |
131 | |
|
|
132 | =item $cv = resolve_node $noderef |
|
|
133 | |
|
|
134 | Takes an unresolved node reference that may contain hostnames and |
|
|
135 | abbreviated IDs, resolves all of them and returns a resolved node |
|
|
136 | reference. |
|
|
137 | |
|
|
138 | In addition to C<address:port> pairs allowed in resolved noderefs, the |
|
|
139 | following forms are supported: |
|
|
140 | |
|
|
141 | =over 4 |
|
|
142 | |
|
|
143 | =item the empty string |
|
|
144 | |
|
|
145 | An empty-string component gets resolved as if the default port (4040) was |
|
|
146 | specified. |
|
|
147 | |
|
|
148 | =item naked port numbers (e.g. C<1234>) |
|
|
149 | |
|
|
150 | These are resolved by prepending the local nodename and a colon, to be |
|
|
151 | further resolved. |
|
|
152 | |
|
|
153 | =item hostnames (e.g. C<localhost:1234>, C<localhost>) |
|
|
154 | |
|
|
155 | These are resolved by using AnyEvent::DNS to resolve them, optionally |
|
|
156 | looking up SRV records for the C<aemp=4040> port, if no port was |
|
|
157 | specified. |
|
|
158 | |
|
|
159 | =back |
|
|
160 | |
120 | =item $SELF |
161 | =item $SELF |
121 | |
162 | |
122 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
163 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
123 | blocks. |
164 | blocks. |
124 | |
165 | |
… | |
… | |
198 | mon $port, $self => "restart"; |
239 | mon $port, $self => "restart"; |
199 | |
240 | |
200 | =cut |
241 | =cut |
201 | |
242 | |
202 | sub mon { |
243 | sub mon { |
203 | my ($noderef, $port, $cb) = ((split /#/, shift, 2), shift); |
244 | my ($noderef, $port) = split /#/, shift, 2; |
204 | |
245 | |
205 | my $node = $NODE{$noderef} || add_node $noderef; |
246 | my $node = $NODE{$noderef} || add_node $noderef; |
206 | |
247 | |
207 | #TODO: ports must not be references |
248 | my $cb = shift; |
208 | if (!ref $cb or "AnyEvent::MP::Port" eq ref $cb) { |
249 | |
|
|
250 | unless (ref $cb) { |
209 | if (@_) { |
251 | if (@_) { |
210 | # send a kill info message |
252 | # send a kill info message |
211 | my (@msg) = ($cb, @_); |
253 | my (@msg) = ($cb, @_); |
212 | $cb = sub { snd @msg, @_ }; |
254 | $cb = sub { snd @msg, @_ }; |
213 | } else { |
255 | } else { |
… | |
… | |
257 | It means that if either one is killed abnormally, the other one gets |
299 | It means that if either one is killed abnormally, the other one gets |
258 | killed as well. |
300 | killed as well. |
259 | |
301 | |
260 | =item $local_port = port |
302 | =item $local_port = port |
261 | |
303 | |
262 | Create a new local port object that supports message matching. |
304 | Create a new local port object that can be used either as a pattern |
|
|
305 | matching port ("full port") or a single-callback port ("miniport"), |
|
|
306 | depending on how C<rcv> callbacks are bound to the object. |
263 | |
307 | |
264 | =item $portid = port { my @msg = @_; $finished } |
308 | =item $portid = port { my @msg = @_; $finished } |
265 | |
309 | |
266 | Creates a "mini port", that is, a very lightweight port without any |
310 | Creates a "mini port", that is, a very lightweight port without any |
267 | pattern matching behind it, and returns its ID. |
311 | pattern matching behind it, and returns its ID. |
… | |
… | |
273 | The message will be passed as-is, no extra argument (i.e. no port id) will |
317 | The message will be passed as-is, no extra argument (i.e. no port id) will |
274 | be passed to the callback. |
318 | be passed to the callback. |
275 | |
319 | |
276 | If you need the local port id in the callback, this works nicely: |
320 | If you need the local port id in the callback, this works nicely: |
277 | |
321 | |
278 | my $port; $port = miniport { |
322 | my $port; $port = port { |
279 | snd $otherport, reply => $port; |
323 | snd $otherport, reply => $port; |
280 | }; |
324 | }; |
281 | |
325 | |
282 | =cut |
326 | =cut |
283 | |
327 | |
… | |
… | |
344 | my ($portid, $name) = @_; |
388 | my ($portid, $name) = @_; |
345 | |
389 | |
346 | $REG{$name} = $portid; |
390 | $REG{$name} = $portid; |
347 | } |
391 | } |
348 | |
392 | |
|
|
393 | =item rcv $portid, $callback->(@msg) |
|
|
394 | |
|
|
395 | Replaces the callback on the specified miniport (or newly created port |
|
|
396 | object, see C<port>). Full ports are configured with the following calls: |
|
|
397 | |
349 | =item rcv $portid, tagstring => $callback->(@msg), ... |
398 | =item rcv $portid, tagstring => $callback->(@msg), ... |
350 | |
399 | |
351 | =item rcv $portid, $smartmatch => $callback->(@msg), ... |
400 | =item rcv $portid, $smartmatch => $callback->(@msg), ... |
352 | |
401 | |
353 | =item rcv $portid, [$smartmatch...] => $callback->(@msg), ... |
402 | =item rcv $portid, [$smartmatch...] => $callback->(@msg), ... |
… | |
… | |
376 | also the most efficient match (by far). |
425 | also the most efficient match (by far). |
377 | |
426 | |
378 | =cut |
427 | =cut |
379 | |
428 | |
380 | sub rcv($@) { |
429 | sub rcv($@) { |
|
|
430 | my $portid = shift; |
381 | my ($noderef, $port) = split /#/, shift, 2; |
431 | my ($noderef, $port) = split /#/, $port, 2; |
382 | |
432 | |
383 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
433 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
384 | or Carp::croak "$noderef#$port: rcv can only be called on local ports, caught"; |
434 | or Carp::croak "$noderef#$port: rcv can only be called on local ports, caught"; |
385 | |
435 | |
386 | my $self = $PORT_DATA{$port} |
436 | my $self = $PORT_DATA{$port} |
… | |
… | |
401 | : push @{ $self->{rc0}{$match->[0]} }, [$cb]; |
451 | : push @{ $self->{rc0}{$match->[0]} }, [$cb]; |
402 | } else { |
452 | } else { |
403 | push @{ $self->{any} }, [$cb, $match]; |
453 | push @{ $self->{any} }, [$cb, $match]; |
404 | } |
454 | } |
405 | } |
455 | } |
|
|
456 | |
|
|
457 | $portid |
406 | } |
458 | } |
407 | |
459 | |
408 | =item $closure = psub { BLOCK } |
460 | =item $closure = psub { BLOCK } |
409 | |
461 | |
410 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |
462 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |
… | |
… | |
447 | |
499 | |
448 | =head1 FUNCTIONS FOR NODES |
500 | =head1 FUNCTIONS FOR NODES |
449 | |
501 | |
450 | =over 4 |
502 | =over 4 |
451 | |
503 | |
452 | =item become_public endpoint... |
504 | =item become_public $noderef |
453 | |
505 | |
454 | Tells the node to become a public node, i.e. reachable from other nodes. |
506 | Tells the node to become a public node, i.e. reachable from other nodes. |
455 | |
507 | |
456 | If no arguments are given, or the first argument is C<undef>, then |
508 | 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 |
509 | (if missing then the empty string is used). |
458 | local nodename resolves to. |
|
|
459 | |
510 | |
460 | Otherwise the first argument must be an array-reference with transport |
511 | 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 |
512 | 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 |
513 | which case AnyEvent::MP tries to guess the local addresses. |
463 | will become the node reference. |
|
|
464 | |
514 | |
465 | =cut |
515 | =cut |
466 | |
516 | |
467 | =back |
517 | =back |
468 | |
518 | |
… | |
… | |
471 | Nodes understand the following messages sent to them. Many of them take |
521 | 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 |
522 | 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 |
523 | 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. |
524 | the remaining arguments are simply the message data. |
475 | |
525 | |
|
|
526 | While other messages exist, they are not public and subject to change. |
|
|
527 | |
476 | =over 4 |
528 | =over 4 |
477 | |
529 | |
478 | =cut |
530 | =cut |
479 | |
531 | |
480 | =item lookup => $name, @reply |
532 | =item lookup => $name, @reply |
… | |
… | |
508 | snd $NODE, time => $myport, timereply => 1, 2; |
560 | snd $NODE, time => $myport, timereply => 1, 2; |
509 | # => snd $myport, timereply => 1, 2, <time> |
561 | # => snd $myport, timereply => 1, 2, <time> |
510 | |
562 | |
511 | =back |
563 | =back |
512 | |
564 | |
|
|
565 | =head1 AnyEvent::MP vs. Distributed Erlang |
|
|
566 | |
|
|
567 | AnyEvent::MP got lots of its ideas from distributed erlang (erlang node |
|
|
568 | == aemp node, erlang process == aemp port), so many of the documents and |
|
|
569 | programming techniques employed by erlang apply to AnyEvent::MP. Here is a |
|
|
570 | sample: |
|
|
571 | |
|
|
572 | http://www.erlang.se/doc/programming_rules.shtml |
|
|
573 | http://erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4 |
|
|
574 | http://erlang.org/download/erlang-book-part1.pdf # chapters 5 and 6 |
|
|
575 | http://erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5 |
|
|
576 | |
|
|
577 | Despite the similarities, there are also some important differences: |
|
|
578 | |
|
|
579 | =over 4 |
|
|
580 | |
|
|
581 | =item * Node references contain the recipe on how to contact them. |
|
|
582 | |
|
|
583 | Erlang relies on special naming and DNS to work everywhere in the |
|
|
584 | same way. AEMP relies on each node knowing it's own address(es), with |
|
|
585 | convenience functionality. |
|
|
586 | |
|
|
587 | This means that AEMP requires a less tightly controlled environment at the |
|
|
588 | cost of longer node references and a slightly higher management overhead. |
|
|
589 | |
|
|
590 | =item * Erlang uses processes and a mailbox, AEMP does not queue. |
|
|
591 | |
|
|
592 | Erlang uses processes that selctively receive messages, and therefore |
|
|
593 | needs a queue. AEMP is event based, queuing messages would serve no useful |
|
|
594 | purpose. |
|
|
595 | |
|
|
596 | (But see L<Coro::MP> for a more erlang-like process model on top of AEMP). |
|
|
597 | |
|
|
598 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
|
|
599 | |
|
|
600 | Sending messages in erlang is synchronous and blocks the process. AEMP |
|
|
601 | sends are immediate, connection establishment is handled in the |
|
|
602 | background. |
|
|
603 | |
|
|
604 | =item * Erlang can silently lose messages, AEMP cannot. |
|
|
605 | |
|
|
606 | Erlang makes few guarantees on messages delivery - messages can get lost |
|
|
607 | without any of the processes realising it (i.e. you send messages a, b, |
|
|
608 | and c, and the other side only receives messages a and c). |
|
|
609 | |
|
|
610 | AEMP guarantees correct ordering, and the guarantee that there are no |
|
|
611 | holes in the message sequence. |
|
|
612 | |
|
|
613 | =item * In erlang, processes can be declared dead and later be found to be |
|
|
614 | alive. |
|
|
615 | |
|
|
616 | In erlang it can happen that a monitored process is declared dead and |
|
|
617 | linked processes get killed, but later it turns out that the process is |
|
|
618 | still alive - and can receive messages. |
|
|
619 | |
|
|
620 | In AEMP, when port monitoring detects a port as dead, then that port will |
|
|
621 | eventually be killed - it cannot happen that a node detects a port as dead |
|
|
622 | and then later sends messages to it, finding it is still alive. |
|
|
623 | |
|
|
624 | =item * Erlang can send messages to the wrong port, AEMP does not. |
|
|
625 | |
|
|
626 | In erlang it is quite possible that a node that restarts reuses a process |
|
|
627 | ID known to other nodes for a completely different process, causing |
|
|
628 | messages destined for that process to end up in an unrelated process. |
|
|
629 | |
|
|
630 | AEMP never reuses port IDs, so old messages or old port IDs floating |
|
|
631 | around in the network will not be sent to an unrelated port. |
|
|
632 | |
|
|
633 | =item * Erlang uses unprotected connections, AEMP uses secure |
|
|
634 | authentication and can use TLS. |
|
|
635 | |
|
|
636 | AEMP can use a proven protocol - SSL/TLS - to protect connections and |
|
|
637 | securely authenticate nodes. |
|
|
638 | |
|
|
639 | =item * The AEMP protocol is optimised for both text-based and binary |
|
|
640 | communications. |
|
|
641 | |
|
|
642 | The AEMP protocol, unlike the erlang protocol, supports both |
|
|
643 | language-independent text-only protocols (good for debugging) and binary, |
|
|
644 | language-specific serialisers (e.g. Storable). |
|
|
645 | |
|
|
646 | It has also been carefully designed to be implementable in other languages |
|
|
647 | with a minimum of work while gracefully degrading fucntionality to make the |
|
|
648 | protocol simple. |
|
|
649 | |
|
|
650 | =back |
|
|
651 | |
513 | =head1 SEE ALSO |
652 | =head1 SEE ALSO |
514 | |
653 | |
515 | L<AnyEvent>. |
654 | L<AnyEvent>. |
516 | |
655 | |
517 | =head1 AUTHOR |
656 | =head1 AUTHOR |