|
1 | 1 | """ |
2 | | -Ping-Pong server (Bob). |
| 2 | +Ping-Pong — Bob (server). |
3 | 3 |
|
4 | | -Bob listens for messages from Alice. For each message he receives: |
5 | | - - If the message is "ping", he replies with "pong". |
6 | | - - For anything else, he replies with "no way!". |
7 | | -
|
8 | | -Bob keeps listening until Alice disconnects. |
| 4 | +Bob listens for Alice's PINGs and replies with PONGs. |
| 5 | +Both sides know NUM_ROUNDS, so no BYE is needed — Bob stops after |
| 6 | +sending the last PONG and the connection closes naturally. |
9 | 7 |
|
10 | 8 | This is a purely classical example — no quantum operations. |
11 | | -It demonstrates the event-based programming pattern that we will |
12 | | -later extend with quantum operations (teleportation, etc.). |
| 9 | +It demonstrates the event-based state-machine pattern used throughout |
| 10 | +SimulaQron examples. |
| 11 | +
|
| 12 | +Bob's state diagram |
| 13 | +------------------- |
| 14 | +
|
| 15 | + ┌─ (connect) ──────────────────────────────────────┐ |
| 16 | + │ │ |
| 17 | + ▼ │ |
| 18 | + IDLE ──[recv "PING"]──► PLAYING (start) |
| 19 | + │ |
| 20 | + send "PONG" |
| 21 | + │ |
| 22 | + ▼ |
| 23 | + IDLE (next round) |
| 24 | + ... after NUM_ROUNDS ... |
| 25 | + │ |
| 26 | + recv "PING" (last) |
| 27 | + ▼ |
| 28 | + DONE |
| 29 | +
|
| 30 | +Transition table: |
| 31 | +
|
| 32 | + State │ Event │ Action │ Next state |
| 33 | + ─────────┼──────────────┼──────────────┼───────────── |
| 34 | + IDLE │ recv "PING" │ — │ PLAYING |
| 35 | + PLAYING │ (entry) │ send "PONG" │ IDLE |
| 36 | + PLAYING │ (entry) │ (last round) │ DONE |
| 37 | +
|
| 38 | + PLAYING → IDLE/DONE is an *entry action*: Bob sends PONG immediately |
| 39 | + on entering PLAYING, before waiting for the next message. |
13 | 40 | """ |
| 41 | + |
14 | 42 | from asyncio import StreamReader, StreamWriter |
15 | 43 | from pathlib import Path |
16 | 44 |
|
|
20 | 48 | from simulaqron.settings.network_config import NodeConfigType |
21 | 49 |
|
22 | 50 |
|
23 | | -async def run_bob(reader: StreamReader, writer: StreamWriter): |
24 | | - """ |
25 | | - Bob's event loop. |
| 51 | +NUM_ROUNDS = 5 |
| 52 | + |
| 53 | +# ── States ─────────────────────────────────────────────────────────────────── |
| 54 | + |
| 55 | +STATE_IDLE = "IDLE" |
| 56 | +STATE_PLAYING = "PLAYING" |
| 57 | +STATE_DONE = "DONE" # noqa: E221 |
| 58 | + |
| 59 | + |
| 60 | +# ── Handlers ───────────────────────────────────────────────────────────────── |
| 61 | + |
| 62 | +async def handle_ping(_writer: StreamWriter) -> str: |
| 63 | + """Transition: IDLE ──[recv "PING"]──► PLAYING""" |
| 64 | + print("Bob: received PING", flush=True) |
| 65 | + return STATE_PLAYING |
26 | 66 |
|
27 | | - Each iteration: |
28 | | - 1. Wait for a message from Alice |
29 | | - 2. Decide on a reply based on the message content |
30 | | - 3. Send the reply back |
31 | | - """ |
32 | | - print("Bob: Alice connected, waiting for messages...", flush=True) |
33 | 67 |
|
34 | | - while True: |
35 | | - # Wait until Alice sends something |
36 | | - data = await reader.read(255) |
| 68 | +# ── Dispatch table ──────────────────────────────────────────────────────────── |
37 | 69 |
|
38 | | - # If we get empty data, Alice has disconnected |
| 70 | +BOB_DISPATCH = { |
| 71 | + (STATE_IDLE, "PING"): handle_ping, |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +# ── Event loop ──────────────────────────────────────────────────────────────── |
| 76 | + |
| 77 | +async def run_bob(reader: StreamReader, writer: StreamWriter) -> None: |
| 78 | + print("Bob: Alice connected.", flush=True) |
| 79 | + rounds_done = 0 |
| 80 | + state = STATE_IDLE |
| 81 | + |
| 82 | + while state != STATE_DONE: |
| 83 | + # Entry action: PLAYING → send PONG → IDLE (or DONE) |
| 84 | + if state == STATE_PLAYING: |
| 85 | + rounds_done += 1 |
| 86 | + writer.write(b"PONG\n") |
| 87 | + print(f"Bob [round {rounds_done}]: sent PONG", flush=True) |
| 88 | + state = STATE_IDLE if rounds_done < NUM_ROUNDS else STATE_DONE |
| 89 | + continue |
| 90 | + |
| 91 | + data = await reader.readline() |
39 | 92 | if not data: |
40 | | - print("Bob: Alice disconnected.", flush=True) |
| 93 | + print(f"Bob [{state}]: connection dropped unexpectedly.", flush=True) |
41 | 94 | break |
| 95 | + msg = data.decode("utf-8") |
| 96 | + print(f"Bob [{state}]: received '{msg}'", flush=True) |
| 97 | + |
| 98 | + handler = BOB_DISPATCH.get((state, msg)) |
| 99 | + |
| 100 | + if handler is None: |
| 101 | + print( |
| 102 | + f"Bob [{state}]: no transition for '{msg}' — ignoring.", |
| 103 | + flush=True, |
| 104 | + ) |
| 105 | + continue |
42 | 106 |
|
43 | | - message = data.decode("utf-8") |
44 | | - print(f"Bob: received '{message}'", flush=True) |
| 107 | + state = await handler(writer) |
45 | 108 |
|
46 | | - # Decide on a reply |
47 | | - if message == "ping": |
48 | | - reply = "pong" |
49 | | - else: |
50 | | - reply = "no way!" |
| 109 | + print(f"Bob: event loop finished (final state: {state}).", flush=True) |
51 | 110 |
|
52 | | - # Send the reply |
53 | | - print(f"Bob: sending '{reply}'", flush=True) |
54 | | - writer.write(reply.encode("utf-8")) |
55 | | - await writer.drain() |
56 | 111 |
|
| 112 | +# ── Entry point ─────────────────────────────────────────────────────────────── |
57 | 113 |
|
58 | 114 | if __name__ == "__main__": |
59 | 115 | # Load configuration files — paths are relative to this script's location |
|
0 commit comments