Created
January 13, 2015 08:16
-
-
Save mehdisadeghi/ac87bd67ab5421ca2567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.application import service, internet | |
from twisted.python.log import ILogObserver | |
import sys, os | |
sys.path.append(os.path.dirname(__file__)) | |
from kademlia.network import Server | |
from kademlia import log | |
application = service.Application("kademlia") | |
application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit) | |
if os.path.isfile('cache.pickle'): | |
kserver = Server.loadState('cache.pickle') | |
else: | |
kserver = Server() | |
kserver.bootstrap([("178.62.215.131", 8468)]) | |
kserver.saveStateRegularly('cache.pickle', 10) | |
server = internet.UDPServer(8469, kserver.protocol) | |
server.setServiceParent(application) | |
# Exposing Kademlia get/set API | |
from txzmq import ZmqEndpoint, ZmqFactory, ZmqREPConnection, ZmqREQConnection | |
zf = ZmqFactory() | |
e = ZmqEndpoint("bind", "tcp://127.0.0.1:40002") | |
s = ZmqREPConnection(zf, e) | |
def getDone(result, msgId, s): | |
print "Key result:", result | |
s.reply(msgId, str(result)) | |
def doGetSet(msgId, *args): | |
print("Inside doPrint") | |
print msgId, args | |
if args[0] == "set:": | |
kserver.set(args[1], args[2]) | |
s.reply(msgId, 'OK') | |
elif args[0] == "get:": | |
print args[1] | |
kserver.get(args[1]).addCallback(getDone, msgId, s) | |
else: | |
s.reply(msgId, "Err") | |
s.gotMessage = doGetSet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment