|
1 | 1 | import argparse
|
2 | 2 | import base64
|
3 | 3 | import logging
|
| 4 | +import sys |
4 | 5 |
|
5 | 6 | import multiaddr
|
6 | 7 | import trio
|
@@ -112,7 +113,12 @@ async def custom_identify_handler(stream):
|
112 | 113 | # Replace the handler with our custom one
|
113 | 114 | host_a.set_stream_handler(IDENTIFY_PROTOCOL_ID, custom_identify_handler)
|
114 | 115 |
|
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 |
116 | 122 |
|
117 | 123 | else:
|
118 | 124 | # Create second host (dialer)
|
@@ -147,38 +153,13 @@ async def custom_identify_handler(stream):
|
147 | 153 | try:
|
148 | 154 | print("Starting identify protocol...")
|
149 | 155 |
|
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 |
175 | 158 |
|
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 |
182 | 163 |
|
183 | 164 | await stream.close()
|
184 | 165 |
|
@@ -254,16 +235,27 @@ def main() -> None:
|
254 | 235 | "length-prefixed (new format)"
|
255 | 236 | ),
|
256 | 237 | )
|
| 238 | + |
257 | 239 | args = parser.parse_args()
|
258 | 240 |
|
259 | 241 | # Determine format: raw format if --raw-format is specified, otherwise
|
260 | 242 | # length-prefixed
|
261 | 243 | use_varint_format = not args.raw_format
|
262 | 244 |
|
263 | 245 | 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)) |
265 | 252 | 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) |
267 | 259 |
|
268 | 260 |
|
269 | 261 | if __name__ == "__main__":
|
|
0 commit comments