fixed a few bugs
This commit is contained in:
parent
e2b748a108
commit
e99d87c0e8
@ -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 pickle
|
||||||
|
|
||||||
from twisted.internet.task import LoopingCall
|
from twisted.internet.task import LoopingCall
|
||||||
from twisted.internet import defer, reactor
|
from twisted.internet import defer, reactor
|
||||||
@ -93,7 +94,7 @@ class SpiderCrawl(object):
|
|||||||
# where <value> will be a list of tuples if not found or
|
# where <value> will be a list of tuples if not found or
|
||||||
# a dictionary of {'value': v} where v is the value desired
|
# a dictionary of {'value': v} where v is the value desired
|
||||||
if not response[0]:
|
if not response[0]:
|
||||||
toremove.push(peerid)
|
toremove.append(peerid)
|
||||||
elif isinstance(response[1], dict):
|
elif isinstance(response[1], dict):
|
||||||
self.log.debug("found value for %i" % self.node.long_id)
|
self.log.debug("found value for %i" % self.node.long_id)
|
||||||
return response[1]
|
return response[1]
|
||||||
@ -235,3 +236,30 @@ class Server(object):
|
|||||||
nearest = self.protocol.router.findNeighbors(node)
|
nearest = self.protocol.router.findNeighbors(node)
|
||||||
spider = SpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
|
spider = SpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
|
||||||
return spider.findNodes().addCallback(store)
|
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
|
||||||
|
@ -112,7 +112,7 @@ class RoutingTable(object):
|
|||||||
return [b for b in self.buckets if b.lastUpdated < (time.time() - 3600)]
|
return [b for b in self.buckets if b.lastUpdated < (time.time() - 3600)]
|
||||||
|
|
||||||
def removeContact(self, node):
|
def removeContact(self, node):
|
||||||
index = self.getBucketFor(self, node)
|
index = self.getBucketFor(node)
|
||||||
self.buckets[index].removeNode(node)
|
self.buckets[index].removeNode(node)
|
||||||
|
|
||||||
def addContact(self, node):
|
def addContact(self, node):
|
||||||
|
25
server.tac
25
server.tac
@ -2,27 +2,20 @@ from twisted.application import service, internet
|
|||||||
from twisted.python.log import ILogObserver
|
from twisted.python.log import ILogObserver
|
||||||
from twisted.internet import reactor, task
|
from twisted.internet import reactor, task
|
||||||
|
|
||||||
import sys, os, pickle
|
import sys, os
|
||||||
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)
|
||||||
|
|
||||||
|
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 = internet.UDPServer(8468, 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)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user