… | |
… | |
305 | $port |
305 | $port |
306 | } |
306 | } |
307 | |
307 | |
308 | =item reg $port, $name |
308 | =item reg $port, $name |
309 | |
309 | |
310 | Registers the given port under the name C<$name>. If the name already |
310 | =item reg $name |
311 | exists it is replaced. |
311 | |
|
|
312 | Registers the given port (or C<$SELF><<< if missing) under the name |
|
|
313 | C<$name>. If the name already exists it is replaced. |
312 | |
314 | |
313 | A port can only be registered under one well known name. |
315 | A port can only be registered under one well known name. |
314 | |
316 | |
315 | A port automatically becomes unregistered when it is killed. |
317 | A port automatically becomes unregistered when it is killed. |
316 | |
318 | |
317 | =cut |
319 | =cut |
318 | |
320 | |
319 | sub reg(@) { |
321 | sub reg(@) { |
320 | my ($port, $name) = @_; |
322 | my $port = @_ > 1 ? shift : $SELF || Carp::croak 'reg: called with one argument only, but $SELF not set,'; |
321 | |
323 | |
322 | $REG{$name} = $port; |
324 | $REG{$_[0]} = $port; |
323 | } |
325 | } |
324 | |
326 | |
325 | =item rcv $port, $callback->(@msg) |
327 | =item rcv $port, $callback->(@msg) |
326 | |
328 | |
327 | Replaces the callback on the specified miniport (after converting it to |
329 | Replaces the callback on the specified miniport (after converting it to |
… | |
… | |
332 | =item rcv $port, $smartmatch => $callback->(@msg), ... |
334 | =item rcv $port, $smartmatch => $callback->(@msg), ... |
333 | |
335 | |
334 | =item rcv $port, [$smartmatch...] => $callback->(@msg), ... |
336 | =item rcv $port, [$smartmatch...] => $callback->(@msg), ... |
335 | |
337 | |
336 | Register callbacks to be called on matching messages on the given full |
338 | Register callbacks to be called on matching messages on the given full |
337 | port (after converting it to one if required). |
339 | port (after converting it to one if required) and return the port. |
338 | |
340 | |
339 | The callback has to return a true value when its work is done, after |
341 | The callback has to return a true value when its work is done, after |
340 | which is will be removed, or a false value in which case it will stay |
342 | which is will be removed, or a false value in which case it will stay |
341 | registered. |
343 | registered. |
342 | |
344 | |
… | |
… | |
354 | exported by this module) matches any single element of the message. |
356 | exported by this module) matches any single element of the message. |
355 | |
357 | |
356 | While not required, it is highly recommended that the first matching |
358 | While not required, it is highly recommended that the first matching |
357 | element is a string identifying the message. The one-string-only match is |
359 | element is a string identifying the message. The one-string-only match is |
358 | also the most efficient match (by far). |
360 | also the most efficient match (by far). |
|
|
361 | |
|
|
362 | Example: create a port and bind receivers on it in one go. |
|
|
363 | |
|
|
364 | my $port = rcv port, |
|
|
365 | msg1 => sub { ...; 0 }, |
|
|
366 | msg2 => sub { ...; 0 }, |
|
|
367 | ; |
|
|
368 | |
|
|
369 | Example: create a port, bind receivers and send it in a message elsewhere |
|
|
370 | in one go: |
|
|
371 | |
|
|
372 | snd $otherport, reply => |
|
|
373 | rcv port, |
|
|
374 | msg1 => sub { ...; 0 }, |
|
|
375 | ... |
|
|
376 | ; |
359 | |
377 | |
360 | =cut |
378 | =cut |
361 | |
379 | |
362 | sub rcv($@) { |
380 | sub rcv($@) { |
363 | my $port = shift; |
381 | my $port = shift; |
… | |
… | |
470 | } |
488 | } |
471 | } |
489 | } |
472 | |
490 | |
473 | =item $guard = mon $port, $cb->(@reason) |
491 | =item $guard = mon $port, $cb->(@reason) |
474 | |
492 | |
475 | =item $guard = mon $port, $otherport |
493 | =item $guard = mon $port, $rcvport |
476 | |
494 | |
|
|
495 | =item $guard = mon $port |
|
|
496 | |
477 | =item $guard = mon $port, $otherport, @msg |
497 | =item $guard = mon $port, $rcvport, @msg |
478 | |
498 | |
479 | Monitor the given port and do something when the port is killed. |
499 | Monitor the given port and do something when the port is killed, and |
|
|
500 | optionally return a guard that can be used to stop monitoring again. |
480 | |
501 | |
481 | In the first form, the callback is simply called with any number |
502 | In the first form (callback), the callback is simply called with any |
482 | of C<@reason> elements (no @reason means that the port was deleted |
503 | number 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 |
504 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
484 | C<eval> if unsure. |
505 | C<eval> if unsure. |
485 | |
506 | |
486 | In the second form, the other port will be C<kil>'ed with C<@reason>, iff |
507 | In the second form (another port given), the other port (C<$rcvport) |
487 | a @reason was specified, i.e. on "normal" kils nothing happens, while |
508 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
488 | under all other conditions, the other port is killed with the same reason. |
509 | "normal" kils nothing happens, while under all other conditions, the other |
|
|
510 | port is killed with the same reason. |
489 | |
511 | |
|
|
512 | The third form (kill self) is the same as the second form, except that |
|
|
513 | C<$rvport> defaults to C<$SELF>. |
|
|
514 | |
490 | In the last form, a message of the form C<@msg, @reason> will be C<snd>. |
515 | In the last form (message), a message of the form C<@msg, @reason> will be |
|
|
516 | C<snd>. |
491 | |
517 | |
492 | Example: call a given callback when C<$port> is killed. |
518 | Example: call a given callback when C<$port> is killed. |
493 | |
519 | |
494 | mon $port, sub { warn "port died because of <@_>\n" }; |
520 | mon $port, sub { warn "port died because of <@_>\n" }; |
495 | |
521 | |
496 | Example: kill ourselves when C<$port> is killed abnormally. |
522 | Example: kill ourselves when C<$port> is killed abnormally. |
497 | |
523 | |
498 | mon $port, $self; |
524 | mon $port; |
499 | |
525 | |
500 | Example: send us a restart message another C<$port> is killed. |
526 | Example: send us a restart message when another C<$port> is killed. |
501 | |
527 | |
502 | mon $port, $self => "restart"; |
528 | mon $port, $self => "restart"; |
503 | |
529 | |
504 | =cut |
530 | =cut |
505 | |
531 | |
506 | sub mon { |
532 | sub mon { |
507 | my ($noderef, $port) = split /#/, shift, 2; |
533 | my ($noderef, $port) = split /#/, shift, 2; |
508 | |
534 | |
509 | my $node = $NODE{$noderef} || add_node $noderef; |
535 | my $node = $NODE{$noderef} || add_node $noderef; |
510 | |
536 | |
511 | my $cb = shift; |
537 | my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
512 | |
538 | |
513 | unless (ref $cb) { |
539 | unless (ref $cb) { |
514 | if (@_) { |
540 | if (@_) { |
515 | # send a kill info message |
541 | # send a kill info message |
516 | my (@msg) = ($cb, @_); |
542 | my (@msg) = @_; |
517 | $cb = sub { snd @msg, @_ }; |
543 | $cb = sub { snd @msg, @_ }; |
518 | } else { |
544 | } else { |
519 | # simply kill other port |
545 | # simply kill other port |
520 | my $port = $cb; |
546 | my $port = $cb; |
521 | $cb = sub { kil $port, @_ if @_ }; |
547 | $cb = sub { kil $port, @_ if @_ }; |
… | |
… | |
547 | =cut |
573 | =cut |
548 | |
574 | |
549 | sub mon_guard { |
575 | sub mon_guard { |
550 | my ($port, @refs) = @_; |
576 | my ($port, @refs) = @_; |
551 | |
577 | |
|
|
578 | #TODO: mon-less form? |
|
|
579 | |
552 | mon $port, sub { 0 && @refs } |
580 | mon $port, sub { 0 && @refs } |
553 | } |
581 | } |
554 | |
582 | |
555 | =item lnk $port1, $port2 |
583 | =item lnk $port1, $port2 |
556 | |
584 | |
|
|
585 | =item lnk $otherport |
|
|
586 | |
557 | Link two ports. This is simply a shorthand for: |
587 | Link two ports. This is simply a shorthand for: |
558 | |
588 | |
559 | mon $port1, $port2; |
589 | mon $port1, $port2; |
560 | mon $port2, $port1; |
590 | mon $port2, $port1; |
561 | |
591 | |
562 | It means that if either one is killed abnormally, the other one gets |
592 | It means that if either one is killed abnormally, the other one gets |
563 | killed as well. |
593 | killed as well. |
|
|
594 | |
|
|
595 | The one-argument form assumes that one port is C<$SELF>. |
|
|
596 | |
|
|
597 | =cut |
|
|
598 | |
|
|
599 | sub lnk { |
|
|
600 | my $port1 = shift; |
|
|
601 | my $port2 = @_ ? shift : $SELF || Carp::croak 'lnk: called with one argument only, but $SELF not set,'; |
|
|
602 | |
|
|
603 | mon $port1, $port2; |
|
|
604 | mon $port2, $port1; |
|
|
605 | } |
564 | |
606 | |
565 | =item kil $port[, @reason] |
607 | =item kil $port[, @reason] |
566 | |
608 | |
567 | Kill the specified port with the given C<@reason>. |
609 | Kill the specified port with the given C<@reason>. |
568 | |
610 | |