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

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

revision 126 by dpavlin, Wed Oct 7 10:05:32 2009 UTC revision 127 by dpavlin, Wed Oct 7 16:21:33 2009 UTC
# Line 11  use Storable; Line 11  use Storable;
11  use Time::HiRes qw(time);  use Time::HiRes qw(time);
12    
13  use lib 'lib';  use lib 'lib';
14    use base qw(Sack::Pid);
15  use Sack::Color;  use Sack::Color;
16    
17  our $VERSION = '0.08';  our $VERSION = '0.09';
18    
19  sub new {  sub new {
20          my $class = shift;          my $class = shift;
21          my $port = shift;          my $port = shift;
22          my $self  = bless { port => $port }, $class;          my $self  = bless { port => $port }, $class;
23    
24          my $pid_path = "/tmp/sack.$port.pid";          $self->port_pid( $port );
         if ( -e $pid_path ) {  
                 my $pid = read_file $pid_path;  
                 kill 9, $pid; # warn "[$port] kill old $pid\n";  
         }  
         write_file $pid_path, $$;  
25    
26          my $sock = IO::Socket::INET->new(          my $sock = IO::Socket::INET->new(
27                  Listen    => SOMAXCONN,                  Listen    => SOMAXCONN,
# Line 35  sub new { Line 31  sub new {
31                  Reuse     => 1,                  Reuse     => 1,
32          ) or die "[$port] die $!";          ) or die "[$port] die $!";
33    
34          warn "[$port] accept $VERSION\n";          my $client;
35    
36          my $client = $sock->accept();          while ( 1 ) {
37    
38          warn "[$port] connect from ", $client->peerhost, $/;                  if ( ! $client ) {
39                            warn "[$port] accept $VERSION\n";
40                            $client = $sock->accept();
41                            warn "[$port] connect from ", $client->peerhost, $/;
42                    }
43    
44          while ( 1 ) {                  my $data = eval { Storable::fd_retrieve( $client ) };
45                    if ( $@ ) {
46                            warn "[$port] ERROR $@\n";
47                            close $client;
48                            next;
49                    }
50    
51                  my $data = Storable::fd_retrieve( $client );                  if ( defined $data->{data} ) {
52  #               warn "[$port] <<<<\n";                          warn "# [$port] <<<< data\n";
53                  warn "[$port] data = ", dump( $data ) if $self->{debug};                  } else {
54                            warn "# [$port] <<<< ", dump( $data ), $/;
55                    }
56    
57                  my $result;                  my $result;
58    
# Line 55  sub new { Line 62  sub new {
62                          $self->{data} = delete $data->{data};                          $self->{data} = delete $data->{data};
63                          $result = { data => 'loaded' };                          $result = { data => 'loaded' };
64                  } elsif ( $data->{exit} ) {                  } elsif ( $data->{exit} ) {
65                          warn "[$port] exit";                          warn "[$port] exit\n";
66                          close $sock;                          close $sock;
67                          exit;                          exit;
68                  } elsif ( $data->{restart} ) {                  } elsif ( $data->{restart} ) {
# Line 66  sub new { Line 73  sub new {
73                          $result = {                          $result = {
74                                  version => $VERSION,                                  version => $VERSION,
75                                  size => $#{ $self->{data} } + 1,                                  size => $#{ $self->{data} } + 1,
76                                    reports => $self->{reports},
77                          };                          };
78                  } elsif ( my $sh = delete $data->{sh} ) {                  } elsif ( my $sh = delete $data->{sh} ) {
79                          $result = { sh => scalar `$sh` };                          $result = { sh => scalar `$sh` };
# Line 74  sub new { Line 82  sub new {
82                          $result = { 'error' => $data };                          $result = { 'error' => $data };
83                  }                  }
84    
85  #               warn "[$port] >>>>\n";                  warn "# [$port] >>>>\n";
86                  Storable::store_fd( $result => $client );                  Storable::store_fd( $result => $client );
87          }          }
88    
# Line 122  sub view { Line 130  sub view {
130          my $report = [ $self->{port}, $affected, $dt, $affected / $dt ];          my $report = [ $self->{port}, $affected, $dt, $affected / $dt ];
131          warn sprintf "[%d] %d affected in %1.4fs %.2f/s\n", @$report;          warn sprintf "[%d] %d affected in %1.4fs %.2f/s\n", @$report;
132    
133            push @{ $self->{reports} }, "$affected in ${dt}s";
134    
135  #       warn "# out ", dump( $out );  #       warn "# out ", dump( $out );
136    
137          return {          return {

Legend:
Removed from v.126  
changed lines
  Added in v.127

  ViewVC Help
Powered by ViewVC 1.1.26