--- trunk/experiments/protocol-v3/server.pl 2009/11/01 21:03:26 165 +++ trunk/experiments/protocol-v3/server.pl 2009/11/03 18:02:33 175 @@ -8,20 +8,36 @@ use Data::Dump qw(dump); use Storable qw(); +use File::Slurp; + +my @cloud = qw(localhost tab.lan llin.lan); + +my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib'; +@cloud = read_file $cloud_path; +@cloud = map { chomp $_; $_ } @cloud; + +warn "# cloud ",dump( @cloud ); my $listen_port = 4444; -my $node_path = '/tmp/client.pl'; +my $node_path = $0; +$node_path =~ s{server.pl}{client.pl}; -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 $!; my $sel = IO::Select->new($lsn); +my $info; +sub info { + my $port = shift; + push @{ $info->{$port} }, [ @_ ]; +} + sub fork_node { my ( $port, $host ) = @_; if ( my $pid = fork ) { # parent - + info $port => 'forked', $pid; return $port; } elsif ( ! defined $pid ) { @@ -29,7 +45,7 @@ return; } else { # child - 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|; warn "# exec: $cmd\n"; exec $cmd; } @@ -37,8 +53,8 @@ my $node_port = 4000; -foreach my $host ( qw/localhost tab.lan llin.lan/ ) { - system "scp client.pl $host:$node_path"; +foreach my $host ( @cloud ) { + system "echo $node_path | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional --verbose"; fork_node( $node_port++, $host ); } @@ -49,9 +65,10 @@ if ($sock == $lsn) { my $new = $lsn->accept; $sel->add($new); - $session->{$new} = $new->peerhost; + $session->{peerport}->{ $new->peerport } = $new; warn "[socket] connect\n"; Storable::store_fd( { ping => 1 }, $new ); + info 0 => 'ping', $new->peerport; } else { my $data = eval { Storable::fd_retrieve( $sock ) }; if ( $@ ) { @@ -62,7 +79,20 @@ } else { warn "<<<< ", dump($data), $/; if ( $data->{repl} ) { - Storable::store_fd( { repl => $data }, $sock ); + my $response = { repl => $$ }; + if ( $data->{repl} =~ m/ping/ ) { + foreach my $port ( keys %{ $session->{port} } ) { + warn ">>>> [$port]\n"; + Storable::store_fd( { ping => 1 }, $session->{port}->{$port} ); + } + } elsif ( $data->{repl} =~ m/info/ ) { + $response->{info} = $info; + } + Storable::store_fd( $response, $sock ); + } elsif ( $data->{ping} ) { + my $port = $data->{port}; + info $port => 'ping', $port; + $session->{port}->{ $data->{port} } = $sock; } } }