public override void Execute(Cancellation.CancellationToken cancellationToken)
        {
            base.Execute(cancellationToken);

            DebugLogger.Log("Downloading package.");

            _status.IsActive.Value   = true;
            _status.TotalBytes.Value = _resource.Size;

            var downloader = new RemoteResourceDownloader(_destinationPackagePath, _destinationMetaPath, _resource);

            downloader.DownloadProgressChanged += bytes => { _status.Bytes.Value = bytes; };

            var downloadStartTime = DateTime.Now;

            var stalledTimeout = TimeSpan.FromSeconds(10);

            using (_status.BytesPerSecond.Subscribe(bps =>
                                                    _status.Description.Value = bps > 0.01 || DateTime.Now - downloadStartTime < stalledTimeout ? "Downloading package..." : "Stalled..."))
            {
                downloader.Download(cancellationToken);
            }

            _status.IsActive.Value = false;
        }
    public void UseChunkedHttpDownloader()
    {
        RemoteResource resource = CreateTestRemoteResource();

        var httpDownloader        = Substitute.For <IHttpDownloader>();
        var chunkedHttpDownloader = Substitute.For <IChunkedHttpDownloader>();

        var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource,
                                                      (path, urls) => httpDownloader,
                                                      (path, urls, data, size) => chunkedHttpDownloader);

        downloader.Download(CancellationToken.Empty);

        httpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
        chunkedHttpDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
    }
        public override void Execute(CancellationToken cancellationToken)
        {
            base.Execute(cancellationToken);

            DebugLogger.Log("Downloading package.");

            var downloader = new RemoteResourceDownloader(_destinationPackagePath, _destinationMetaPath, _resource,
                                                          _useTorrents);

            downloader.DownloadProgressChanged += bytes => _statusReporter.OnDownloadProgressChanged(bytes, _resource.Size);

            _statusReporter.OnDownloadStarted();

            downloader.Download(cancellationToken);

            _statusReporter.OnDownloadEnded();
        }
    public void UseTorrentDownloaderFirst()
    {
        RemoteResource resource = CreateTestRemoteResource();

        var httpDownloader        = Substitute.For <IHttpDownloader>();
        var chunkedHttpDownloader = Substitute.For <IChunkedHttpDownloader>();
        var torrentDownloader     = Substitute.For <ITorrentDownloader>();

        var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, true,
                                                      (path, remoteResource, timeout) => httpDownloader,
                                                      (path, remoteResource, timeout) => chunkedHttpDownloader,
                                                      (path, remoteResource, timeout) => torrentDownloader);

        downloader.Download(CancellationToken.Empty);

        httpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
        chunkedHttpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
        torrentDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
    }
    public void UseHttpDownloaderIfChunksAreNotAvailable()
    {
        RemoteResource resource = CreateTestRemoteResource();

        resource.ChunksData.Chunks = new Chunk[0];

        var httpDownloader        = Substitute.For <IHttpDownloader>();
        var chunkedHttpDownloader = Substitute.For <IChunkedHttpDownloader>();
        var torrentDownloader     = Substitute.For <ITorrentDownloader>();

        var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, false,
                                                      (path, remoteResource, timeout) => httpDownloader,
                                                      (path, remoteResource, timeout) => chunkedHttpDownloader,
                                                      (path, remoteResource, timeout) => torrentDownloader);

        downloader.Download(CancellationToken.Empty);

        httpDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
        chunkedHttpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
        torrentDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
    }
    public void UseChunkedHttpDownloaderIfTorrentFails()
    {
        RemoteResource resource = CreateTestRemoteResource();

        var httpDownloader        = Substitute.For <IHttpDownloader>();
        var chunkedHttpDownloader = Substitute.For <IChunkedHttpDownloader>();
        var torrentDownloader     = Substitute.For <ITorrentDownloader>();

        torrentDownloader.When(t => t.Download(CancellationToken.Empty)).Do(
            info => { throw new DownloadFailureException("Test."); });

        var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, true,
                                                      (path, urls) => httpDownloader,
                                                      (path, urls, data, size) => chunkedHttpDownloader,
                                                      (path, filePath, bytes) => torrentDownloader);

        downloader.Download(CancellationToken.Empty);

        chunkedHttpDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
        torrentDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
    }
    public void UseChunkedHttpDownloaderIfTorrentFails()
    {
        RemoteResource resource = CreateTestRemoteResource();

        var httpDownloader        = Substitute.For <IHttpDownloader>();
        var chunkedHttpDownloader = Substitute.For <IChunkedHttpDownloader>();
        var torrentDownloader     = Substitute.For <ITorrentDownloader>();

        torrentDownloader.When(t => t.Download(CancellationToken.Empty)).Do(
            info => { throw new DownloaderException("Test.", DownloaderExceptionStatus.Other); });

        var downloader = new RemoteResourceDownloader(_filePath, _metaFilePath, resource, true,
                                                      (path, remoteResource, timeout) => httpDownloader,
                                                      (path, remoteResource, timeout) => chunkedHttpDownloader,
                                                      (path, remoteResource, timeout) => torrentDownloader);

        downloader.Download(CancellationToken.Empty);

        httpDownloader.DidNotReceiveWithAnyArgs().Download(CancellationToken.Empty);
        chunkedHttpDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
        torrentDownloader.ReceivedWithAnyArgs().Download(CancellationToken.Empty);
    }