2019-07-24 18:43:49 +08:00
|
|
|
import argparse
|
2018-11-27 01:39:36 +08:00
|
|
|
import sys
|
2018-11-19 15:58:07 +08:00
|
|
|
|
2019-02-11 02:07:42 +08:00
|
|
|
import multiaddr
|
2019-11-19 18:04:48 +08:00
|
|
|
import trio
|
2018-11-27 01:39:36 +08:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
from libp2p import new_host_trio
|
2019-08-11 16:47:54 +08:00
|
|
|
from libp2p.network.stream.net_stream_interface import INetStream
|
2019-01-10 03:01:46 +08:00
|
|
|
from libp2p.peer.peerinfo import info_from_p2p_addr
|
2019-08-11 16:47:54 +08:00
|
|
|
from libp2p.typing import TProtocol
|
2018-11-27 01:39:36 +08:00
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
PROTOCOL_ID = TProtocol("/chat/1.0.0")
|
2019-09-06 17:26:40 +08:00
|
|
|
MAX_READ_LEN = 2 ** 32 - 1
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2018-11-19 15:58:07 +08:00
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
async def read_data(stream: INetStream) -> None:
|
2018-11-19 00:22:56 +08:00
|
|
|
while True:
|
2019-09-06 17:26:40 +08:00
|
|
|
read_bytes = await stream.read(MAX_READ_LEN)
|
2019-08-11 16:47:54 +08:00
|
|
|
if read_bytes is not None:
|
|
|
|
read_string = read_bytes.decode()
|
2019-02-11 02:07:42 +08:00
|
|
|
if read_string != "\n":
|
|
|
|
# Green console colour: \x1b[32m
|
|
|
|
# Reset console colour: \x1b[0m
|
|
|
|
print("\x1b[32m %s\x1b[0m " % read_string, end="")
|
2018-11-19 15:58:07 +08:00
|
|
|
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
async def write_data(stream: INetStream) -> None:
|
2019-12-24 18:03:18 +08:00
|
|
|
async_f = trio.wrap_file(sys.stdin)
|
2018-11-19 15:58:07 +08:00
|
|
|
while True:
|
2019-12-24 18:03:18 +08:00
|
|
|
line = await async_f.readline()
|
2018-11-19 15:58:07 +08:00
|
|
|
await stream.write(line.encode())
|
2018-11-19 00:22:56 +08:00
|
|
|
|
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
async def run(port: int, destination: str) -> None:
|
|
|
|
localhost_ip = "127.0.0.1"
|
|
|
|
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
|
|
|
|
async with new_host_trio(
|
|
|
|
listen_addrs=[listen_addr]
|
|
|
|
) as host, trio.open_nursery() as nursery:
|
|
|
|
if not destination: # its the server
|
2019-04-19 03:56:02 +08:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
async def stream_handler(stream: INetStream) -> None:
|
|
|
|
nursery.start_soon(read_data, stream)
|
|
|
|
nursery.start_soon(write_data, stream)
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
host.set_stream_handler(PROTOCOL_ID, stream_handler)
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
print(
|
|
|
|
f"Run 'python ./examples/chat/chat.py "
|
|
|
|
f"-p {int(port) + 1} "
|
|
|
|
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}' "
|
|
|
|
"on another console."
|
|
|
|
)
|
|
|
|
print("Waiting for incoming connection...")
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
else: # its the client
|
|
|
|
maddr = multiaddr.Multiaddr(destination)
|
|
|
|
info = info_from_p2p_addr(maddr)
|
|
|
|
# Associate the peer with local ip address
|
|
|
|
await host.connect(info)
|
|
|
|
# Start a stream with the destination.
|
|
|
|
# Multiaddress of the destination peer is fetched from the peerstore using 'peerId'.
|
|
|
|
stream = await host.new_stream(info.peer_id, [PROTOCOL_ID])
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
nursery.start_soon(read_data, stream)
|
|
|
|
nursery.start_soon(write_data, stream)
|
|
|
|
print("Connected to peer %s" % info.addrs[0])
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
await trio.sleep_forever()
|
2019-11-11 22:52:48 +08:00
|
|
|
|
2018-11-19 15:58:07 +08:00
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def main() -> None:
|
2019-07-24 18:43:49 +08:00
|
|
|
description = """
|
|
|
|
This program demonstrates a simple p2p chat application using libp2p.
|
|
|
|
To use it, first run 'python ./chat -p <PORT>', where <PORT> is the port number.
|
|
|
|
Then, run another host with 'python ./chat -p <ANOTHER_PORT> -d <DESTINATION>',
|
|
|
|
where <DESTINATION> is the multiaddress of the previous listener host.
|
|
|
|
"""
|
2019-08-14 05:36:42 +08:00
|
|
|
example_maddr = (
|
|
|
|
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
|
|
|
)
|
2019-07-24 18:43:49 +08:00
|
|
|
parser = argparse.ArgumentParser(description=description)
|
2019-08-14 05:36:42 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"-p", "--port", default=8000, type=int, help="source port number"
|
2019-07-24 18:43:49 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-08-14 05:36:42 +08:00
|
|
|
"-d",
|
|
|
|
"--destination",
|
|
|
|
type=str,
|
|
|
|
help=f"destination multiaddr string, e.g. {example_maddr}",
|
2019-07-24 18:43:49 +08:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-08-03 01:21:41 +08:00
|
|
|
if not args.port:
|
|
|
|
raise RuntimeError("was not able to determine a local port")
|
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
try:
|
|
|
|
trio.run(run, *(args.port, args.destination))
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2018-11-19 15:58:07 +08:00
|
|
|
|
2019-11-19 18:04:48 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
if __name__ == "__main__":
|
2018-11-19 00:22:56 +08:00
|
|
|
main()
|