fix issue in republishing keys with only digest available, fixing #25

This commit is contained in:
Brian Muller 2017-03-15 14:36:00 -04:00
parent f3d1d2e6a5
commit e58c710da0
2 changed files with 14 additions and 5 deletions

View File

@ -2,5 +2,5 @@ language: python
python: python:
- "2.7" - "2.7"
- "pypy" - "pypy"
install: pip install . --use-mirrors && pip install pep8 pyflakes install: pip install . pep8 pyflakes
script: make test script: make test

View File

@ -2,6 +2,7 @@
Package for interacting on the network at a high level. Package for interacting on the network at a high level.
""" """
import random import random
import binascii
import pickle import pickle
from twisted.internet.task import LoopingCall from twisted.internet.task import LoopingCall
@ -67,8 +68,8 @@ class Server(object):
def republishKeys(_): def republishKeys(_):
ds = [] ds = []
# Republish keys older than one hour # Republish keys older than one hour
for key, value in self.storage.iteritemsOlderThan(3600): for dkey, value in self.storage.iteritemsOlderThan(3600):
ds.append(self.set(key, value)) ds.append(self.digest_set(dkey, value))
return defer.gatherResults(ds) return defer.gatherResults(ds)
return defer.gatherResults(ds).addCallback(republishKeys) return defer.gatherResults(ds).addCallback(republishKeys)
@ -153,10 +154,18 @@ class Server(object):
""" """
self.log.debug("setting '%s' = '%s' on network" % (key, value)) self.log.debug("setting '%s' = '%s' on network" % (key, value))
dkey = digest(key) dkey = digest(key)
return self.digest_set(dkey, value)
def digest_set(self, dkey, value):
"""
Set the given SHA1 digest key to the given value in the network.
"""
node = Node(dkey) node = Node(dkey)
# this is useful for debugging messages
hkey = binascii.hexlify(dkey)
def store(nodes): def store(nodes):
self.log.info("setting '%s' on %s" % (key, map(str, nodes))) self.log.info("setting '%s' on %s" % (hkey, map(str, nodes)))
# if this node is close too, then store here as well # if this node is close too, then store here as well
if self.node.distanceTo(node) < max([n.distanceTo(node) for n in nodes]): if self.node.distanceTo(node) < max([n.distanceTo(node) for n in nodes]):
self.storage[dkey] = value self.storage[dkey] = value
@ -165,7 +174,7 @@ class Server(object):
nearest = self.protocol.router.findNeighbors(node) nearest = self.protocol.router.findNeighbors(node)
if len(nearest) == 0: if len(nearest) == 0:
self.log.warning("There are no known neighbors to set key %s" % key) self.log.warning("There are no known neighbors to set key %s" % hkey)
return defer.succeed(False) return defer.succeed(False)
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha) spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
return spider.find().addCallback(store) return spider.find().addCallback(store)