Skip to content

Commit c2ba14d

Browse files
authored
Merge branch 'main' into feat/add-webrtc-transport
2 parents cd019ce + 8bf261c commit c2ba14d

File tree

13 files changed

+1183
-314
lines changed

13 files changed

+1183
-314
lines changed

examples/identify/identify.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import base64
33
import logging
4+
import sys
45

56
import multiaddr
67
import trio
@@ -112,7 +113,12 @@ async def custom_identify_handler(stream):
112113
# Replace the handler with our custom one
113114
host_a.set_stream_handler(IDENTIFY_PROTOCOL_ID, custom_identify_handler)
114115

115-
await trio.sleep_forever()
116+
try:
117+
await trio.sleep_forever()
118+
except KeyboardInterrupt:
119+
print("\n🛑 Shutting down listener...")
120+
logger.info("Listener interrupted by user")
121+
return
116122

117123
else:
118124
# Create second host (dialer)
@@ -147,38 +153,13 @@ async def custom_identify_handler(stream):
147153
try:
148154
print("Starting identify protocol...")
149155

150-
# Read the response properly based on the format
151-
if use_varint_format:
152-
# For length-prefixed format, read varint length first
153-
from libp2p.utils.varint import decode_varint_from_bytes
154-
155-
# Read varint length prefix
156-
length_bytes = b""
157-
while True:
158-
b = await stream.read(1)
159-
if not b:
160-
raise Exception("Stream closed while reading varint length")
161-
length_bytes += b
162-
if b[0] & 0x80 == 0:
163-
break
164-
165-
msg_length = decode_varint_from_bytes(length_bytes)
166-
print(f"Expected message length: {msg_length} bytes")
167-
168-
# Read the protobuf message
169-
response = await stream.read(msg_length)
170-
if len(response) != msg_length:
171-
raise Exception(
172-
f"Incomplete message: expected {msg_length} bytes, "
173-
f"got {len(response)}"
174-
)
156+
# Read the response using the utility function
157+
from libp2p.utils.varint import read_length_prefixed_protobuf
175158

176-
# Combine length prefix and message
177-
full_response = length_bytes + response
178-
else:
179-
# For raw format, read all available data
180-
response = await stream.read(8192)
181-
full_response = response
159+
response = await read_length_prefixed_protobuf(
160+
stream, use_varint_format
161+
)
162+
full_response = response
182163

183164
await stream.close()
184165

@@ -254,16 +235,27 @@ def main() -> None:
254235
"length-prefixed (new format)"
255236
),
256237
)
238+
257239
args = parser.parse_args()
258240

259241
# Determine format: raw format if --raw-format is specified, otherwise
260242
# length-prefixed
261243
use_varint_format = not args.raw_format
262244

263245
try:
264-
trio.run(run, *(args.port, args.destination, use_varint_format))
246+
if args.destination:
247+
# Run in dialer mode
248+
trio.run(run, *(args.port, args.destination, use_varint_format))
249+
else:
250+
# Run in listener mode
251+
trio.run(run, *(args.port, args.destination, use_varint_format))
265252
except KeyboardInterrupt:
266-
pass
253+
print("\n👋 Goodbye!")
254+
logger.info("Application interrupted by user")
255+
except Exception as e:
256+
print(f"\n❌ Error: {str(e)}")
257+
logger.error("Error: %s", str(e))
258+
sys.exit(1)
267259

268260

269261
if __name__ == "__main__":

0 commit comments

Comments
 (0)