mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-11 23:51:07 +00:00
Merge branch 'main' into main
This commit is contained in:
@ -40,7 +40,6 @@ async def write_data(stream: INetStream) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def run(port: int, destination: str) -> None:
|
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}")
|
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
|
||||||
host = new_host()
|
host = new_host()
|
||||||
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
|
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
|
||||||
@ -54,8 +53,8 @@ async def run(port: int, destination: str) -> None:
|
|||||||
|
|
||||||
print(
|
print(
|
||||||
"Run this from the same folder in another console:\n\n"
|
"Run this from the same folder in another console:\n\n"
|
||||||
f"chat-demo -p {int(port) + 1} "
|
f"chat-demo "
|
||||||
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
|
f"-d {host.get_addrs()[0]}\n"
|
||||||
)
|
)
|
||||||
print("Waiting for incoming connection...")
|
print("Waiting for incoming connection...")
|
||||||
|
|
||||||
@ -87,9 +86,7 @@ def main() -> None:
|
|||||||
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
||||||
)
|
)
|
||||||
parser = argparse.ArgumentParser(description=description)
|
parser = argparse.ArgumentParser(description=description)
|
||||||
parser.add_argument(
|
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
|
||||||
"-p", "--port", default=8000, type=int, help="source port number"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--destination",
|
"--destination",
|
||||||
@ -98,9 +95,6 @@ def main() -> None:
|
|||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if not args.port:
|
|
||||||
raise RuntimeError("was not able to determine a local port")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
trio.run(run, *(args.port, args.destination))
|
trio.run(run, *(args.port, args.destination))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|||||||
@ -30,7 +30,6 @@ async def _echo_stream_handler(stream: INetStream) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def run(port: int, destination: str, seed: int | None = None) -> None:
|
async def run(port: int, destination: str, seed: int | None = None) -> None:
|
||||||
localhost_ip = "127.0.0.1"
|
|
||||||
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
|
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
|
||||||
|
|
||||||
if seed:
|
if seed:
|
||||||
@ -53,8 +52,8 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
|
|||||||
|
|
||||||
print(
|
print(
|
||||||
"Run this from the same folder in another console:\n\n"
|
"Run this from the same folder in another console:\n\n"
|
||||||
f"echo-demo -p {int(port) + 1} "
|
f"echo-demo "
|
||||||
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
|
f"-d {host.get_addrs()[0]}\n"
|
||||||
)
|
)
|
||||||
print("Waiting for incoming connections...")
|
print("Waiting for incoming connections...")
|
||||||
await trio.sleep_forever()
|
await trio.sleep_forever()
|
||||||
@ -73,6 +72,7 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
|
|||||||
msg = b"hi, there!\n"
|
msg = b"hi, there!\n"
|
||||||
|
|
||||||
await stream.write(msg)
|
await stream.write(msg)
|
||||||
|
# TODO: check why the stream is closed after the first write ???
|
||||||
# Notify the other side about EOF
|
# Notify the other side about EOF
|
||||||
await stream.close()
|
await stream.close()
|
||||||
response = await stream.read()
|
response = await stream.read()
|
||||||
@ -94,9 +94,7 @@ def main() -> None:
|
|||||||
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
||||||
)
|
)
|
||||||
parser = argparse.ArgumentParser(description=description)
|
parser = argparse.ArgumentParser(description=description)
|
||||||
parser.add_argument(
|
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
|
||||||
"-p", "--port", default=8000, type=int, help="source port number"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--destination",
|
"--destination",
|
||||||
@ -110,10 +108,6 @@ def main() -> None:
|
|||||||
help="provide a seed to the random number generator (e.g. to fix peer IDs across runs)", # noqa: E501
|
help="provide a seed to the random number generator (e.g. to fix peer IDs across runs)", # noqa: E501
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if not args.port:
|
|
||||||
raise RuntimeError("was not able to determine a local port")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
trio.run(run, args.port, args.destination, args.seed)
|
trio.run(run, args.port, args.destination, args.seed)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|||||||
@ -61,20 +61,20 @@ async def run(port: int, destination: str) -> None:
|
|||||||
async with host_a.run(listen_addrs=[listen_addr]):
|
async with host_a.run(listen_addrs=[listen_addr]):
|
||||||
print(
|
print(
|
||||||
"First host listening. Run this from another console:\n\n"
|
"First host listening. Run this from another console:\n\n"
|
||||||
f"identify-demo -p {int(port) + 1} "
|
f"identify-demo "
|
||||||
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host_a.get_id().pretty()}\n"
|
f"-d {host_a.get_addrs()[0]}\n"
|
||||||
)
|
)
|
||||||
print("Waiting for incoming identify request...")
|
print("Waiting for incoming identify request...")
|
||||||
await trio.sleep_forever()
|
await trio.sleep_forever()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Create second host (dialer)
|
# Create second host (dialer)
|
||||||
print(f"dialer (host_b) listening on /ip4/{localhost_ip}/tcp/{port}")
|
|
||||||
listen_addr = multiaddr.Multiaddr(f"/ip4/{localhost_ip}/tcp/{port}")
|
listen_addr = multiaddr.Multiaddr(f"/ip4/{localhost_ip}/tcp/{port}")
|
||||||
host_b = new_host()
|
host_b = new_host()
|
||||||
|
|
||||||
async with host_b.run(listen_addrs=[listen_addr]):
|
async with host_b.run(listen_addrs=[listen_addr]):
|
||||||
# Connect to the first host
|
# Connect to the first host
|
||||||
|
print(f"dialer (host_b) listening on {host_b.get_addrs()[0]}")
|
||||||
maddr = multiaddr.Multiaddr(destination)
|
maddr = multiaddr.Multiaddr(destination)
|
||||||
info = info_from_p2p_addr(maddr)
|
info = info_from_p2p_addr(maddr)
|
||||||
print(f"Second host connecting to peer: {info.peer_id}")
|
print(f"Second host connecting to peer: {info.peer_id}")
|
||||||
@ -104,13 +104,11 @@ def main() -> None:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
example_maddr = (
|
example_maddr = (
|
||||||
"/ip4/127.0.0.1/tcp/8888/p2p/QmQn4SwGkDZkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
"/ip4/127.0.0.1/tcp/8888/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
||||||
)
|
)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=description)
|
parser = argparse.ArgumentParser(description=description)
|
||||||
parser.add_argument(
|
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
|
||||||
"-p", "--port", default=8888, type=int, help="source port number"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--destination",
|
"--destination",
|
||||||
@ -119,9 +117,6 @@ def main() -> None:
|
|||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if not args.port:
|
|
||||||
raise RuntimeError("failed to determine local port")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
trio.run(run, *(args.port, args.destination))
|
trio.run(run, *(args.port, args.destination))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|||||||
@ -56,9 +56,6 @@ from libp2p.peer.peerinfo import (
|
|||||||
# Configure logging
|
# Configure logging
|
||||||
logger = logging.getLogger("libp2p.identity.identify-push-example")
|
logger = logging.getLogger("libp2p.identity.identify-push-example")
|
||||||
|
|
||||||
# Default port configuration
|
|
||||||
DEFAULT_PORT = 8888
|
|
||||||
|
|
||||||
|
|
||||||
def custom_identify_push_handler_for(host):
|
def custom_identify_push_handler_for(host):
|
||||||
"""
|
"""
|
||||||
@ -241,25 +238,16 @@ def main() -> None:
|
|||||||
"""Parse arguments and start the appropriate mode."""
|
"""Parse arguments and start the appropriate mode."""
|
||||||
description = """
|
description = """
|
||||||
This program demonstrates the libp2p identify/push protocol.
|
This program demonstrates the libp2p identify/push protocol.
|
||||||
Without arguments, it runs as a listener on port 8888.
|
Without arguments, it runs as a listener on random port.
|
||||||
With -d parameter, it runs as a dialer on port 8889.
|
With -d parameter, it runs as a dialer on random port.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
example = (
|
example = (
|
||||||
f"/ip4/127.0.0.1/tcp/{DEFAULT_PORT}/p2p/"
|
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
||||||
"QmQn4SwGkDZkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=description)
|
parser = argparse.ArgumentParser(description=description)
|
||||||
parser.add_argument(
|
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
|
||||||
"-p",
|
|
||||||
"--port",
|
|
||||||
type=int,
|
|
||||||
help=(
|
|
||||||
f"port to listen on (default: {DEFAULT_PORT} for listener, "
|
|
||||||
f"{DEFAULT_PORT + 1} for dialer)"
|
|
||||||
),
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--destination",
|
"--destination",
|
||||||
@ -270,13 +258,11 @@ def main() -> None:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if args.destination:
|
if args.destination:
|
||||||
# Run in dialer mode with default port DEFAULT_PORT + 1 if not specified
|
# Run in dialer mode with random available port if not specified
|
||||||
port = args.port if args.port is not None else DEFAULT_PORT + 1
|
trio.run(run_dialer, args.port, args.destination)
|
||||||
trio.run(run_dialer, port, args.destination)
|
|
||||||
else:
|
else:
|
||||||
# Run in listener mode with default port DEFAULT_PORT if not specified
|
# Run in listener mode with random available port if not specified
|
||||||
port = args.port if args.port is not None else DEFAULT_PORT
|
trio.run(run_listener, args.port)
|
||||||
trio.run(run_listener, port)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\nInterrupted by user")
|
print("\nInterrupted by user")
|
||||||
logger.info("Interrupted by user")
|
logger.info("Interrupted by user")
|
||||||
|
|||||||
@ -55,7 +55,6 @@ async def send_ping(stream: INetStream) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def run(port: int, destination: str) -> None:
|
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}")
|
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
|
||||||
host = new_host(listen_addrs=[listen_addr])
|
host = new_host(listen_addrs=[listen_addr])
|
||||||
|
|
||||||
@ -65,8 +64,8 @@ async def run(port: int, destination: str) -> None:
|
|||||||
|
|
||||||
print(
|
print(
|
||||||
"Run this from the same folder in another console:\n\n"
|
"Run this from the same folder in another console:\n\n"
|
||||||
f"ping-demo -p {int(port) + 1} "
|
f"ping-demo "
|
||||||
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
|
f"-d {host.get_addrs()[0]}\n"
|
||||||
)
|
)
|
||||||
print("Waiting for incoming connection...")
|
print("Waiting for incoming connection...")
|
||||||
|
|
||||||
@ -96,10 +95,8 @@ def main() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=description)
|
parser = argparse.ArgumentParser(description=description)
|
||||||
|
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-p", "--port", default=8000, type=int, help="source port number"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--destination",
|
"--destination",
|
||||||
@ -108,9 +105,6 @@ def main() -> None:
|
|||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if not args.port:
|
|
||||||
raise RuntimeError("failed to determine local port")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
trio.run(run, *(args.port, args.destination))
|
trio.run(run, *(args.port, args.destination))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|||||||
1
newsfragments/661.docs.rst
Normal file
1
newsfragments/661.docs.rst
Normal file
@ -0,0 +1 @@
|
|||||||
|
Updated examples to automatically use random port, when `-p` flag is not given
|
||||||
Reference in New Issue
Block a user