13 |
use File::Slurp; |
use File::Slurp; |
14 |
use Cwd qw(abs_path); |
use Cwd qw(abs_path); |
15 |
|
|
16 |
|
use lib '/srv/Sack/lib'; |
17 |
|
use Sack::Merge; |
18 |
|
|
19 |
my @cloud; |
my @cloud; |
20 |
my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n"; |
my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n"; |
21 |
@cloud = read_file $cloud_path; |
@cloud = read_file $cloud_path; |
26 |
my $listen_port = 4444; |
my $listen_port = 4444; |
27 |
|
|
28 |
my $node_path = abs_path $0; |
my $node_path = abs_path $0; |
29 |
$node_path =~ s{server.pl}{client.pl}; |
$node_path =~ s{Server}{Client}; |
30 |
|
|
31 |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!; |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!; |
32 |
my $sel = IO::Select->new($lsn); |
my $sel = IO::Select->new($lsn); |
65 |
|
|
66 |
my $session; |
my $session; |
67 |
|
|
68 |
|
sub to_all { |
69 |
|
my $data = shift; |
70 |
|
foreach my $port ( keys %{ $session->{port} } ) { |
71 |
|
warn ">>>> [$port]\n"; |
72 |
|
Storable::store_fd( $data, $session->{port}->{$port} ); |
73 |
|
} |
74 |
|
} |
75 |
|
|
76 |
|
my @shard_paths; |
77 |
|
|
78 |
while (1) { |
while (1) { |
79 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
80 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
93 |
$sock->close; |
$sock->close; |
94 |
} else { |
} else { |
95 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/; |
96 |
if ( $data->{repl} ) { |
|
97 |
my $response = { repl => $$ }; |
if ( my $path = $data->{shard} ) { |
98 |
if ( $data->{repl} =~ m/ping/ ) { |
push @{ $info->{shard}->{ $data->{port} } }, $path; |
99 |
foreach my $port ( keys %{ $session->{port} } ) { |
} |
100 |
warn ">>>> [$port]\n"; |
|
101 |
Storable::store_fd( { ping => 1 }, $session->{port}->{$port} ); |
if ( my $repl = $data->{repl} ) { |
102 |
} |
my $response = { repl_pid => $$ }; |
103 |
} elsif ( $data->{repl} =~ m/info/ ) { |
if ( $repl =~ m/ping/ ) { |
104 |
|
to_all { ping => 1 }; |
105 |
|
} elsif ( $repl =~ m/info/ ) { |
106 |
$response->{info} = $info; |
$response->{info} = $info; |
107 |
|
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
108 |
|
my $name = $1 || 'shard'; |
109 |
|
@shard_paths = glob "/tmp/sack/$name/*"; |
110 |
|
warn "loading shard $name from ", dump( @shard_paths ); |
111 |
|
to_all { load => $name }; |
112 |
|
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
113 |
|
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
114 |
|
my $code = read_file $path; |
115 |
|
Sack::Merge->clean; |
116 |
|
to_all { view => $code, path => $path }; |
117 |
|
$response->{view}->{$path}->{running}; |
118 |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
119 |
|
to_all { debug => $1 }; |
120 |
|
} elsif ( $repl =~ m{out} ) { |
121 |
|
my $out = Sack::Merge->out; |
122 |
|
warn "out ",dump( $out ); |
123 |
|
$response->{out} = $out; |
124 |
|
} elsif ( $repl =~ m{clean} ) { |
125 |
|
delete $info->{shard}; |
126 |
|
to_all { clean => 1 }; |
127 |
|
} else { |
128 |
|
$response->{error}->{unknown} = $data; |
129 |
} |
} |
130 |
Storable::store_fd( $response, $sock ); |
Storable::store_fd( $response, $sock ); |
131 |
} elsif ( $data->{ping} ) { |
} elsif ( $data->{ping} ) { |
132 |
my $port = $data->{port}; |
my $port = $data->{port}; |
133 |
info $port => 'ping', $port; |
info $port => 'ping', $port; |
134 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
135 |
|
} elsif ( $data->{load} eq 'shard' ) { |
136 |
|
if ( my $path = shift @shard_paths ) { |
137 |
|
warn "retrieve $path ", -s $path; |
138 |
|
my $shard = Storable::retrieve $path; |
139 |
|
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
140 |
|
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
141 |
|
} else { |
142 |
|
warn "no more shards for [", $data->{port}, "]\n"; |
143 |
|
} |
144 |
|
} elsif ( defined $data->{out} ) { |
145 |
|
Sack::Merge->add( $data->{out} ); |
146 |
|
} else { |
147 |
|
warn "UNKNOWN ",dump($data); |
148 |
} |
} |
149 |
} |
} |
150 |
} |
} |