… | |
… | |
53 | |
53 | |
54 | =item port id - C<noderef#portname> |
54 | =item port id - C<noderef#portname> |
55 | |
55 | |
56 | A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as |
56 | A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as |
57 | separator, and a port name (a printable string of unspecified format). An |
57 | separator, and a port name (a printable string of unspecified format). An |
58 | exception is the the node port, whose ID is identical to it's node |
58 | exception is the the node port, whose ID is identical to its node |
59 | reference. |
59 | reference. |
60 | |
60 | |
61 | =item node |
61 | =item node |
62 | |
62 | |
63 | A node is a single process containing at least one port - the node |
63 | A node is a single process containing at least one port - the node |
… | |
… | |
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; |
… | |
… | |
240 | mon $port, $self => "restart"; |
239 | mon $port, $self => "restart"; |
241 | |
240 | |
242 | =cut |
241 | =cut |
243 | |
242 | |
244 | sub mon { |
243 | sub 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 { |
… | |
… | |
299 | 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 |
300 | killed as well. |
300 | killed as well. |
301 | |
301 | |
302 | =item $local_port = port |
302 | =item $local_port = port |
303 | |
303 | |
304 | 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. |
305 | |
307 | |
306 | =item $portid = port { my @msg = @_; $finished } |
308 | =item $portid = port { my @msg = @_; $finished } |
307 | |
309 | |
308 | 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 |
309 | pattern matching behind it, and returns its ID. |
311 | pattern matching behind it, and returns its ID. |
… | |
… | |
315 | 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 |
316 | be passed to the callback. |
318 | be passed to the callback. |
317 | |
319 | |
318 | 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: |
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 | |
|
|
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 | |
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), ... |
… | |
… | |
418 | also the most efficient match (by far). |
425 | also the most efficient match (by far). |
419 | |
426 | |
420 | =cut |
427 | =cut |
421 | |
428 | |
422 | sub rcv($@) { |
429 | sub 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 | |
452 | 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 |