kademlia/server.tac

29 lines
898 B
Python
Raw Normal View History

2014-01-04 20:27:45 +01:00
from twisted.application import service, internet
2014-01-14 01:49:16 +01:00
from twisted.python.log import ILogObserver
2014-01-14 03:44:44 +01:00
from twisted.internet import reactor, task
2014-01-14 01:49:16 +01:00
2014-01-14 03:44:44 +01:00
import sys, os, pickle
2014-01-04 20:27:45 +01:00
sys.path.append(os.path.dirname(__file__))
from kademlia.network import Server
2014-01-14 01:49:16 +01:00
from kademlia import log
2014-01-04 20:27:45 +01:00
2014-01-14 03:44:44 +01:00
if os.path.isfile('cache.pickle'):
data = pickle.load(open('cache.pickle'))
kserver = Server(id = data['id'])
kserver.bootstrap(data['servers'])
else:
kserver = Server()
2014-01-04 20:27:45 +01:00
application = service.Application("kademlia")
2014-01-14 01:49:16 +01:00
application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit)
2014-01-14 03:44:44 +01:00
server = internet.UDPServer(8468, kserver.protocol)
2014-01-04 20:27:45 +01:00
server.setServiceParent(application)
2014-01-14 03:44:44 +01:00
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)