fixed examples

This commit is contained in:
Brian Muller 2014-01-19 23:04:22 -05:00
parent 7418819f7a
commit 8aab226598
6 changed files with 28 additions and 27 deletions

View File

@ -44,11 +44,13 @@ server.bootstrap([('127.0.0.1', 1234)]).addCallback(done, server)
reactor.run() reactor.run()
``` ```
Check out the examples folder for other examples.
## Stand-alone Server ## Stand-alone Server
If all you want to do is run a local server, just start the example 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 ## Running Tests

View File

@ -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()

22
examples/example.py Normal file
View File

@ -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()

View File

@ -5,7 +5,7 @@ import random
import pickle 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, task
from kademlia.log import Logger from kademlia.log import Logger
from kademlia.protocol import KademliaProtocol from kademlia.protocol import KademliaProtocol
@ -90,8 +90,7 @@ class Server(object):
""" """
# if the transport hasn't been initialized yet, wait a second # if the transport hasn't been initialized yet, wait a second
if self.protocol.transport is None: if self.protocol.transport is None:
reactor.callLater(1, self.bootstrap, addrs) return task.deferLater(reactor, 1, self.bootstrap, addrs)
return
def initTable(results): def initTable(results):
nodes = [] nodes = []

View File

@ -76,7 +76,7 @@ class NodeHeap(object):
def markContacted(self, node): def markContacted(self, node):
self.contacted.add(node.id) self.contacted.add(node.id)
def leftpop(self): def popleft(self):
if len(self) > 0: if len(self) > 0:
return heapq.heappop(self.heap)[1] return heapq.heappop(self.heap)[1]
return None return None