… | |
… | |
11 | This module provides most of the basic functionality of AnyEvent::MP, |
11 | This module provides most of the basic functionality of AnyEvent::MP, |
12 | exposed through higher level interfaces such as L<AnyEvent::MP> and |
12 | exposed through higher level interfaces such as L<AnyEvent::MP> and |
13 | L<Coro::MP>. |
13 | L<Coro::MP>. |
14 | |
14 | |
15 | This module is mainly of interest when knowledge about connectivity, |
15 | This module is mainly of interest when knowledge about connectivity, |
16 | connected nodes etc. are needed. |
16 | connected nodes etc. is sought. |
17 | |
17 | |
18 | =head1 GLOBALS AND FUNCTIONS |
18 | =head1 GLOBALS AND FUNCTIONS |
19 | |
19 | |
20 | =over 4 |
20 | =over 4 |
21 | |
21 | |
… | |
… | |
52 | our $NETWORK_LATENCY = 3; # activity timeout |
52 | our $NETWORK_LATENCY = 3; # activity timeout |
53 | our $MONITOR_TIMEOUT = 15; # fail monitoring after this time |
53 | our $MONITOR_TIMEOUT = 15; # fail monitoring after this time |
54 | |
54 | |
55 | =item $AnyEvent::MP::Kernel::WARN->($level, $msg) |
55 | =item $AnyEvent::MP::Kernel::WARN->($level, $msg) |
56 | |
56 | |
57 | This value is called with an error or warning message, when e.g. a connection |
57 | This value is called with an error or warning message, when e.g. a |
58 | could not be created, authorisation failed and so on. |
58 | connection could not be created, authorisation failed and so on. |
|
|
59 | |
|
|
60 | It I<must not> block or send messages -queue it and use an idle watcher if |
|
|
61 | you need to do any of these things. |
59 | |
62 | |
60 | C<$level> sould be C<0> for messages ot be logged always, C<1> for |
63 | C<$level> sould be C<0> for messages ot be logged always, C<1> for |
61 | unexpected messages and errors, C<2> for warnings, C<7> for messages about |
64 | unexpected messages and errors, C<2> for warnings, C<7> for messages about |
62 | node connectivity and services, C<8> for debugging messages and C<9> for |
65 | node connectivity and services, C<8> for debugging messages and C<9> for |
63 | tracing messages. |
66 | tracing messages. |
… | |
… | |
129 | } |
132 | } |
130 | |
133 | |
131 | sub gen_uniq { |
134 | sub gen_uniq { |
132 | alnumbits pack "wNa*", $$, time, nonce 2 |
135 | alnumbits pack "wNa*", $$, time, nonce 2 |
133 | } |
136 | } |
134 | |
|
|
135 | =item $AnyEvent::MP::Kernel::PUBLIC |
|
|
136 | |
|
|
137 | A boolean indicating whether this is a public node, which can create and |
|
|
138 | accept direct connections from other nodes. |
|
|
139 | |
|
|
140 | =cut |
|
|
141 | |
137 | |
142 | our $CONFIG; # this node's configuration |
138 | our $CONFIG; # this node's configuration |
143 | |
139 | |
144 | our $RUNIQ = alnumbits nonce 16;; # remote uniq value |
140 | our $RUNIQ = alnumbits nonce 16;; # remote uniq value |
145 | our $UNIQ = gen_uniq; # per-process/node unique cookie |
141 | our $UNIQ = gen_uniq; # per-process/node unique cookie |
… | |
… | |
407 | map $_->{id}, grep $_->{transport}, values %NODE |
403 | map $_->{id}, grep $_->{transport}, values %NODE |
408 | } |
404 | } |
409 | |
405 | |
410 | =item $guard = mon_nodes $callback->($nodeid, $is_up, @reason) |
406 | =item $guard = mon_nodes $callback->($nodeid, $is_up, @reason) |
411 | |
407 | |
412 | Registers a callback that is called each time a node goes up (connection |
408 | Registers a callback that is called each time a node goes up (a connection |
413 | is established) or down (connection is lost). |
409 | is established) or down (the connection is lost). |
414 | |
410 | |
415 | Node up messages can only be followed by node down messages for the same |
411 | Node up messages can only be followed by node down messages for the same |
416 | node, and vice versa. |
412 | node, and vice versa. |
417 | |
413 | |
|
|
414 | Note that monitoring a node is usually better done by monitoring it's node |
|
|
415 | port. This function is mainly of interest to modules that are concerned |
|
|
416 | about the network topology and low-level connection handling. |
|
|
417 | |
|
|
418 | Callbacks I<must not> block and I<should not> send any messages. |
|
|
419 | |
418 | The function returns an optional guard which can be used to de-register |
420 | The function returns an optional guard which can be used to unregister |
419 | the monitoring callback again. |
421 | the monitoring callback again. |
420 | |
422 | |
421 | =cut |
423 | =cut |
422 | |
424 | |
423 | our %MON_NODES; |
425 | our %MON_NODES; |
… | |
… | |
446 | |
448 | |
447 | our %node_req = ( |
449 | our %node_req = ( |
448 | # internal services |
450 | # internal services |
449 | |
451 | |
450 | # monitoring |
452 | # monitoring |
451 | mon0 => sub { # disable monitoring |
453 | mon0 => sub { # stop monitoring a port |
452 | my $portid = shift; |
454 | my $portid = shift; |
453 | my $node = $SRCNODE; |
455 | my $node = $SRCNODE; |
454 | $NODE{""}->unmonitor ($portid, delete $node->{rmon}{$portid}); |
456 | $NODE{""}->unmonitor ($portid, delete $node->{rmon}{$portid}); |
455 | }, |
457 | }, |
456 | mon1 => sub { # enable monitoring |
458 | mon1 => sub { # start monitoring a port |
457 | my $portid = shift; |
459 | my $portid = shift; |
458 | my $node = $SRCNODE; |
460 | my $node = $SRCNODE; |
|
|
461 | Scalar::Util::weaken $node; #TODO# ugly |
459 | $NODE{""}->monitor ($portid, $node->{rmon}{$portid} = sub { |
462 | $NODE{""}->monitor ($portid, $node->{rmon}{$portid} = sub { |
460 | $node->send (["", kil => $portid, @_]); |
463 | $node->send (["", kil => $portid, @_]) |
|
|
464 | if $node && $node->{transport}; #TODO# ugly, should use snd and remove-on-disocnnect |
461 | }); |
465 | }); |
462 | }, |
466 | }, |
463 | kil => sub { |
467 | kil => sub { |
464 | my $cbs = delete $SRCNODE->{lmon}{+shift} |
468 | my $cbs = delete $SRCNODE->{lmon}{+shift} |
465 | or return; |
469 | or return; |
… | |
… | |
469 | |
473 | |
470 | # "public" services - not actually public |
474 | # "public" services - not actually public |
471 | |
475 | |
472 | # relay message to another node / generic echo |
476 | # relay message to another node / generic echo |
473 | snd => \&snd, |
477 | snd => \&snd, |
474 | snd_multi => sub { |
478 | snd_multiple => sub { |
475 | snd @$_ for @_ |
479 | snd @$_ for @_ |
476 | }, |
480 | }, |
477 | |
481 | |
478 | # informational |
482 | # informational |
479 | info => sub { |
483 | info => sub { |
… | |
… | |
496 | }, |
500 | }, |
497 | devnull => sub { |
501 | devnull => sub { |
498 | # |
502 | # |
499 | }, |
503 | }, |
500 | "" => sub { |
504 | "" => sub { |
501 | # empty messages are sent by monitoring |
505 | # empty messages are keepalives or similar devnull-applications |
502 | }, |
506 | }, |
503 | ); |
507 | ); |
504 | |
508 | |
505 | $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self $NODE; |
509 | $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self $NODE; |
506 | $PORT{""} = sub { |
510 | $PORT{""} = sub { |