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.28 by root, Tue Aug 4 22:16:54 2009 UTC vs.
Revision 1.31 by root, Wed Aug 5 19:55:58 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_
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
101our $SELF; 113our $SELF;
115 127
116=item $noderef = node_of $portid 128=item $noderef = node_of $portid
117 129
118Extracts and returns the noderef from a portid or a noderef. 130Extracts and returns the noderef from a portid or a noderef.
119 131
132=item $cv = resolve_node $noderef
133
134Takes an unresolved node reference that may contain hostnames and
135abbreviated IDs, resolves all of them and returns a resolved node
136reference.
137
138In addition to C<address:port> pairs allowed in resolved noderefs, the
139following forms are supported:
140
141=over 4
142
143=item the empty string
144
145An empty-string component gets resolved as if the default port (4040) was
146specified.
147
148=item naked port numbers (e.g. C<1234>)
149
150These are resolved by prepending the local nodename and a colon, to be
151further resolved.
152
153=item hostnames (e.g. C<localhost:1234>, C<localhost>)
154
155These are resolved by using AnyEvent::DNS to resolve them, optionally
156looking up SRV records for the C<aemp=4040> port, if no port was
157specified.
158
159=back
160
120=item $SELF 161=item $SELF
121 162
122Contains the current port id while executing C<rcv> callbacks or C<psub> 163Contains the current port id while executing C<rcv> callbacks or C<psub>
123blocks. 164blocks.
124 165
198 mon $port, $self => "restart"; 239 mon $port, $self => "restart";
199 240
200=cut 241=cut
201 242
202sub mon { 243sub 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 {
257It means that if either one is killed abnormally, the other one gets 299It means that if either one is killed abnormally, the other one gets
258killed as well. 300killed as well.
259 301
260=item $local_port = port 302=item $local_port = port
261 303
262Create a new local port object that supports message matching. 304Create a new local port object that can be used either as a pattern
305matching port ("full port") or a single-callback port ("miniport"),
306depending 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
266Creates a "mini port", that is, a very lightweight port without any 310Creates a "mini port", that is, a very lightweight port without any
267pattern matching behind it, and returns its ID. 311pattern matching behind it, and returns its ID.
273The message will be passed as-is, no extra argument (i.e. no port id) will 317The message will be passed as-is, no extra argument (i.e. no port id) will
274be passed to the callback. 318be passed to the callback.
275 319
276If you need the local port id in the callback, this works nicely: 320If 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
395Replaces the callback on the specified miniport (or newly created port
396object, 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), ...
376also the most efficient match (by far). 425also the most efficient match (by far).
377 426
378=cut 427=cut
379 428
380sub rcv($@) { 429sub 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
410Remembers C<$SELF> and creates a closure out of the BLOCK. When the 462Remembers 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
454Tells the node to become a public node, i.e. reachable from other nodes. 506Tells the node to become a public node, i.e. reachable from other nodes.
455 507
456If no arguments are given, or the first argument is C<undef>, then 508The 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 509(if missing then the empty string is used).
458local nodename resolves to.
459 510
460Otherwise the first argument must be an array-reference with transport 511It 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 512tries 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 513which case AnyEvent::MP tries to guess the local addresses.
463will become the node reference.
464 514
465=cut 515=cut
466 516
467=back 517=back
468 518
471Nodes understand the following messages sent to them. Many of them take 521Nodes understand the following messages sent to them. Many of them take
472arguments called C<@reply>, which will simply be used to compose a reply 522arguments 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 523message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
474the remaining arguments are simply the message data. 524the remaining arguments are simply the message data.
475 525
526While 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines