mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-09 14:40:53 +00:00
ran make lint
This commit is contained in:
committed by
Paul Robinson
parent
1549d4e5d3
commit
4620142ed1
@ -1,18 +1,26 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import sys
|
|
||||||
|
|
||||||
import multiaddr
|
import multiaddr
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
from libp2p import new_host
|
from libp2p import (
|
||||||
from libp2p.network.stream.net_stream_interface import INetStream
|
new_host,
|
||||||
from libp2p.peer.peerinfo import info_from_p2p_addr
|
)
|
||||||
from libp2p.typing import TProtocol
|
from libp2p.network.stream.net_stream_interface import (
|
||||||
|
INetStream,
|
||||||
|
)
|
||||||
|
from libp2p.peer.peerinfo import (
|
||||||
|
info_from_p2p_addr,
|
||||||
|
)
|
||||||
|
from libp2p.typing import (
|
||||||
|
TProtocol,
|
||||||
|
)
|
||||||
|
|
||||||
PING_PROTOCOL_ID = TProtocol("/ipfs/ping/1.0.0")
|
PING_PROTOCOL_ID = TProtocol("/ipfs/ping/1.0.0")
|
||||||
PING_LENGTH = 32
|
PING_LENGTH = 32
|
||||||
RESP_TIMEOUT = 60
|
RESP_TIMEOUT = 60
|
||||||
|
|
||||||
|
|
||||||
async def handle_ping(stream: INetStream) -> None:
|
async def handle_ping(stream: INetStream) -> None:
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -27,11 +35,12 @@ async def handle_ping(stream: INetStream) -> None:
|
|||||||
except:
|
except:
|
||||||
await stream.reset()
|
await stream.reset()
|
||||||
|
|
||||||
|
|
||||||
async def send_ping(stream: INetStream) -> None:
|
async def send_ping(stream: INetStream) -> None:
|
||||||
try:
|
try:
|
||||||
payload = b"\x01" * PING_LENGTH
|
payload = b"\x01" * PING_LENGTH
|
||||||
print(f"sending ping to {stream.muxed_conn.peer_id}")
|
print(f"sending ping to {stream.muxed_conn.peer_id}")
|
||||||
|
|
||||||
await stream.write(payload)
|
await stream.write(payload)
|
||||||
|
|
||||||
with trio.fail_after(RESP_TIMEOUT):
|
with trio.fail_after(RESP_TIMEOUT):
|
||||||
@ -39,10 +48,11 @@ async def send_ping(stream: INetStream) -> None:
|
|||||||
|
|
||||||
if response == payload:
|
if response == payload:
|
||||||
print(f"received pong from {stream.muxed_conn.peer_id}")
|
print(f"received pong from {stream.muxed_conn.peer_id}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"error occurred : {e}")
|
print(f"error occurred : {e}")
|
||||||
|
|
||||||
|
|
||||||
async def run(port: int, destination: str) -> None:
|
async def run(port: int, destination: str) -> None:
|
||||||
localhost_ip = "127.0.0.1"
|
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}")
|
||||||
@ -58,7 +68,7 @@ async def run(port: int, destination: str) -> None:
|
|||||||
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
|
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
|
||||||
)
|
)
|
||||||
print("Waiting for incoming connection...")
|
print("Waiting for incoming connection...")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
maddr = multiaddr.Multiaddr(destination)
|
maddr = multiaddr.Multiaddr(destination)
|
||||||
info = info_from_p2p_addr(maddr)
|
info = info_from_p2p_addr(maddr)
|
||||||
@ -66,20 +76,20 @@ async def run(port: int, destination: str) -> None:
|
|||||||
stream = await host.new_stream(info.peer_id, [PING_PROTOCOL_ID])
|
stream = await host.new_stream(info.peer_id, [PING_PROTOCOL_ID])
|
||||||
|
|
||||||
nursery.start_soon(send_ping, stream)
|
nursery.start_soon(send_ping, stream)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
await trio.sleep_forever()
|
await trio.sleep_forever()
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
|
||||||
description = """
|
description = """
|
||||||
This program demonstrates a simple p2p ping application using libp2p.
|
This program demonstrates a simple p2p ping application using libp2p.
|
||||||
To use it, first run 'python ping.py -p <PORT>', where <PORT> is the port number.
|
To use it, first run 'python ping.py -p <PORT>', where <PORT> is the port number.
|
||||||
Then, run another instance with 'python ping.py -p <ANOTHER_PORT> -d <DESTINATION>',
|
Then, run another instance with 'python ping.py -p <ANOTHER_PORT> -d <DESTINATION>',
|
||||||
where <DESTINATION> is the multiaddress of the previous listener host.
|
where <DESTINATION> is the multiaddress of the previous listener host.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
example_maddr = (
|
example_maddr = (
|
||||||
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
||||||
)
|
)
|
||||||
@ -99,11 +109,12 @@ def main() -> None:
|
|||||||
|
|
||||||
if not args.port:
|
if not args.port:
|
||||||
raise RuntimeError("failed to determine local 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:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user