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

Contents of /trunk/lib/Sack/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 188 - (show annotations)
Sun Nov 8 13:40:58 2009 UTC (14 years, 6 months ago) by dpavlin
File size: 2418 byte(s)
extract to_all
1 #!/usr/bin/perl
2
3 package Sack::Server;
4
5 use warnings;
6 use strict;
7
8 use IO::Socket::INET;
9 use IO::Select;
10
11 use Data::Dump qw(dump);
12 use Storable qw();
13 use File::Slurp;
14 use Cwd qw(abs_path);
15
16 my @cloud;
17 my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n";
18 @cloud = read_file $cloud_path;
19 @cloud = map { chomp $_; $_ } @cloud;
20
21 warn "# cloud ",dump( @cloud );
22
23 my $listen_port = 4444;
24
25 my $node_path = abs_path $0;
26 $node_path =~ s{Server}{Client};
27
28 my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!;
29 my $sel = IO::Select->new($lsn);
30
31 my $info;
32 sub info {
33 my $port = shift;
34 push @{ $info->{$port} }, [ @_ ];
35 }
36
37 sub fork_ssh {
38 my ( $port, $host ) = @_;
39
40 if ( my $pid = fork ) {
41 # parent
42 info $port => 'forked', $pid;
43 return $port;
44
45 } elsif ( ! defined $pid ) {
46 warn "can't fork $host $port";
47 return;
48 } else {
49 # child
50 my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|;
51 warn "# exec: $cmd\n";
52 exec $cmd;
53 }
54 }
55
56 my $node_port = 4000;
57
58 foreach my $host ( @cloud ) {
59 system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
60 fork_ssh( $node_port++, $host );
61 }
62
63 my $session;
64
65 sub to_all {
66 my $data = shift;
67 foreach my $port ( keys %{ $session->{port} } ) {
68 warn ">>>> [$port]\n";
69 Storable::store_fd( $data, $session->{port}->{$port} );
70 }
71 }
72
73 while (1) {
74 for my $sock ($sel->can_read(1)) {
75 if ($sock == $lsn) {
76 my $new = $lsn->accept;
77 $sel->add($new);
78 $session->{peerport}->{ $new->peerport } = $new;
79 warn "[socket] connect\n";
80 Storable::store_fd( { ping => 1 }, $new );
81 info 0 => 'ping', $new->peerport;
82 } else {
83 my $data = eval { Storable::fd_retrieve( $sock ) };
84 if ( $@ ) {
85 delete $session->{$sock};
86 warn "[socket] disconnect: $@\n";
87 $sel->remove($sock);
88 $sock->close;
89 } else {
90 warn "<<<< ", dump($data), $/;
91 if ( $data->{repl} ) {
92 my $response = { repl => $$ };
93 if ( $data->{repl} =~ m/ping/ ) {
94 to_all { ping => 1 };
95 } elsif ( $data->{repl} =~ m/info/ ) {
96 $response->{info} = $info;
97 }
98 Storable::store_fd( $response, $sock );
99 } elsif ( $data->{ping} ) {
100 my $port = $data->{port};
101 info $port => 'ping', $port;
102 $session->{port}->{ $data->{port} } = $sock;
103 }
104 }
105 }
106 }
107 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26