2 from twisted.internet import defer
3 from zope.interface import implements
5 from apt_dht.interfaces import IDHT
6 from khashmir import Khashmir
8 class DHTError(Exception):
9 """Represents errors that occur in the DHT."""
18 def loadConfig(self, config, section):
19 """See L{apt_dht.interfaces.IDHT}."""
21 self.section = section
22 if self.config.has_option(section, 'port'):
23 self.port = self.config.get(section, 'port')
25 self.port = self.config.get('DEFAULT', 'port')
28 """See L{apt_dht.interfaces.IDHT}."""
29 if self.config is None:
30 raise DHTError, "configuration not loaded"
32 self.khashmir = Khashmir('', self.port)
34 for node in self.config.get(self.section, 'bootstrap'):
35 host, port = node.rsplit(':', 1)
36 self.khashmir.addContact(host, port)
38 self.khashmir.findCloseNodes()
41 """See L{apt_dht.interfaces.IDHT}."""
42 if self.config is None:
43 raise DHTError, "configuration not loaded"
45 self.khashmir.listenport.stopListening()
47 def getValue(self, key):
48 """See L{apt_dht.interfaces.IDHT}."""
49 if self.config is None:
50 raise DHTError, "configuration not loaded"
53 self.khashmir.valueForKey(key, d.callback)
56 def storeValue(self, key, value):
57 """See L{apt_dht.interfaces.IDHT}."""
58 if self.config is None:
59 raise DHTError, "configuration not loaded"
61 self.khashmir.storeValueForKey(key, value)