/[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

trunk/experiments/protocol-v3/server.pl revision 173 by dpavlin, Tue Nov 3 17:28:57 2009 UTC trunk/lib/Sack/Server.pm revision 212 by dpavlin, Sat Nov 21 16:34:12 2009 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3    package Sack::Server;
4    
5  use warnings;  use warnings;
6  use strict;  use strict;
7    
# Line 9  use IO::Select; Line 11  use IO::Select;
11  use Data::Dump qw(dump);  use Data::Dump qw(dump);
12  use Storable qw();  use Storable qw();
13  use File::Slurp;  use File::Slurp;
14    use Cwd qw(abs_path);
15    
16  my @cloud = qw(localhost tab.lan llin.lan);  use lib '/srv/Sack/lib';
17    use Sack::Merge;
18  my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib';  use Sack::Server::HTTP;
19    
20    my @cloud;
21    my $cloud_path = $ENV{CLOUD} || "etc/cloud";
22    die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path;
23  @cloud = read_file $cloud_path;  @cloud = read_file $cloud_path;
24  @cloud = map { chomp $_; $_ } @cloud;  @cloud = map { chomp $_; $_ } @cloud;
25    
26  warn "# cloud ",dump( @cloud );  warn "# cloud ",dump( @cloud );
27    
28  my $listen_port = 4444;  my $listen_port = 4444;
29    my $http_port   = 4480;
30    
31  my $node_path = $0;  my $node_path = abs_path $0;
32  $node_path =~ s{server.pl}{client.pl};  $node_path =~ s{Server}{Client};
33    
34  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 $!";
35    my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port,   Reuse => 1) or die "$http_port $!";
36  my $sel = IO::Select->new($lsn);  my $sel = IO::Select->new($lsn);
37    $sel->add( $www );
38    
39  my $info;  my $info;
40  sub info {  sub info {
41          my $port = shift;          my $port = shift;
42          push @{ $info->{$port} }, [ @_ ];          push @{ $info->{node}->{$port} }, [ @_ ];
43  }  }
44    
45  sub fork_node {  sub fork_ssh {
46          my ( $port, $host ) = @_;          my ( $port, $host ) = @_;
47    
48          if ( my $pid = fork ) {          if ( my $pid = fork ) {
# Line 54  sub fork_node { Line 64  sub fork_node {
64  my $node_port = 4000;  my $node_port = 4000;
65    
66  foreach my $host ( @cloud ) {  foreach my $host ( @cloud ) {
67          system "echo $node_path | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional --verbose";          system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
68          fork_node( $node_port++, $host );          fork_ssh( $node_port++, $host );
69  }  }
70    
71  my $session;  my $session;
72    
73    sub to_all {
74            my $data = shift;
75            foreach my $port ( keys %{ $session->{port} } ) {
76                    warn ">>>> [$port]\n";
77                    Storable::store_fd( $data, $session->{port}->{$port} );
78            }
79    }
80    
81    my @shard_paths;
82    
83  while (1) {  while (1) {
84          for my $sock ($sel->can_read(1)) {          for my $sock ($sel->can_read(1)) {
85                  if ($sock == $lsn) {                  if ($sock == $lsn) {
86                          my $new = $lsn->accept;                          my $new = $lsn->accept;
87                          $sel->add($new);                          $sel->add($new);
88                          $session->{$new} = $new->peerport;                          $session->{peerport}->{ $new->peerport } = $new;
89                          warn "[socket] connect\n";                          warn "[socket] connect\n";
90                          Storable::store_fd( { ping => 1 }, $new );                          Storable::store_fd( { ping => 1 }, $new );
91                          info 0 => 'ping', $new->peerport;                          info 0 => 'ping', $new->peerport;
92                    } elsif ( $sock == $www ) {
93                            my $client = $www->accept;
94                            Sack::Server::HTTP::request( $client, sub {
95    warn "XXX callback ",dump( @_ );
96                                    my ( $send, $method, $param ) = @_;
97    
98                                    if ( $method =~ m{views} ) {
99                                            warn "$method ", -s $method, " bytes";
100                                            my $code = read_file $method;
101                                            Sack::Merge->clean;
102                                            to_all { view => $code, path => $method };
103                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
104                                            return;
105                                    } elsif ( $method =~ m{^/tmp/sack} ) {
106                                            @shard_paths = glob "$method/*";
107                                            warn "loading shard $method from ", dump( @shard_paths );
108                                            to_all { load => $method };
109                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
110                                            return;
111                                    }
112    
113                                    print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
114    
115                                    print $send qq|<h1>Views</h1><ul>|
116                                            , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/srv/Sack/views/*/*.pl' )
117                                            , qq|</ul>|
118                                            ;
119    
120                                    print $send '<h1>Info</h1><pre>' . dump( $info ) . '</pre>';
121    
122                                    print $send qq|<h1>Data</h1><ul>|
123                                            , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/tmp/sack/*' )
124                                            , qq|</ul>|
125                                            ;
126                            } );
127                  } else {                  } else {
128                          my $data = eval { Storable::fd_retrieve( $sock ) };                          my $data = eval { Storable::fd_retrieve( $sock ) };
129                          if ( $@ ) {                          if ( $@ ) {
# Line 78  while (1) { Line 133  while (1) {
133                                  $sock->close;                                  $sock->close;
134                          } else {                          } else {
135                                  warn "<<<< ", dump($data), $/;                                  warn "<<<< ", dump($data), $/;
136                                  if ( $data->{repl} ) {  
137                                          my $response = { repl => $$, info => $info };                                  if ( my $path = $data->{shard} ) {
138                                            $info->{shard}->{ $path } = $data->{port};
139                                            # FIXME will need push for multiple copies of shards
140                                    }
141    
142                                    if ( my $repl = $data->{repl} ) {
143                                            my $response = { repl_pid => $$ };
144                                            if ( $repl =~ m/ping/ ) {
145                                                    to_all { ping => 1 };
146                                            } elsif ( $repl =~ m/info/ ) {
147                                                    $response->{info} = $info;
148                                            } elsif ( $repl =~ m{load\s*(\S+)?} ) {
149                                                    my $name = $1 || 'shard';
150                                                    @shard_paths = glob "/tmp/sack/$name/*";
151                                                    warn "loading shard $name from ", dump( @shard_paths );
152                                                    to_all { load => $name };
153                                            } elsif ( $repl =~ m{view\s*(\S+)?} ) {
154                                                    my $path = $1 || '/srv/Sack/views/00.demo.pl';
155                                                    my $code = read_file $path;
156                                                    Sack::Merge->clean;
157                                                    to_all { view => $code, path => $path };
158                                                    $response->{view}->{$path}->{running};
159                                            } elsif ( $repl =~ m{debug\s*(.+)?} ) {
160                                                    to_all { debug => $1 };
161                                            } elsif ( $repl =~ m{out} ) {
162                                                    my $out = Sack::Merge->out;    
163                                                    warn "out ",dump( $out );
164                                                    $response->{out} = $out;
165                                            } elsif ( $repl =~ m{clean} ) {
166                                                    delete $info->{shard};
167                                                    to_all { clean => 1 };
168                                            } else {
169                                                    $response->{error}->{unknown} = $data;
170                                            }
171                                          Storable::store_fd( $response, $sock );                                          Storable::store_fd( $response, $sock );
172                                  } elsif ( $data->{ping} ) {                                  } elsif ( $data->{ping} ) {
173                                          $info->{_peer_port}->{$sock->peerport} = $data->{port};                                          my $port = $data->{port};
174                                          info $data->{port} => 'peer port', $sock->peerport;                                          info $port => 'ping', $port;
175                                            $session->{port}->{ $data->{port} } = $sock;
176                                    } elsif ( $data->{load} eq 'shard' ) {
177                                            if ( my $path = shift @shard_paths ) {
178                                                    warn "retrieve $path ", -s $path;
179                                                    my $shard = Storable::retrieve $path;
180                                                    warn ">>>> [", $data->{port}, "] sending shard $path\n";
181                                                    Storable::store_fd( { path => $path, shard => $shard }, $sock );
182                                            } else {
183                                                    warn "no more shards for [", $data->{port}, "]\n";
184                                            }
185                                    } elsif ( defined $data->{out} ) {
186                                            Sack::Merge->add( $data->{out} );
187                                    } else {
188                                            warn "UNKNOWN ",dump($data);
189                                  }                                  }
190                          }                          }
191                  }                  }

Legend:
Removed from v.173  
changed lines
  Added in v.212

  ViewVC Help
Powered by ViewVC 1.1.26