ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-MP/MP/Kernel.pm
(Generate patch)

Comparing cvsroot/AnyEvent-MP/MP/Kernel.pm (file contents):
Revision 1.26 by root, Fri Aug 28 21:01:02 2009 UTC vs.
Revision 1.27 by root, Fri Aug 28 23:06:33 2009 UTC

11This module provides most of the basic functionality of AnyEvent::MP, 11This module provides most of the basic functionality of AnyEvent::MP,
12exposed through higher level interfaces such as L<AnyEvent::MP> and 12exposed through higher level interfaces such as L<AnyEvent::MP> and
13L<Coro::MP>. 13L<Coro::MP>.
14 14
15This module is mainly of interest when knowledge about connectivity, 15This module is mainly of interest when knowledge about connectivity,
16connected nodes etc. are needed. 16connected 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
52our $NETWORK_LATENCY = 3; # activity timeout 52our $NETWORK_LATENCY = 3; # activity timeout
53our $MONITOR_TIMEOUT = 15; # fail monitoring after this time 53our $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
57This value is called with an error or warning message, when e.g. a connection 57This value is called with an error or warning message, when e.g. a
58could not be created, authorisation failed and so on. 58connection could not be created, authorisation failed and so on.
59
60It I<must not> block or send messages -queue it and use an idle watcher if
61you need to do any of these things.
59 62
60C<$level> sould be C<0> for messages ot be logged always, C<1> for 63C<$level> sould be C<0> for messages ot be logged always, C<1> for
61unexpected messages and errors, C<2> for warnings, C<7> for messages about 64unexpected messages and errors, C<2> for warnings, C<7> for messages about
62node connectivity and services, C<8> for debugging messages and C<9> for 65node connectivity and services, C<8> for debugging messages and C<9> for
63tracing messages. 66tracing messages.
129} 132}
130 133
131sub gen_uniq { 134sub 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
137A boolean indicating whether this is a public node, which can create and
138accept direct connections from other nodes.
139
140=cut
141 137
142our $CONFIG; # this node's configuration 138our $CONFIG; # this node's configuration
143 139
144our $RUNIQ = alnumbits nonce 16;; # remote uniq value 140our $RUNIQ = alnumbits nonce 16;; # remote uniq value
145our $UNIQ = gen_uniq; # per-process/node unique cookie 141our $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
412Registers a callback that is called each time a node goes up (connection 408Registers a callback that is called each time a node goes up (a connection
413is established) or down (connection is lost). 409is established) or down (the connection is lost).
414 410
415Node up messages can only be followed by node down messages for the same 411Node up messages can only be followed by node down messages for the same
416node, and vice versa. 412node, and vice versa.
417 413
414Note that monitoring a node is usually better done by monitoring it's node
415port. This function is mainly of interest to modules that are concerned
416about the network topology and low-level connection handling.
417
418Callbacks I<must not> block and I<should not> send any messages.
419
418The function returns an optional guard which can be used to de-register 420The function returns an optional guard which can be used to unregister
419the monitoring callback again. 421the monitoring callback again.
420 422
421=cut 423=cut
422 424
423our %MON_NODES; 425our %MON_NODES;
446 448
447our %node_req = ( 449our %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 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines