[hyena] [Hyena.Downloader] Add DownloadManagerJob class



commit 8da3085bee4a5e6f395bf3f6bc51814691fd7806
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Thu Sep 2 12:41:47 2010 -0500

    [Hyena.Downloader] Add DownloadManagerJob class

 Hyena/Hyena.Downloader/DownloadManager.cs    |    4 +-
 Hyena/Hyena.Downloader/DownloadManagerJob.cs |  100 ++++++++++++++++++++++++++
 Hyena/Hyena.Downloader/Tests/Tests.cs        |    1 +
 Hyena/Makefile.am                            |    1 +
 4 files changed, 104 insertions(+), 2 deletions(-)
---
diff --git a/Hyena/Hyena.Downloader/DownloadManager.cs b/Hyena/Hyena.Downloader/DownloadManager.cs
index 6d12943..5825efa 100644
--- a/Hyena/Hyena.Downloader/DownloadManager.cs
+++ b/Hyena/Hyena.Downloader/DownloadManager.cs
@@ -32,7 +32,7 @@ namespace Hyena.Downloader
     public class DownloadManager
     {
         private object sync_root = new object ();
-        protected object SyncRoot {
+        internal object SyncRoot {
             get { return sync_root; }
         }
 
@@ -43,7 +43,7 @@ namespace Hyena.Downloader
             get { return pending_downloaders; }
         }
 
-        protected List<HttpDownloader> ActiveDownloaders {
+        internal List<HttpDownloader> ActiveDownloaders {
             get { return active_downloaders; }
         }
 
diff --git a/Hyena/Hyena.Downloader/DownloadManagerJob.cs b/Hyena/Hyena.Downloader/DownloadManagerJob.cs
new file mode 100644
index 0000000..4718db0
--- /dev/null
+++ b/Hyena/Hyena.Downloader/DownloadManagerJob.cs
@@ -0,0 +1,100 @@
+//
+// DownloadManagerJob.cs
+//
+// Author:
+//   Aaron Bockover <abockover novell com>
+//   Gabriel Burt <gburt novell com>
+//
+// Copyright 2010 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.IO;
+using System.Linq;
+using System.Collections.Generic;
+
+using Mono.Unix;
+
+using Hyena;
+
+namespace Hyena.Downloader
+{
+    public class DownloadManagerJob : Hyena.Jobs.Job
+    {
+        private DownloadManager manager;
+        private int finished_count = 0;
+
+        public DownloadManagerJob (DownloadManager manager)
+        {
+            this.manager = manager;
+            manager.Progress += OnDownloaderProgress;
+            manager.Finished += OnDownloaderFinished;
+        }
+
+        private void OnDownloaderProgress (HttpDownloader downloader)
+        {
+            FreezeUpdate ();
+
+            lock (manager.SyncRoot) {
+                double weight = 1.0 / (manager.TotalDownloadCount + finished_count);
+                double progress = finished_count * weight;
+                double speed = 0;
+                int count = 0;
+                foreach (var active_downloader in manager.ActiveDownloaders) {
+                    progress += weight * active_downloader.State.PercentComplete;
+                    speed = active_downloader.State.TransferRate;
+                    count++;
+                }
+                Progress = progress;
+
+                var human_speed = new Hyena.Query.FileSizeQueryValue ((long)Math.Round (speed)).ToUserQuery ();
+                if (manager.PendingDownloadCount == 0) {
+                    Status = String.Format (
+                        Catalog.GetPluralString (
+                            "{0} download at {1}/s",
+                            "{0} downloads at {1}/s",
+                            count),
+                        count, human_speed
+                    );
+                } else {
+                    Status = String.Format (
+                        Catalog.GetPluralString (
+                            "{0} download at {1}/s ({2} pending)",
+                            "{0} downloads at {1}/s ({2} pending)",
+                            count),
+                        count, human_speed, manager.PendingDownloadCount
+                    );
+                }
+            }
+
+            ThawUpdate (true);
+        }
+
+        private void OnDownloaderFinished (HttpDownloader downloader)
+        {
+            lock (manager.SyncRoot) {
+                finished_count++;
+                if (manager.TotalDownloadCount <= 0) {
+                    OnFinished ();
+                }
+            }
+        }
+    }
+}
diff --git a/Hyena/Hyena.Downloader/Tests/Tests.cs b/Hyena/Hyena.Downloader/Tests/Tests.cs
index 76b4a40..abb4086 100644
--- a/Hyena/Hyena.Downloader/Tests/Tests.cs
+++ b/Hyena/Hyena.Downloader/Tests/Tests.cs
@@ -58,6 +58,7 @@ namespace Hyena.Downloader.Tests
                 var size = new System.IO.FileInfo (d.LocalPath).Length;
                 Assert.IsTrue (size <= server.MaxResourceSize);
                 Assert.IsTrue (size >= server.MinResourceSize);
+                System.IO.File.Delete (d.LocalPath);
             };
             f.StartSync ();
         }
diff --git a/Hyena/Makefile.am b/Hyena/Makefile.am
index e4301f4..e4f6fe9 100644
--- a/Hyena/Makefile.am
+++ b/Hyena/Makefile.am
@@ -39,6 +39,7 @@ SOURCES =  \
 	Hyena.Data/SortType.cs \
 	Hyena.Downloader/Buffer.cs \
 	Hyena.Downloader/DownloadManager.cs \
+	Hyena.Downloader/DownloadManagerJob.cs \
 	Hyena.Downloader/HttpDownloader.cs \
 	Hyena.Downloader/HttpDownloaderState.cs \
 	Hyena.Downloader/HttpFileDownloader.cs \



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]