… | |
… | |
27 | This module (-family) implements a simple message passing framework. |
27 | This module (-family) implements a simple message passing framework. |
28 | |
28 | |
29 | Despite its simplicity, you can securely message other processes running |
29 | Despite its simplicity, you can securely message other processes running |
30 | on the same or other hosts. |
30 | on the same or other hosts. |
31 | |
31 | |
|
|
32 | At the moment, this module family is severly brokena nd underdocumented, |
|
|
33 | so do not use. This was uploaded mainly to resreve the CPAN namespace - |
|
|
34 | stay 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 | |
53 | Initially, nodes are either private (single-process only) or hidden |
57 | Initially, 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 |
55 | public" can you send them messages from unrelated other nodes. |
59 | public" 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 | |
59 | A noderef is a string that either uniquely identifies a given node (for |
63 | A noderef is a string that either uniquely identifies a given node (for |
60 | private and hidden nodes), or contains a recipe on how to reach a given |
64 | private and hidden nodes), or contains a recipe on how to reach a given |
61 | node (for public nodes). |
65 | node (for public nodes). |
62 | |
66 | |
… | |
… | |
81 | |
85 | |
82 | use AE (); |
86 | use AE (); |
83 | |
87 | |
84 | use base "Exporter"; |
88 | use base "Exporter"; |
85 | |
89 | |
86 | our $VERSION = '0.0'; |
90 | our $VERSION = '0.01'; |
87 | our @EXPORT = qw(NODE $NODE $PORT snd rcv _any_); |
91 | our @EXPORT = qw(NODE $NODE $PORT snd rcv _any_); |
88 | |
92 | |
89 | our $DEFAULT_SECRET; |
93 | our $DEFAULT_SECRET; |
90 | our $DEFAULT_PORT = "4040"; |
94 | our $DEFAULT_PORT = "4040"; |
91 | |
95 | |
… | |
… | |
111 | C<become_slave>, after which all local port identifiers become invalid. |
115 | C<become_slave>, after which all local port identifiers become invalid. |
112 | |
116 | |
113 | =cut |
117 | =cut |
114 | |
118 | |
115 | our $UNIQ = sprintf "%x.%x", $$, time; # per-process/node unique cookie |
119 | our $UNIQ = sprintf "%x.%x", $$, time; # per-process/node unique cookie |
|
|
120 | our $ID = "a0"; |
116 | our $PUBLIC = 0; |
121 | our $PUBLIC = 0; |
117 | our $NODE; |
122 | our $NODE; |
118 | our $PORT; |
123 | our $PORT; |
119 | |
124 | |
120 | our %NODE; # node id to transport mapping, or "undef", for local node |
125 | our %NODE; # node id to transport mapping, or "undef", for local node |
… | |
… | |
172 | that Storable can serialise and deserialise is allowed, and for the local |
177 | that Storable can serialise and deserialise is allowed, and for the local |
173 | node, anything can be passed. |
178 | node, anything can be passed. |
174 | |
179 | |
175 | =cut |
180 | =cut |
176 | |
181 | |
177 | sub snd($@) { |
182 | sub snd(@) { |
178 | my ($noderef, $port) = split /#/, shift, 2; |
183 | my ($noderef, $port) = split /#/, shift, 2; |
179 | |
184 | |
180 | add_node $noderef |
185 | add_node $noderef |
181 | unless exists $NODE{$noderef}; |
186 | unless exists $NODE{$noderef}; |
182 | |
187 | |
… | |
… | |
362 | } |
367 | } |
363 | |
368 | |
364 | $PUBLIC = 1; |
369 | $PUBLIC = 1; |
365 | } |
370 | } |
366 | |
371 | |
|
|
372 | =back |
|
|
373 | |
|
|
374 | =head1 NODE MESSAGES |
|
|
375 | |
|
|
376 | Nodes understand the following messages sent to them. Many of them take |
|
|
377 | arguments called C<@reply>, which will simply be used to compose a reply |
|
|
378 | message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and |
|
|
379 | the remaining arguments are simply the message data. |
|
|
380 | |
|
|
381 | =over 4 |
|
|
382 | |
|
|
383 | =cut |
|
|
384 | |
367 | ############################################################################# |
385 | ############################################################################# |
368 | # self node code |
386 | # self node code |
369 | |
387 | |
370 | sub _new_port($) { |
388 | sub _new_port($) { |
371 | my ($name) = @_; |
389 | my ($name) = @_; |
… | |
… | |
379 | } |
397 | } |
380 | |
398 | |
381 | $NODE{""} = new AnyEvent::MP::Node::Self noderef => $NODE; |
399 | $NODE{""} = new AnyEvent::MP::Node::Self noderef => $NODE; |
382 | _new_port ""; |
400 | _new_port ""; |
383 | |
401 | |
|
|
402 | =item relay => $port, @msg |
|
|
403 | |
|
|
404 | Simply forwards the message to the given port. |
|
|
405 | |
|
|
406 | =cut |
|
|
407 | |
384 | rcv "", relay => \&snd; |
408 | rcv "", relay => \&snd; |
|
|
409 | |
|
|
410 | =item eval => $string[ @reply] |
|
|
411 | |
|
|
412 | Evaluates the given string. If C<@reply> is given, then a message of the |
|
|
413 | form C<@reply, $@, @evalres> is sent. |
|
|
414 | |
|
|
415 | Example: crash another node. |
|
|
416 | |
|
|
417 | snd $othernode, eval => "exit"; |
|
|
418 | |
|
|
419 | =cut |
|
|
420 | |
|
|
421 | rcv "", eval => sub { |
|
|
422 | my (undef, $string, @reply) = @_; |
|
|
423 | my @res = eval $string; |
|
|
424 | snd @reply, "$@", @res if @reply; |
|
|
425 | }; |
|
|
426 | |
|
|
427 | =item time => @reply |
|
|
428 | |
|
|
429 | Replies the the current node time to C<@reply>. |
|
|
430 | |
|
|
431 | Example: tell the current node to send the current time to C<$myport> in a |
|
|
432 | C<timereply> message. |
|
|
433 | |
|
|
434 | snd $NODE, time => $myport, timereply => 1, 2; |
|
|
435 | # => snd $myport, timereply => 1, 2, <time> |
|
|
436 | |
|
|
437 | =cut |
|
|
438 | |
|
|
439 | rcv "", time => sub { shift; snd @_, AE::time }; |
385 | |
440 | |
386 | =back |
441 | =back |
387 | |
442 | |
388 | =head1 SEE ALSO |
443 | =head1 SEE ALSO |
389 | |
444 | |