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

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 122 CC 14 SCAR 49.0
CC
14
Cov
0%
run
210.0
new
2.0
BEGIN
2.0
CRAP 134.4
1 package MYDan::Util::MIO::UDP;
2  
3 =head1 NAME
4  
5 MYDan::Util::MIO::UDP - Send multiple UPD datagrams in parallel.
6  
7 =head1 SYNOPSIS
8
9 use MYDan::Util::MIO::UDP;
10  
11 my $udp = MYDan::Util::MIO::UDP->new( qw( host1:port1 host1:port2 ... ) );
12 my $result = $udp->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 );
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 Send UDP datagrams in parallel.
42 The following parameters may be defined in I<%param>:
43  
44 max : ( default 128 ) number of datagrams sent in parallel.
45 log : ( default STDERR ) a handle to report progress.
46 timeout : ( default 300 ) number of seconds allotted for each response.
47 input : ( default from STDIN ) input buffer.
48 resp : ( default 1 ) want response.
49  
50 Returns HASH of HASH of nodes. First level is indexed by type
51 ( I<mesg> or I<error> ). Second level is indexed by message.
52  
53 =cut
54 sub run
55 {
56 confess "poll: $!" unless my $poll = IO::Poll->new();
57  
58 local $| = 1;
59 local $/ = undef;
60  
61 my $self = shift;
62 my @node = keys %$self;
63 my ( %run, %result, %busy ) = ( resp => 1, %RUN, @_ );
64 my ( $log, $max, $timeout, $input, $resp ) =
65 @run{ qw( log max timeout input resp ) };
66  
67 $input ||= -t STDIN ? '' : <STDIN>;
68  
69 for ( my $time = time; @node || $poll->handles; )
70 {
71 if ( time - $time > $timeout ) ## timeout
72 {
73 for my $sock ( keys %busy )
74 {
75 $poll->remove( $sock );
76 eval { $sock->shutdown( 2 ) };
77 push @{ $result{error}{timeout} }, delete $busy{$sock};
78 }
79  
80 print $log "timeout!\n";
81 last;
82 }
83  
84 while ( @node && keys %busy < $max )
85 {
86 my $node = shift @node;
87 my %inet =
88 (
89 PeerAddr => $node, Blocking => 0, Timeout => $timeout,
90 Proto => 'udp', Type => SOCK_DGRAM,
91 );
92  
93 my $sock = $self->{$node}
94 ? IO::Socket::INET->new( %inet )
95 : IO::Socket::UNIX->new( %inet );
96  
97 unless ( $sock )
98 {
99 push @{ $result{error}{ "socket: $!" } }, $node;
100 next;
101 }
102  
103 $sock->send( $input ) if defined $input;
104 print $log "$node started.\n" if $run{verbose};
105  
106 if ( $resp )
107 {
108 $poll->mask( $sock => POLLIN );
109 $busy{$sock} = $node;
110 }
111 else
112 {
113 eval { $sock->shutdown( 2 ) };
114 print $log "$node done.\n" if $run{verbose};
115 }
116 }
117  
118 next unless $resp;
119 $poll->poll( $MAX{period} );
120  
121 for my $sock ( $poll->handles( POLLIN ) ) ## read
122 {
123 my $buffer; $sock->recv( $buffer, $MAX{buffer} );
124 my $node = delete $busy{$sock};
125  
126 push @{ $result{mesg}{$buffer} }, $node;
127 $poll->remove( $sock );
128 eval { $sock->shutdown( 2 ) };
129 print $log "$node done.\n" if $run{verbose};
130 }
131 }
132  
133 push @{ $result{error}{'not run'} }, @node if @node;
134 return wantarray ? %result : \%result;
135 }
136  
137 1;