-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathprotocol.py
More file actions
178 lines (142 loc) · 5.02 KB
/
protocol.py
File metadata and controls
178 lines (142 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Protocol definitions for the GPU inference game.
Shared between server and client.
"""
import json
import socket
import time
from dataclasses import dataclass, asdict
from typing import List, Dict, Optional, Any
from enum import Enum
class MessageType(Enum):
REGISTER = "register"
INFERENCE_REQUEST = "inference_request"
INFERENCE_RESPONSE = "inference_response"
SCORE_UPDATE = "score_update"
HEARTBEAT = "heartbeat"
ERROR = "error"
STATS_UPDATE = "stats_update"
@dataclass
class RegisterMessage:
message_type: str = MessageType.REGISTER.value
@dataclass
class InferenceRequest:
unique_ids: list[int]
symbols: list[str]
features: list[list[float]]
timestamp: float # When sent by server
message_type: str = MessageType.INFERENCE_REQUEST.value
@dataclass
class InferenceResponse:
unique_ids: list[int]
predictions: list[list[float]]
client_timestamp: float # When client processed
message_type: str = MessageType.INFERENCE_RESPONSE.value
@dataclass
class ScoreUpdate:
unique_ids: list[int]
trade_pnls: list[float]
accuracies: list[float]
latencies_ms: list[float]
message_type: str = MessageType.SCORE_UPDATE.value
@dataclass
class Heartbeat:
timestamp: float
message_type: str = MessageType.HEARTBEAT.value
@dataclass
class ErrorMessage:
error: str
details: Optional[str] = None
message_type: str = MessageType.ERROR.value
class ProtocolHandler:
"""Handles encoding/decoding of protocol messages."""
MESSAGE_CLASSES = {
MessageType.REGISTER.value: RegisterMessage,
MessageType.INFERENCE_REQUEST.value: InferenceRequest,
MessageType.INFERENCE_RESPONSE.value: InferenceResponse,
MessageType.SCORE_UPDATE.value: ScoreUpdate,
MessageType.HEARTBEAT.value: Heartbeat,
MessageType.ERROR.value: ErrorMessage,
}
@staticmethod
def encode(message: Any) -> bytes:
"""Encode a message object to JSON bytes with newline."""
data = asdict(message)
return (json.dumps(data) + "\n").encode("utf-8")
@staticmethod
def decode(data: bytes) -> Any:
"""Decode JSON bytes to appropriate message object."""
try:
msg_dict = json.loads(data.decode("utf-8").strip())
msg_type = msg_dict.get("message_type")
if msg_type not in ProtocolHandler.MESSAGE_CLASSES:
raise ValueError(f"Unknown message type: {msg_type}")
cls = ProtocolHandler.MESSAGE_CLASSES[msg_type]
# Remove message_type from dict before creating object
msg_dict.pop("message_type", None)
obj = cls(**msg_dict)
obj.message_type = msg_type
return obj
except Exception as e:
return ErrorMessage(error=str(e))
class SocketReader:
"""Buffered socket reader for handling newline-delimited messages."""
def __init__(self, sock: socket.socket, buffer_size: int = 4096):
self.sock = sock
self.buffer = b""
self.buffer_size = buffer_size
def read_message(self, timeout: Optional[float] = None) -> Optional[Any]:
"""Read one complete message from socket."""
if timeout:
self.sock.settimeout(timeout)
while b"\n" not in self.buffer:
try:
data = self.sock.recv(self.buffer_size)
if not data:
return None # Connection closed
self.buffer += data
except socket.timeout:
return None
except Exception:
return None
# Extract one complete message
line, self.buffer = self.buffer.split(b"\n", 1)
if line:
return ProtocolHandler.decode(line)
return None
def read_all_available(self) -> List[Any]:
"""Read all currently available messages without blocking."""
messages = []
self.sock.setblocking(False)
try:
# First, read all available data
while True:
try:
data = self.sock.recv(self.buffer_size)
if not data:
break
self.buffer += data
except socket.error:
break
# Then parse all complete messages
while b"\n" in self.buffer:
line, self.buffer = self.buffer.split(b"\n", 1)
if line:
msg = ProtocolHandler.decode(line)
if msg:
messages.append(msg)
finally:
self.sock.setblocking(True)
return messages
class SocketWriter:
"""Socket writer for sending protocol messages."""
def __init__(self, sock: socket.socket):
self.sock = sock
def send_message(self, message: Any) -> bool:
"""Send a message over the socket."""
try:
data = ProtocolHandler.encode(message)
self.sock.sendall(data)
return True
except Exception:
return False