Commits

Shlomi Fish  committed 622da03

Extract _BytesDownloaded class.

This is a refactoring / yak shaving precursor for fixing the problem with
the refreshing the connections. I am going to keep track of how many times
the connection stalled.

  • Participants
  • Parent commits 380b86a

Comments (0)

Files changed (3)

File App-ManiacDownloader/lib/App/ManiacDownloader.pm

 use JSON qw(decode_json encode_json);
 
 use App::ManiacDownloader::_SegmentTask;
+use App::ManiacDownloader::_BytesDownloaded;
 
 our $VERSION = '0.0.8';
 
 has '_url' => (is => 'rw');
 has '_url_basename' => (isa => 'Str', is => 'rw');
 has '_remaining_connections' => (isa => 'Int', is => 'rw');
-has ['_bytes_dled', '_bytes_dled_last_timer'] =>
-    (isa => 'Int', is => 'rw', default => sub { return 0;});
 has '_stats_timer' => (is => 'rw');
 has '_last_timer_time' => (is => 'rw', isa => 'Num');
 has '_len' => (is => 'rw', isa => 'Int');
+has '_downloaded' => (is => 'rw', isa => 'App::ManiacDownloader::_BytesDownloaded', default => sub { return App::ManiacDownloader::_BytesDownloaded->new; });
 
 sub _serialize
 {
     +{
         _ranges => [map { $_->_serialize() } @{$self->_ranges}],
         _remaining_connections => $self->_remaining_connections,
-        _bytes_dled => $self->_bytes_dled,
+        _bytes_dled => $self->_downloaded->_total_downloaded,
         _len => $self->_len,
     };
 }
 
         my $ret = $r->_write_data(\$data);
 
-        $self->_bytes_dled(
-            $self->_bytes_dled + $ret->{num_written},
-        );
+        $self->_downloaded->_add($ret->{num_written});
+
         my $cont = $ret->{should_continue};
         if (! $cont)
         {
 {
     my ($self) = @_;
 
-    my $num_dloaded = $self->_bytes_dled - $self->_bytes_dled_last_timer;
+    my ($num_dloaded, $total_downloaded)
+        = $self->_downloaded->_flush_and_report;
+
+    foreach my $r (@{$self->_ranges()})
+    {
+        $r->_flush_and_report;
+    }
 
     my $time = AnyEvent->now;
     my $last_time = $self->_last_timer_time;
 
     printf "Downloaded %i%% (Currently: %.2fKB/s)\r",
-        int($self->_bytes_dled * 100 / $self->_len),
+        int($total_downloaded * 100 / $self->_len),
         ($num_dloaded / (1024 * ($time-$last_time))),
     ;
     STDOUT->flush;
 
     $self->_last_timer_time($time);
-    $self->_bytes_dled_last_timer($self->_bytes_dled);
 
     return;
 }
     {
         my $record = decode_json(_slurp($self->_resume_info_path));
         $self->_len($record->{_len});
-        $self->_bytes_dled($record->{_bytes_dled});
-        $self->_bytes_dled_last_timer($self->_bytes_dled());
+        $self->_downloaded->_my_init($record->{_bytes_dled});
         $self->_remaining_connections($record->{_remaining_connections});
         my $ranges_ref = $record->{_ranges};
         $self->_init_from_len(

File App-ManiacDownloader/lib/App/ManiacDownloader/_BytesDownloaded.pm

+package App::ManiacDownloader::_BytesDownloaded;
+
+use strict;
+use warnings;
+
+use MooX qw/late/;
+
+use List::Util qw/min/;
+
+has ['_bytes_dled', '_bytes_dled_last_timer'] =>
+    (isa => 'Int', is => 'rw', default => sub { return 0;});
+
+sub _add
+{
+    my ($self, $num_written) = @_;
+
+    $self->_bytes_dled(
+        $self->_bytes_dled + $num_written,
+    );
+
+    return;
+}
+
+sub _total_downloaded
+{
+    my ($self) = @_;
+
+    return $self->_bytes_dled;
+}
+
+sub _flush_and_report
+{
+    my $self = shift;
+
+    my $difference = $self->_bytes_dled - $self->_bytes_dled_last_timer;
+
+    $self->_bytes_dled_last_timer($self->_bytes_dled);
+
+    return ($difference, $self->_bytes_dled);
+}
+
+sub _my_init
+{
+    my ($self, $num_bytes) = @_;
+
+    $self->_bytes_dled($num_bytes);
+    $self->_bytes_dled_last_timer($num_bytes);
+
+    return;
+}
+
+
+1;
+

File App-ManiacDownloader/lib/App/ManiacDownloader/_SegmentTask.pm

 has '_fh' => (is => 'rw',);
 has 'is_active' => (is => 'rw', isa => 'Bool', default => 1);
 
+has '_downloaded' => (is => 'rw', isa => 'App::ManiacDownloader::_BytesDownloaded', default => sub { return App::ManiacDownloader::_BytesDownloaded->new; }, handles => ['_flush_and_report'],);
+
 sub _serialize
 {
     my ($self) = @_;
         die "Written bytes mismatch.";
     }
 
+    $self->_downloaded->_add($written);
+
     my $init_start = $self->_start;
     $self->_start($init_start + $written);