From e99d87c0e871795a480cf4cca88738758cd60016 Mon Sep 17 00:00:00 2001 From: Brian Muller Date: Tue, 14 Jan 2014 18:21:40 -0500 Subject: [PATCH] fixed a few bugs --- kademlia/network.py | 30 +++++++++++++++++++++++++++++- kademlia/routing.py | 2 +- server.tac | 25 +++++++++---------------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/kademlia/network.py b/kademlia/network.py index 2aed90b..9df994b 100644 --- a/kademlia/network.py +++ b/kademlia/network.py @@ -2,6 +2,7 @@ Package for interacting on the network at a high level. """ import random +import pickle from twisted.internet.task import LoopingCall from twisted.internet import defer, reactor @@ -93,7 +94,7 @@ class SpiderCrawl(object): # where will be a list of tuples if not found or # a dictionary of {'value': v} where v is the value desired if not response[0]: - toremove.push(peerid) + toremove.append(peerid) elif isinstance(response[1], dict): self.log.debug("found value for %i" % self.node.long_id) return response[1] @@ -235,3 +236,30 @@ class Server(object): nearest = self.protocol.router.findNeighbors(node) spider = SpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha) return spider.findNodes().addCallback(store) + + def save(self, fname): + data = { 'ksize': self.ksize, + 'alpha': self.alpha, + 'id': self.node.id, + 'neighbors': self.bootstrappableNeighbors() } + with open(fname, 'w') as f: + pickle.dump(data, f) + + @classmethod + def load(self, fname): + with open(fname, 'r') as f: + data = pickle.load(f) + s = Server(data['ksize'], data['alpha'], data['id']) + if len(data['neighbors']) > 0: + s.bootstrap(data['neighbors']) + return s + + def saveRegularly(self, fname, frequency=600): + """ + @param fname: File to save retularly to + @param frequencey: Frequency in seconds that the state + should be saved. By default, 10 minutes. + """ + loop = LoopingCall(self.save, fname) + loop.start(frequency) + return loop diff --git a/kademlia/routing.py b/kademlia/routing.py index 1be310e..3bbe3ed 100644 --- a/kademlia/routing.py +++ b/kademlia/routing.py @@ -112,7 +112,7 @@ class RoutingTable(object): return [b for b in self.buckets if b.lastUpdated < (time.time() - 3600)] def removeContact(self, node): - index = self.getBucketFor(self, node) + index = self.getBucketFor(node) self.buckets[index].removeNode(node) def addContact(self, node): diff --git a/server.tac b/server.tac index 947aa6d..56ea272 100644 --- a/server.tac +++ b/server.tac @@ -2,27 +2,20 @@ from twisted.application import service, internet from twisted.python.log import ILogObserver from twisted.internet import reactor, task -import sys, os, pickle +import sys, os sys.path.append(os.path.dirname(__file__)) from kademlia.network import Server from kademlia import log - -if os.path.isfile('cache.pickle'): - data = pickle.load(open('cache.pickle')) - kserver = Server(id = data['id']) - kserver.bootstrap(data['servers']) -else: - kserver = Server() - application = service.Application("kademlia") application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit) + +if os.path.isfile('cache.pickle'): + kserver = Server.load('cache.pickle') +else: + kserver = Server() + kserver.bootstrap([("1.2.3.4", 8468)]) +kserver.saveRegularly('cache.pickle', 10) + server = internet.UDPServer(8468, kserver.protocol) server.setServiceParent(application) - -def updateCache(): - data = { 'id': kserver.node.id, 'servers': kserver.bootstrappableNeighbors() } - with open('cache.pickle', 'w') as f: - pickle.dump(data, f) - -task.LoopingCall(updateCache).start(5.0, False)