8 |
|
|
9 |
use Data::Dump qw(dump); |
use Data::Dump qw(dump); |
10 |
use Storable qw(); |
use Storable qw(); |
11 |
|
use File::Slurp; |
12 |
|
|
13 |
|
my @cloud = qw(localhost tab.lan llin.lan); |
14 |
|
|
15 |
|
my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib'; |
16 |
|
@cloud = read_file $cloud_path; |
17 |
|
@cloud = map { chomp $_; $_ } @cloud; |
18 |
|
|
19 |
|
warn "# cloud ",dump( @cloud ); |
20 |
|
|
21 |
my $listen_port = 4444; |
my $listen_port = 4444; |
22 |
|
|
23 |
my $node_path = '/tmp/client.pl'; |
my $node_path = $0; |
24 |
|
$node_path =~ s{server.pl}{client.pl}; |
25 |
|
|
26 |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or exit; |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!; |
27 |
my $sel = IO::Select->new($lsn); |
my $sel = IO::Select->new($lsn); |
28 |
|
|
29 |
|
my $info; |
30 |
|
sub info { |
31 |
|
my $port = shift; |
32 |
|
push @{ $info->{$port} }, [ @_ ]; |
33 |
|
} |
34 |
|
|
35 |
sub fork_node { |
sub fork_node { |
36 |
my ( $port, $host ) = @_; |
my ( $port, $host ) = @_; |
37 |
|
|
38 |
if ( my $pid = fork ) { |
if ( my $pid = fork ) { |
39 |
# parent |
# parent |
40 |
|
info $port => 'forked', $pid; |
41 |
return $port; |
return $port; |
42 |
|
|
43 |
} elsif ( ! defined $pid ) { |
} elsif ( ! defined $pid ) { |
45 |
return; |
return; |
46 |
} else { |
} else { |
47 |
# child |
# child |
48 |
my $cmd = qq|ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|; |
my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|; |
49 |
warn "# exec: $cmd\n"; |
warn "# exec: $cmd\n"; |
50 |
exec $cmd; |
exec $cmd; |
51 |
} |
} |
53 |
|
|
54 |
my $node_port = 4000; |
my $node_port = 4000; |
55 |
|
|
56 |
foreach my $host ( qw/localhost tab.lan llin.lan/ ) { |
foreach my $host ( @cloud ) { |
57 |
system "scp client.pl $host:$node_path"; |
system "echo $node_path | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional --verbose"; |
58 |
fork_node( $node_port++, $host ); |
fork_node( $node_port++, $host ); |
59 |
} |
} |
60 |
|
|
65 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
66 |
my $new = $lsn->accept; |
my $new = $lsn->accept; |
67 |
$sel->add($new); |
$sel->add($new); |
68 |
$session->{$new} = $new->peerhost; |
$session->{$new} = $new->peerport; |
69 |
warn "[socket] connect from ", $new->peerhost, "\n"; |
warn "[socket] connect\n"; |
70 |
Storable::store_fd( { ping => 1 }, $new ); |
Storable::store_fd( { ping => 1 }, $new ); |
71 |
|
info 0 => 'ping', $new->peerport; |
72 |
} else { |
} else { |
73 |
if ( $sock->connected ) { |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
74 |
my $data = Storable::fd_retrieve( $sock ); |
if ( $@ ) { |
|
warn "<<<< ", dump($data), $/; |
|
|
} else { |
|
|
$session->{$sock}->close(); |
|
75 |
delete $session->{$sock}; |
delete $session->{$sock}; |
76 |
warn "[socket] disconnect\n"; |
warn "[socket] disconnect: $@\n"; |
77 |
$sel->remove($sock); |
$sel->remove($sock); |
78 |
$sock->close; |
$sock->close; |
79 |
|
} else { |
80 |
|
warn "<<<< ", dump($data), $/; |
81 |
|
if ( $data->{repl} ) { |
82 |
|
my $response = { repl => $$, info => $info }; |
83 |
|
Storable::store_fd( $response, $sock ); |
84 |
|
} elsif ( $data->{ping} ) { |
85 |
|
$info->{_peer_port}->{$sock->peerport} = $data->{port}; |
86 |
|
info $data->{port} => 'peer port', $sock->peerport; |
87 |
|
} |
88 |
} |
} |
89 |
} |
} |
90 |
} |
} |