/[Sack]/trunk/bin/sack.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/sack.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 60 by dpavlin, Fri Sep 25 13:02:32 2009 UTC revision 64 by dpavlin, Fri Sep 25 14:01:31 2009 UTC
# Line 27  GetOptions( Line 27  GetOptions(
27          'offset=i' => \$offset,          'offset=i' => \$offset,
28          'limit=i'  => \$limit,          'limit=i'  => \$limit,
29          'view=s'   => \@views,          'view=s'   => \@views,
30          'listen|port=i' => \$port,          'port|listen=i' => \$port,
31          'connect=s'   => \@nodes,          'node|connect=i'   => \@nodes,
32          'debug!'   => \$debug,          'debug!'   => \$debug,
33  ) or die $!;  ) or die $!;
34    
# Line 91  our $cache; Line 91  our $cache;
91    
92  our $connected;  our $connected;
93    
94    sub node_sock {
95            my $node = shift;
96            my $sock = IO::Socket::INET->new(
97                            PeerAddr => '127.0.0.1',
98                            PeerPort => $node,
99                            Proto    => 'tcp',
100            );
101            
102            return $sock if $sock && $sock->connected;
103    
104            warn "[$port] can't connect to $node - $!\n"; # FIXME die?
105            return;
106    }
107    
108  sub send_nodes {  sub send_nodes {
109          my $content = $#_ > 0 ? pop @_ : '';    # no content with just one argument!          my $content = $#_ > 0 ? pop @_ : '';    # no content with just one argument!
110          my $header = defined $content ? length($content) : 0;          my $header = defined $content ? length($content) : 0;
# Line 100  sub send_nodes { Line 114  sub send_nodes {
114    
115          foreach my $node ( @nodes ) {          foreach my $node ( @nodes ) {
116    
117                  my $sock = IO::Socket::INET->new(                  my $sock = node_sock($node) || next;
                         PeerAddr => '127.0.0.1',  
                         PeerPort => $node,  
                         Proto    => 'tcp',  
                 );  
   
                 if ( ! $sock ) {  
                         warn "[$port] can't connect to $node - $!\n"; # FIXME die?  
                         next;  
                 }  
118    
119                  warn "[$port] >>>> $node $header\n";                  warn "[$port] >>>> $node $header\n";
120                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";
# Line 122  sub get_node { Line 127  sub get_node {
127          my $node = shift;          my $node = shift;
128    
129          my $sock = $connected->{$node};          my $sock = $connected->{$node};
130          if ( ! $sock ) {          if ( ! $sock || ! $sock->connected ) {
131                  warn "[$port] ERROR lost connection to $node";                  warn "[$port] no connection to $node";
132                  delete $connected->{$node};                  delete $connected->{$node};
133                  return;                  return;
134          }          }
# Line 141  sub send_sock { Line 146  sub send_sock {
146          print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;          print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
147  }  }
148    
149    sub pull_node_file {
150            my ( $node, $file ) = @_;
151    
152            my $path = "/dev/shm/sack.$node.$file";
153    #       return if -e $path; # FIXME
154    
155            my $sock = node_sock $node || die "not connected to $node";
156    
157            print $sock "0 file $file\n";
158    
159            my $size = <$sock>;
160            chomp($size);
161            warn "[$port] pull_node_file $node $file $size bytes\n";
162    
163            my $block = 4096;
164            my $buff  = ' ' x $block;
165    
166            open(my $fh, '>', $path) || die "can't open $path";
167            while ( read $sock, $buff, $block ) {
168                    print $fh $buff;
169            }
170            close($fh);
171    }
172    
173  sub merge_out {  sub merge_out {
174          my ( $from_node, $new ) = @_;          my ( $from_node, $new ) = @_;
175    
176            pull_node_file $from_node => 'nr_md5';
177            pull_node_file $from_node => 'md5';
178    
179          my $remote_digest = Sack::Digest->new( port => $from_node );          my $remote_digest = Sack::Digest->new( port => $from_node );
180          my ( $local, $remote ) = ( 0, 0 );          my ( $local, $remote ) = ( 0, 0 );
181    
182          my $tick = 0;          my $tick = 0;
183          print STDERR "[$port] merge $from_node";          print STDERR "[$port] merge $from_node";
184    
185            my $missing;
186    
187          foreach my $k1 ( keys %$new ) {          foreach my $k1 ( keys %$new ) {
188    
189                  foreach my $k2 ( keys %{ $new->{$k1} } ) {                  foreach my $k2 ( keys %{ $new->{$k1} } ) {
# Line 159  sub merge_out { Line 193  sub merge_out {
193                          if ( $k1 =~ m{#} ) {                          if ( $k1 =~ m{#} ) {
194                                  die "ASSERT $k1 $k2" unless $k2 =~ m{^\d+$};                                  die "ASSERT $k1 $k2" unless $k2 =~ m{^\d+$};
195  #warn "XXX $k1 $k2";  #warn "XXX $k1 $k2";
196                                  my $md5 = $remote_digest->{nr_md5}->[$k2] || warn "[$port] no2md5 $n not found in $from_node\n";                                  my $md5 = $remote_digest->{nr_md5}->[$k2];
197    
198                                    if ( ! $md5 ) {
199                                            $missing->{$from_node}++; # FIXME die?
200                                            next;
201                                    }
202    
203                                  if ( my $local_k2 = $digest->{md5_nr}->{$md5} ) {                                  if ( my $local_k2 = $digest->{md5_nr}->{$md5} ) {
204                                          $k2 = $local_k2;                                          $k2 = $local_k2;
205                                          $local++;                                          $local++;
# Line 198  sub merge_out { Line 238  sub merge_out {
238    
239          warn "$tick $local/$remote\n";          warn "$tick $local/$remote\n";
240    
241            warn "[$port] missing ", dump $missing if $missing;
242    
243          warn "## merge out ", dump $out if $debug;          warn "## merge out ", dump $out if $debug;
244  }  }
245    
# Line 242  sub run_code { Line 284  sub run_code {
284                  foreach my $node ( keys %$connected ) {                  foreach my $node ( keys %$connected ) {
285                          warn "[$port] get_node $node\n";                          warn "[$port] get_node $node\n";
286                          my $o = get_node $node;                          my $o = get_node $node;
287                            next unless $o;
288                          my $s = length $o;                          my $s = length $o;
289                          $o = thaw $o;                          $o = thaw $o;
290                          warn "[$port] merge $node $s bytes\n";                          warn "[$port] merge $node $s bytes\n";
# Line 330  if ( $port ) { Line 373  if ( $port ) {
373                  } elsif ( $header[0] eq 'exit' ) {                  } elsif ( $header[0] eq 'exit' ) {
374                          warn "[$port] exit";                          warn "[$port] exit";
375                          exit;                          exit;
376                    } elsif ( $header[0] eq 'file' ) {
377                            $digest->close;
378                            my $path = "/dev/shm/sack.$port.$header[1]";
379                            my $size = -s $path;
380                            warn "[$port] >>>> file $path $size bytes\n";
381                            print $client "$size\n";
382                            my $block = 4096;
383                            my $buff  = ' ' x $block;
384                            open(my $fh, '<', $path) || die "can't open $path";
385                            while ( read $fh, $buff, $block ) {
386                                    print $client $buff;
387                            }
388                  } else {                  } else {
389                          warn "[$port] UNKNOWN $header[0]";                          warn "[$port] UNKNOWN $header[0]";
390                  }                  }

Legend:
Removed from v.60  
changed lines
  Added in v.64

  ViewVC Help
Powered by ViewVC 1.1.26