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