… | |
… | |
103 | use base "Exporter"; |
103 | use base "Exporter"; |
104 | |
104 | |
105 | our $VERSION = '0.1'; |
105 | our $VERSION = '0.1'; |
106 | our @EXPORT = qw( |
106 | our @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 | |
114 | our $SELF; |
113 | our $SELF; |
… | |
… | |
300 | It means that if either one is killed abnormally, the other one gets |
299 | It means that if either one is killed abnormally, the other one gets |
301 | killed as well. |
300 | killed as well. |
302 | |
301 | |
303 | =item $local_port = port |
302 | =item $local_port = port |
304 | |
303 | |
305 | Create a new local port object that supports message matching. |
304 | Create a new local port object that can be used either as a pattern |
|
|
305 | matching port ("full port") or a single-callback port ("miniport"), |
|
|
306 | depending 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 | |
309 | Creates a "mini port", that is, a very lightweight port without any |
310 | Creates a "mini port", that is, a very lightweight port without any |
310 | pattern matching behind it, and returns its ID. |
311 | pattern matching behind it, and returns its ID. |
… | |
… | |
316 | The message will be passed as-is, no extra argument (i.e. no port id) will |
317 | The message will be passed as-is, no extra argument (i.e. no port id) will |
317 | be passed to the callback. |
318 | be passed to the callback. |
318 | |
319 | |
319 | If you need the local port id in the callback, this works nicely: |
320 | If 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 | |
|
|
395 | Replaces the callback on the specified miniport (or newly created port |
|
|
396 | object, 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), ... |
… | |
… | |
419 | also the most efficient match (by far). |
425 | also the most efficient match (by far). |
420 | |
426 | |
421 | =cut |
427 | =cut |
422 | |
428 | |
423 | sub rcv($@) { |
429 | sub 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 | |
453 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |
462 | Remembers C<$SELF> and creates a closure out of the BLOCK. When the |