fixed examples
This commit is contained in:
parent
7418819f7a
commit
8aab226598
@ -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
|
||||||
|
@ -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
22
examples/example.py
Normal 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()
|
@ -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 = []
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user