Skip to content

Commit 390ac2e

Browse files
authored
Merge branch 'main' into main
2 parents 13d730a + 4e9fa87 commit 390ac2e

File tree

6 files changed

+24
-60
lines changed

6 files changed

+24
-60
lines changed

examples/chat/chat.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ async def write_data(stream: INetStream) -> None:
4040

4141

4242
async def run(port: int, destination: str) -> None:
43-
localhost_ip = "127.0.0.1"
4443
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
4544
host = new_host()
4645
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
@@ -54,8 +53,8 @@ async def stream_handler(stream: INetStream) -> None:
5453

5554
print(
5655
"Run this from the same folder in another console:\n\n"
57-
f"chat-demo -p {int(port) + 1} "
58-
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
56+
f"chat-demo "
57+
f"-d {host.get_addrs()[0]}\n"
5958
)
6059
print("Waiting for incoming connection...")
6160

@@ -87,9 +86,7 @@ def main() -> None:
8786
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
8887
)
8988
parser = argparse.ArgumentParser(description=description)
90-
parser.add_argument(
91-
"-p", "--port", default=8000, type=int, help="source port number"
92-
)
89+
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
9390
parser.add_argument(
9491
"-d",
9592
"--destination",
@@ -98,9 +95,6 @@ def main() -> None:
9895
)
9996
args = parser.parse_args()
10097

101-
if not args.port:
102-
raise RuntimeError("was not able to determine a local port")
103-
10498
try:
10599
trio.run(run, *(args.port, args.destination))
106100
except KeyboardInterrupt:

examples/echo/echo.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ async def _echo_stream_handler(stream: INetStream) -> None:
3030

3131

3232
async def run(port: int, destination: str, seed: int | None = None) -> None:
33-
localhost_ip = "127.0.0.1"
3433
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
3534

3635
if seed:
@@ -53,8 +52,8 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
5352

5453
print(
5554
"Run this from the same folder in another console:\n\n"
56-
f"echo-demo -p {int(port) + 1} "
57-
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
55+
f"echo-demo "
56+
f"-d {host.get_addrs()[0]}\n"
5857
)
5958
print("Waiting for incoming connections...")
6059
await trio.sleep_forever()
@@ -73,6 +72,7 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
7372
msg = b"hi, there!\n"
7473

7574
await stream.write(msg)
75+
# TODO: check why the stream is closed after the first write ???
7676
# Notify the other side about EOF
7777
await stream.close()
7878
response = await stream.read()
@@ -94,9 +94,7 @@ def main() -> None:
9494
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
9595
)
9696
parser = argparse.ArgumentParser(description=description)
97-
parser.add_argument(
98-
"-p", "--port", default=8000, type=int, help="source port number"
99-
)
97+
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
10098
parser.add_argument(
10199
"-d",
102100
"--destination",
@@ -110,10 +108,6 @@ def main() -> None:
110108
help="provide a seed to the random number generator (e.g. to fix peer IDs across runs)", # noqa: E501
111109
)
112110
args = parser.parse_args()
113-
114-
if not args.port:
115-
raise RuntimeError("was not able to determine a local port")
116-
117111
try:
118112
trio.run(run, args.port, args.destination, args.seed)
119113
except KeyboardInterrupt:

examples/identify/identify.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ async def run(port: int, destination: str) -> None:
6161
async with host_a.run(listen_addrs=[listen_addr]):
6262
print(
6363
"First host listening. Run this from another console:\n\n"
64-
f"identify-demo -p {int(port) + 1} "
65-
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host_a.get_id().pretty()}\n"
64+
f"identify-demo "
65+
f"-d {host_a.get_addrs()[0]}\n"
6666
)
6767
print("Waiting for incoming identify request...")
6868
await trio.sleep_forever()
6969

7070
else:
7171
# Create second host (dialer)
72-
print(f"dialer (host_b) listening on /ip4/{localhost_ip}/tcp/{port}")
7372
listen_addr = multiaddr.Multiaddr(f"/ip4/{localhost_ip}/tcp/{port}")
7473
host_b = new_host()
7574

7675
async with host_b.run(listen_addrs=[listen_addr]):
7776
# Connect to the first host
77+
print(f"dialer (host_b) listening on {host_b.get_addrs()[0]}")
7878
maddr = multiaddr.Multiaddr(destination)
7979
info = info_from_p2p_addr(maddr)
8080
print(f"Second host connecting to peer: {info.peer_id}")
@@ -104,13 +104,11 @@ def main() -> None:
104104
"""
105105

106106
example_maddr = (
107-
"/ip4/127.0.0.1/tcp/8888/p2p/QmQn4SwGkDZkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
107+
"/ip4/127.0.0.1/tcp/8888/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
108108
)
109109

110110
parser = argparse.ArgumentParser(description=description)
111-
parser.add_argument(
112-
"-p", "--port", default=8888, type=int, help="source port number"
113-
)
111+
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
114112
parser.add_argument(
115113
"-d",
116114
"--destination",
@@ -119,9 +117,6 @@ def main() -> None:
119117
)
120118
args = parser.parse_args()
121119

122-
if not args.port:
123-
raise RuntimeError("failed to determine local port")
124-
125120
try:
126121
trio.run(run, *(args.port, args.destination))
127122
except KeyboardInterrupt:

examples/identify_push/identify_push_listener_dialer.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@
5656
# Configure logging
5757
logger = logging.getLogger("libp2p.identity.identify-push-example")
5858

59-
# Default port configuration
60-
DEFAULT_PORT = 8888
61-
6259

6360
def custom_identify_push_handler_for(host):
6461
"""
@@ -241,25 +238,16 @@ def main() -> None:
241238
"""Parse arguments and start the appropriate mode."""
242239
description = """
243240
This program demonstrates the libp2p identify/push protocol.
244-
Without arguments, it runs as a listener on port 8888.
245-
With -d parameter, it runs as a dialer on port 8889.
241+
Without arguments, it runs as a listener on random port.
242+
With -d parameter, it runs as a dialer on random port.
246243
"""
247244

248245
example = (
249-
f"/ip4/127.0.0.1/tcp/{DEFAULT_PORT}/p2p/"
250-
"QmQn4SwGkDZkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
246+
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
251247
)
252248

253249
parser = argparse.ArgumentParser(description=description)
254-
parser.add_argument(
255-
"-p",
256-
"--port",
257-
type=int,
258-
help=(
259-
f"port to listen on (default: {DEFAULT_PORT} for listener, "
260-
f"{DEFAULT_PORT + 1} for dialer)"
261-
),
262-
)
250+
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
263251
parser.add_argument(
264252
"-d",
265253
"--destination",
@@ -270,13 +258,11 @@ def main() -> None:
270258

271259
try:
272260
if args.destination:
273-
# Run in dialer mode with default port DEFAULT_PORT + 1 if not specified
274-
port = args.port if args.port is not None else DEFAULT_PORT + 1
275-
trio.run(run_dialer, port, args.destination)
261+
# Run in dialer mode with random available port if not specified
262+
trio.run(run_dialer, args.port, args.destination)
276263
else:
277-
# Run in listener mode with default port DEFAULT_PORT if not specified
278-
port = args.port if args.port is not None else DEFAULT_PORT
279-
trio.run(run_listener, port)
264+
# Run in listener mode with random available port if not specified
265+
trio.run(run_listener, args.port)
280266
except KeyboardInterrupt:
281267
print("\nInterrupted by user")
282268
logger.info("Interrupted by user")

examples/ping/ping.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ async def send_ping(stream: INetStream) -> None:
5555

5656

5757
async def run(port: int, destination: str) -> None:
58-
localhost_ip = "127.0.0.1"
5958
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
6059
host = new_host(listen_addrs=[listen_addr])
6160

@@ -65,8 +64,8 @@ async def run(port: int, destination: str) -> None:
6564

6665
print(
6766
"Run this from the same folder in another console:\n\n"
68-
f"ping-demo -p {int(port) + 1} "
69-
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
67+
f"ping-demo "
68+
f"-d {host.get_addrs()[0]}\n"
7069
)
7170
print("Waiting for incoming connection...")
7271

@@ -96,10 +95,8 @@ def main() -> None:
9695
)
9796

9897
parser = argparse.ArgumentParser(description=description)
98+
parser.add_argument("-p", "--port", default=0, type=int, help="source port number")
9999

100-
parser.add_argument(
101-
"-p", "--port", default=8000, type=int, help="source port number"
102-
)
103100
parser.add_argument(
104101
"-d",
105102
"--destination",
@@ -108,9 +105,6 @@ def main() -> None:
108105
)
109106
args = parser.parse_args()
110107

111-
if not args.port:
112-
raise RuntimeError("failed to determine local port")
113-
114108
try:
115109
trio.run(run, *(args.port, args.destination))
116110
except KeyboardInterrupt:

newsfragments/661.docs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated examples to automatically use random port, when `-p` flag is not given

0 commit comments

Comments
 (0)