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.27 by root, Tue Aug 4 22:13:45 2009 UTC vs.
Revision 1.30 by root, Tue Aug 4 23:35:51 2009 UTC

43 43
44=over 4 44=over 4
45 45
46=item port 46=item port
47 47
48A 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).
49you can register C<rcv> handlers with. All C<rcv> handlers will receive 49
50messages 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.
51 53
52=item port id - C<noderef#portname> 54=item port id - C<noderef#portname>
53 55
54A 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
55by 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.
56 60
57=item node 61=item node
58 62
59A 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
60port. 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
61among other things. 65create new ports, among other things.
62 66
63Initially, nodes are either private (single-process only) or hidden 67Nodes are either private (single-process only), slaves (connected to a
64(connected to a master node only). Only when they epxlicitly "become 68master node only) or public nodes (connectable from unrelated nodes).
65public" 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
69A 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
70private 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
71node (for public nodes). 74node (for public nodes).
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.
72 84
73=back 85=back
74 86
75=head1 VARIABLES/FUNCTIONS 87=head1 VARIABLES/FUNCTIONS
76 88
91use base "Exporter"; 103use base "Exporter";
92 104
93our $VERSION = '0.1'; 105our $VERSION = '0.1';
94our @EXPORT = qw( 106our @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
118Extracts and returns the noderef from a portid or a noderef. 131Extracts and returns the noderef from a portid or a noderef.
119 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
120=item $SELF 162=item $SELF
121 163
122Contains 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>
123blocks. 165blocks.
124 166
198 mon $port, $self => "restart"; 240 mon $port, $self => "restart";
199 241
200=cut 242=cut
201 243
202sub mon { 244sub 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
454Tells 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.
455 498
456If no arguments are given, or the first argument is C<undef>, then 499The first argument is the (unresolved) node reference of the local node
457AnyEvent::MP tries to bind on port C<4040> on all IP addresses that the 500(if missing then the empty string is used).
458local nodename resolves to.
459 501
460Otherwise the first argument must be an array-reference with transport 502It is quite common to not specify anything, in which case the local node
461endpoints ("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
462local nodename is used as hostname). The endpoints are all resolved and 504which case AnyEvent::MP tries to guess the local addresses.
463will become the node reference.
464 505
465=cut 506=cut
466 507
467=back 508=back
468 509
471Nodes understand the following messages sent to them. Many of them take 512Nodes understand the following messages sent to them. Many of them take
472arguments 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
473message - 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
474the remaining arguments are simply the message data. 515the remaining arguments are simply the message data.
475 516
517While 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
582authentication and can use TLS. 625authentication and can use TLS.
583 626
584AEMP can use a proven protocol - SSL/TLS - to protect connections and 627AEMP can use a proven protocol - SSL/TLS - to protect connections and
585securely authenticate nodes. 628securely authenticate nodes.
586 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
587=back 641=back
588 642
589=head1 SEE ALSO 643=head1 SEE ALSO
590 644
591L<AnyEvent>. 645L<AnyEvent>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines