/[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 189 by dpavlin, Sun Nov 8 13:44:10 2009 UTC revision 219 by dpavlin, Sun Nov 22 21:54:25 2009 UTC
# Line 13  use Storable qw(); Line 13  use Storable qw();
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    use Sack::Server::HTTP;
19    use Sack::Server::HTML;
20    
21  my @cloud;  my @cloud;
22  my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n";  my $cloud_path = $ENV{CLOUD} || "etc/cloud";
23    die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path;
24  @cloud = read_file $cloud_path;  @cloud = read_file $cloud_path;
25  @cloud = map { chomp $_; $_ } @cloud;  @cloud = map { chomp $_; $_ } @cloud;
26    
27  warn "# cloud ",dump( @cloud );  warn "# cloud ",dump( @cloud );
28    
29  my $listen_port = 4444;  my $listen_port = 4444;
30    my $http_port   = 4480;
31    
32  my $node_path = abs_path $0;  my $node_path = abs_path $0;
33  $node_path =~ s{Server}{Client};  $node_path =~ s{Server}{Client};
34    
35  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 "$listen_port $!";
36    my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port,   Reuse => 1) or die "$http_port $!";
37  my $sel = IO::Select->new($lsn);  my $sel = IO::Select->new($lsn);
38    $sel->add( $www );
39    
40  my $info;  my $info;
41  sub info {  sub info {
42          my $port = shift;          my $port = shift;
43          push @{ $info->{$port} }, [ @_ ];          push @{ $info->{node}->{$port} }, [ @_ ];
44  }  }
45    
46  sub fork_ssh {  sub fork_ssh {
# Line 70  sub to_all { Line 79  sub to_all {
79          }          }
80  }  }
81    
82    my @shard_paths;
83    
84  while (1) {  while (1) {
85          for my $sock ($sel->can_read(1)) {          for my $sock ($sel->can_read(1)) {
86                  if ($sock == $lsn) {                  if ($sock == $lsn) {
# Line 78  while (1) { Line 89  while (1) {
89                          $session->{peerport}->{ $new->peerport } = $new;                          $session->{peerport}->{ $new->peerport } = $new;
90                          warn "[socket] connect\n";                          warn "[socket] connect\n";
91                          Storable::store_fd( { ping => 1 }, $new );                          Storable::store_fd( { ping => 1 }, $new );
92                          info 0 => 'ping', $new->peerport;                  } elsif ( $sock == $www ) {
93                            my $client = $www->accept;
94                            Sack::Server::HTTP::request( $client, sub {
95                                    my ( $send, $method, $param ) = @_;
96    
97                                    if ( $method =~ m{views} ) {
98                                            warn "$method ", -s $method, " bytes";
99                                            my $code = read_file $method;
100                                            Sack::Merge->clean;
101                                            to_all { view => $code, path => $method };
102                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
103                                            return 1;
104                                    } elsif ( $method =~ m{^/tmp/sack} ) {
105                                            @shard_paths = glob "$method/*";
106                                            warn "loading shard $method from ", dump( @shard_paths );
107                                            to_all { load => $method };
108                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
109                                            return 1;
110                                    } elsif ( $method =~ m{^/out/(.+)} ) {
111                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
112                                            Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
113                                            return 1;
114                                    }
115    
116                                    print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
117    
118                                    print $send qq|<h1>Views</h1><ul>|
119                                            , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/srv/Sack/views/*/*.pl' )
120                                            , qq|</ul>|
121                                            ;
122    
123                                    my $out = Sack::Merge->out;    
124                                    print $send qq|<h1>Results</h1><ul>|
125                                            , join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out )
126                                            , qq|</ul>|
127                                            ;
128    
129                                    print $send qq|<h1>Nodes</h1>
130    <style type="text/css">
131    .forked {
132            color: #f00;
133    }
134    
135    .ping {
136            color: #0f0;
137    }
138    </style>
139                                    |, join("\n", map {
140                                            my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} });
141                                            qq|<span class="$class">$_</span>|;
142                                    } sort keys %{ $info->{node} } );
143    
144                                    print $send qq|<h1>Data</h1><ul>|
145                                            , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/tmp/sack/*' )
146                                            , qq|</ul>|
147                                            ;
148    
149                                    print $send '<pre>', dump($info), '</pre>';
150    
151                            } );
152                  } else {                  } else {
153                          my $data = eval { Storable::fd_retrieve( $sock ) };                          my $data = eval { Storable::fd_retrieve( $sock ) };
154                          if ( $@ ) {                          if ( $@ ) {
# Line 88  while (1) { Line 158  while (1) {
158                                  $sock->close;                                  $sock->close;
159                          } else {                          } else {
160                                  warn "<<<< ", dump($data), $/;                                  warn "<<<< ", dump($data), $/;
161                                  if ( $data->{repl} ) {  
162                                          my $response = { repl => $$ };                                  if ( my $path = $data->{shard} ) {
163                                          if ( $data->{repl} =~ m/ping/ ) {                                          $info->{shard}->{ $path } = $data->{port};
164                                            # FIXME will need push for multiple copies of shards
165                                    }
166    
167                                    if ( my $repl = $data->{repl} ) {
168                                            my $response = { repl_pid => $$ };
169                                            if ( $repl =~ m/ping/ ) {
170                                                  to_all { ping => 1 };                                                  to_all { ping => 1 };
171                                          } elsif ( $data->{repl} =~ m/info/ ) {                                          } elsif ( $repl =~ m/info/ ) {
172                                                  $response->{info} = $info;                                                  $response->{info} = $info;
173                                            } elsif ( $repl =~ m{load\s*(\S+)?} ) {
174                                                    my $name = $1 || 'shard';
175                                                    @shard_paths = glob "/tmp/sack/$name/*";
176                                                    warn "loading shard $name from ", dump( @shard_paths );
177                                                    to_all { load => $name };
178                                            } elsif ( $repl =~ m{view\s*(\S+)?} ) {
179                                                    my $path = $1 || '/srv/Sack/views/00.demo.pl';
180                                                    my $code = read_file $path;
181                                                    Sack::Merge->clean;
182                                                    to_all { view => $code, path => $path };
183                                                    $response->{view}->{$path}->{running};
184                                            } elsif ( $repl =~ m{debug\s*(.+)?} ) {
185                                                    to_all { debug => $1 };
186                                            } elsif ( $repl =~ m{out} ) {
187                                                    my $out = Sack::Merge->out;    
188                                                    warn "out ",dump( $out );
189                                                    $response->{out} = $out;
190                                            } elsif ( $repl =~ m{clean} ) {
191                                                    delete $info->{shard};
192                                                    to_all { clean => 1 };
193                                          } else {                                          } else {
194                                                  $response->{error}->{unknown} = $data;                                                  $response->{error}->{unknown} = $data;
195                                          }                                          }
# Line 102  while (1) { Line 198  while (1) {
198                                          my $port = $data->{port};                                          my $port = $data->{port};
199                                          info $port => 'ping', $port;                                          info $port => 'ping', $port;
200                                          $session->{port}->{ $data->{port} } = $sock;                                          $session->{port}->{ $data->{port} } = $sock;
201                                    } elsif ( $data->{load} eq 'shard' ) {
202                                            if ( my $path = shift @shard_paths ) {
203                                                    warn "retrieve $path ", -s $path;
204                                                    my $shard = Storable::retrieve $path;
205                                                    warn ">>>> [", $data->{port}, "] sending shard $path\n";
206                                                    Storable::store_fd( { path => $path, shard => $shard }, $sock );
207                                            } else {
208                                                    warn "no more shards for [", $data->{port}, "]\n";
209                                            }
210                                    } elsif ( defined $data->{out} ) {
211                                            Sack::Merge->add( $data->{out} );
212                                    } else {
213                                            warn "UNKNOWN ",dump($data);
214                                  }                                  }
215                          }                          }
216                  }                  }

Legend:
Removed from v.189  
changed lines
  Added in v.219

  ViewVC Help
Powered by ViewVC 1.1.26