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

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;
300It 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
301killed as well. 300killed as well.
302 301
303=item $local_port = port 302=item $local_port = port
304 303
305Create 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.
306 307
307=item $portid = port { my @msg = @_; $finished } 308=item $portid = port { my @msg = @_; $finished }
308 309
309Creates a "mini port", that is, a very lightweight port without any 310Creates a "mini port", that is, a very lightweight port without any
310pattern matching behind it, and returns its ID. 311pattern matching behind it, and returns its ID.
316The 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
317be passed to the callback. 318be passed to the callback.
318 319
319If 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:
320 321
321 my $port; $port = miniport { 322 my $port; $port = port {
322 snd $otherport, reply => $port; 323 snd $otherport, reply => $port;
323 }; 324 };
324 325
325=cut 326=cut
326 327
387 my ($portid, $name) = @_; 388 my ($portid, $name) = @_;
388 389
389 $REG{$name} = $portid; 390 $REG{$name} = $portid;
390} 391}
391 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
392=item rcv $portid, tagstring => $callback->(@msg), ... 398=item rcv $portid, tagstring => $callback->(@msg), ...
393 399
394=item rcv $portid, $smartmatch => $callback->(@msg), ... 400=item rcv $portid, $smartmatch => $callback->(@msg), ...
395 401
396=item rcv $portid, [$smartmatch...] => $callback->(@msg), ... 402=item rcv $portid, [$smartmatch...] => $callback->(@msg), ...
419also the most efficient match (by far). 425also the most efficient match (by far).
420 426
421=cut 427=cut
422 428
423sub rcv($@) { 429sub rcv($@) {
430 my $portid = shift;
424 my ($noderef, $port) = split /#/, shift, 2; 431 my ($noderef, $port) = split /#/, $port, 2;
425 432
426 ($NODE{$noderef} || add_node $noderef) == $NODE{""} 433 ($NODE{$noderef} || add_node $noderef) == $NODE{""}
427 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";
428 435
429 my $self = $PORT_DATA{$port} 436 my $self = $PORT_DATA{$port}
444 : push @{ $self->{rc0}{$match->[0]} }, [$cb]; 451 : push @{ $self->{rc0}{$match->[0]} }, [$cb];
445 } else { 452 } else {
446 push @{ $self->{any} }, [$cb, $match]; 453 push @{ $self->{any} }, [$cb, $match];
447 } 454 }
448 } 455 }
456
457 $portid
449} 458}
450 459
451=item $closure = psub { BLOCK } 460=item $closure = psub { BLOCK }
452 461
453Remembers 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