From 8aab226598886940b802904e62dae5c44dd2ec68 Mon Sep 17 00:00:00 2001 From: Brian Muller Date: Sun, 19 Jan 2014 23:04:22 -0500 Subject: [PATCH] fixed examples --- README.markdown | 4 +++- example/example.py | 22 ---------------------- examples/example.py | 22 ++++++++++++++++++++++ server.tac => examples/server.tac | 0 kademlia/network.py | 5 ++--- kademlia/node.py | 2 +- 6 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 example/example.py create mode 100644 examples/example.py rename server.tac => examples/server.tac (100%) diff --git a/README.markdown b/README.markdown index 0f7b145..625a467 100644 --- a/README.markdown +++ b/README.markdown @@ -44,11 +44,13 @@ server.bootstrap([('127.0.0.1', 1234)]).addCallback(done, server) reactor.run() ``` +Check out the examples folder for other examples. + ## Stand-alone Server If all you want to do is run a local server, just start the example server: ``` -twistd -noy server.tac +twistd -noy examples/server.tac ``` ## Running Tests diff --git a/example/example.py b/example/example.py deleted file mode 100644 index 111929c..0000000 --- a/example/example.py +++ /dev/null @@ -1,22 +0,0 @@ -from twisted.internet import reactor -from twisted.python import log -from kademlia.network import Server -import sys - -log.startLogging(sys.stdout) - -def quit(result): - print "Key result:", result - reactor.stop() - -def get(result, server): - reactor.stop() - #return server.get("a key").addCallback(quit) - -def done(found, server): - log.msg("Found nodes: %s" % found) - return server.set("a key", "a value").addCallback(get, server) -two = Server(5678) -two.bootstrap([('127.0.0.1', 1234)]).addCallback(done, two) - -reactor.run() diff --git a/examples/example.py b/examples/example.py new file mode 100644 index 0000000..54b6beb --- /dev/null +++ b/examples/example.py @@ -0,0 +1,22 @@ +from twisted.internet import reactor +from twisted.python import log +from kademlia.network import Server +import sys + +log.startLogging(sys.stdout) + +def done(result): + print "Key result:", result + reactor.stop() + +def setDone(result, server): + server.get("a key").addCallback(done) + +def bootstrapDone(found, server): + server.set("a key", "a value").addCallback(setDone, server) + +server = Server() +server.listen(8468) +server.bootstrap([("1.2.3.4", 8468)]).addCallback(bootstrapDone, server) + +reactor.run() diff --git a/server.tac b/examples/server.tac similarity index 100% rename from server.tac rename to examples/server.tac diff --git a/kademlia/network.py b/kademlia/network.py index 7b15bec..77e80b0 100644 --- a/kademlia/network.py +++ b/kademlia/network.py @@ -5,7 +5,7 @@ import random import pickle from twisted.internet.task import LoopingCall -from twisted.internet import defer, reactor +from twisted.internet import defer, reactor, task from kademlia.log import Logger from kademlia.protocol import KademliaProtocol @@ -90,8 +90,7 @@ class Server(object): """ # if the transport hasn't been initialized yet, wait a second if self.protocol.transport is None: - reactor.callLater(1, self.bootstrap, addrs) - return + return task.deferLater(reactor, 1, self.bootstrap, addrs) def initTable(results): nodes = [] diff --git a/kademlia/node.py b/kademlia/node.py index 236a840..ddec9d4 100644 --- a/kademlia/node.py +++ b/kademlia/node.py @@ -76,7 +76,7 @@ class NodeHeap(object): def markContacted(self, node): self.contacted.add(node.id) - def leftpop(self): + def popleft(self): if len(self) > 0: return heapq.heappop(self.heap)[1] return None