… | |
… | |
31 | |
31 | |
32 | use base "Exporter"; |
32 | use base "Exporter"; |
33 | |
33 | |
34 | our $VERSION = '0.01'; |
34 | our $VERSION = '0.01'; |
35 | our @EXPORT = qw( |
35 | our @EXPORT = qw( |
36 | %NODE %PORT %PORT_DATA %REG $UNIQ $ID add_node |
36 | %NODE %PORT %PORT_DATA %REG $UNIQ $RUNIQ $ID add_node load_func |
37 | |
37 | |
38 | NODE $NODE node_of snd kil _any_ |
38 | NODE $NODE node_of snd kil _any_ |
39 | resolve_node initialise_node |
39 | resolve_node initialise_node |
40 | ); |
40 | ); |
41 | |
41 | |
… | |
… | |
110 | |
110 | |
111 | sub gen_uniq { |
111 | sub gen_uniq { |
112 | asciibits pack "wNa*", $$, time, nonce 2 |
112 | asciibits pack "wNa*", $$, time, nonce 2 |
113 | } |
113 | } |
114 | |
114 | |
115 | our $NODE = asciibits nonce 12; |
|
|
116 | our $UNIQ = gen_uniq; # per-process/node unique cookie |
|
|
117 | our $ID = "a"; |
|
|
118 | our $PUBLIC = 0; |
115 | our $PUBLIC = 0; |
119 | our $SLAVE = 0; |
116 | our $SLAVE = 0; |
|
|
117 | |
|
|
118 | our $NODE = asciibits nonce 16; |
|
|
119 | our $RUNIQ = $NODE; # remote uniq value |
|
|
120 | our $UNIQ = gen_uniq; # per-process/node unique cookie |
|
|
121 | our $ID = "a"; |
120 | |
122 | |
121 | our %NODE; # node id to transport mapping, or "undef", for local node |
123 | our %NODE; # node id to transport mapping, or "undef", for local node |
122 | our (%PORT, %PORT_DATA); # local ports |
124 | our (%PORT, %PORT_DATA); # local ports |
123 | |
125 | |
124 | our %RMON; # local ports monitored by remote nodes ($RMON{noderef}{portid} == cb) |
126 | our %RMON; # local ports monitored by remote nodes ($RMON{noderef}{portid} == cb) |
… | |
… | |
323 | } |
325 | } |
324 | |
326 | |
325 | ############################################################################# |
327 | ############################################################################# |
326 | # self node code |
328 | # self node code |
327 | |
329 | |
|
|
330 | sub load_func($) { |
|
|
331 | my $func = $_[0]; |
|
|
332 | |
|
|
333 | unless (defined &$func) { |
|
|
334 | my $pkg = $func; |
|
|
335 | do { |
|
|
336 | $pkg =~ s/::[^:]+$// |
|
|
337 | or return sub { die "unable to resolve $func" }; |
|
|
338 | eval "require $pkg"; |
|
|
339 | } until defined &$func; |
|
|
340 | } |
|
|
341 | |
|
|
342 | \&$func |
|
|
343 | } |
|
|
344 | |
328 | our %node_req = ( |
345 | our %node_req = ( |
329 | # internal services |
346 | # internal services |
330 | |
347 | |
331 | # monitoring |
348 | # monitoring |
332 | mon0 => sub { # disable monitoring |
349 | mon0 => sub { # disable monitoring |
… | |
… | |
357 | |
374 | |
358 | # well-known-port lookup |
375 | # well-known-port lookup |
359 | lookup => sub { |
376 | lookup => sub { |
360 | my $name = shift; |
377 | my $name = shift; |
361 | my $port = $REG{$name}; |
378 | my $port = $REG{$name}; |
362 | #TODO: check vailidity |
379 | #TODO: check validity |
363 | snd @_, $port; |
380 | snd @_, $port; |
364 | }, |
381 | }, |
365 | |
382 | |
366 | # relay message to another node / generic echo |
383 | # relay message to another node / generic echo |
367 | relay => sub { |
384 | relay => sub { |
368 | &snd; |
385 | &snd; |
|
|
386 | }, |
|
|
387 | relay_multiple => sub { |
|
|
388 | snd @$_ for @_ |
369 | }, |
389 | }, |
370 | |
390 | |
371 | # random garbage |
391 | # random garbage |
372 | eval => sub { |
392 | eval => sub { |
373 | my @res = eval shift; |
393 | my @res = eval shift; |
… | |
… | |
380 | # |
400 | # |
381 | }, |
401 | }, |
382 | ); |
402 | ); |
383 | |
403 | |
384 | $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self noderef => $NODE; |
404 | $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self noderef => $NODE; |
385 | $PORT{""} = sub { &{ $node_req{+shift} or return } }; |
405 | $PORT{""} = sub { |
|
|
406 | my $tag = shift; |
|
|
407 | eval { &{ $node_req{$tag} ||= load_func $tag } }; |
|
|
408 | $WARN->("error processing node message: $@") if $@; |
|
|
409 | }; |
386 | |
410 | |
387 | =back |
411 | =back |
388 | |
412 | |
389 | =head1 SEE ALSO |
413 | =head1 SEE ALSO |
390 | |
414 | |