/[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 14 by dpavlin, Tue Sep 22 10:40:32 2009 UTC revision 16 by dpavlin, Tue Sep 22 13:32:04 2009 UTC
# Line 71  our $cache; Line 71  our $cache;
71  our $connected;  our $connected;
72    
73  sub send_nodes {  sub send_nodes {
74          my $content = pop @_;          my $content = pop @_ if $#_ > 0;        # no content with just one argument!
75          my $header = length($content);          my $header = length($content);
76          $header .= ' ' . join(' ', @_) if @_;          $header .= ' ' . join(' ', @_) if @_;
77    
# Line 80  sub send_nodes { Line 80  sub send_nodes {
80                  my $sock = IO::Socket::INET->new(                  my $sock = IO::Socket::INET->new(
81                          PeerAddr => $node,                          PeerAddr => $node,
82                          Proto    => 'tcp',                          Proto    => 'tcp',
83                  ) or die "can't connect to $node - $!";                  );
84    
85                  print ">>>> $node $header\n";                  if ( ! $sock ) {
86                            warn "can't connect to $node - $!"; # FIXME die?
87                            next;
88                    }
89    
90                    print ">>>> $node $header\n";
91                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";
92    
93                  $connected->{$node} = $sock;                  $connected->{$node} = $sock;
94          }          }
95  }  }
96    
97    sub get_node {
98            my $node = shift;
99    
100            my $sock = $connected->{$node};
101            if ( ! $sock ) {
102                    warn "ERROR: lost connection to $node";
103                    delete $connected->{$node};
104                    return;
105            }
106            chomp( my $size = <$sock> );
107            warn "<<<< $node $size bytes\n";
108            my $data;
109            read $sock, $data, $size;
110            return $data;
111    }
112    
113    sub send_sock {
114            my ( $sock, $data ) = @_;
115            my $size   = length $data;
116            warn ">>>> ", $sock->peerhost, " $size bytes";
117            print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
118    }
119    
120  sub merge_out {  sub merge_out {
121          my $new = shift;          my $new = shift;
122    
# Line 153  sub run_code { Line 180  sub run_code {
180    
181          if ( $connected ) {          if ( $connected ) {
182                  warn "# get results from ", join(' ', keys %$connected );                  warn "# get results from ", join(' ', keys %$connected );
183                    merge_out( thaw( get_node( $_ ) ) ) foreach keys %$connected;
                 foreach my $node ( keys %$connected ) {  
                         my $sock = $connected->{$node};  
                         my $size = <$sock>;  
                         warn "<<<< $node $size bytes\n";  
                         my $part;  
                         read $sock, $part, $size;  
                         merge_out( thaw $part );  
                 }  
184          }          }
185  }  }
186    
# Line 215  if ( $listen ) { Line 234  if ( $listen ) {
234    
235                  my $client = $sock->accept();                  my $client = $sock->accept();
236    
237                  warn "<<<< connect from ", $client->peerhost, $/;                  warn "<<<< $listen connect from ", $client->peerhost, $/;
238    
239                  my @header = split(/\s/, <$client>);                  my @header = split(/\s/, <$client>);
240                  warn "# header ",dump @header;                  warn "# header ",dump @header;
# Line 227  if ( $listen ) { Line 246  if ( $listen ) {
246    
247                  if ( $header[0] eq 'view' ) {                  if ( $header[0] eq 'view' ) {
248                          run_code $header[1] => $content;                          run_code $header[1] => $content;
249                            send_sock $client => freeze $out;
250                          my $frozen = freeze $out;                  } elsif ( $header[0] eq 'info' ) {
251                          my $size   = length $frozen;                          my $info = "$listen\t$offset\t$limit\t$path";
252                          warn ">>>> $size bytes";                          warn "info $info\n";
253                          print $client "$size\n$frozen";                          send_sock $client => $info;
254                    } elsif ( $header[0] eq 'exit' ) {
255                            warn "exit $listen";
256                            exit;
257                  } else {                  } else {
258                          warn "WARN unknown";                          warn "WARN $listen unknown";
259                  }                  }
260    
261          }          }
# Line 250  while ( 1 ) { Line 271  while ( 1 ) {
271          if ( $cmd =~ m{^(vi?|\\e|o(?:ut)?)}i ) {          if ( $cmd =~ m{^(vi?|\\e|o(?:ut)?)}i ) {
272                  system "vi out/*";                  system "vi out/*";
273          } elsif ( $cmd =~ m{^i(nfo)?}i ) {          } elsif ( $cmd =~ m{^i(nfo)?}i ) {
274                  print "nodes: ", dump @nodes, $/;                  print "# nodes: ", join(' ',@nodes), $/;
275    
276                    my @info = (
277                            "node\toffset\tlimit\tpath",
278                            "----\t------\t-----\t----",
279                            "here\t$offset\t$limit\t$path",
280                    );
281    
282                    send_nodes 'info';
283                    push @info, get_node $_ foreach @nodes;
284    
285                    print "$_\n" foreach @info;
286    
287            } elsif ( $cmd =~ m{^(q(uit)|e(xit))}i ) {
288                    warn "# exit";
289                    send_nodes 'exit';
290                    exit;
291          } else {          } else {
292                  run_views;                  run_views;
293          }          }

Legend:
Removed from v.14  
changed lines
  Added in v.16

  ViewVC Help
Powered by ViewVC 1.1.26