… | |
… | |
22 | =cut |
22 | =cut |
23 | |
23 | |
24 | package AnyEvent::MP::Kernel; |
24 | package AnyEvent::MP::Kernel; |
25 | |
25 | |
26 | use common::sense; |
26 | use common::sense; |
|
|
27 | use POSIX (); |
27 | use Carp (); |
28 | use Carp (); |
28 | use MIME::Base64 (); |
29 | use MIME::Base64 (); |
29 | |
30 | |
30 | use AE (); |
31 | use AE (); |
31 | |
32 | |
… | |
… | |
48 | |
49 | |
49 | our $CONNECT_INTERVAL = 2; # new connect every 2s, at least |
50 | our $CONNECT_INTERVAL = 2; # new connect every 2s, at least |
50 | our $NETWORK_LATENCY = 3; # activity timeout |
51 | our $NETWORK_LATENCY = 3; # activity timeout |
51 | our $MONITOR_TIMEOUT = 15; # fail monitoring after this time |
52 | our $MONITOR_TIMEOUT = 15; # fail monitoring after this time |
52 | |
53 | |
53 | =item $AnyEvent::MP::Kernel::WARN |
54 | =item $AnyEvent::MP::Kernel::WARN->($level, $msg) |
54 | |
55 | |
55 | This value is called with an error or warning message, when e.g. a connection |
56 | This value is called with an error or warning message, when e.g. a connection |
56 | could not be created, authorisation failed and so on. |
57 | could not be created, authorisation failed and so on. |
57 | |
58 | |
|
|
59 | C<$level> sould be C<0> for messages ot be logged always, C<1> for |
|
|
60 | unexpected messages and errors, C<2> for warnings, C<7> for messages about |
|
|
61 | node connectivity and services, C<8> for debugging messages and C<9> for |
|
|
62 | tracing messages. |
|
|
63 | |
58 | The default simply logs the message to STDERR. |
64 | The default simply logs the message to STDERR. |
59 | |
65 | |
60 | =cut |
66 | =cut |
61 | |
67 | |
62 | our $WARN = sub { |
68 | our $WARN = sub { |
63 | my $msg = $_[0]; |
69 | my ($level, $msg) = @_; |
|
|
70 | |
64 | $msg =~ s/\n$//; |
71 | $msg =~ s/\n$//; |
65 | warn "$msg\n"; |
72 | |
|
|
73 | printf STDERR "%s <%d> %s\n", |
|
|
74 | (POSIX::strftime "%Y-%m-%d %H:%M:%S", localtime time), |
|
|
75 | $level, |
|
|
76 | $msg; |
66 | }; |
77 | }; |
67 | |
78 | |
68 | sub load_func($) { |
79 | sub load_func($) { |
69 | my $func = $_[0]; |
80 | my $func = $_[0]; |
70 | |
81 | |
… | |
… | |
461 | |
472 | |
462 | wantarray && AnyEvent::Util::guard { delete $MON_NODES{$cb+0} } |
473 | wantarray && AnyEvent::Util::guard { delete $MON_NODES{$cb+0} } |
463 | } |
474 | } |
464 | |
475 | |
465 | sub _inject_nodeevent($$;@) { |
476 | sub _inject_nodeevent($$;@) { |
466 | my ($node, @args) = @_; |
477 | my ($node, $up, @reason) = @_; |
467 | |
|
|
468 | unshift @args, $node->{noderef}; |
|
|
469 | |
478 | |
470 | for my $cb (values %MON_NODES) { |
479 | for my $cb (values %MON_NODES) { |
471 | eval { $cb->(@args); 1 } |
480 | eval { $cb->($node->{noderef}, $up, @reason); 1 } |
472 | or $WARN->($@); |
481 | or $WARN->(1, $@); |
473 | } |
482 | } |
|
|
483 | |
|
|
484 | $WARN->(7, "$node->{noderef} is " . ($up ? "up" : "down") . " (@reason)"); |
474 | } |
485 | } |
475 | |
486 | |
476 | ############################################################################# |
487 | ############################################################################# |
477 | # self node code |
488 | # self node code |
478 | |
489 | |
… | |
… | |
547 | |
558 | |
548 | $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self noderef => $NODE; |
559 | $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self noderef => $NODE; |
549 | $PORT{""} = sub { |
560 | $PORT{""} = sub { |
550 | my $tag = shift; |
561 | my $tag = shift; |
551 | eval { &{ $node_req{$tag} ||= load_func $tag } }; |
562 | eval { &{ $node_req{$tag} ||= load_func $tag } }; |
552 | $WARN->("error processing node message: $@") if $@; |
563 | $WARN->(2, "error processing node message: $@") if $@; |
553 | }; |
564 | }; |
554 | |
565 | |
555 | =back |
566 | =back |
556 | |
567 | |
557 | =head1 SEE ALSO |
568 | =head1 SEE ALSO |