ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-MP/MP.pm
(Generate patch)

Comparing cvsroot/AnyEvent-MP/MP.pm (file contents):
Revision 1.35 by root, Thu Aug 6 10:21:48 2009 UTC vs.
Revision 1.37 by root, Fri Aug 7 16:47:23 2009 UTC

22 snd $port2, ping => $port1; 22 snd $port2, ping => $port1;
23 23
24 # more, smarter, matches (_any_ is exported by this module) 24 # more, smarter, matches (_any_ is exported by this module)
25 rcv $port, [child_died => $pid] => sub { ... 25 rcv $port, [child_died => $pid] => sub { ...
26 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 26 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3
27
28 # linking two ports, so they both crash together
29 lnk $port1, $port2;
30 27
31 # monitoring 28 # monitoring
32 mon $port, $cb->(@msg) # callback is invoked on death 29 mon $port, $cb->(@msg) # callback is invoked on death
33 mon $port, $otherport # kill otherport on abnormal death 30 mon $port, $otherport # kill otherport on abnormal death
34 mon $port, $otherport, @msg # send message on death 31 mon $port, $otherport, @msg # send message on death
305 $port 302 $port
306} 303}
307 304
308=item reg $port, $name 305=item reg $port, $name
309 306
310Registers the given port under the name C<$name>. If the name already 307=item reg $name
311exists it is replaced. 308
309Registers the given port (or C<$SELF><<< if missing) under the name
310C<$name>. If the name already exists it is replaced.
312 311
313A port can only be registered under one well known name. 312A port can only be registered under one well known name.
314 313
315A port automatically becomes unregistered when it is killed. 314A port automatically becomes unregistered when it is killed.
316 315
317=cut 316=cut
318 317
319sub reg(@) { 318sub reg(@) {
320 my ($port, $name) = @_; 319 my $port = @_ > 1 ? shift : $SELF || Carp::croak 'reg: called with one argument only, but $SELF not set,';
321 320
322 $REG{$name} = $port; 321 $REG{$_[0]} = $port;
323} 322}
324 323
325=item rcv $port, $callback->(@msg) 324=item rcv $port, $callback->(@msg)
326 325
327Replaces the callback on the specified miniport (after converting it to 326Replaces the callback on the specified miniport (after converting it to
332=item rcv $port, $smartmatch => $callback->(@msg), ... 331=item rcv $port, $smartmatch => $callback->(@msg), ...
333 332
334=item rcv $port, [$smartmatch...] => $callback->(@msg), ... 333=item rcv $port, [$smartmatch...] => $callback->(@msg), ...
335 334
336Register callbacks to be called on matching messages on the given full 335Register callbacks to be called on matching messages on the given full
337port (after converting it to one if required). 336port (after converting it to one if required) and return the port.
338 337
339The callback has to return a true value when its work is done, after 338The callback has to return a true value when its work is done, after
340which is will be removed, or a false value in which case it will stay 339which is will be removed, or a false value in which case it will stay
341registered. 340registered.
342 341
354exported by this module) matches any single element of the message. 353exported by this module) matches any single element of the message.
355 354
356While not required, it is highly recommended that the first matching 355While not required, it is highly recommended that the first matching
357element is a string identifying the message. The one-string-only match is 356element is a string identifying the message. The one-string-only match is
358also the most efficient match (by far). 357also the most efficient match (by far).
358
359Example: create a port and bind receivers on it in one go.
360
361 my $port = rcv port,
362 msg1 => sub { ...; 0 },
363 msg2 => sub { ...; 0 },
364 ;
365
366Example: create a port, bind receivers and send it in a message elsewhere
367in one go:
368
369 snd $otherport, reply =>
370 rcv port,
371 msg1 => sub { ...; 0 },
372 ...
373 ;
359 374
360=cut 375=cut
361 376
362sub rcv($@) { 377sub rcv($@) {
363 my $port = shift; 378 my $port = shift;
470 } 485 }
471} 486}
472 487
473=item $guard = mon $port, $cb->(@reason) 488=item $guard = mon $port, $cb->(@reason)
474 489
475=item $guard = mon $port, $otherport 490=item $guard = mon $port, $rcvport
476 491
492=item $guard = mon $port
493
477=item $guard = mon $port, $otherport, @msg 494=item $guard = mon $port, $rcvport, @msg
478 495
479Monitor the given port and do something when the port is killed. 496Monitor the given port and do something when the port is killed, and
497optionally return a guard that can be used to stop monitoring again.
480 498
481In the first form, the callback is simply called with any number 499In the first form (callback), the callback is simply called with any
482of C<@reason> elements (no @reason means that the port was deleted 500number of C<@reason> elements (no @reason means that the port was deleted
483"normally"). Note also that I<< the callback B<must> never die >>, so use 501"normally"). Note also that I<< the callback B<must> never die >>, so use
484C<eval> if unsure. 502C<eval> if unsure.
485 503
486In the second form, the other port will be C<kil>'ed with C<@reason>, iff 504In the second form (another port given), the other port (C<$rcvport)
487a @reason was specified, i.e. on "normal" kils nothing happens, while 505will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on
488under all other conditions, the other port is killed with the same reason. 506"normal" kils nothing happens, while under all other conditions, the other
507port is killed with the same reason.
489 508
509The third form (kill self) is the same as the second form, except that
510C<$rvport> defaults to C<$SELF>.
511
490In the last form, a message of the form C<@msg, @reason> will be C<snd>. 512In the last form (message), a message of the form C<@msg, @reason> will be
513C<snd>.
514
515As a rule of thumb, monitoring requests should always monitor a port from
516a local port (or callback). The reason is that kill messages might get
517lost, just like any other message. Another less obvious reason is that
518even monitoring requests can get lost (for exmaple, when the connection
519to the other node goes down permanently). When monitoring a port locally
520these problems do not exist.
491 521
492Example: call a given callback when C<$port> is killed. 522Example: call a given callback when C<$port> is killed.
493 523
494 mon $port, sub { warn "port died because of <@_>\n" }; 524 mon $port, sub { warn "port died because of <@_>\n" };
495 525
496Example: kill ourselves when C<$port> is killed abnormally. 526Example: kill ourselves when C<$port> is killed abnormally.
497 527
498 mon $port, $self; 528 mon $port;
499 529
500Example: send us a restart message another C<$port> is killed. 530Example: send us a restart message when another C<$port> is killed.
501 531
502 mon $port, $self => "restart"; 532 mon $port, $self => "restart";
503 533
504=cut 534=cut
505 535
506sub mon { 536sub mon {
507 my ($noderef, $port) = split /#/, shift, 2; 537 my ($noderef, $port) = split /#/, shift, 2;
508 538
509 my $node = $NODE{$noderef} || add_node $noderef; 539 my $node = $NODE{$noderef} || add_node $noderef;
510 540
511 my $cb = shift; 541 my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,';
512 542
513 unless (ref $cb) { 543 unless (ref $cb) {
514 if (@_) { 544 if (@_) {
515 # send a kill info message 545 # send a kill info message
516 my (@msg) = ($cb, @_); 546 my (@msg) = @_;
517 $cb = sub { snd @msg, @_ }; 547 $cb = sub { snd @msg, @_ };
518 } else { 548 } else {
519 # simply kill other port 549 # simply kill other port
520 my $port = $cb; 550 my $port = $cb;
521 $cb = sub { kil $port, @_ if @_ }; 551 $cb = sub { kil $port, @_ if @_ };
547=cut 577=cut
548 578
549sub mon_guard { 579sub mon_guard {
550 my ($port, @refs) = @_; 580 my ($port, @refs) = @_;
551 581
582 #TODO: mon-less form?
583
552 mon $port, sub { 0 && @refs } 584 mon $port, sub { 0 && @refs }
553} 585}
554
555=item lnk $port1, $port2
556
557Link two ports. This is simply a shorthand for:
558
559 mon $port1, $port2;
560 mon $port2, $port1;
561
562It means that if either one is killed abnormally, the other one gets
563killed as well.
564 586
565=item kil $port[, @reason] 587=item kil $port[, @reason]
566 588
567Kill the specified port with the given C<@reason>. 589Kill the specified port with the given C<@reason>.
568 590
717or I<none>, there is no in-between, so monitoring single processes is 739or I<none>, there is no in-between, so monitoring single processes is
718difficult to implement. Monitoring in AEMP is more flexible than in 740difficult to implement. Monitoring in AEMP is more flexible than in
719Erlang, as one can choose between automatic kill, exit message or callback 741Erlang, as one can choose between automatic kill, exit message or callback
720on a per-process basis. 742on a per-process basis.
721 743
722=item * Erlang has different semantics for monitoring and linking, AEMP has the same. 744=item * Erlang tries to hide remote/local connections, AEMP does not.
723 745
724Monitoring in Erlang is not an indicator of process death/crashes, 746Monitoring in Erlang is not an indicator of process death/crashes,
725as linking is (except linking is unreliable in Erlang). In AEMP, the 747as linking is (except linking is unreliable in Erlang).
726semantics of monitoring and linking are identical, linking is simply 748
727two-way monitoring with automatic kill. 749In AEMP, you don't "look up" registered port names or send to named ports
750that might or might not be persistent. Instead, you normally spawn a port
751on the remote node. The init function monitors the you, and you monitor
752the remote port. Since both monitors are local to the node, they are much
753more reliable.
754
755This also saves round-trips and avoids sending messages to the wrong port
756(hard to do in Erlang).
728 757
729=back 758=back
730 759
731=head1 SEE ALSO 760=head1 SEE ALSO
732 761

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines