From 4a4a4f95202ec97630ec20a455eff9de153a9cc6 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Sat, 15 Aug 2009 12:25:43 -0700
Subject: [PATCH] immutable.Downloader: pass StorageBroker to constructor, stop
 being a Service child of the client, access with client.downloader instead of
 client.getServiceNamed("downloader"). The single "Downloader" instance is
 scheduled for demolition anyways, to be replaced by individual
 filenode.download calls.

---
 src/allmydata/client.py              |  4 ++--
 src/allmydata/control.py             |  3 +--
 src/allmydata/immutable/download.py  | 14 +++++---------
 src/allmydata/test/common.py         |  3 +--
 src/allmydata/test/test_dirnode.py   |  2 +-
 src/allmydata/test/test_immutable.py |  9 +++------
 src/allmydata/test/test_system.py    |  3 +--
 7 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/src/allmydata/client.py b/src/allmydata/client.py
index a664660a..197b4386 100644
--- a/src/allmydata/client.py
+++ b/src/allmydata/client.py
@@ -264,7 +264,7 @@ class Client(node.Node, pollmixin.PollMixin):
                                          "private", "cache", "download")
         self.download_cache_dirman = cachedir.CacheDirectoryManager(download_cachedir)
         self.download_cache_dirman.setServiceParent(self)
-        self.add_service(Downloader(self.stats_provider))
+        self.downloader = Downloader(self.storage_broker, self.stats_provider)
         self.init_stub_client()
         self.init_nodemaker()
 
@@ -323,7 +323,7 @@ class Client(node.Node, pollmixin.PollMixin):
                                    self._secret_holder,
                                    self.get_history(),
                                    self.getServiceNamed("uploader"),
-                                   self.getServiceNamed("downloader"),
+                                   self.downloader,
                                    self.download_cache_dirman,
                                    self.get_encoding_parameters(),
                                    self._key_generator)
diff --git a/src/allmydata/control.py b/src/allmydata/control.py
index 060608b4..d629c136 100644
--- a/src/allmydata/control.py
+++ b/src/allmydata/control.py
@@ -50,8 +50,7 @@ class ControlServer(Referenceable, service.Service):
         return d
 
     def remote_download_from_uri_to_file(self, uri, filename):
-        downloader = self.parent.getServiceNamed("downloader")
-        d = downloader.download_to_filename(uri, filename)
+        d = self.parent.downloader.download_to_filename(uri, filename)
         d.addCallback(lambda res: filename)
         return d
 
diff --git a/src/allmydata/immutable/download.py b/src/allmydata/immutable/download.py
index 01ce4127..cd185bea 100644
--- a/src/allmydata/immutable/download.py
+++ b/src/allmydata/immutable/download.py
@@ -2,7 +2,6 @@ import os, random, weakref, itertools, time
 from zope.interface import implements
 from twisted.internet import defer
 from twisted.internet.interfaces import IPushProducer, IConsumer
-from twisted.application import service
 from foolscap.api import DeadReferenceError, RemoteException, eventually
 
 from allmydata.util import base32, deferredutil, hashutil, log, mathutil, idlib
@@ -1185,22 +1184,19 @@ class ConsumerAdapter:
         pass
 
 
-class Downloader(service.MultiService):
+class Downloader:
     """I am a service that allows file downloading.
     """
     # TODO: in fact, this service only downloads immutable files (URI:CHK:).
     # It is scheduled to go away, to be replaced by filenode.download()
     implements(IDownloader)
-    name = "downloader"
 
-    def __init__(self, stats_provider=None):
-        service.MultiService.__init__(self)
+    def __init__(self, storage_broker, stats_provider):
+        self.storage_broker = storage_broker
         self.stats_provider = stats_provider
         self._all_downloads = weakref.WeakKeyDictionary() # for debugging
 
     def download(self, u, t, _log_msg_id=None, monitor=None, history=None):
-        assert self.parent
-        assert self.running
         u = IFileURI(u)
         t = IDownloadTarget(t)
         assert t.write
@@ -1212,12 +1208,12 @@ class Downloader(service.MultiService):
             # include LIT files
             self.stats_provider.count('downloader.files_downloaded', 1)
             self.stats_provider.count('downloader.bytes_downloaded', u.get_size())
-        storage_broker = self.parent.get_storage_broker()
 
         target = DecryptingTarget(t, u.key, _log_msg_id=_log_msg_id)
         if not monitor:
             monitor=Monitor()
-        dl = CiphertextDownloader(storage_broker, u.get_verify_cap(), target,
+        dl = CiphertextDownloader(self.storage_broker,
+                                  u.get_verify_cap(), target,
                                   monitor=monitor)
         self._all_downloads[dl] = None
         if history:
diff --git a/src/allmydata/test/common.py b/src/allmydata/test/common.py
index 85cf3cf4..2b0777cc 100644
--- a/src/allmydata/test/common.py
+++ b/src/allmydata/test/common.py
@@ -1027,8 +1027,7 @@ class ShareManglingMixin(SystemTestMixin):
         return sum_of_write_counts
 
     def _download_and_check_plaintext(self, unused=None):
-        self.downloader = self.clients[1].getServiceNamed("downloader")
-        d = self.downloader.download_to_data(self.uri)
+        d = self.clients[1].downloader.download_to_data(self.uri)
 
         def _after_download(result):
             self.failUnlessEqual(result, TEST_DATA)
diff --git a/src/allmydata/test/test_dirnode.py b/src/allmydata/test/test_dirnode.py
index 38fc02e2..d1bf2073 100644
--- a/src/allmydata/test/test_dirnode.py
+++ b/src/allmydata/test/test_dirnode.py
@@ -918,7 +918,7 @@ class Deleter(GridTestMixin, unittest.TestCase):
         def _do_delete(ignored):
             nm = UCWEingNodeMaker(c0.storage_broker, c0._secret_holder,
                                   c0.get_history(), c0.getServiceNamed("uploader"),
-                                  c0.getServiceNamed("downloader"),
+                                  c0.downloader,
                                   c0.download_cache_dirman,
                                   c0.get_encoding_parameters(),
                                   c0._key_generator)
diff --git a/src/allmydata/test/test_immutable.py b/src/allmydata/test/test_immutable.py
index cf46d00d..ad1843c0 100644
--- a/src/allmydata/test/test_immutable.py
+++ b/src/allmydata/test/test_immutable.py
@@ -26,8 +26,7 @@ class Test(common.ShareManglingMixin, unittest.TestCase):
         d.addCallback(_then_delete_8)
 
         def _then_download(unused=None):
-            self.downloader = self.clients[1].getServiceNamed("downloader")
-            d2 = self.downloader.download_to_data(self.uri)
+            d2 = self.clients[1].downloader.download_to_data(self.uri)
 
             def _after_download_callb(result):
                 self.fail() # should have gotten an errback instead
@@ -94,8 +93,7 @@ class Test(common.ShareManglingMixin, unittest.TestCase):
 
         before_download_reads = self._count_reads()
         def _attempt_to_download(unused=None):
-            downloader = self.clients[1].getServiceNamed("downloader")
-            d2 = downloader.download_to_data(self.uri)
+            d2 = self.clients[1].downloader.download_to_data(self.uri)
 
             def _callb(res):
                 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
@@ -128,8 +126,7 @@ class Test(common.ShareManglingMixin, unittest.TestCase):
 
         before_download_reads = self._count_reads()
         def _attempt_to_download(unused=None):
-            downloader = self.clients[1].getServiceNamed("downloader")
-            d2 = downloader.download_to_data(self.uri)
+            d2 = self.clients[1].downloader.download_to_data(self.uri)
 
             def _callb(res):
                 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
diff --git a/src/allmydata/test/test_system.py b/src/allmydata/test/test_system.py
index 5fa7e41d..b3126fef 100644
--- a/src/allmydata/test/test_system.py
+++ b/src/allmydata/test/test_system.py
@@ -135,8 +135,7 @@ class SystemTest(SystemTestMixin, unittest.TestCase):
             log.msg("upload finished: uri is %s" % (theuri,))
             self.uri = theuri
             assert isinstance(self.uri, str), self.uri
-            dl = self.clients[1].getServiceNamed("downloader")
-            self.downloader = dl
+            self.downloader = self.clients[1].downloader
         d.addCallback(_upload_done)
 
         def _upload_again(res):
-- 
2.45.2