progressing
This commit is contained in:
parent
f5c725788e
commit
d4d345c3c7
@ -40,9 +40,9 @@ async def run(port: int, destination: str, localhost: bool) -> None:
|
|||||||
else:
|
else:
|
||||||
ip = urllib.request.urlopen("https://v4.ident.me/").read().decode("utf8")
|
ip = urllib.request.urlopen("https://v4.ident.me/").read().decode("utf8")
|
||||||
transport_opt = f"/ip4/{ip}/tcp/{port}"
|
transport_opt = f"/ip4/{ip}/tcp/{port}"
|
||||||
host = await new_node(transport_opt=[transport_opt])
|
host = new_node(transport_opt=[transport_opt])
|
||||||
|
|
||||||
await host.get_network().listen(multiaddr.Multiaddr(transport_opt))
|
await trio_asyncio.run_asyncio(host.get_network().listen,multiaddr.Multiaddr(transport_opt) )
|
||||||
|
|
||||||
if not destination: # its the server
|
if not destination: # its the server
|
||||||
|
|
||||||
@ -66,22 +66,18 @@ async def run(port: int, destination: str, localhost: bool) -> None:
|
|||||||
maddr = multiaddr.Multiaddr(destination)
|
maddr = multiaddr.Multiaddr(destination)
|
||||||
info = info_from_p2p_addr(maddr)
|
info = info_from_p2p_addr(maddr)
|
||||||
# Associate the peer with local ip address
|
# Associate the peer with local ip address
|
||||||
await host.connect(info)
|
await trio_asyncio.run_asyncio(host.connect, info)
|
||||||
|
|
||||||
# Start a stream with the destination.
|
# Start a stream with the destination.
|
||||||
# Multiaddress of the destination peer is fetched from the peerstore using 'peerId'.
|
# Multiaddress of the destination peer is fetched from the peerstore using 'peerId'.
|
||||||
stream = await host.new_stream(info.peer_id, [PROTOCOL_ID])
|
stream = await trio_asyncio.run_asyncio(host.new_stream, *(info.peer_id, [PROTOCOL_ID]))
|
||||||
|
|
||||||
asyncio.ensure_future(read_data(stream))
|
asyncio.ensure_future(read_data(stream))
|
||||||
asyncio.ensure_future(write_data(stream))
|
asyncio.ensure_future(write_data(stream))
|
||||||
print("Connected to peer %s" % info.addrs[0])
|
print("Connected to peer %s" % info.addrs[0])
|
||||||
|
|
||||||
async def async_main_wrapper(*args):
|
stopped_event = trio.Event()
|
||||||
async with trio_asyncio.open_loop() as loop:
|
await stopped_event.wait()
|
||||||
assert loop == asyncio.get_event_loop()
|
|
||||||
stopped_event = trio.Event()
|
|
||||||
await trio_asyncio.run_asyncio(run, *args)
|
|
||||||
await stopped_event.wait()
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
@ -121,7 +117,7 @@ def main() -> None:
|
|||||||
if not args.port:
|
if not args.port:
|
||||||
raise RuntimeError("was not able to determine a local port")
|
raise RuntimeError("was not able to determine a local port")
|
||||||
|
|
||||||
trio.run(async_main_wrapper, *(args.port, args.destination, args.localhost))
|
trio_asyncio.run(run, *(args.port, args.destination, args.localhost))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@ -97,7 +97,7 @@ def initialize_default_swarm(
|
|||||||
return Swarm(id_opt, peerstore, upgrader, transport)
|
return Swarm(id_opt, peerstore, upgrader, transport)
|
||||||
|
|
||||||
|
|
||||||
async def new_node(
|
def new_node(
|
||||||
key_pair: KeyPair = None,
|
key_pair: KeyPair = None,
|
||||||
swarm_opt: INetwork = None,
|
swarm_opt: INetwork = None,
|
||||||
transport_opt: Sequence[str] = None,
|
transport_opt: Sequence[str] = None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user