Example #1
0
        public static void Main()
        {
            bool useDownloadSpeedThrottling = false;

            // Please insert an URL of a large file here, otherwise the download will be finished too quickly to really demonstrate the functionality.
            var url               = new Uri("http://yourdomain.com/yourfile.zip");
            var file              = new System.IO.FileInfo("yourfile.zip");
            var requestBuilder    = new SimpleWebRequestBuilder();
            var dlChecker         = new DownloadChecker();
            var httpDlBuilder     = new SimpleDownloadBuilder(requestBuilder, dlChecker);
            var timeForHeartbeat  = 3000;
            var timeToRetry       = 5000;
            var maxRetries        = 5;
            var resumingDlBuilder = new ResumingDownloadBuilder(timeForHeartbeat, timeToRetry, maxRetries, httpDlBuilder);
            List <DownloadRange> alreadyDownloadedRanges = null;
            var bufferSize            = 4096;
            var numberOfParts         = 4;
            var maxRetryDownloadParts = 2;
            var download     = new MultiPartDownload(url, bufferSize, numberOfParts, resumingDlBuilder, requestBuilder, dlChecker, alreadyDownloadedRanges, maxRetryDownloadParts);
            var speedMonitor = new DownloadSpeedMonitor(maxSampleCount: 128);

            speedMonitor.Attach(download);
            var progressMonitor = new DownloadProgressMonitor();

            progressMonitor.Attach(download);

            if (useDownloadSpeedThrottling)
            {
                var downloadThrottling = new DownloadThrottling(maxBytesPerSecond: 200 * 1024, maxSampleCount: 128);
                downloadThrottling.Attach(download);
            }

            var dlSaver = new DownloadToFileSaver(file);

            dlSaver.Attach(download);
            download.DownloadCompleted += OnCompleted;
            download.DownloadCancelled += OnDownloadCancelled;
            download.DownloadStopped   += OnDownloadStopped;
            download.Start();

            while (!finished)
            {
                Thread.Sleep(1000);

                var alreadyDownloadedSizeInBytes = progressMonitor.GetCurrentProgressInBytes(download);
                var totalDownloadSizeInBytes     = progressMonitor.GetTotalFilesizeInBytes(download);
                var currentSpeedInBytesPerSecond = speedMonitor.GetCurrentBytesPerSecond();

                var currentProgressInPercent   = progressMonitor.GetCurrentProgressPercentage(download) * 100;
                var alreadyDownloadedSizeInKiB = (alreadyDownloadedSizeInBytes / 1024);
                var totalDownloadSizeInKiB     = (totalDownloadSizeInBytes / 1024);
                var currentSpeedInKiBPerSecond = (currentSpeedInBytesPerSecond / 1024);
                var remainingTimeInSeconds     = currentSpeedInBytesPerSecond == 0 ? 0 : (totalDownloadSizeInBytes - alreadyDownloadedSizeInBytes) / currentSpeedInBytesPerSecond;

                Console.WriteLine(
                    "Progress: " + currentProgressInPercent + "% " + "(" + alreadyDownloadedSizeInKiB + " of " + totalDownloadSizeInKiB + " KiB)" +
                    "   Speed: " + currentSpeedInKiBPerSecond + " KiB/sec." +
                    "   Remaining time: " + remainingTimeInSeconds + " sec.");
            }
        }
Example #2
0
        /// <summary>
        /// Update Downlaoder
        /// </summary>
        /// <param name="downloadurl">file url to download</param>
        public void DownloadUpdate()
        {
            bool useDownloadSpeedThrottling = false;

            var file              = new FileInfo(Path.GetFileName(DownloadUrl.ToString()));
            var requestBuilder    = new SimpleWebRequestBuilder();
            var dlChecker         = new DownloadChecker();
            var httpDlBuilder     = new SimpleDownloadBuilder(requestBuilder, dlChecker);
            var timeForHeartbeat  = 3000;
            var timeToRetry       = 5000;
            var maxRetries        = 25;
            var resumingDlBuilder = new ResumingDownloadBuilder(timeForHeartbeat, timeToRetry, maxRetries, httpDlBuilder);
            List <DownloadRange> alreadyDownloadedRanges = null;
            var bufferSize    = 4096;
            var numberOfParts = 4;
            var download      = new MultiPartDownload(DownloadUrl, bufferSize, numberOfParts, resumingDlBuilder, requestBuilder, dlChecker, alreadyDownloadedRanges);
            var speedMonitor  = new DownloadSpeedMonitor(maxSampleCount: 50000);

            speedMonitor.Attach(download);
            var progressMonitor = new DownloadProgressMonitor();

            progressMonitor.Attach(download);

            if (useDownloadSpeedThrottling)
            {
                var downloadThrottling = new DownloadThrottling(maxBytesPerSecond: 200 * 10024, maxSampleCount: 1288);
                downloadThrottling.Attach(download);
            }

            var dlSaver = new DownloadToFileSaver(file);

            dlSaver.Attach(download);
            download.DownloadCompleted += OnCompleted;
            download.Start();

            while (!DownloadFinished)
            {
                var alreadyDownloadedSizeInBytes = progressMonitor.GetCurrentProgressInBytes(download);
                var totalDownloadSizeInBytes     = progressMonitor.GetTotalFilesizeInBytes(download);
                var currentSpeedInBytesPerSecond = speedMonitor.GetCurrentBytesPerSecond();

                var currentProgressInPercent   = progressMonitor.GetCurrentProgressPercentage(download) * 100;
                var alreadyDownloadedSizeInKiB = (alreadyDownloadedSizeInBytes / 1024);
                var totalDownloadSizeInKiB     = (totalDownloadSizeInBytes / 1024);
                var currentSpeedInKiBPerSecond = (currentSpeedInBytesPerSecond / 1024);
                var remainingTimeInSeconds     = currentSpeedInBytesPerSecond == 0 ? 0 : (totalDownloadSizeInBytes - alreadyDownloadedSizeInBytes) / currentSpeedInBytesPerSecond;

                DownloaderProgress.Progress       = currentProgressInPercent;
                DownloaderProgress.DownlaodedSize = alreadyDownloadedSizeInKiB;
                DownloaderProgress.TotalSize      = totalDownloadSizeInKiB;
                DownloaderProgress.Speed          = currentSpeedInKiBPerSecond;
                DownloaderProgress.RemainingTime  = remainingTimeInSeconds;
            }
        }