From abf5fe19adf0a2d30619f2be2f81edd7f80c2b83 Mon Sep 17 00:00:00 2001 From: Brian Muller Date: Mon, 13 Jan 2014 21:44:44 -0500 Subject: [PATCH] fixed a few bugs --- .gitignore | 2 ++ kademlia/network.py | 12 ++++++++++-- kademlia/node.py | 2 +- server.tac | 21 ++++++++++++++++++--- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index f2f5292..cad8e67 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.pickle +*.pid _trial_temp apidoc *.pyc diff --git a/kademlia/network.py b/kademlia/network.py index 8f68bc7..0907e1f 100644 --- a/kademlia/network.py +++ b/kademlia/network.py @@ -70,6 +70,7 @@ class SpiderCrawl(object): yet queried 4. repeat, unless nearest list has all been queried, then ur done """ + self.log.info("crawling with nearest: %s" % str(tuple(self.nearest))) count = self.alpha if self.nearest.getIDs() == self.lastIDsCrawled: self.log.info("last iteration same as current - checking all in list now") @@ -160,14 +161,21 @@ class Server(object): storing them if this server is going down for a while. When it comes back up, the list of nodes can be used to bootstrap. """ - return self.protocol.router.findNeighbors(self.node) + neighbors = self.protocol.router.findNeighbors(self.node) + return [ tuple(n)[-2:] for n in neighbors ] def bootstrap(self, addrs): """ Bootstrap the server by connecting to other known nodes in the network. - @param addrs: A C{list} of (ip, port) C{tuple}s + @param addrs: A C{list} of (ip, port) C{tuple}s. Note that only IP addresses + are acceptable - hostnames will cause an error. """ + # if the transport hasn't been initialized yet, wait a second + if self.protocol.transport == None: + reactor.callLater(1, self.bootstrap, addrs) + return + def initTable(results): nodes = [] for addr, result in results.items(): diff --git a/kademlia/node.py b/kademlia/node.py index b8d8d59..83c7e16 100644 --- a/kademlia/node.py +++ b/kademlia/node.py @@ -19,7 +19,7 @@ class Node: """ Enables use of Node as a tuple - i.e., tuple(node) works. """ - return iter([self.ip, self.port, self.id]) + return iter([self.id, self.ip, self.port]) def __repr__(self): return repr([self.long_id, self.ip, self.port]) diff --git a/server.tac b/server.tac index 77a4768..947aa6d 100644 --- a/server.tac +++ b/server.tac @@ -1,13 +1,28 @@ from twisted.application import service, internet from twisted.python.log import ILogObserver +from twisted.internet import reactor, task -import sys, os +import sys, os, pickle 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) -kserver = Server() -server = internet.UDPServer(1234, kserver.protocol) +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)