blib/lib/MYDan/Util/MIO/TCP.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 123 CC 11 SCAR 43.5
CC
11
Cov
0%
run
132.0
new
2.0
BEGIN
2.0
CRAP 77.3
1 package MYDan::Util::MIO::TCP;
2  
3 =head1 NAME
4  
5 MYDan::Util::MIO::TCP - Make multiple TCP connections in parallel.
6  
7 =head1 SYNOPSIS
8
9 use MYDan::Util::MIO::TCP;
10  
11 my $tcp = MYDan::Util::MIO::TCP->new( qw( host1:port1 host1:port2 ... ) );
12 my $result = $tcp->run( max => 128, log => \*STDERR, timeout => 300 );
13  
14 my $mesg = $result->{mesg};
15 my $error = $result->{error};
16  
17 =cut
18 use strict;
19 use warnings;
20  
21 use Carp;
22 use IO::Socket;
23 use Time::HiRes qw( time );
24 use IO::Poll qw( POLLIN POLLHUP POLLOUT );
25  
26 use base qw( MYDan::Util::MIO );
27  
28 our %RUN = %MYDan::Util::MIO::RUN;
29 our %MAX = %MYDan::Util::MIO::MAX;
30  
31 sub new
32 {
33 my $self = shift;
34 $self->net( @_ );
35 }
36  
37 =head1 METHODS
38  
39 =head3 run( %param )
40  
41 Make TCP connections in parallel.
42 The following parameters may be defined in I<%param>:
43  
44 max : ( default 128 ) number of connections in parallel.
45 log : ( default STDERR ) a handle to report progress.
46 timeout : ( default 300 ) number of seconds allotted for each connection.
47 input : ( default from STDIN ) input buffer.
48  
49 Returns HASH of HASH of nodes. First level is indexed by type
50 ( I<mesg> or I<error> ). Second level is indexed by message.
51  
52 =cut
53 sub run
54 {
55 confess "poll: $!" unless my $poll = IO::Poll->new();
56  
57 local $| = 1;
58 local $/ = undef;
59  
60 my $self = shift;
61 my @node = keys %$self;
62 my ( %run, %result, %buffer, %busy ) = ( %RUN, @_ );
63 my ( $log, $max, $timeout, $input ) = @run{ qw( log max timeout input ) };
64  
65 $input ||= -t STDIN ? '' : <STDIN>;
66  
67 for ( my $time = time; @node || $poll->handles; )
68 {
69 if ( time - $time > $timeout ) ## timeout
70 {
71 for my $sock ( keys %busy )
72 {
73 $poll->remove( $sock );
74 eval { $sock->shutdown( 2 ) };
75 push @{ $result{error}{timeout} }, delete $busy{$sock};
76 }
77  
78 print $log "timeout!\n";
79 last;
80 }
81  
82 while ( @node && keys %busy < $max )
83 {
84 my $node = shift @node;
85 my %inet =
86 (
87 PeerAddr => $node, Blocking => 0, Timeout => $timeout,
88 Proto => 'tcp', Type => SOCK_STREAM,
89 );
90  
91 my $sock = $self->{$node}
92 ? IO::Socket::INET->new( %inet )
93 : IO::Socket::UNIX->new( %inet );
94  
95 unless ( $sock )
96 {
97 push @{ $result{error}{ "socket: $!" } }, $node;
98 next;
99 }
100  
101 $poll->mask( $sock => POLLIN | POLLOUT );
102 $busy{$sock} = $node;
103 print $log "$node started.\n" if $run{verbose};
104 }
105  
106 $poll->poll( $MAX{period} );
107  
108 for my $sock ( $poll->handles( POLLIN ) ) ## read
109 {
110 my $buffer; $sock->recv( $buffer, $MAX{buffer} );
111 $buffer{$sock} .= $buffer;
112 }
113  
114 for my $sock ( $poll->handles( POLLOUT ) ) ## write
115 {
116 $sock->send( $input ) if defined $input;
117 $poll->mask( $sock, $poll->mask( $sock ) & ~POLLOUT );
118 eval { $sock->shutdown( 1 ) };
119 }
120  
121 for my $sock ( $poll->handles( POLLHUP ) ) ## done
122 {
123 my $node = delete $busy{$sock};
124  
125 push @{ $result{mesg}{ delete $buffer{$sock} } }, $node;
126 $poll->remove( $sock );
127 eval { $sock->shutdown( 0 ) };
128 print $log "$node done.\n" if $run{verbose};
129 }
130 }
131  
132 push @{ $result{error}{'not run'} }, @node if @node;
133 return wantarray ? %result : \%result;
134 }
135  
136 1;