{
"id": "273",
"prompt": "Message-rate monitor for a socket: drain host::net::poll each frame into a per-window counter, and every 60 frames push that window's count into an 8-entry ring of state slots (head index in another slot) so the last 8 seconds render as bars, oldest to newest, with the count under each and a running total up top. If open() was refused (negative handle) say so - the rate just stays at zero.",
"solution_rl": "fn frame(t: i32) {\n host::display::clear(0x0a0e12);\n if host::display::state_get(0) == 0 {\n host::display::state_set(1, host::net::open(\"wss://relay.example.net/ticker\"));\n host::display::state_set(0, 1);\n }\n let h: i32 = host::display::state_get(1);\n if h >= 0 {\n let mut got: i32 = host::net::poll(h, 4096, 220);\n while got > 0 {\n host::display::state_set(2, host::display::state_get(2) + 1);\n got = host::net::poll(h, 4096, 220);\n }\n }\n if t % 60 == 59 {\n let head: i32 = host::display::state_get(11);\n host::display::state_set(3 + head, host::display::state_get(2));\n host::display::state_set(11, (head + 1) % 8);\n host::display::state_set(12, host::display::state_get(12) + host::display::state_get(2));\n host::display::state_set(2, 0);\n }\n host::display::draw_string(110, 40, \"MESSAGE RATE\", 0xffffff, 2);\n host::display::draw_string(80, 90, \"TOTAL\", 0x888888, 1);\n host::display::draw_number(140, 88, host::display::state_get(12), 0x66ddff, 1);\n let head2: i32 = host::display::state_get(11);\n let mut i: i32 = 0;\n while i < 8 {\n let v: i32 = host::display::state_get(3 + (head2 + i) % 8);\n let mut bh: i32 = v * 12;\n if bh > 240 {\n bh = 240;\n }\n host::display::fill_rect(80 + i * 45, 400 - bh, 36, bh + 4, 0x3388cc);\n host::display::draw_number(88 + i * 45, 420, v, 0x888888, 1);\n i = i + 1;\n }\n if h < 0 {\n host::display::draw_string(80, 460, \"SOCKET OFFLINE - RATE STAYS 0\", 0xbb3333, 1);\n }\n host::display::present();\n}\n",
"tags": [
"net",
"ring-buffer",
"chart"
]
}