82840b5e6c
* Add generic protocol handler * Add generic protocol handler to stream muxing pipeline * Modify conn_handler to only deal with connections * mplex accept stream architecture changes * Add create generic protocol handler * Fix minor bugs * who would win 4 devs or one not * Debugging * rearch with handle_incoming infinite loop, seems to work, needs cleanup" * passing linting, still needs cleanup * fixing linting again; code still needs cleanup * fixing tests; code still needs cleanup * adding test cleanup and task cleanup, removing prints * linting, and cleanup complete * storing connections based on peer id * remove dead code * remove unnecessary peer_id
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
import asyncio
|
|
import multiaddr
|
|
|
|
from Crypto.PublicKey import RSA
|
|
from .peer.peerstore import PeerStore
|
|
from .peer.id import id_from_public_key
|
|
from .network.swarm import Swarm
|
|
from .host.basic_host import BasicHost
|
|
from .transport.upgrader import TransportUpgrader
|
|
from .transport.tcp.tcp import TCP
|
|
|
|
|
|
async def cleanup_done_tasks():
|
|
while True:
|
|
for task in asyncio.all_tasks():
|
|
if task.done():
|
|
await task
|
|
|
|
# Need not run often
|
|
# Some sleep necessary to context switch
|
|
await asyncio.sleep(3)
|
|
|
|
async def new_node(
|
|
id_opt=None, transport_opt=None,
|
|
muxer_opt=None, sec_opt=None, peerstore=None):
|
|
|
|
if id_opt is None:
|
|
new_key = RSA.generate(2048, e=65537)
|
|
id_opt = id_from_public_key(new_key.publickey())
|
|
# private_key = new_key.exportKey("PEM")
|
|
|
|
transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"]
|
|
transport_opt = [multiaddr.Multiaddr(t) for t in transport_opt]
|
|
muxer_opt = muxer_opt or ["mplex/6.7.0"]
|
|
sec_opt = sec_opt or ["secio"]
|
|
peerstore = peerstore or PeerStore()
|
|
|
|
upgrader = TransportUpgrader(sec_opt, transport_opt)
|
|
swarm = Swarm(id_opt, peerstore, upgrader)
|
|
tcp = TCP()
|
|
swarm.add_transport(tcp)
|
|
await swarm.listen(transport_opt[0])
|
|
|
|
# TODO enable support for other host type
|
|
# TODO routing unimplemented
|
|
host = BasicHost(swarm)
|
|
|
|
# Kick off cleanup job
|
|
asyncio.ensure_future(cleanup_done_tasks())
|
|
|
|
return host
|