line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cinnamon::HandleManager; |
2
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
87
|
|
3
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
87
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
13
|
use Cinnamon::Logger; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
110
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
12
|
use AnyEvent; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
27
|
|
8
|
2
|
|
|
2
|
|
2526
|
use AnyEvent::Handle; |
|
2
|
|
|
|
|
45558
|
|
|
2
|
|
|
|
|
83
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
78
|
use POSIX; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
22
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
0
|
|
|
0
|
0
|
|
my ($class, %args) = @_; |
14
|
0
|
|
|
|
|
|
bless \%args, $class; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub register_fh { |
18
|
0
|
|
|
0
|
0
|
|
my ($self, $name, $fh) = @_; |
19
|
0
|
|
0
|
|
|
|
my $handle_container = $self->{handle_container} ||= {}; |
20
|
0
|
|
|
|
|
|
$handle_container->{$name} = { |
21
|
|
|
|
|
|
|
fh => $fh, |
22
|
|
|
|
|
|
|
output_lines => [], |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub start_async_read { |
27
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
28
|
0
|
0
|
|
|
|
|
my $handle_container = $self->{handle_container} or return; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my $cv = AnyEvent->condvar; |
31
|
0
|
|
|
|
|
|
my $handles = []; |
32
|
0
|
|
|
|
|
|
for my $name (keys %$handle_container) { |
33
|
0
|
|
|
|
|
|
my $info = $handle_container->{$name}; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$cv->begin; |
36
|
0
|
|
|
|
|
|
my $handle; $handle = AnyEvent::Handle->new( |
37
|
|
|
|
|
|
|
fh => $info->{fh}, |
38
|
|
|
|
|
|
|
on_read => sub { |
39
|
|
|
|
|
|
|
$handle->push_read(line => qr|\r?\n|, sub { |
40
|
0
|
|
|
|
|
|
my $line = $_[1]; |
41
|
0
|
|
|
|
|
|
push @{$info->{output_lines}}, $line; |
|
0
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
log info => sprintf "[%s :: %s] %s", |
43
|
|
|
|
|
|
|
$self->{host}, $name, $line; |
44
|
0
|
|
|
0
|
|
|
}); |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
on_eof => sub { |
47
|
0
|
|
|
0
|
|
|
$cv->end; |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
on_error => sub { |
50
|
0
|
|
|
0
|
|
|
my $msg = $_[2]; |
51
|
0
|
0
|
|
|
|
|
log error => sprintf "[%s :: %s] %s", $self->{host}, $name, $msg |
52
|
|
|
|
|
|
|
unless $! == POSIX::EPIPE; |
53
|
0
|
|
|
|
|
|
$cv->end; |
54
|
|
|
|
|
|
|
}, |
55
|
0
|
|
|
|
|
|
); |
56
|
0
|
|
|
|
|
|
push @$handles, $handle; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
$cv->recv; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
for my $h (@$handles) { |
62
|
0
|
|
|
|
|
|
$h->destroy; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub captured_str { |
67
|
0
|
|
|
0
|
0
|
|
my ($self, $name) = @_; |
68
|
0
|
0
|
|
|
|
|
my $hinfo = $self->{handle_container}->{$name} or return ''; |
69
|
0
|
|
|
|
|
|
my $str = join("\n", @{$hinfo->{output_lines}}); |
|
0
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return $str; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |