ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP.pm
(Generate patch)

Comparing AnyEvent-MP/MP.pm (file contents):
Revision 1.22 by root, Tue Aug 4 18:33:30 2009 UTC vs.
Revision 1.29 by root, Tue Aug 4 23:16:57 2009 UTC

30This module (-family) implements a simple message passing framework. 30This module (-family) implements a simple message passing framework.
31 31
32Despite its simplicity, you can securely message other processes running 32Despite its simplicity, you can securely message other processes running
33on the same or other hosts. 33on the same or other hosts.
34 34
35For an introduction to this module family, see the L<AnyEvent::MP::Intro>
36manual page.
37
35At the moment, this module family is severly brokena nd underdocumented, 38At the moment, this module family is severly broken and underdocumented,
36so do not use. This was uploaded mainly to reserve the CPAN namespace - 39so do not use. This was uploaded mainly to reserve the CPAN namespace -
37stay tuned! 40stay tuned! The basic API should be finished, however.
38 41
39=head1 CONCEPTS 42=head1 CONCEPTS
40 43
41=over 4 44=over 4
42 45
43=item port 46=item port
44 47
45A port is something you can send messages to with the C<snd> function, and 48A port is something you can send messages to (with the C<snd> function).
46you can register C<rcv> handlers with. All C<rcv> handlers will receive 49
47messages they match, messages will not be queued. 50Some ports allow you to register C<rcv> handlers that can match specific
51messages. All C<rcv> handlers will receive messages they match, messages
52will not be queued.
48 53
49=item port id - C<noderef#portname> 54=item port id - C<noderef#portname>
50 55
51A port id is always the noderef, a hash-mark (C<#>) as separator, followed 56A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as
52by a port name (a printable string of unspecified format). 57separator, and a port name (a printable string of unspecified format). An
58exception is the the node port, whose ID is identical to it's node
59reference.
53 60
54=item node 61=item node
55 62
56A node is a single process containing at least one port - the node 63A node is a single process containing at least one port - the node
57port. You can send messages to node ports to let them create new ports, 64port. You can send messages to node ports to find existing ports or to
58among other things. 65create new ports, among other things.
59 66
60Initially, nodes are either private (single-process only) or hidden 67Nodes are either private (single-process only), slaves (connected to a
61(connected to a master node only). Only when they epxlicitly "become 68master node only) or public nodes (connectable from unrelated nodes).
62public" can you send them messages from unrelated other nodes.
63 69
64=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>
65 71
66A noderef is a string that either uniquely identifies a given node (for 72A node reference is a string that either simply identifies the node (for
67private and hidden nodes), or contains a recipe on how to reach a given 73private and slave nodes), or contains a recipe on how to reach a given
68node (for public nodes). 74node (for public nodes).
69 75
76This recipe is simply a comma-separated list of C<address:port> pairs (for
77TCP/IP, other protocols might look different).
78
79Node references come in two flavours: resolved (containing only numerical
80addresses) or unresolved (where hostnames are used instead of addresses).
81
82Before using an unresolved node reference in a message you first have to
83resolve it.
84
70=back 85=back
71 86
72=head1 VARIABLES/FUNCTIONS 87=head1 VARIABLES/FUNCTIONS
73 88
74=over 4 89=over 4
85 100
86use AE (); 101use AE ();
87 102
88use base "Exporter"; 103use base "Exporter";
89 104
90our $VERSION = '0.02'; 105our $VERSION = '0.1';
91our @EXPORT = qw( 106our @EXPORT = qw(
92 NODE $NODE *SELF node_of _any_ 107 NODE $NODE *SELF node_of _any_
108 resolve_node
93 become_slave become_public 109 become_slave become_public
94 snd rcv mon kil reg psub 110 snd rcv mon kil reg psub
95 port 111 port
96); 112);
97 113
111identifiers become invalid. 127identifiers become invalid.
112 128
113=item $noderef = node_of $portid 129=item $noderef = node_of $portid
114 130
115Extracts and returns the noderef from a portid or a noderef. 131Extracts and returns the noderef from a portid or a noderef.
132
133=item $cv = resolve_node $noderef
134
135Takes an unresolved node reference that may contain hostnames and
136abbreviated IDs, resolves all of them and returns a resolved node
137reference.
138
139In addition to C<address:port> pairs allowed in resolved noderefs, the
140following forms are supported:
141
142=over 4
143
144=item the empty string
145
146An empty-string component gets resolved as if the default port (4040) was
147specified.
148
149=item naked port numbers (e.g. C<1234>)
150
151These are resolved by prepending the local nodename and a colon, to be
152further resolved.
153
154=item hostnames (e.g. C<localhost:1234>, C<localhost>)
155
156These are resolved by using AnyEvent::DNS to resolve them, optionally
157looking up SRV records for the C<aemp=4040> port, if no port was
158specified.
159
160=back
116 161
117=item $SELF 162=item $SELF
118 163
119Contains the current port id while executing C<rcv> callbacks or C<psub> 164Contains the current port id while executing C<rcv> callbacks or C<psub>
120blocks. 165blocks.
241sub mon_guard { 286sub mon_guard {
242 my ($port, @refs) = @_; 287 my ($port, @refs) = @_;
243 288
244 mon $port, sub { 0 && @refs } 289 mon $port, sub { 0 && @refs }
245} 290}
291
292=item lnk $port1, $port2
293
294Link two ports. This is simply a shorthand for:
295
296 mon $port1, $port2;
297 mon $port2, $port1;
298
299It means that if either one is killed abnormally, the other one gets
300killed as well.
246 301
247=item $local_port = port 302=item $local_port = port
248 303
249Create a new local port object that supports message matching. 304Create a new local port object that supports message matching.
250 305
434 489
435=head1 FUNCTIONS FOR NODES 490=head1 FUNCTIONS FOR NODES
436 491
437=over 4 492=over 4
438 493
439=item become_public endpoint... 494=item become_public $noderef
440 495
441Tells the node to become a public node, i.e. reachable from other nodes. 496Tells the node to become a public node, i.e. reachable from other nodes.
442 497
443If no arguments are given, or the first argument is C<undef>, then 498The first argument is the (unresolved) node reference of the local node
444AnyEvent::MP tries to bind on port C<4040> on all IP addresses that the 499(if missing then the empty string is used).
445local nodename resolves to.
446 500
447Otherwise the first argument must be an array-reference with transport 501It is quite common to not specify anything, in which case the local node
448endpoints ("ip:port", "hostname:port") or port numbers (in which case the 502tries to listen on the default port, or to only specify a port number, in
449local nodename is used as hostname). The endpoints are all resolved and 503which case AnyEvent::MP tries to guess the local addresses.
450will become the node reference.
451 504
452=cut 505=cut
453 506
454=back 507=back
455 508
458Nodes understand the following messages sent to them. Many of them take 511Nodes understand the following messages sent to them. Many of them take
459arguments called C<@reply>, which will simply be used to compose a reply 512arguments called C<@reply>, which will simply be used to compose a reply
460message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and 513message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
461the remaining arguments are simply the message data. 514the remaining arguments are simply the message data.
462 515
516While other messages exist, they are not public and subject to change.
517
463=over 4 518=over 4
464 519
465=cut 520=cut
466 521
467=item lookup => $name, @reply 522=item lookup => $name, @reply
495 snd $NODE, time => $myport, timereply => 1, 2; 550 snd $NODE, time => $myport, timereply => 1, 2;
496 # => snd $myport, timereply => 1, 2, <time> 551 # => snd $myport, timereply => 1, 2, <time>
497 552
498=back 553=back
499 554
555=head1 AnyEvent::MP vs. Distributed Erlang
556
557AnyEvent::MP got lots of its ideas from distributed erlang (erlang node
558== aemp node, erlang process == aemp port), so many of the documents and
559programming techniques employed by erlang apply to AnyEvent::MP. Here is a
560sample:
561
562 http://www.erlang.se/doc/programming_rules.shtml
563 http://erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4
564 http://erlang.org/download/erlang-book-part1.pdf # chapters 5 and 6
565 http://erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5
566
567Despite the similarities, there are also some important differences:
568
569=over 4
570
571=item * Node references contain the recipe on how to contact them.
572
573Erlang relies on special naming and DNS to work everywhere in the
574same way. AEMP relies on each node knowing it's own address(es), with
575convenience functionality.
576
577This means that AEMP requires a less tightly controlled environment at the
578cost of longer node references and a slightly higher management overhead.
579
580=item * Erlang uses processes and a mailbox, AEMP does not queue.
581
582Erlang uses processes that selctively receive messages, and therefore
583needs a queue. AEMP is event based, queuing messages would serve no useful
584purpose.
585
586(But see L<Coro::MP> for a more erlang-like process model on top of AEMP).
587
588=item * Erlang sends are synchronous, AEMP sends are asynchronous.
589
590Sending messages in erlang is synchronous and blocks the process. AEMP
591sends are immediate, connection establishment is handled in the
592background.
593
594=item * Erlang can silently lose messages, AEMP cannot.
595
596Erlang makes few guarantees on messages delivery - messages can get lost
597without any of the processes realising it (i.e. you send messages a, b,
598and c, and the other side only receives messages a and c).
599
600AEMP guarantees correct ordering, and the guarantee that there are no
601holes in the message sequence.
602
603=item * In erlang, processes can be declared dead and later be found to be
604alive.
605
606In erlang it can happen that a monitored process is declared dead and
607linked processes get killed, but later it turns out that the process is
608still alive - and can receive messages.
609
610In AEMP, when port monitoring detects a port as dead, then that port will
611eventually be killed - it cannot happen that a node detects a port as dead
612and then later sends messages to it, finding it is still alive.
613
614=item * Erlang can send messages to the wrong port, AEMP does not.
615
616In erlang it is quite possible that a node that restarts reuses a process
617ID known to other nodes for a completely different process, causing
618messages destined for that process to end up in an unrelated process.
619
620AEMP never reuses port IDs, so old messages or old port IDs floating
621around in the network will not be sent to an unrelated port.
622
623=item * Erlang uses unprotected connections, AEMP uses secure
624authentication and can use TLS.
625
626AEMP can use a proven protocol - SSL/TLS - to protect connections and
627securely authenticate nodes.
628
629=item * The AEMP protocol is optimised for both text-based and binary
630communications.
631
632The AEMP protocol, unlike the erlang protocol, supports both
633language-independent text-only protocols (good for debugging) and binary,
634language-specific serialisers (e.g. Storable).
635
636It has also been carefully designed to be implementable in other languages
637with a minimum of work while gracefully degrading fucntionality to make the
638protocol simple.
639
640=back
641
500=head1 SEE ALSO 642=head1 SEE ALSO
501 643
502L<AnyEvent>. 644L<AnyEvent>.
503 645
504=head1 AUTHOR 646=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines