]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
Delay the creation of files until after the application has finished loading.
authorCameron Dale <camrdale@gmail.com>
Thu, 17 Apr 2008 19:57:18 +0000 (12:57 -0700)
committerCameron Dale <camrdale@gmail.com>
Thu, 17 Apr 2008 19:57:18 +0000 (12:57 -0700)
apt-p2p.py
apt_p2p/apt_p2p.py

index adbc7bdd72e679c67d3140a756760a71b102217c..277cd76a93e25747165669f8fa1214c09e41bb23 100644 (file)
@@ -58,32 +58,32 @@ try:
 except:
     uid,gid = None,None
 
-log.msg('Starting application')
+log.msg('Starting application with uid/gid %r/%r' % (uid, gid))
 application = service.Application("apt-p2p", uid, gid)
 #print service.IProcess(application).processName
 #service.IProcess(application).processName = 'apt-p2p'
 
-log.msg('Starting DHT')
 DHT = __import__(config.get('DEFAULT', 'DHT')+'.DHT', globals(), locals(), ['DHT'])
 assert IDHT.implementedBy(DHT.DHT), "You must provide a DHT implementation that implements the IDHT interface."
 
 if not config.getboolean('DEFAULT', 'DHT-only'):
     log.msg('Starting main application server')
     from apt_p2p.apt_p2p import AptP2P
-    myapp = AptP2P(DHT.DHT)
-    factory = myapp.getHTTPFactory()
+    factory = AptP2P(DHT.DHT)
     s = strports.service('tcp:'+config.get('DEFAULT', 'port'), factory)
     s.setServiceParent(application)
 else:
+    log.msg('Starting the DHT')
     myDHT = DHT.DHT()
+    
     if IDHTStatsFactory.implementedBy(DHT.DHT):
         log.msg("Starting the DHT's HTTP stats displayer")
         factory = myDHT.getStatsFactory()
         s = strports.service('tcp:'+config.get('DEFAULT', 'port'), factory)
         s.setServiceParent(application)
         
-    myDHT.loadConfig(config, config.get('DEFAULT', 'DHT'))
-    myDHT.join()
+    reactor.callLater(0, myDHT.loadConfig, config, config.get('DEFAULT', 'DHT'))
+    reactor.callLater(0, myDHT.join)
 
 if __name__ == '__main__':
     # Run on command line
index 4200f6299b78377593ce17cd6ad7b6973de7578e..af7790a7bb6ec06165cdfd552b27cba988c681b0 100644 (file)
@@ -14,7 +14,7 @@ from urlparse import urlunparse
 from urllib import unquote
 import os, re, sha
 
-from twisted.internet import defer, reactor
+from twisted.internet import defer, reactor, protocol
 from twisted.web2 import server, http, http_headers, static
 from twisted.python import log, failure
 from twisted.python.filepath import FilePath
@@ -36,7 +36,7 @@ TORRENT_PIECES = 70
 download_dir = 'cache'
 peer_dir = 'peers'
 
-class AptP2P:
+class AptP2P(protocol.Factory):
     """The main code object that does all of the work.
     
     Contains all of the sub-components that do all the low-level work, and
@@ -75,29 +75,40 @@ class AptP2P:
         """
         log.msg('Initializing the main apt_p2p application')
         self.dhtClass = dhtClass
+
+    #{ Factory interface
+    def startFactory(self):
+        reactor.callLater(0, self._startFactory)
+        
+    def _startFactory(self):
+        log.msg('Starting the main apt_p2p application')
         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()
         if not self.cache_dir.child(peer_dir).exists():
             self.cache_dir.child(peer_dir).makedirs()
         self.db = DB(self.cache_dir.child('apt-p2p.db'))
-        self.dht = dhtClass()
+        self.dht = self.dhtClass()
         self.dht.loadConfig(config, config.get('DEFAULT', 'DHT'))
         self.dht.join().addCallbacks(self.joinComplete, self.joinError)
         self.stats = StatsLogger(self.db)
         self.http_server = TopLevel(self.cache_dir.child(download_dir), self.db, self)
-        self.getHTTPFactory = self.http_server.getHTTPFactory
+        self.http_server.getHTTPFactory().startFactory()
         self.peers = PeerManager(self.cache_dir.child(peer_dir), self.dht, self.stats)
         self.mirrors = MirrorManager(self.cache_dir)
         self.cache = CacheManager(self.cache_dir.child(download_dir), self.db, self)
         self.my_contact = None
-        reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown)
-
-    #{ Maintenance
-    def shutdown(self):
+        
+    def stopFactory(self):
+        log.msg('Stoppping the main apt_p2p application')
+        self.http_server.getHTTPFactory().stopFactory()
         self.stats.save()
         self.db.close()
+    
+    def buildProtocol(self, addr):
+        return self.http_server.getHTTPFactory().buildProtocol(addr)
         
+    #{ DHT Maintenance
     def joinComplete(self, result):
         """Complete the DHT join process and determine our download information.