blib/lib/MYDan/UDPFileServer/Client.pm untested

filter: Statementstmt 0.0% 0 / 229 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 356 CC 41 SCAR 60.7
CC
41
Cov
0%
put
756.0
get
210.0
new
6.0
CRAP 433.1
1 package MYDan::UDPFileServer::Client;
2  
3 =head1 NAME
4  
5 MYDan::UDPFileServer::Client
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::UDPFileServer::Client;
10  
11 my $client = MYDan::UDPFileServer::Client->new( ip => '127.0.0.1', port => 65111 );
12 my $gstat = $client->get( 'filename' );
13 my $pstat = $client->put( '/path/file' );
14  
15 =cut
16 use strict;
17 use warnings;
18 use File::Basename;
19  
20 use Fcntl qw(:flock SEEK_END);
21 use Time::HiRes qw( sleep time );
22 use File::Basename;
23 use AnyEvent;
24 use AnyEvent::Handle::UDP;
25 use YAML::XS;
26 use File::Temp;
27  
28 use base qw( MYDan::UDPFileServer );
29  
30 our %RUN = ( %MYDan::UDPFileServer::RUN, RTimeout => 6 );
31  
32 sub new
33 {
34 my ( $class, %self ) = @_;
35 map{ die "$_ undef" unless $self{$_} }qw( ip port );
36 bless \%self, ref $class || $class;
37 }
38  
39 sub put
40 {
41 my $this = shift @_;
42 my %run = ( %RUN, @_ );
43 die "nofile\n" unless my $file = $run{file};
44 die "file name format is not supported\n" unless $file =~ /^[a-zA-Z0-9\._\-]+$/;
45 die "$file: not a file\n" unless -f $file;
46 my $basename = File::Basename::basename $file;
47  
48 die "$file: open: $!\n" unless open my $fh => $file;
49  
50 my $size = ( stat $file )[7];
51  
52 my $buflen = $run{MTU} - $RUN{HEAD};
53 my $count = int ( $size / $buflen );
54 $count ++ if $size % $buflen;
55  
56 my ( $cv, $deleteid, $ctrlid, $sendcount, %data ) = ( AE::cv, 0, 0, 0 );
57 my %ctrl = ( rtt => $run{RTT}, recv => 0, repeat => 0 );
58  
59 my $conn = AnyEvent::Handle::UDP->new(
60 connect => [$this->{ip}, $this->{port}],
61 on_recv => sub {
62 my $data = shift @_;
63 my $index = substr( $data, 0, $RUN{HEAD}, '' );
64 return unless my ( $type, $idx ) = $index =~ /^DG([A-Z])([0-9a-f]{8})$/;
65 my $id = hex $idx;
66  
67 if( $type eq 'K' )
68 {
69 warn "GET OK $data\n";
70 $cv->send;
71 return;
72 }elsif( $type eq 'C' )
73 {
74 return if $id <= $ctrlid;
75 my ( $recv, $repeat ) = split /:/, $data;
76 %ctrl = ( %ctrl, id => $id, data => $data,
77 time => time, recv => $recv, repeat => $repeat );
78 $ctrlid = $id;
79 return;
80 }elsif( $type eq 'B' )
81 {
82 return if $id <= $deleteid;
83 map{ delete $data{$_}; }$deleteid -1 .. $id -1;
84 $deleteid = $id;
85 }
86 },
87 rtimeout => $run{RTimeout},
88 on_rtimeout => sub
89 {
90 print YAML::XS::Dump \@_;
91 $cv->send;
92 die "on_rtimeout.\n";
93 },
94 autoflush => 1,
95 on_error => sub{
96 $cv->send;
97 die "udp error $_[2].\n";
98 },
99 );
100  
101 map{ $conn->push_send("DGA00000000$count:$basename"); } 1 .. 2;
102 my ( $ctrltime, $ctrlindex, $waitRepeat, $sendSec, $ctrldata, %ctrlsendtime )
103 = ( 0, 0, 0, $run{SendSec} );
104  
105 my $ctrlcb;
106 $ctrlcb = sub {
107 my $time = time;
108 my $idx = sprintf "DGC%08x", $ctrlindex;
109 $ctrlsendtime{$ctrlindex} = $time;
110 $ctrlindex ++;
111 map{ $conn->push_send($idx); }1..2;
112 if( $ctrl{time} )
113 {
114 $ctrl{rtt} = sprintf "%0.4f", $ctrl{time} - $ctrlsendtime{$ctrl{id}};
115 $ctrl{rtt} = 0.0001 if $ctrl{rtt} < 0.0001;
116 }
117 else
118 {
119 $ctrl{rtt} += 0.001;
120 }
121  
122 if( $ctrl{repeat} > 0 && $ctrl{recv} > 0 )
123 {
124 $waitRepeat += 0.001 * int( $ctrl{repeat} * 1000 / $ctrl{recv} );
125 }
126 else
127 {
128 $waitRepeat = 0;
129 }
130  
131 if( $ctrl{recv} > 0 )
132 {
133 my $temp = int( $ctrl{recv} * $run{TransmitRatio} );
134 $sendSec = $temp if $temp > $sendSec;
135 }
136  
137 $ctrltime = sprintf "%0.6f", time - $time;
138 $ctrldata = AnyEvent->timer(
139 after => 1,
140 cb => $ctrlcb,
141 );
142 };
143  
144 $ctrldata = AnyEvent->timer(
145 after => 1,
146 interval => 0.001,
147 cb => $ctrlcb,
148 );
149  
150  
151 my ( $data_index, $sendtime, $sendinterval, $sendSec_temp, $sendSec_time, $senddata, %stime )
152 = ( 0, 0, 1, 0, int time );
153  
154 my $sendcb;
155 $sendcb = sub {
156 my $time = time;
157 my $itime = int $time;
158 if( $itime ne $sendSec_time )
159 {
160 $sendSec_temp = 0;
161 $sendSec_time = $itime;
162 }
163 my $i = 0;
164  
165 return if @{$conn->{buffers}} > $run{Buffers};
166  
167 my $timeout = $ctrl{rtt} + $run{SendTimeoutAddTime} + $waitRepeat;
168 $timeout = $run{MaxRTO} if $timeout > $run{MaxRTO};
169  
170 for my $id ( $deleteid -1 .. $data_index -1 )
171 {
172 next if ( ! $data{$id} ) || ( $stime{$id} && $stime{$id} + $timeout > time );
173 $i++;
174 $conn->push_send($data{$id});
175 $stime{$id} = time;
176 $sendcount ++;
177 $sendSec_temp ++;
178 last if $sendSec_temp > $sendSec || $i >= $run{SendOne};
179 }
180  
181 $sendtime = sprintf "%0.6f", time - $time;
182 $sendinterval = sprintf "%0.3f", time - $time;
183 $sendinterval = 0.001 if $sendinterval < 0.001;
184  
185 $senddata = AnyEvent->timer(
186 after => $sendinterval,
187 interval => $sendinterval,
188 cb => $sendcb,
189 );
190 };
191  
192 $senddata = AnyEvent->timer(
193 after => 1,
194 interval => 0.001,
195 cb => $sendcb,
196 );
197  
198 my ( $readinterval, $readfile, $readtime ) = ( 0.001 );
199  
200 my $readcb;
201 $readcb = sub {
202 my $time = time;
203 return if $run{ReadFileCache} < $data_index - $deleteid;
204 my $n = read $fh, my ( $data ), $buflen * $run{ReadFileOneTime};
205  
206 unless( $n ) { undef $readfile; return; }
207  
208 while( my ( $d ) = substr( $data, 0, $buflen, '' ) )
209 {
210 last unless length $d;
211 $data{$data_index} = sprintf( "DGT%08x", $data_index ).$d;
212 $data_index ++;
213 }
214 $readtime = sprintf "%0.6f", time - $time;
215 $readinterval = sprintf "%0.3f", time - $time;
216 $readinterval *= 2;
217 $readinterval = 0.001 if $readinterval < 0.001;
218 $readfile = AnyEvent->timer(
219 after => $readinterval,
220 interval => $readinterval,
221 cb => $readcb
222 );
223 };
224 $readfile = AnyEvent->timer(
225 after => $readinterval,
226 interval => $readinterval,
227 cb => $readcb
228 );
229  
230 my $deleteid_ = 0;
231 my $stat = AnyEvent->timer(
232 after => 1,
233 interval => 1,
234 cb => sub {
235 printf "rt: %0.3f\tri: %s\tst: %s\tsi: %s\tct: %s\t",
236 map{ 1000 * $_}( $readtime, $readinterval, $sendtime, $sendinterval, $ctrltime );
237 printf "rfilecache: %s\t", $data_index - $deleteid;
238 printf "buffers: %s\t", scalar @{$conn->{buffers}};
239 printf "rtt: %s\t", 1000*$ctrl{rtt};
240 printf "speed: %0.2fM/s\t", ( $run{MTU} * ( $sendcount ) ) / ( 1024 * 1024 );
241 printf "ack: %0.2fM/s\n", ( $run{MTU} * ( $deleteid - $deleteid_ ) ) / ( 1024 * 1024 );
242 $deleteid_ = $deleteid;
243 $sendcount = 0;
244 }
245 );
246  
247 $cv->recv;
248 }
249  
250 sub get
251 {
252 my $this = shift @_;
253 my %run = ( %RUN, @_ );
254 die "file undef\n" unless my $file = $run{file};
255 die "file name format is not supported\n" unless $file =~ /^[a-zA-Z0-9\._\-]+$/;
256  
257 my ( $cv, %data, %temp_cache ) = ( AE::cv );
258  
259 my $conn;
260 $conn = AnyEvent::Handle::UDP->new(
261 connect => [$this->{ip}, $this->{port}],
262 on_recv => sub {
263 my $data = shift @_;
264 my $index = substr( $data, 0, $RUN{HEAD}, '' );
265 $temp_cache{recv}++;
266 return unless my ( $type, $idx ) = $index =~ /^DP([A-Z])([0-9a-f]{8})$/;
267 my $id = hex $idx;
268  
269 if( $type eq 'C' )
270 {
271 map{ $conn->push_send("$index$temp_cache{recv_sec}:$temp_cache{repeat_sec}") } 1 .. 2;
272 return;
273  
274 }elsif( $type eq 'A' )
275 {
276 my ( $t, $count ) = split /:/, $data;
277 unless( $t && $t eq 'ok' )
278 {
279 warn "GET $file error: $data\n";
280 $cv->send;
281 return;
282 }
283  
284 return if $temp_cache{fh};
285  
286 warn "GET $data\n";
287  
288 my $fh = File::Temp->new( DIR => '.', SUFFIX => ".udp.mydan", UNLINK => 0 );
289  
290 %temp_cache = (
291 fh => $fh,
292 dp => $file,
293 filename => $fh->filename,
294 stat => AnyEvent->timer(
295 after => 1,
296 interval => 1,
297 cb => sub{
298 $temp_cache{recv_sec} = $temp_cache{recv} - $temp_cache{recv_};
299 $temp_cache{repeat_sec} = $temp_cache{repeat} - $temp_cache{repeat_};
300  
301 printf "$temp_cache{dp} => count: %s\tcachequeue:%s\tackto: %s\trecv: %s/s\trepeat: %s/s\n",
302 $temp_cache{count},scalar( keys %{$temp_cache{data}}), $temp_cache{ackto}, $temp_cache{recv_sec}, $temp_cache{repeat_sec};
303  
304 $temp_cache{recv_} = $temp_cache{recv};
305 $temp_cache{repeat_} = $temp_cache{repeat};
306 }
307 ),
308 ack => AnyEvent->timer(
309 after => $run{ACKInterval},
310 interval => $run{ACKInterval},
311 cb => sub{
312 $conn->push_send( sprintf "DPB%08x", $temp_cache{ackto} );
313 $temp_cache{send} ++;
314 }
315 ),
316  
317 writefile => AnyEvent->timer(
318 after => $run{WriteFileInterval},
319 interval => $run{WriteFileInterval},
320 cb => sub{
321 if( $run{WriteFileWidth} < $temp_cache{ackto} - $temp_cache{writeto} )
322 {
323 for( $temp_cache{writeto} .. $temp_cache{writeto} + $run{WriteFileWidth} -1 )
324 {
325 syswrite( $temp_cache{fh}, delete $temp_cache{data}{$_});
326 }
327 $temp_cache{writeto} += $run{WriteFileWidth};
328 return;
329 }
330  
331 if( $temp_cache{ackto} eq $temp_cache{count} )
332 {
333 for my $id ( sort{ $a <=> $b } keys %{$temp_cache{data}} )
334 {
335 syswrite( $temp_cache{fh}, delete $temp_cache{data}{$id} );
336 }
337 close $fh;
338  
339 map{ delete $temp_cache{$_}; }qw( writefile stat ack id );
340  
341 die "rename fail: $!\n" if system "mv '$temp_cache{filename}' '$temp_cache{dp}'";
342 print "$temp_cache{dp} => done.\n";
343 $conn->push_send("DPK00000000");
344 $cv->send;
345 }
346 }
347 ),
348  
349 count => $count,
350 map{ $_ => 0 }qw( acktime recv recv_ recv_sec repeat repeat_ repeat_sec send ackto writeto )
351 );
352  
353 return;
354 }
355 if( $temp_cache{id}{$id} )
356 {
357 $temp_cache{repeat}++;
358 return;
359 }
360  
361 $temp_cache{id}{$id} = 1;
362 $temp_cache{data}{$id} = $data;
363  
364 return unless $id == $temp_cache{ackto};
365 $temp_cache{ackto}++;
366 for( $temp_cache{ackto} .. $temp_cache{count} )
367 {
368 last unless $temp_cache{id}{$_};
369 $temp_cache{ackto} ++;
370 }
371 },
372 rtimeout => $run{RTimeout},
373 on_rtimeout => sub
374 {
375 print YAML::XS::Dump \@_;
376 $cv->send;
377 die "on_rtimeout.\n";
378 },
379 autoflush => 1,
380 on_error => sub{
381 $cv->send;
382 die "udp error $_[2].\n";
383 },
384 );
385  
386 map{ $conn->push_send("DPA00000000$file"); } 1 .. 2;
387  
388 $cv->recv;
389 }
390  
391 1;