Skip to content

Commit e67cbb3

Browse files
committed
Updated ping example
- to include PNET as optional - to specify transport layer TCP/WS with tcp as default
1 parent 9543e99 commit e67cbb3

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

examples/ping/ping.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,24 @@ async def send_ping(stream: INetStream) -> None:
6161
print(f"error occurred : {e}")
6262

6363

64-
async def run(port: int, destination: str) -> None:
64+
async def run(port: int, destination: str, psk: int, transport: str) -> None:
6565
from libp2p.utils.address_validation import (
6666
find_free_port,
6767
get_available_interfaces,
68-
get_optimal_binding_address,
6968
)
7069

7170
if port <= 0:
7271
port = find_free_port()
7372

74-
listen_addrs = get_available_interfaces(port)
75-
host = new_host(listen_addrs=listen_addrs, psk=PSK)
73+
if transport == "tcp":
74+
listen_addrs = get_available_interfaces(port)
75+
if transport == "ws":
76+
listen_addrs = [multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}/ws")]
77+
78+
if psk == 1:
79+
host = new_host(listen_addrs=listen_addrs, psk=PSK)
80+
else:
81+
host = new_host(listen_addrs=listen_addrs)
7682

7783
async with host.run(listen_addrs=listen_addrs), trio.open_nursery() as nursery:
7884
# Start the peer-store cleanup task
@@ -88,12 +94,9 @@ async def run(port: int, destination: str) -> None:
8894
for addr in all_addrs:
8995
print(f"{addr}")
9096

91-
# Use optimal address for the client command
92-
optimal_addr = get_optimal_binding_address(port)
93-
optimal_addr_with_peer = f"{optimal_addr}/p2p/{host.get_id().to_string()}"
9497
print(
9598
f"\nRun this from the same folder in another console:\n\n"
96-
f"ping-demo -d {optimal_addr_with_peer}\n"
99+
f"ping-demo -d {host.get_addrs()[0]} -psk {psk} -t {transport}\n"
97100
)
98101
print("Waiting for incoming connection...")
99102

@@ -131,10 +134,23 @@ def main() -> None:
131134
type=str,
132135
help=f"destination multiaddr string, e.g. {example_maddr}",
133136
)
137+
138+
parser.add_argument(
139+
"-psk", "--psk", default=0, type=int, help="Enable PSK in the transport layer"
140+
)
141+
142+
parser.add_argument(
143+
"-t",
144+
"--transport",
145+
default="tcp",
146+
type=str,
147+
help="Choose the transport layer for ping TCP/WS",
148+
)
149+
134150
args = parser.parse_args()
135151

136152
try:
137-
trio.run(run, *(args.port, args.destination))
153+
trio.run(run, *(args.port, args.destination, args.psk, args.transport))
138154
except KeyboardInterrupt:
139155
pass
140156

0 commit comments

Comments
 (0)