-
- def store(self, hash):
- """Add a key/value pair for the file to the DHT.
-
- Sets the key and value from the hash information, and tries to add
- it to the DHT.
- """
- key = hash.digest()
- value = {'c': self.my_contact}
- pieces = hash.pieceDigests()
-
- # Determine how to store any piece data
- if len(pieces) <= 1:
- pass
- elif len(pieces) <= DHT_PIECES:
- # Short enough to be stored with our peer contact info
- value['t'] = {'t': ''.join(pieces)}
- elif len(pieces) <= TORRENT_PIECES:
- # Short enough to be stored in a separate key in the DHT
- value['h'] = sha.new(''.join(pieces)).digest()
- else:
- # Too long, must be served up by our peer HTTP server
- value['l'] = sha.new(''.join(pieces)).digest()
-
- storeDefer = self.dht.storeValue(key, value)
- storeDefer.addCallbacks(self.store_done, self.store_error,
- callbackArgs = (hash, ), errbackArgs = (hash.digest(), ))
- return storeDefer
-
- def store_done(self, result, hash):
- """Add a key/value pair for the pieces of the file to the DHT (if necessary)."""
- log.msg('Added %s to the DHT: %r' % (hash.hexdigest(), result))
- pieces = hash.pieceDigests()
- if len(pieces) > DHT_PIECES and len(pieces) <= TORRENT_PIECES:
- # Add the piece data key and value to the DHT
- key = sha.new(''.join(pieces)).digest()
- value = {'t': ''.join(pieces)}
-
- storeDefer = self.dht.storeValue(key, value)
- storeDefer.addCallbacks(self.store_torrent_done, self.store_error,
- callbackArgs = (key, ), errbackArgs = (key, ))
- return storeDefer
- return result
-
- def store_torrent_done(self, result, key):
- """Adding the file to the DHT is complete, and so is the workflow."""
- log.msg('Added torrent string %s to the DHT: %r' % (b2a_hex(key), result))
- return result
-
- def store_error(self, err, key):
- """Adding to the DHT failed."""
- log.msg('An error occurred adding %s to the DHT: %r' % (b2a_hex(key), err))
- return err