TASK
Implementation
TCP is a byte stream, not a message protocol. You need framing to know where one message ends and the next begins. Length-prefixed framing prepends each message with its size.
Format: [4-byte big-endian length][payload]
Implement send_frame and recv_frame:
Request: {"type": "frame_encode", "msg_id": 1, "payload": "hello world"}
Response: {"type": "frame_encode_ok", "in_reply_to": 1, "frame_hex": "0000000b68656c6c6f20776f726c64", "total_bytes": 15}
Request: {"type": "frame_decode", "msg_id": 2, "frame_hex": "0000000b68656c6c6f20776f726c64"}
Response: {"type": "frame_decode_ok", "in_reply_to": 2, "payload": "hello world", "payload_length": 11}
Request: {"type": "frame_decode_partial", "msg_id": 3, "chunks": ["0000000b68", "656c6c6f20", "776f726c64"]}
Response: {"type": "frame_decode_partial_ok", "in_reply_to": 3, "payload": "hello world", "chunks_needed": 3}Sample Test Cases
Encode a simple messageTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"frame_encode","msg_id":2,"payload":"hello"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "frame_encode_ok", "in_reply_to": 2, "frame_hex": "0000000568656c6c6f", "total_bytes": 9, "msg_id": 1}}
Decode a framed messageTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"frame_decode","msg_id":2,"frame_hex":"0000000568656c6c6f"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "frame_decode_ok", "in_reply_to": 2, "payload": "hello", "payload_length": 5, "msg_id": 1}}
Hints
Hint 1▾
Each message is [4-byte big-endian length][payload]
Hint 2▾
The receiver must read exactly 4 bytes first to get the length
Hint 3▾
Then read exactly length bytes for the payload
Hint 4▾
TCP does not guarantee message boundaries; handle partial reads
Hint 5▾
Use a buffer to accumulate bytes until a full frame is available
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
message framinglength prefixTCP streampartial reads
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()