fixed a few bugs

This commit is contained in:
Brian Muller 2014-01-13 21:44:44 -05:00
parent 9e8d239d73
commit abf5fe19ad
4 changed files with 31 additions and 6 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.pickle
*.pid
_trial_temp _trial_temp
apidoc apidoc
*.pyc *.pyc

View File

@ -70,6 +70,7 @@ class SpiderCrawl(object):
yet queried yet queried
4. repeat, unless nearest list has all been queried, then ur done 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 count = self.alpha
if self.nearest.getIDs() == self.lastIDsCrawled: if self.nearest.getIDs() == self.lastIDsCrawled:
self.log.info("last iteration same as current - checking all in list now") 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 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. 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): def bootstrap(self, addrs):
""" """
Bootstrap the server by connecting to other known nodes in the network. 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): def initTable(results):
nodes = [] nodes = []
for addr, result in results.items(): for addr, result in results.items():

View File

@ -19,7 +19,7 @@ class Node:
""" """
Enables use of Node as a tuple - i.e., tuple(node) works. 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): def __repr__(self):
return repr([self.long_id, self.ip, self.port]) return repr([self.long_id, self.ip, self.port])

View File

@ -1,13 +1,28 @@
from twisted.application import service, internet from twisted.application import service, internet
from twisted.python.log import ILogObserver 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__)) sys.path.append(os.path.dirname(__file__))
from kademlia.network import Server from kademlia.network import Server
from kademlia import log 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 = service.Application("kademlia")
application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit) application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit)
kserver = Server() server = internet.UDPServer(8468, kserver.protocol)
server = internet.UDPServer(1234, kserver.protocol)
server.setServiceParent(application) 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)