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

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

revision 118 by dpavlin, Tue Oct 6 00:11:08 2009 UTC revision 119 by dpavlin, Tue Oct 6 23:04:45 2009 UTC
# Line 3  package Sack::Lorry; Line 3  package Sack::Lorry;
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6    our $VERSION = '0.08';
7    
8  use IO::Socket::INET;  use IO::Socket::INET;
9  use Data::Dump qw(dump);  use Data::Dump qw(dump);
10  use Storable;  use Storable;
# Line 20  my $port = 4000; Line 22  my $port = 4000;
22  sub new {  sub new {
23          my $class = shift;          my $class = shift;
24          my $self  = bless {@_}, $class;          my $self  = bless {@_}, $class;
25            $self->{sock} = {};
26            warn __PACKAGE__, " $VERSION\n";
27          return $self;          return $self;
28  }  }
29    
30    sub connected {
31            sort keys %{ $_[0]->{sock} }
32    }
33    
34    sub connect_to {
35            my ( $self, $port ) = @_;
36    
37            my $sock;
38    
39            print STDERR "waiting for $port";
40    
41            while ( ! $sock ) {
42    
43                    $sock = IO::Socket::INET->new(
44                            PeerAddr => '127.0.0.1',
45                            PeerPort => $port,
46                            Proto    => 'tcp',
47                    );
48    
49                    if ( ! $sock ) {
50                            print STDERR ".";
51                            sleep 1;
52                    }
53    
54            }
55    
56            $self->{sock}->{$port} = $sock;
57    
58            warn "OK connected to $port\n";
59    
60            return $port;
61    }
62    
63    
64  sub start_node {  sub start_node {
65          my ( $self, $host ) = @_;          my ( $self, $host ) = @_;
66    
# Line 44  sub start_node { Line 82  sub start_node {
82                  $pids->{ "$host:$port" } = $pid;                  $pids->{ "$host:$port" } = $pid;
83                  $ports->{ $port } = $host;                  $ports->{ $port } = $host;
84    
85                  my $sock;                  $self->connect_to( $port );
   
                 print STDERR "waiting for $port";  
   
                 while ( ! $sock ) {  
   
                         $sock = IO::Socket::INET->new(  
                                 PeerAddr => '127.0.0.1',  
                                 PeerPort => $port,  
                                 Proto    => 'tcp',  
                         );  
86    
87                          if ( ! $sock ) {                  $self->{port_on_host}->{$port} = $host;
                                 print STDERR ".";  
                                 sleep 1;  
                         }  
   
                 }  
   
                 $self->{sock}->{$port} = $sock;  
   
                 warn "# connected to $port\n";  
   
                 $self->{connected}->{$port} = $host;  
88    
89                  return $port++;                  return $port++;
90    
# Line 109  sub get_from { Line 126  sub get_from {
126    
127  sub send_to_all {  sub send_to_all {
128          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
129          $self->send_to( $_, $data ) foreach sort keys %{ $self->{connected} };          $self->send_to( $_, $data ) foreach $self->connected;
130  }  }
131    
132  sub get_from_all {  sub get_from_all {
133          my ( $self ) = @_;          my ( $self ) = @_;
134          my $result;          my $result;
135          $result->{$_} = $self->get_from( $_ ) foreach sort keys %{ $self->{connected} };          $result->{$_} = $self->get_from( $_ ) foreach $self->connected;
136          return $result;          return $result;
137  }  }
138    
139    sub restart_nodes {
140            my ( $self ) = @_;
141            foreach my $port ( $self->connected ) {
142                    warn "restart [$port]\n";
143                    $self->send_to( $port, { restart => 1 } );
144                    $self->connect_to( $port );
145            }
146    }
147    
148    
149  our $out;  our $out;
150    
# Line 204  sub view { Line 230  sub view {
230    
231          my $total;          my $total;
232    
233          foreach my $port ( keys %{ $self->{connected} } ) {          foreach my $port ( $self->connected ) {
234                  my $result = $self->get_from( $port );                  my $result = $self->get_from( $port );
235                  warn "# result ", dump $result if $self->{debug};                  warn "# result ", dump $result if $self->{debug};
236                  if ( my $out = delete $result->{out} ) {                  if ( my $out = delete $result->{out} ) {

Legend:
Removed from v.118  
changed lines
  Added in v.119

  ViewVC Help
Powered by ViewVC 1.1.26