1. Shlomi Fish
  2. Maniac-Downloader

Commits

Shlomi Fish  committed 6deedfb

Convert from AnyEvent to IO::Async.

  • Participants
  • Parent commits 2bc25be
  • Branches convert-from-AnyEvent-to-IO-Async

Comments (0)

Files changed (2)

File App-ManiacDownloader/Changes

View file
  • Ignore whitespace
 Revision history for App-ManiacDownloader
 
+v0.0.11     2014-05-12
+    - Convert from AnyEvent to IO::Async , so we can have FTP functionality.
+
 v0.0.10     2014-04-30
     - Convert _open_fh_for_read_write_without_clobbering to a single sysopen
     call.

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

View file
  • Ignore whitespace
 
 use MooX qw/late/;
 use URI;
-use AnyEvent::HTTP qw/http_head http_get/;
+use IO::Async::Loop;
+use IO::Async::Timer::Periodic;
+use Net::Async::HTTP;
 use Getopt::Long qw/GetOptionsFromArray/;
 use File::Basename qw(basename);
 use Fcntl qw( SEEK_SET );
 my $DEFAULT_NUM_CONNECTIONS = 4;
 my $NUM_CONN_BYTES_THRESHOLD = 4_096 * 2;
 
-has '_finished_condvar' => (is => 'rw');
+has '_net_async_http' => (is => 'rw');
+has '_loop' => (is => 'rw');
 has '_ranges' => (isa => 'ArrayRef', is => 'rw');
 has '_url' => (is => 'rw');
 has '_url_basename' => (isa => 'Str', is => 'rw');
     # We do these to make sure the cancellation guard does not get
     # preserved because it's in the context of the closures.
     my $on_body = sub {
-        my ($data, $hdr) = @_;
+        my ($data) = @_;
+
+        # The end of the request.
+        if (!defined($data))
+        {
+            return;
+        }
 
         my $ret = $r->_write_data(\$data);
 
                     )
                 )
                 {
-                    $self->_finished_condvar->send;
+                    $self->_loop->stop();
                 }
             }
             else
         return $cont;
     };
 
-    my $final_cb = sub { return ; };
+    my $req = HTTP::Request->new(GET => $self->_url);
+    $req->header('Range' => sprintf("bytes=%d-%d", $r->_start, $r->_end-1));
 
-    my $guard = http_get $self->_url,
-    headers => { 'Range'
-        => sprintf("bytes=%d-%d", $r->_start, $r->_end-1)
-    },
-    on_body => $on_body,
-    $final_cb;
+    my $guard = $self->_net_async_http->do_request(
+        request => $req,
+        on_header => sub { return $on_body; },
+    );
 
     $r->_guard($guard);
 
 {
     my ($self) = @_;
 
+    my $loop = $self->_loop;
+
     my ($num_dloaded, $total_downloaded)
         = $self->_downloaded->_flush_and_report;
 
         $r->_flush_and_report;
         if ($r->is_active && $r->_increment_check_count($MAX_CHECKS))
         {
+            if (! $r->_guard->is_cancelled() )
+            {
+                $r->_guard->cancel();
+            }
             $r->_guard('');
             $self->_start_connection($idx);
         }
     }
 
-    my $time = AnyEvent->now;
+    my $time = $loop->time;
     my $last_time = $self->_last_timer_time;
 
     printf "Downloaded %i%% (Currently: %.2fKB/s)\r",
         }
     }
 
-    my $timer = AnyEvent->timer(
-        after => 3,
+    my $timer = IO::Async::Timer::Periodic->new(
         interval => 3,
-        cb => sub {
+        on_tick => sub {
             $self->_handle_stats_timer;
             return;
         },
     );
-    $self->_last_timer_time(AnyEvent->time());
+
+    my $loop = $self->_loop;
+
+    $timer->start;
+    $loop->add($timer);
+
+    $self->_last_timer_time($loop->time());
     $self->_stats_timer($timer);
 
     {
 {
     my ($self, $args) = @_;
 
+    my $loop = IO::Async::Loop->new;
+
+    $self->_loop($loop);
+
+    my $http = Net::Async::HTTP->new();
+
+    $self->_net_async_http($http);
+
+    $loop->add($http);
+
     my $num_connections = $DEFAULT_NUM_CONNECTIONS;
 
     my @argv = @{ $args->{argv} };
         return;
     }
 
-    $self->_finished_condvar(
-        scalar(AnyEvent->condvar)
-    );
-
     if (-e $self->_resume_info_path)
     {
         my $record = decode_json(_slurp($self->_resume_info_path));
     }
     else
     {
-        http_head $url, sub {
-            my (undef, $headers) = @_;
-            my $len = $headers->{'content-length'};
+        $self->_net_async_http->HEAD($url, on_response => sub {
+            my ($response) = @_;
+            my $len = $response->header('Content-Length');
 
             if (!defined($len)) {
                 die "Cannot find a content-length header.";
                     num_connections => $num_connections,
                 }
             );
-        };
+        },
+        );
     }
 
     my $signal_handler = sub { $self->_abort_signal_handler(); };
     local $SIG{INT} = $signal_handler;
     local $SIG{TERM} = $signal_handler;
 
-    $self->_finished_condvar->recv;
+    $loop->run;
     $self->_stats_timer(undef());
 
     if (! $self->_remaining_connections())