|
1 |
| -{.used.} |
2 |
| - |
3 |
| -import chronos |
4 |
| -import stew/byteutils |
5 |
| -import libp2p |
6 |
| - |
7 |
| -## |
8 |
| -# Simple Echo Protocol Implementation for py-libp2p Interop Testing |
9 |
| -## |
10 |
| -const EchoCodec = "/echo/1.0.0" |
11 |
| - |
12 |
| -type EchoProto = ref object of LPProtocol |
13 |
| - |
14 |
| -proc new(T: typedesc[EchoProto]): T = |
15 |
| - proc handle(conn: Connection, proto: string) {.async: (raises: [CancelledError]).} = |
16 |
| - try: |
17 |
| - echo "Echo server: Received connection from ", conn.peerId |
18 |
| - |
19 |
| - # Read and echo messages in a loop |
20 |
| - while not conn.atEof: |
21 |
| - try: |
22 |
| - # Read length-prefixed message using nim-libp2p's readLp |
23 |
| - let message = await conn.readLp(1024 * 1024) # Max 1MB |
24 |
| - if message.len == 0: |
25 |
| - echo "Echo server: Empty message, closing connection" |
26 |
| - break |
27 |
| - |
28 |
| - let messageStr = string.fromBytes(message) |
29 |
| - echo "Echo server: Received (", message.len, " bytes): ", messageStr |
30 |
| - |
31 |
| - # Echo back using writeLp |
32 |
| - await conn.writeLp(message) |
33 |
| - echo "Echo server: Echoed message back" |
34 |
| - |
35 |
| - except CatchableError as e: |
36 |
| - echo "Echo server: Error processing message: ", e.msg |
37 |
| - break |
38 |
| - |
39 |
| - except CancelledError as e: |
40 |
| - echo "Echo server: Connection cancelled" |
41 |
| - raise e |
42 |
| - except CatchableError as e: |
43 |
| - echo "Echo server: Exception in handler: ", e.msg |
44 |
| - finally: |
45 |
| - echo "Echo server: Connection closed" |
46 |
| - await conn.close() |
47 |
| - |
48 |
| - return T.new(codecs = @[EchoCodec], handler = handle) |
49 |
| - |
50 |
| -## |
51 |
| -# Create QUIC-enabled switch |
52 |
| -## |
53 |
| -proc createSwitch(ma: MultiAddress, rng: ref HmacDrbgContext): Switch = |
54 |
| - var switch = SwitchBuilder |
55 |
| - .new() |
56 |
| - .withRng(rng) |
57 |
| - .withAddress(ma) |
58 |
| - .withQuicTransport() |
59 |
| - .build() |
60 |
| - result = switch |
61 |
| - |
62 |
| -## |
63 |
| -# Main server |
64 |
| -## |
65 |
| -proc main() {.async.} = |
66 |
| - let |
67 |
| - rng = newRng() |
68 |
| - localAddr = MultiAddress.init("/ip4/0.0.0.0/udp/0/quic-v1").tryGet() |
69 |
| - echoProto = EchoProto.new() |
70 |
| - |
71 |
| - echo "=== Nim Echo Server for py-libp2p Interop ===" |
72 |
| - |
73 |
| - # Create switch |
74 |
| - let switch = createSwitch(localAddr, rng) |
75 |
| - switch.mount(echoProto) |
76 |
| - |
77 |
| - # Start server |
78 |
| - await switch.start() |
79 |
| - |
80 |
| - # Print connection info |
81 |
| - echo "Peer ID: ", $switch.peerInfo.peerId |
82 |
| - echo "Listening on:" |
83 |
| - for addr in switch.peerInfo.addrs: |
84 |
| - echo " ", $addr, "/p2p/", $switch.peerInfo.peerId |
85 |
| - echo "Protocol: ", EchoCodec |
86 |
| - echo "Ready for py-libp2p connections!" |
87 |
| - echo "" |
88 |
| - |
89 |
| - # Keep running |
90 |
| - try: |
91 |
| - await sleepAsync(100.hours) |
92 |
| - except CancelledError: |
93 |
| - echo "Shutting down..." |
94 |
| - finally: |
95 |
| - await switch.stop() |
96 |
| - |
97 |
| -# Graceful shutdown handler |
98 |
| -proc signalHandler() {.noconv.} = |
99 |
| - echo "\nShutdown signal received" |
100 |
| - quit(0) |
101 |
| - |
102 |
| -when isMainModule: |
103 |
| - setControlCHook(signalHandler) |
104 |
| - try: |
105 |
| - waitFor(main()) |
106 |
| - except CatchableError as e: |
107 |
| - echo "Error: ", e.msg |
108 |
| - quit(1) |
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | +PROJECT_DIR="${SCRIPT_DIR}/.." |
| 6 | + |
| 7 | +# Colors |
| 8 | +GREEN='\033[0;32m' |
| 9 | +RED='\033[0;31m' |
| 10 | +NC='\033[0m' |
| 11 | + |
| 12 | +log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } |
| 13 | +log_error() { echo -e "${RED}[ERROR]${NC} $1"; } |
| 14 | + |
| 15 | +main() { |
| 16 | + log_info "Setting up nim echo server for interop testing..." |
| 17 | + |
| 18 | + # Check if nim is available |
| 19 | + if ! command -v nim &> /dev/null || ! command -v nimble &> /dev/null; then |
| 20 | + log_error "Nim not found. Please install nim first." |
| 21 | + exit 1 |
| 22 | + fi |
| 23 | + |
| 24 | + cd "${PROJECT_DIR}" |
| 25 | + |
| 26 | + # Create logs directory |
| 27 | + mkdir -p logs |
| 28 | + |
| 29 | + # Check if binary already exists |
| 30 | + if [[ -f "nim_echo_server" ]]; then |
| 31 | + log_info "nim_echo_server already exists, skipping build" |
| 32 | + return 0 |
| 33 | + fi |
| 34 | + |
| 35 | + log_info "Installing nim-libp2p globally..." |
| 36 | + # Install libp2p globally so nim can find it |
| 37 | + nimble install -y libp2p |
| 38 | + |
| 39 | + log_info "Building nim echo server..." |
| 40 | + # Compile the echo server |
| 41 | + nim c \ |
| 42 | + -d:release \ |
| 43 | + -d:chronicles_log_level=INFO \ |
| 44 | + --opt:speed \ |
| 45 | + -d:libp2p_quic_support \ |
| 46 | + -d:ssl \ |
| 47 | + --mm:orc \ |
| 48 | + -o:nim_echo_server \ |
| 49 | + nim_echo_server.nim |
| 50 | + |
| 51 | + # Verify binary was created |
| 52 | + if [[ -f "nim_echo_server" ]]; then |
| 53 | + log_info "✅ nim_echo_server built successfully" |
| 54 | + else |
| 55 | + log_error "❌ Failed to build nim_echo_server" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + |
| 59 | + log_info "🎉 Setup complete!" |
| 60 | +} |
| 61 | + |
| 62 | +main "$@" |
0 commit comments