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.30 by root, Tue Aug 4 23:35:51 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 its 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
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.
116 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
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.
121 166
195 mon $port, $self => "restart"; 240 mon $port, $self => "restart";
196 241
197=cut 242=cut
198 243
199sub mon { 244sub mon {
200 my ($noderef, $port, $cb) = ((split /#/, shift, 2), shift); 245 my ($noderef, $port) = split /#/, shift, 2;
201 246
202 my $node = $NODE{$noderef} || add_node $noderef; 247 my $node = $NODE{$noderef} || add_node $noderef;
203 248
204 #TODO: ports must not be references 249 my $cb = shift;
205 if (!ref $cb or "AnyEvent::MP::Port" eq ref $cb) { 250
251 unless (ref $cb) {
206 if (@_) { 252 if (@_) {
207 # send a kill info message 253 # send a kill info message
208 my (@msg) = ($cb, @_); 254 my (@msg) = ($cb, @_);
209 $cb = sub { snd @msg, @_ }; 255 $cb = sub { snd @msg, @_ };
210 } else { 256 } else {
241sub mon_guard { 287sub mon_guard {
242 my ($port, @refs) = @_; 288 my ($port, @refs) = @_;
243 289
244 mon $port, sub { 0 && @refs } 290 mon $port, sub { 0 && @refs }
245} 291}
292
293=item lnk $port1, $port2
294
295Link two ports. This is simply a shorthand for:
296
297 mon $port1, $port2;
298 mon $port2, $port1;
299
300It means that if either one is killed abnormally, the other one gets
301killed as well.
246 302
247=item $local_port = port 303=item $local_port = port
248 304
249Create a new local port object that supports message matching. 305Create a new local port object that supports message matching.
250 306
434 490
435=head1 FUNCTIONS FOR NODES 491=head1 FUNCTIONS FOR NODES
436 492
437=over 4 493=over 4
438 494
439=item become_public endpoint... 495=item become_public $noderef
440 496
441Tells the node to become a public node, i.e. reachable from other nodes. 497Tells the node to become a public node, i.e. reachable from other nodes.
442 498
443If no arguments are given, or the first argument is C<undef>, then 499The 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 500(if missing then the empty string is used).
445local nodename resolves to.
446 501
447Otherwise the first argument must be an array-reference with transport 502It 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 503tries 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 504which case AnyEvent::MP tries to guess the local addresses.
450will become the node reference.
451 505
452=cut 506=cut
453 507
454=back 508=back
455 509
458Nodes understand the following messages sent to them. Many of them take 512Nodes understand the following messages sent to them. Many of them take
459arguments called C<@reply>, which will simply be used to compose a reply 513arguments 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 514message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
461the remaining arguments are simply the message data. 515the remaining arguments are simply the message data.
462 516
517While other messages exist, they are not public and subject to change.
518
463=over 4 519=over 4
464 520
465=cut 521=cut
466 522
467=item lookup => $name, @reply 523=item lookup => $name, @reply
495 snd $NODE, time => $myport, timereply => 1, 2; 551 snd $NODE, time => $myport, timereply => 1, 2;
496 # => snd $myport, timereply => 1, 2, <time> 552 # => snd $myport, timereply => 1, 2, <time>
497 553
498=back 554=back
499 555
556=head1 AnyEvent::MP vs. Distributed Erlang
557
558AnyEvent::MP got lots of its ideas from distributed erlang (erlang node
559== aemp node, erlang process == aemp port), so many of the documents and
560programming techniques employed by erlang apply to AnyEvent::MP. Here is a
561sample:
562
563 http://www.erlang.se/doc/programming_rules.shtml
564 http://erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4
565 http://erlang.org/download/erlang-book-part1.pdf # chapters 5 and 6
566 http://erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5
567
568Despite the similarities, there are also some important differences:
569
570=over 4
571
572=item * Node references contain the recipe on how to contact them.
573
574Erlang relies on special naming and DNS to work everywhere in the
575same way. AEMP relies on each node knowing it's own address(es), with
576convenience functionality.
577
578This means that AEMP requires a less tightly controlled environment at the
579cost of longer node references and a slightly higher management overhead.
580
581=item * Erlang uses processes and a mailbox, AEMP does not queue.
582
583Erlang uses processes that selctively receive messages, and therefore
584needs a queue. AEMP is event based, queuing messages would serve no useful
585purpose.
586
587(But see L<Coro::MP> for a more erlang-like process model on top of AEMP).
588
589=item * Erlang sends are synchronous, AEMP sends are asynchronous.
590
591Sending messages in erlang is synchronous and blocks the process. AEMP
592sends are immediate, connection establishment is handled in the
593background.
594
595=item * Erlang can silently lose messages, AEMP cannot.
596
597Erlang makes few guarantees on messages delivery - messages can get lost
598without any of the processes realising it (i.e. you send messages a, b,
599and c, and the other side only receives messages a and c).
600
601AEMP guarantees correct ordering, and the guarantee that there are no
602holes in the message sequence.
603
604=item * In erlang, processes can be declared dead and later be found to be
605alive.
606
607In erlang it can happen that a monitored process is declared dead and
608linked processes get killed, but later it turns out that the process is
609still alive - and can receive messages.
610
611In AEMP, when port monitoring detects a port as dead, then that port will
612eventually be killed - it cannot happen that a node detects a port as dead
613and then later sends messages to it, finding it is still alive.
614
615=item * Erlang can send messages to the wrong port, AEMP does not.
616
617In erlang it is quite possible that a node that restarts reuses a process
618ID known to other nodes for a completely different process, causing
619messages destined for that process to end up in an unrelated process.
620
621AEMP never reuses port IDs, so old messages or old port IDs floating
622around in the network will not be sent to an unrelated port.
623
624=item * Erlang uses unprotected connections, AEMP uses secure
625authentication and can use TLS.
626
627AEMP can use a proven protocol - SSL/TLS - to protect connections and
628securely authenticate nodes.
629
630=item * The AEMP protocol is optimised for both text-based and binary
631communications.
632
633The AEMP protocol, unlike the erlang protocol, supports both
634language-independent text-only protocols (good for debugging) and binary,
635language-specific serialisers (e.g. Storable).
636
637It has also been carefully designed to be implementable in other languages
638with a minimum of work while gracefully degrading fucntionality to make the
639protocol simple.
640
641=back
642
500=head1 SEE ALSO 643=head1 SEE ALSO
501 644
502L<AnyEvent>. 645L<AnyEvent>.
503 646
504=head1 AUTHOR 647=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines