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.29 by root, Tue Aug 4 23:16:57 2009 UTC vs.
Revision 1.31 by root, Wed Aug 5 19:55:58 2009 UTC

53 53
54=item port id - C<noderef#portname> 54=item port id - C<noderef#portname>
55 55
56A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as 56A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as
57separator, and a port name (a printable string of unspecified format). An 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 58exception is the the node port, whose ID is identical to its node
59reference. 59reference.
60 60
61=item node 61=item node
62 62
63A 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
103use base "Exporter"; 103use base "Exporter";
104 104
105our $VERSION = '0.1'; 105our $VERSION = '0.1';
106our @EXPORT = qw( 106our @EXPORT = qw(
107 NODE $NODE *SELF node_of _any_ 107 NODE $NODE *SELF node_of _any_
108 resolve_node 108 resolve_node initialise_node
109 become_slave become_public
110 snd rcv mon kil reg psub 109 snd rcv mon kil reg psub
111 port 110 port
112); 111);
113 112
114our $SELF; 113our $SELF;
240 mon $port, $self => "restart"; 239 mon $port, $self => "restart";
241 240
242=cut 241=cut
243 242
244sub mon { 243sub mon {
245 my ($noderef, $port, $cb) = ((split /#/, shift, 2), shift); 244 my ($noderef, $port) = split /#/, shift, 2;
246 245
247 my $node = $NODE{$noderef} || add_node $noderef; 246 my $node = $NODE{$noderef} || add_node $noderef;
248 247
249 #TODO: ports must not be references 248 my $cb = shift;
250 if (!ref $cb or "AnyEvent::MP::Port" eq ref $cb) { 249
250 unless (ref $cb) {
251 if (@_) { 251 if (@_) {
252 # send a kill info message 252 # send a kill info message
253 my (@msg) = ($cb, @_); 253 my (@msg) = ($cb, @_);
254 $cb = sub { snd @msg, @_ }; 254 $cb = sub { snd @msg, @_ };
255 } else { 255 } else {
299It 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
300killed as well. 300killed as well.
301 301
302=item $local_port = port 302=item $local_port = port
303 303
304Create 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.
305 307
306=item $portid = port { my @msg = @_; $finished } 308=item $portid = port { my @msg = @_; $finished }
307 309
308Creates a "mini port", that is, a very lightweight port without any 310Creates a "mini port", that is, a very lightweight port without any
309pattern matching behind it, and returns its ID. 311pattern matching behind it, and returns its ID.
315The 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
316be passed to the callback. 318be passed to the callback.
317 319
318If 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:
319 321
320 my $port; $port = miniport { 322 my $port; $port = port {
321 snd $otherport, reply => $port; 323 snd $otherport, reply => $port;
322 }; 324 };
323 325
324=cut 326=cut
325 327
386 my ($portid, $name) = @_; 388 my ($portid, $name) = @_;
387 389
388 $REG{$name} = $portid; 390 $REG{$name} = $portid;
389} 391}
390 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
391=item rcv $portid, tagstring => $callback->(@msg), ... 398=item rcv $portid, tagstring => $callback->(@msg), ...
392 399
393=item rcv $portid, $smartmatch => $callback->(@msg), ... 400=item rcv $portid, $smartmatch => $callback->(@msg), ...
394 401
395=item rcv $portid, [$smartmatch...] => $callback->(@msg), ... 402=item rcv $portid, [$smartmatch...] => $callback->(@msg), ...
418also the most efficient match (by far). 425also the most efficient match (by far).
419 426
420=cut 427=cut
421 428
422sub rcv($@) { 429sub rcv($@) {
430 my $portid = shift;
423 my ($noderef, $port) = split /#/, shift, 2; 431 my ($noderef, $port) = split /#/, $port, 2;
424 432
425 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 433 ($NODE{$noderef} || add_node $noderef) == $NODE{""}
426 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";
427 435
428 my $self = $PORT_DATA{$port} 436 my $self = $PORT_DATA{$port}
443 : push @{ $self->{rc0}{$match->[0]} }, [$cb]; 451 : push @{ $self->{rc0}{$match->[0]} }, [$cb];
444 } else { 452 } else {
445 push @{ $self->{any} }, [$cb, $match]; 453 push @{ $self->{any} }, [$cb, $match];
446 } 454 }
447 } 455 }
456
457 $portid
448} 458}
449 459
450=item $closure = psub { BLOCK } 460=item $closure = psub { BLOCK }
451 461
452Remembers 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines