/[Sack]/trunk/lib/Sack/Server.pm
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/lib/Sack/Server.pm

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

revision 222 by dpavlin, Sun Nov 22 22:57:15 2009 UTC revision 232 by dpavlin, Mon Nov 23 23:15:31 2009 UTC
# Line 5  package Sack::Server; Line 5  package Sack::Server;
5  use warnings;  use warnings;
6  use strict;  use strict;
7    
8    my $debug = 0;
9    
10  use IO::Socket::INET;  use IO::Socket::INET;
11  use IO::Select;  use IO::Select;
12    
# Line 74  my $session; Line 76  my $session;
76  sub to_all {  sub to_all {
77          my $data = shift;          my $data = shift;
78          foreach my $port ( keys %{ $session->{port} } ) {          foreach my $port ( keys %{ $session->{port} } ) {
79                  warn ">>>> [$port]\n";                  warn ">>>> [$port]\n" if $debug;
80                  Storable::store_fd( $data, $session->{port}->{$port} );                  Storable::store_fd( $data, $session->{port}->{$port} );
81          }          }
82  }  }
# Line 99  sub load_shard { Line 101  sub load_shard {
101          to_all { load => $shard };          to_all { load => $shard };
102  }  }
103    
104    sub run_view {
105            my ( $path ) = @_;
106            if ( ! -r $path ) {
107                    warn "ERROR view $path: $!";
108                    return;
109            }
110            my $code = read_file $path;
111            Sack::Merge->clean;
112            delete( $info->{view} );
113            to_all { code => $code, view => $path };
114    };
115    
116  while (1) {  while (1) {
117          for my $sock ($sel->can_read(1)) {          for my $sock ($sel->can_read(1)) {
118                  if ($sock == $lsn) {                  if ($sock == $lsn) {
# Line 113  while (1) { Line 127  while (1) {
127                                  my ( $send, $method, $param ) = @_;                                  my ( $send, $method, $param ) = @_;
128    
129                                  if ( $method =~ m{views} ) {                                  if ( $method =~ m{views} ) {
130                                          warn "$method ", -s $method, " bytes";                                          run_view $method;
                                         my $code = read_file $method;  
                                         Sack::Merge->clean;  
                                         to_all { view => $code, path => $method };  
131                                          print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";                                          print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
132                                          return 1;                                          return 1;
133                                  } elsif ( $method =~ m{^/tmp/sack} ) {                                  } elsif ( $method =~ m{^/tmp/sack} ) {
# Line 131  while (1) { Line 142  while (1) {
142    
143                                  print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";                                  print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
144    
145                                  print $send qq|<h1>Views</h1><ul>|                                  print $send qq|
                                         , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/srv/Sack/views/*/*.pl' )  
                                         , qq|</ul>|  
                                         ;  
   
                                 my $out = Sack::Merge->out;      
                                 print $send qq|<h1>Results</h1><ul>|  
                                         , join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out )  
                                         , qq|</ul>|  
                                         ;  
146    
                                 print $send qq|<h1>Nodes</h1>  
147  <style type="text/css">  <style type="text/css">
148    
149    .running {
150            color: #f00;
151    }
152    
153    
154  .forked {  .forked {
155          color: #f00;          color: #f00;
156  }  }
# Line 168  while (1) { Line 175  while (1) {
175    
176    
177  </style>  </style>
178    
179    |;
180    
181                                    print $send qq|<h1>Views</h1><ul>|
182                                            , join("\n", map {
183                                                    my $view = $_;
184                                                    my $html = '';
185                                                    if ( my $s = $info->{view}->{$view} ) {
186                                                            my @nodes = sort keys %$s;
187                                                            my $total = 0;
188                                                            $html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>&sum;</th></tr>|;
189                                                            foreach my $node ( @nodes ) {
190                                                                    my $h = '';
191                                                                    my $shard = 0;
192                                                                    foreach my $path ( sort keys %{ $s->{$node} } ) {
193                                                                            my $affected = $s->{$node}->{$path};
194                                                                            $h .= qq|<span title="$affected $path">*</span>|;
195                                                                            $shard += $affected;
196                                                                            $total += $affected;
197                                                                    }
198                                                                    $html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td></tr>\n|;
199                                                            }
200    
201                                                            $html .= qq|</table>|;
202                                                    };
203                                                    qq|<li><a href="$view">$view</a>$html|
204                                            } glob '/srv/Sack/views/*/*.pl' )
205                                            , qq|</ul>|
206                                            ;
207    
208                                    my $out = Sack::Merge->out;    
209                                    print $send qq|<h1>Results</h1><ul>|
210                                            , join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out )
211                                            , qq|</ul>|
212                                            ;
213    
214                                    print $send qq|<h1>Nodes</h1>
215                                  |, join("\n", map {                                  |, join("\n", map {
216                                          my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} });                                          my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} });
217                                          qq|<span class="$class">$_</span>|;                                          qq|<span class="$class">$_</span>|;
# Line 184  while (1) { Line 228  while (1) {
228                                  }                                  }
229                                  print $send qq|</ul>|;                                  print $send qq|</ul>|;
230    
231  #                               print $send '<pre>', dump($info), '</pre>';                                  print $send '<pre>', dump($info), '</pre>' if $param->{debug};
232    
233                                    return 1;
234                          } );                          } );
235                  } else {                  } else {
236                          my $data = eval { Storable::fd_retrieve( $sock ) };                          my $data = eval { Storable::fd_retrieve( $sock ) };
# Line 195  while (1) { Line 240  while (1) {
240                                  $sel->remove($sock);                                  $sel->remove($sock);
241                                  $sock->close;                                  $sock->close;
242                          } else {                          } else {
243                                  warn "<<<< ", dump($data), $/;                                  warn "<<<< ", dump($data), $/ if $debug;
244    
245                                  if ( my $path = $data->{shard} ) {                                  if ( my $path = $data->{shard} ) {
246                                          $info->{shard}->{ $path } = $data->{port};                                          $info->{shard}->{ $path } = $data->{port};
# Line 211  while (1) { Line 256  while (1) {
256                                          } elsif ( $repl =~ m{load\s*(\S+)?} ) {                                          } elsif ( $repl =~ m{load\s*(\S+)?} ) {
257                                                  load_shard $1;                                                  load_shard $1;
258                                          } elsif ( $repl =~ m{view\s*(\S+)?} ) {                                          } elsif ( $repl =~ m{view\s*(\S+)?} ) {
259                                                  my $path = $1 || '/srv/Sack/views/00.demo.pl';                                                  run_view $1;
                                                 my $code = read_file $path;  
                                                 Sack::Merge->clean;  
                                                 to_all { view => $code, path => $path };  
                                                 $response->{view}->{$path}->{running};  
260                                          } elsif ( $repl =~ m{debug\s*(.+)?} ) {                                          } elsif ( $repl =~ m{debug\s*(.+)?} ) {
261                                                  to_all { debug => $1 };                                                  to_all { debug => $1 };
262                                          } elsif ( $repl =~ m{out} ) {                                          } elsif ( $repl =~ m{out} ) {
# Line 225  while (1) { Line 266  while (1) {
266                                          } elsif ( $repl =~ m{clean} ) {                                          } elsif ( $repl =~ m{clean} ) {
267                                                  delete $info->{shard};                                                  delete $info->{shard};
268                                                  to_all { clean => 1 };                                                  to_all { clean => 1 };
269                                            } elsif ( $repl eq 'exit' ) {
270                                                    to_all { exit => 1 };
271                                                    sleep 1;
272                                                    exit;
273                                          } else {                                          } else {
274                                                  $response->{error}->{unknown} = $data;                                                  $response->{error}->{unknown} = $data;
275                                          }                                          }
# Line 233  while (1) { Line 278  while (1) {
278                                          my $port = $data->{port};                                          my $port = $data->{port};
279                                          info $port => 'ping', $port;                                          info $port => 'ping', $port;
280                                          $session->{port}->{ $data->{port} } = $sock;                                          $session->{port}->{ $data->{port} } = $sock;
281                                  } elsif ( $data->{load} eq 'shard' ) {                                  } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
282                                          if ( my $path = shift @shard_load_queue ) {                                          if ( my $path = shift @shard_load_queue ) {
283                                                  $info->{shard}->{$path} = 'read';                                                  $info->{shard}->{$path} = 'read';
284                                                  my $shard = Storable::retrieve $path;                                                  my $shard = Storable::retrieve $path;
# Line 245  while (1) { Line 290  while (1) {
290                                          }                                          }
291                                  } elsif ( defined $data->{out} ) {                                  } elsif ( defined $data->{out} ) {
292                                          Sack::Merge->add( $data->{out} );                                          Sack::Merge->add( $data->{out} );
293                                            $info->{view}->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};
294                                  } else {                                  } else {
295                                          warn "UNKNOWN ",dump($data);                                          warn "UNKNOWN ",dump($data);
296                                  }                                  }

Legend:
Removed from v.222  
changed lines
  Added in v.232

  ViewVC Help
Powered by ViewVC 1.1.26