Asynchronous Server listening (#58) (#60)

This commit is contained in:
joriscarrier 2019-01-12 17:44:39 +01:00 committed by Brian Muller
parent 0953e0edb2
commit be6b60847b
6 changed files with 16 additions and 11 deletions

View File

@ -23,14 +23,15 @@ Assuming you want to connect to an existing network:
import asyncio
from kademlia.network import Server
loop = asyncio.get_event_loop()
# Create a node and start listening on port 5678
node = Server()
node.listen(5678)
loop.run_until_complete(node.listen(5678))
# Bootstrap the node by connecting to other known nodes, in this case
# replace 123.123.123.123 with the IP of another node and optionally
# give as many ip/port combos as you can for other nodes.
loop = asyncio.get_event_loop()
loop.run_until_complete(node.bootstrap([("123.123.123.123", 5678)]))
# set a value for the key "my-key" on the network

View File

@ -10,12 +10,13 @@ log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)
server = Server()
server.listen(8468)
loop = asyncio.get_event_loop()
loop.set_debug(True)
server = Server()
loop.run_until_complete(server.listen(8468))
try:
loop.run_forever()
except KeyboardInterrupt:

View File

@ -19,7 +19,7 @@ loop = asyncio.get_event_loop()
loop.set_debug(True)
server = Server()
server.listen(8469)
loop.run_until_complete(server.listen(8469))
bootstrap_node = (sys.argv[1], int(sys.argv[2]))
loop.run_until_complete(server.bootstrap([bootstrap_node]))
result = loop.run_until_complete(server.get(sys.argv[3]))

View File

@ -19,7 +19,7 @@ loop = asyncio.get_event_loop()
loop.set_debug(True)
server = Server()
server.listen(8469)
loop.run_until_complete(server.listen(8469))
bootstrap_node = (sys.argv[1], int(sys.argv[2]))
loop.run_until_complete(server.bootstrap([bootstrap_node]))
loop.run_until_complete(server.set(sys.argv[3], sys.argv[4]))

View File

@ -58,7 +58,7 @@ class Server:
def _create_protocol(self):
return self.protocol_class(self.node, self.storage, self.ksize)
def listen(self, port, interface='0.0.0.0'):
async def listen(self, port, interface='0.0.0.0'):
"""
Start listening on the given port.
@ -69,7 +69,7 @@ class Server:
local_addr=(interface, port))
log.info("Node %i listening on %s:%i",
self.node.long_id, interface, port)
self.transport, self.protocol = loop.run_until_complete(listen)
self.transport, self.protocol = await listen
# finally, schedule refreshing table
self.refresh_table()

View File

@ -1,4 +1,5 @@
import unittest
import asyncio
from kademlia.network import Server
from kademlia.protocol import KademliaProtocol
@ -12,9 +13,10 @@ class SwappableProtocolTests(unittest.TestCase):
have a KademliaProtocol object as its protocol after its listen()
method is called.
"""
loop = asyncio.get_event_loop()
server = Server()
self.assertIsNone(server.protocol)
server.listen(8469)
loop.run_until_complete(server.listen(8469))
self.assertIsInstance(server.protocol, KademliaProtocol)
server.stop()
@ -33,13 +35,14 @@ class SwappableProtocolTests(unittest.TestCase):
protocol_class = CoconutProtocol
# An ordinary server does NOT have a CoconutProtocol as its protocol...
loop = asyncio.get_event_loop()
server = Server()
server.listen(8469)
loop.run_until_complete(server.listen(8469))
self.assertNotIsInstance(server.protocol, CoconutProtocol)
server.stop()
# ...but our custom server does.
husk_server = HuskServer()
husk_server.listen(8469)
loop.run_until_complete(husk_server.listen(8469))
self.assertIsInstance(husk_server.protocol, CoconutProtocol)
husk_server.stop()