blib/lib/MYDan/Util/TcpServer.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 36 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 210 CC 21 SCAR 50.6
CC
21
Cov
0%
run
306.0
new
30.0
BEGIN
2.0
CRAP 158.1
1 package MYDan::Util::TcpServer;
2  
3 use warnings;
4 use strict;
5 use Carp;
6  
7 use Data::Dumper;
8  
9 use POSIX ":sys_wait_h";
10 use Time::HiRes qw(time);
11 use AnyEvent;
12 use AnyEvent::Impl::Perl;
13 use AnyEvent::Socket;
14 use AnyEvent::Handle;
15 use Fcntl qw(:flock SEEK_END);
16  
17 use MYDan;
18  
19 our %index;
20  
21 sub new
22 {
23 my ( $class, %this ) = @_;
24  
25 map{ die "$_ unkown" unless $this{$_} && $this{$_} =~ /^\d+$/ }qw( port max );
26 die "no file:$this{'exec'}\n" unless $this{'exec'} && -e $this{'exec'};
27  
28 $this{tmp} ||= sprintf "%s/var/run/agent", $MYDan::PATH;
29  
30 system( "mkdir -p '$this{tmp}'" ) unless -d $this{tmp};
31 map{ unlink $_ if $_ =~ /\/\d+$/ || $_ =~ /\/\d+\.out$/ }glob "$this{tmp}/*";
32  
33 bless \%this, ref $class || $class;
34 }
35  
36 sub run
37 {
38 my $this = shift;
39 my ( $port, $max, $exec, $tmp ) = @$this{qw( port max exec tmp )};
40  
41 $SIG{'USR1'} = sub {
42 print Dumper \%index;
43 };
44  
45 $SIG{INT} = $SIG{TERM} = sub {
46 map{ kill 'TERM', $_->{pid} if $_->{pid}; }values %index;
47 die "kill.\n";
48 };
49  
50 $SIG{'CHLD'} = sub {
51 while((my $pid = waitpid(-1, WNOHANG)) >0)
52 {
53 my $code = ( $? == -1 || $? & 127 ) ? 110 : $? >> 8;
54  
55
56 print "chld: $pid exit $code.\n";;
57  
58 my ( $index ) = grep{ $index{$_}{pid} && $index{$_}{pid} eq $pid }keys %index;
59 next unless my $data = delete $index{$index};
60  
61 if( $data->{handle}->fh )
62 {
63 $data->{handle}->push_write('*#*keepalive*#');
64 if ( open my $tmp_handle, '<', "$tmp/$index.out" )
65 {
66 #seek( $tmp_handle, -16384, SEEK_END );
67 while(<$tmp_handle>)
68 {
69 $data->{handle}->push_write($_) if $data->{handle}->fh;
70 }
71 $data->{handle}->push_write("--- $code\n") if $data->{handle}->fh;
72 }
73 $data->{handle}->destroy() if $data->{handle}->fh;
74 }
75  
76 map{ unlink "$tmp/$_" }( $index, "$index.out" );
77 }
78 };
79  
80 my ( $i, $cv ) = ( 0, AnyEvent->condvar );
81  
82
83 tcp_server undef, $port, sub {
84 my ( $fh, $tip, $tport ) = @_ or die "tcp_server: $!";
85  
86 printf "index: %s\n", ++ $i;
87 my $index = $i;
88  
89 my $len = keys %index;
90 printf "tcpserver: status: $len/$max\n";
91  
92 if( $len >= $max )
93 {
94 printf "connection limit reached, from %s:%s\n", $tip, $tport;
95 close $fh;
96 return;
97 }
98  
99 my $tmp_handle;
100 unless( open $tmp_handle, '>', "$tmp/$index" )
101 {
102 print "open '$tmp/$index' fail:$!\n";
103 close $fh;
104 return;
105 }
106  
107 my $handle; $handle = new AnyEvent::Handle(
108 fh => $fh,
109 keepalive => 1,
110 on_eof => sub{
111 close $tmp_handle;
112  
113 if ( my $pid = fork() )
114 {
115 $index{$index}{pid} = $pid;
116 }
117 else
118 {
119 $tip = '0.0.0.0' unless $tip && $tip =~ /^\d+\.\d+\.\d+\.\d+$/;
120 $tport = '0' unless $tport && $tport =~ /^\d+$/;
121  
122 $ENV{TCPREMOTEIP} = $tip;
123 $ENV{TCPREMOTEPORT} = $port;
124
125 open STDIN, '<', "$tmp/$index" or die "Can't open '$tmp/$index': $!";
126 open STDOUT, '>', "$tmp/$index.out" or die "Can't open '$tmp/$index.out': $!";
127 exec $exec;
128 }
129 },
130 on_read => sub {
131 my $self = shift;
132 printf "read len:%s\n", length $self->{rbuf};
133 $self->unshift_read (
134 chunk => length $self->{rbuf},
135 sub { print $tmp_handle $_[1]; }
136 );
137 },
138 on_error => sub {
139 close $tmp_handle;
140  
141 close $fh;
142 delete $handle->{fh};
143  
144 $handle->destroy();
145  
146 my $pid = $index{$index}{pid} if $index{$index};
147 if( $pid ) { kill 15, $pid; }
148 else
149 {
150 unlink "$tmp/$index";
151 }
152 },
153 );
154 $index{$index}{handle} = $handle;
155
156 };
157 my $t = AnyEvent->timer(
158 after => 1,
159 interval => 1,
160 cb => sub {
161 map{
162 $_->{handle}->push_write('*') if $_->{handle} && $_->{handle}->fh;
163 }values %index;
164 }
165 );
166
167 $cv->recv;
168 }
169  
170 1;