]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_p2p/apt_p2p.py
Massive work on the peer downloading, not working or complete yet.
[quix0rs-apt-p2p.git] / apt_p2p / apt_p2p.py
index 83f7c2f84363da4cb6f17efcd4fd87324794378d..0063e9fffd2ebb08373be003c346c4d90c950d63 100644 (file)
@@ -18,6 +18,7 @@ from twisted.web2 import server, http, http_headers, static
 from twisted.python import log, failure
 from twisted.python.filepath import FilePath
 
+from interfaces import IDHT, IDHTStats
 from apt_p2p_conf import config
 from PeerManager import PeerManager
 from HTTPServer import TopLevel
@@ -38,12 +39,14 @@ class AptP2P:
     Contains all of the sub-components that do all the low-level work, and
     coordinates communication between them.
     
+    @type dhtClass: L{interfaces.IDHT}
+    @ivar dhtClass: the DHT class to use
     @type cache_dir: L{twisted.python.filepath.FilePath}
     @ivar cache_dir: the directory to use for storing all files
     @type db: L{db.DB}
     @ivar db: the database to use for tracking files and hashes
     @type dht: L{interfaces.IDHT}
-    @ivar dht: the DHT instance to use
+    @ivar dht: the DHT instance
     @type http_server: L{HTTPServer.TopLevel}
     @ivar http_server: the web server that will handle all requests from apt
         and from other peers
@@ -59,23 +62,25 @@ class AptP2P:
         download information (IP address and port)
     """
     
-    def __init__(self, dht):
+    def __init__(self, dhtClass):
         """Initialize all the sub-components.
         
         @type dht: L{interfaces.IDHT}
-        @param dht: the DHT instance to use
+        @param dht: the DHT class to use
         """
         log.msg('Initializing the main apt_p2p application')
-        self.cache_dir = FilePath(config.get('DEFAULT', 'cache_dir'))
+        self.dhtClass = dhtClass
+        self.cache_dir = FilePath(config.get('DEFAULT', 'CACHE_DIR'))
         if not self.cache_dir.child(download_dir).exists():
             self.cache_dir.child(download_dir).makedirs()
         self.db = DB(self.cache_dir.child('apt-p2p.db'))
-        self.dht = dht
+        self.dht = dhtClass()
         self.dht.loadConfig(config, config.get('DEFAULT', 'DHT'))
         self.dht.join().addCallbacks(self.joinComplete, self.joinError)
-        self.http_server = TopLevel(self.cache_dir.child(download_dir), self.db, self)
+        self.http_server = TopLevel(self.cache_dir.child(download_dir), self.db, self,
+                                    config.getint('DEFAULT', 'UPLOAD_LIMIT'))
         self.getHTTPFactory = self.http_server.getHTTPFactory
-        self.peers = PeerManager()
+        self.peers = PeerManager(self.cache_dir, self.dht)
         self.mirrors = MirrorManager(self.cache_dir, config.gettime('DEFAULT', 'UNLOAD_PACKAGES_CACHE'))
         other_dirs = [FilePath(f) for f in config.getstringlist('DEFAULT', 'OTHER_DIRS')]
         self.cache = CacheManager(self.cache_dir.child(download_dir), self.db, other_dirs, self)
@@ -124,6 +129,18 @@ class AptP2P:
             storeDefer.addBoth(self._refreshFiles, hashes)
         else:
             reactor.callLater(60, self.refreshFiles)
+    
+    def getStats(self):
+        """Retrieve and format the statistics for the program.
+        
+        @rtype: C{string}
+        @return: the formatted HTML page containing the statistics
+        """
+        out = '<html><body>\n\n'
+        if IDHTStats.implementedBy(self.dhtClass):
+            out += self.dht.getStats()
+        out += '\n</body></html>\n'
+        return out
 
     #{ Main workflow
     def check_freshness(self, req, url, modtime, resp):