3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
|
our $VERSION = '0.08'; |
7 |
|
|
8 |
use IO::Socket::INET; |
use IO::Socket::INET; |
9 |
use Data::Dump qw(dump); |
use Data::Dump qw(dump); |
10 |
use Storable; |
use Storable; |
22 |
sub new { |
sub new { |
23 |
my $class = shift; |
my $class = shift; |
24 |
my $self = bless {@_}, $class; |
my $self = bless {@_}, $class; |
25 |
|
$self->{sock} = {}; |
26 |
|
warn __PACKAGE__, " $VERSION\n"; |
27 |
return $self; |
return $self; |
28 |
} |
} |
29 |
|
|
30 |
|
sub connected { |
31 |
|
sort keys %{ $_[0]->{sock} } |
32 |
|
} |
33 |
|
|
34 |
|
sub connect_to { |
35 |
|
my ( $self, $port ) = @_; |
36 |
|
|
37 |
|
my $sock; |
38 |
|
|
39 |
|
print STDERR "waiting for $port"; |
40 |
|
|
41 |
|
while ( ! $sock ) { |
42 |
|
|
43 |
|
$sock = IO::Socket::INET->new( |
44 |
|
PeerAddr => '127.0.0.1', |
45 |
|
PeerPort => $port, |
46 |
|
Proto => 'tcp', |
47 |
|
); |
48 |
|
|
49 |
|
if ( ! $sock ) { |
50 |
|
print STDERR "."; |
51 |
|
sleep 1; |
52 |
|
} |
53 |
|
|
54 |
|
} |
55 |
|
|
56 |
|
$self->{sock}->{$port} = $sock; |
57 |
|
|
58 |
|
warn "OK connected to $port\n"; |
59 |
|
|
60 |
|
return $port; |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
sub start_node { |
sub start_node { |
65 |
my ( $self, $host ) = @_; |
my ( $self, $host ) = @_; |
66 |
|
|
82 |
$pids->{ "$host:$port" } = $pid; |
$pids->{ "$host:$port" } = $pid; |
83 |
$ports->{ $port } = $host; |
$ports->{ $port } = $host; |
84 |
|
|
85 |
my $sock; |
$self->connect_to( $port ); |
|
|
|
|
print STDERR "waiting for $port"; |
|
|
|
|
|
while ( ! $sock ) { |
|
|
|
|
|
$sock = IO::Socket::INET->new( |
|
|
PeerAddr => '127.0.0.1', |
|
|
PeerPort => $port, |
|
|
Proto => 'tcp', |
|
|
); |
|
86 |
|
|
87 |
if ( ! $sock ) { |
$self->{port_on_host}->{$port} = $host; |
|
print STDERR "."; |
|
|
sleep 1; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
$self->{sock}->{$port} = $sock; |
|
|
|
|
|
warn "# connected to $port\n"; |
|
|
|
|
|
$self->{connected}->{$port} = $host; |
|
88 |
|
|
89 |
return $port++; |
return $port++; |
90 |
|
|
126 |
|
|
127 |
sub send_to_all { |
sub send_to_all { |
128 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
129 |
$self->send_to( $_, $data ) foreach sort keys %{ $self->{connected} }; |
$self->send_to( $_, $data ) foreach $self->connected; |
130 |
} |
} |
131 |
|
|
132 |
sub get_from_all { |
sub get_from_all { |
133 |
my ( $self ) = @_; |
my ( $self ) = @_; |
134 |
my $result; |
my $result; |
135 |
$result->{$_} = $self->get_from( $_ ) foreach sort keys %{ $self->{connected} }; |
$result->{$_} = $self->get_from( $_ ) foreach $self->connected; |
136 |
return $result; |
return $result; |
137 |
} |
} |
138 |
|
|
139 |
|
sub restart_nodes { |
140 |
|
my ( $self ) = @_; |
141 |
|
foreach my $port ( $self->connected ) { |
142 |
|
warn "restart [$port]\n"; |
143 |
|
$self->send_to( $port, { restart => 1 } ); |
144 |
|
$self->connect_to( $port ); |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
our $out; |
our $out; |
150 |
|
|
230 |
|
|
231 |
my $total; |
my $total; |
232 |
|
|
233 |
foreach my $port ( keys %{ $self->{connected} } ) { |
foreach my $port ( $self->connected ) { |
234 |
my $result = $self->get_from( $port ); |
my $result = $self->get_from( $port ); |
235 |
warn "# result ", dump $result if $self->{debug}; |
warn "# result ", dump $result if $self->{debug}; |
236 |
if ( my $out = delete $result->{out} ) { |
if ( my $out = delete $result->{out} ) { |