]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_dht_Khashmir/DHT.py
Add more to khashmir's DHT implementation.
[quix0rs-apt-p2p.git] / apt_dht_Khashmir / DHT.py
index 78d913b225767dc581a0326f38101245491430b7..ca5bb609bc38d1ad8a7d86418efb54e088d93d37 100644 (file)
@@ -1,11 +1,61 @@
 
+from twisted.internet import defer
 from zope.interface import implements
 
 from apt_dht.interfaces import IDHT
+from khashmir import Khashmir
+
+class DHTError(Exception):
+    """Represents errors that occur in the DHT."""
 
 class DHT:
     
     implements(IDHT)
     
     def __init__(self):
-        pass
\ No newline at end of file
+        self.config = None
+    
+    def loadConfig(self, config, section):
+        """See L{apt_dht.interfaces.IDHT}."""
+        self.config = config
+        self.section = section
+        if self.config.has_option(section, 'port'):
+            self.port = self.config.get(section, 'port')
+        else:
+            self.port = self.config.get('DEFAULT', 'port')
+    
+    def join(self):
+        """See L{apt_dht.interfaces.IDHT}."""
+        if self.config is None:
+            raise DHTError, "configuration not loaded"
+
+        self.khashmir = Khashmir('', self.port)
+        
+        for node in self.config.get(self.section, 'bootstrap'):
+            host, port = node.rsplit(':', 1)
+            self.khashmir.addContact(host, port)
+            
+        self.khashmir.findCloseNodes()
+        
+    def leave(self):
+        """See L{apt_dht.interfaces.IDHT}."""
+        if self.config is None:
+            raise DHTError, "configuration not loaded"
+
+        self.khashmir.listenport.stopListening()
+        
+    def getValue(self, key):
+        """See L{apt_dht.interfaces.IDHT}."""
+        if self.config is None:
+            raise DHTError, "configuration not loaded"
+
+        d = defer.Deferred()
+        self.khashmir.valueForKey(key, d.callback)
+        return d
+        
+    def storeValue(self, key, value):
+        """See L{apt_dht.interfaces.IDHT}."""
+        if self.config is None:
+            raise DHTError, "configuration not loaded"
+
+        self.khashmir.storeValueForKey(key, value)