mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
chore: add logs to debug connection
This commit is contained in:
@ -1,15 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
QUIC Echo Example - Direct replacement for examples/echo/echo.py
|
||||
QUIC Echo Example - Fixed version with proper client/server separation
|
||||
|
||||
This program demonstrates a simple echo protocol using QUIC transport where a peer
|
||||
listens for connections and copies back any input received on a stream.
|
||||
|
||||
Modified from the original TCP version to use QUIC transport, providing:
|
||||
- Built-in TLS security
|
||||
- Native stream multiplexing
|
||||
- Better performance over UDP
|
||||
- Modern QUIC protocol features
|
||||
Fixed to properly separate client and server modes - clients don't start listeners.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@ -40,16 +36,8 @@ async def _echo_stream_handler(stream: INetStream) -> None:
|
||||
await stream.close()
|
||||
|
||||
|
||||
async def run(port: int, destination: str, seed: int | None = None) -> None:
|
||||
"""
|
||||
Run echo server or client with QUIC transport.
|
||||
|
||||
Key changes from TCP version:
|
||||
1. UDP multiaddr instead of TCP
|
||||
2. QUIC transport configuration
|
||||
3. Everything else remains the same!
|
||||
"""
|
||||
# CHANGED: UDP + QUIC instead of TCP
|
||||
async def run_server(port: int, seed: int | None = None) -> None:
|
||||
"""Run echo server with QUIC transport."""
|
||||
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/udp/{port}/quic")
|
||||
|
||||
if seed:
|
||||
@ -63,7 +51,7 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
|
||||
|
||||
secret = secrets.token_bytes(32)
|
||||
|
||||
# NEW: QUIC transport configuration
|
||||
# QUIC transport configuration
|
||||
quic_config = QUICTransportConfig(
|
||||
idle_timeout=30.0,
|
||||
max_concurrent_streams=1000,
|
||||
@ -71,46 +59,87 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
|
||||
enable_draft29=False,
|
||||
)
|
||||
|
||||
# CHANGED: Add QUIC transport options
|
||||
# Create host with QUIC transport
|
||||
host = new_host(
|
||||
key_pair=create_new_key_pair(secret),
|
||||
transport_opt={"quic_config": quic_config},
|
||||
)
|
||||
|
||||
# Server mode: start listener
|
||||
async with host.run(listen_addrs=[listen_addr]):
|
||||
print(f"I am {host.get_id().to_string()}")
|
||||
host.set_stream_handler(PROTOCOL_ID, _echo_stream_handler)
|
||||
|
||||
if not destination: # Server mode
|
||||
host.set_stream_handler(PROTOCOL_ID, _echo_stream_handler)
|
||||
print(
|
||||
"Run this from the same folder in another console:\n\n"
|
||||
f"python3 ./examples/echo/echo_quic.py "
|
||||
f"-d {host.get_addrs()[0]}\n"
|
||||
)
|
||||
print("Waiting for incoming QUIC connections...")
|
||||
await trio.sleep_forever()
|
||||
|
||||
print(
|
||||
"Run this from the same folder in another console:\n\n"
|
||||
f"python3 ./examples/echo/echo_quic.py "
|
||||
f"-d {host.get_addrs()[0]}\n"
|
||||
)
|
||||
print("Waiting for incoming QUIC connections...")
|
||||
await trio.sleep_forever()
|
||||
|
||||
else: # Client mode
|
||||
maddr = multiaddr.Multiaddr(destination)
|
||||
info = info_from_p2p_addr(maddr)
|
||||
# Associate the peer with local ip address
|
||||
await host.connect(info)
|
||||
async def run_client(destination: str, seed: int | None = None) -> None:
|
||||
"""Run echo client with QUIC transport."""
|
||||
if seed:
|
||||
import random
|
||||
|
||||
# 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])
|
||||
random.seed(seed)
|
||||
secret_number = random.getrandbits(32 * 8)
|
||||
secret = secret_number.to_bytes(length=32, byteorder="big")
|
||||
else:
|
||||
import secrets
|
||||
|
||||
msg = b"hi, there!\n"
|
||||
secret = secrets.token_bytes(32)
|
||||
|
||||
await stream.write(msg)
|
||||
# Notify the other side about EOF
|
||||
await stream.close()
|
||||
response = await stream.read()
|
||||
# QUIC transport configuration
|
||||
quic_config = QUICTransportConfig(
|
||||
idle_timeout=30.0,
|
||||
max_concurrent_streams=1000,
|
||||
connection_timeout=10.0,
|
||||
enable_draft29=False,
|
||||
)
|
||||
|
||||
print(f"Sent: {msg.decode('utf-8')}")
|
||||
print(f"Got: {response.decode('utf-8')}")
|
||||
# Create host with QUIC transport
|
||||
host = new_host(
|
||||
key_pair=create_new_key_pair(secret),
|
||||
transport_opt={"quic_config": quic_config},
|
||||
)
|
||||
|
||||
# Client mode: NO listener, just connect
|
||||
async with host.run(listen_addrs=[]): # Empty listen_addrs for client
|
||||
print(f"I am {host.get_id().to_string()}")
|
||||
|
||||
maddr = multiaddr.Multiaddr(destination)
|
||||
info = info_from_p2p_addr(maddr)
|
||||
|
||||
# Connect to server
|
||||
await host.connect(info)
|
||||
|
||||
# Start a stream with the destination
|
||||
stream = await host.new_stream(info.peer_id, [PROTOCOL_ID])
|
||||
|
||||
msg = b"hi, there!\n"
|
||||
|
||||
await stream.write(msg)
|
||||
# Notify the other side about EOF
|
||||
await stream.close()
|
||||
response = await stream.read()
|
||||
|
||||
print(f"Sent: {msg.decode('utf-8')}")
|
||||
print(f"Got: {response.decode('utf-8')}")
|
||||
|
||||
|
||||
async def run(port: int, destination: str, seed: int | None = None) -> None:
|
||||
"""
|
||||
Run echo server or client with QUIC transport.
|
||||
|
||||
Fixed version that properly separates client and server modes.
|
||||
"""
|
||||
if not destination: # Server mode
|
||||
await run_server(port, seed)
|
||||
else: # Client mode
|
||||
await run_client(destination, seed)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@ -122,16 +151,16 @@ def main() -> None:
|
||||
|
||||
QUIC provides built-in TLS security and stream multiplexing over UDP.
|
||||
|
||||
To use it, first run 'python ./echo.py -p <PORT>', where <PORT> is
|
||||
the UDP port number.Then, run another host with ,
|
||||
'python ./echo.py -p <ANOTHER_PORT> -d <DESTINATION>'
|
||||
To use it, first run 'python ./echo_quic_fixed.py -p <PORT>', where <PORT> is
|
||||
the UDP port number. Then, run another host with ,
|
||||
'python ./echo_quic_fixed.py -d <DESTINATION>'
|
||||
where <DESTINATION> is the QUIC multiaddress of the previous listener host.
|
||||
"""
|
||||
|
||||
example_maddr = "/ip4/127.0.0.1/udp/8000/quic/p2p/QmQn4SwGkDZKkUEpBRBv"
|
||||
|
||||
parser = argparse.ArgumentParser(description=description)
|
||||
parser.add_argument("-p", "--port", default=8000, type=int, help="UDP port number")
|
||||
parser.add_argument("-p", "--port", default=0, type=int, help="UDP port number")
|
||||
parser.add_argument(
|
||||
"-d",
|
||||
"--destination",
|
||||
@ -152,6 +181,7 @@ def main() -> None:
|
||||
pass
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logging.getLogger("aioquic").setLevel(logging.DEBUG)
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user