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.4 by root, Sat Aug 1 07:36:30 2009 UTC vs.
Revision 1.7 by root, Sat Aug 1 15:04:30 2009 UTC

27This module (-family) implements a simple message passing framework. 27This module (-family) implements a simple message passing framework.
28 28
29Despite its simplicity, you can securely message other processes running 29Despite its simplicity, you can securely message other processes running
30on the same or other hosts. 30on the same or other hosts.
31 31
32At the moment, this module family is severly brokena nd underdocumented,
33so do not use. This was uploaded mainly to resreve the CPAN namespace -
34stay tuned!
35
32=head1 CONCEPTS 36=head1 CONCEPTS
33 37
34=over 4 38=over 4
35 39
36=item port 40=item port
52 56
53Initially, nodes are either private (single-process only) or hidden 57Initially, nodes are either private (single-process only) or hidden
54(connected to a master node only). Only when they epxlicitly "become 58(connected to a master node only). Only when they epxlicitly "become
55public" can you send them messages from unrelated other nodes. 59public" can you send them messages from unrelated other nodes.
56 60
57=item noderef - C<host:port,host:port...>, C<id@noderef, C<id> 61=item noderef - C<host:port,host:port...>, C<id@noderef>, C<id>
58 62
59A noderef is a string that either uniquely identifies a given node (for 63A noderef is a string that either uniquely identifies a given node (for
60private and hidden nodes), or contains a recipe on how to reach a given 64private and hidden nodes), or contains a recipe on how to reach a given
61node (for public nodes). 65node (for public nodes).
62 66
81 85
82use AE (); 86use AE ();
83 87
84use base "Exporter"; 88use base "Exporter";
85 89
86our $VERSION = '0.0'; 90our $VERSION = '0.01';
87our @EXPORT = qw(NODE $NODE $PORT snd rcv _any_); 91our @EXPORT = qw(NODE $NODE $PORT snd rcv _any_);
88 92
89our $DEFAULT_SECRET; 93our $DEFAULT_SECRET;
90our $DEFAULT_PORT = "4040"; 94our $DEFAULT_PORT = "4040";
91 95
111C<become_slave>, after which all local port identifiers become invalid. 115C<become_slave>, after which all local port identifiers become invalid.
112 116
113=cut 117=cut
114 118
115our $UNIQ = sprintf "%x.%x", $$, time; # per-process/node unique cookie 119our $UNIQ = sprintf "%x.%x", $$, time; # per-process/node unique cookie
120our $ID = "a0";
116our $PUBLIC = 0; 121our $PUBLIC = 0;
117our $NODE; 122our $NODE;
118our $PORT; 123our $PORT;
119 124
120our %NODE; # node id to transport mapping, or "undef", for local node 125our %NODE; # node id to transport mapping, or "undef", for local node
254 && &{$_->[0]} 259 && &{$_->[0]}
255 && undef $_; 260 && undef $_;
256 } 261 }
257 262
258 for (@{ $port->{any} }) { 263 for (@{ $port->{any} }) {
259 $_ && [@_[0..$#{$_->[1]}]] ~~ $_->[1] 264 $_ && [@_[0..$#{$_->[1]}]] ~~ $_->[1]
260 && &{$_->[0]} 265 && &{$_->[0]}
261 && undef $_; 266 && undef $_;
262 } 267 }
263} 268}
264 269
366 371
367=back 372=back
368 373
369=head1 NODE MESSAGES 374=head1 NODE MESSAGES
370 375
371Nodes understand the following messages sent to them: 376Nodes understand the following messages sent to them. Many of them take
377arguments called C<@reply>, which will simply be used to compose a reply
378message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
379the remaining arguments are simply the message data.
372 380
373=over 4 381=over 4
374 382
375=cut 383=cut
376 384
389} 397}
390 398
391$NODE{""} = new AnyEvent::MP::Node::Self noderef => $NODE; 399$NODE{""} = new AnyEvent::MP::Node::Self noderef => $NODE;
392_new_port ""; 400_new_port "";
393 401
402=item devnull => ...
403
404Generic data sink/CPU heat conversion.
405
406=cut
407
408rcv "", devnull => sub { () };
409
394=item relay => $port, @msg 410=item relay => $port, @msg
395 411
396Simply forwards the message to the given port. 412Simply forwards the message to the given port.
397 413
398=cut 414=cut
399 415
400rcv "", relay => \&snd; 416rcv "", relay => sub { \&snd; () };
401 417
402=item eval => $string[ @reply] 418=item eval => $string[ @reply]
403 419
404Evaluates the given string. If C<@reply> is given, then a message of the 420Evaluates the given string. If C<@reply> is given, then a message of the
405form C<@reply, $@, @evalres> is sent (C<$reply[0]> is the port to reply to). 421form C<@reply, $@, @evalres> is sent.
422
423Example: crash another node.
424
425 snd $othernode, eval => "exit";
406 426
407=cut 427=cut
408 428
409rcv "", eval => sub { 429rcv "", eval => sub {
410 my (undef, $string, @reply) = @_; 430 my (undef, $string, @reply) = @_;
411 my @res = eval $string; 431 my @res = eval $string;
412 snd @reply, "$@", @res if @reply; 432 snd @reply, "$@", @res if @reply;
433 ()
413}; 434};
414 435
415=item time => @reply 436=item time => @reply
416 437
417Replies the the current node time to C<@reply>. 438Replies the the current node time to C<@reply>.
418 439
419=cut 440Example: tell the current node to send the current time to C<$myport> in a
441C<timereply> message.
420 442
443 snd $NODE, time => $myport, timereply => 1, 2;
444 # => snd $myport, timereply => 1, 2, <time>
445
446=cut
447
421rcv "", time => sub { shift; snd @_, AE::time }; 448rcv "", time => sub { shift; snd @_, AE::time; () };
422 449
423=back 450=back
424 451
425=head1 SEE ALSO 452=head1 SEE ALSO
426 453

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines