Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
288 lines (231 loc) · 8.72 KB
/
Copy pathmain.py
File metadata and controls
288 lines (231 loc) · 8.72 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
FastAPI WebSocket server for the Boids Interactive Demo.
Provides real-time simulation streaming and parameter control.
"""
import asyncio
from contextlib import asynccontextmanager
from typing import Dict
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from config import TARGET_FPS, MessageType
from models import (
parse_client_message,
UpdateParamsMessage,
ResetMessage,
PauseMessage,
ResumeMessage,
ParamsSyncMessage,
ErrorMessage,
)
from simulation_manager import SimulationManager
from presets import get_preset_params, is_valid_preset
# =============================================================================
# Application Lifespan
# =============================================================================
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application startup and shutdown."""
print("Boids Interactive Demo starting...")
yield
print("Boids Interactive Demo shutting down...")
# =============================================================================
# FastAPI Application
# =============================================================================
app = FastAPI(
title="Boids Interactive Demo",
description="Real-time boids simulation with WebSocket streaming",
version="1.0.0",
lifespan=lifespan,
)
# CORS middleware for frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# =============================================================================
# REST Endpoints
# =============================================================================
@app.get("/")
async def root():
"""Health check endpoint."""
return {"status": "ok", "service": "boids-interactive"}
@app.get("/health")
async def health():
"""Health check endpoint."""
return {"status": "healthy"}
# =============================================================================
# WebSocket Connection Manager
# =============================================================================
class ConnectionManager:
"""Manages WebSocket connections and their simulations."""
def __init__(self):
self.active_connections: Dict[WebSocket, SimulationManager] = {}
async def connect(self, websocket: WebSocket) -> SimulationManager:
"""Accept connection and create simulation."""
await websocket.accept()
manager = SimulationManager()
manager.start()
self.active_connections[websocket] = manager
return manager
def disconnect(self, websocket: WebSocket) -> None:
"""Remove connection and stop simulation."""
if websocket in self.active_connections:
manager = self.active_connections[websocket]
manager.stop()
del self.active_connections[websocket]
def get_manager(self, websocket: WebSocket) -> SimulationManager:
"""Get simulation manager for connection."""
return self.active_connections.get(websocket)
connection_manager = ConnectionManager()
# =============================================================================
# Message Handlers
# =============================================================================
async def handle_message(
websocket: WebSocket,
manager: SimulationManager,
data: dict
) -> None:
"""Handle incoming WebSocket message."""
msg_type = data.get("type")
# Handle obstacle messages first
if await handle_obstacle_message(websocket, manager, data):
return
# Handle preset separately to give better error messages
if msg_type == MessageType.PRESET:
preset_name = data.get("name", "")
if is_valid_preset(preset_name):
preset_params = get_preset_params(preset_name)
manager.update_params(preset_params)
sync = ParamsSyncMessage(params=manager.get_params_dict())
await websocket.send_json(sync.model_dump())
else:
error = ErrorMessage(message=f"Invalid preset: {preset_name}")
await websocket.send_json(error.model_dump())
return
message = parse_client_message(data)
if message is None:
error = ErrorMessage(message=f"Unknown message type: {msg_type}")
await websocket.send_json(error.model_dump())
return
if isinstance(message, UpdateParamsMessage):
manager.update_params(message.params)
sync = ParamsSyncMessage(params=manager.get_params_dict())
await websocket.send_json(sync.model_dump())
elif isinstance(message, ResetMessage):
manager.reset()
sync = ParamsSyncMessage(params=manager.get_params_dict())
await websocket.send_json(sync.model_dump())
elif isinstance(message, PauseMessage):
manager.pause()
elif isinstance(message, ResumeMessage):
manager.resume()
async def handle_obstacle_message(
websocket: WebSocket,
manager: SimulationManager,
data: dict
) -> bool:
"""
Handle obstacle-related messages.
Returns True if message was handled, False otherwise.
"""
msg_type = data.get("type")
if msg_type == MessageType.ADD_OBSTACLE:
x = data.get("x", 400)
y = data.get("y", 300)
radius = data.get("radius", 30)
result = manager.add_obstacle(x, y, radius)
await websocket.send_json({
"type": MessageType.OBSTACLE_ADDED,
**result
})
return True
elif msg_type == MessageType.REMOVE_OBSTACLE:
index = data.get("index", -1)
success = manager.remove_obstacle(index)
await websocket.send_json({
"type": MessageType.OBSTACLE_REMOVED,
"index": index,
"success": success
})
return True
elif msg_type == MessageType.CLEAR_OBSTACLES:
count = manager.clear_obstacles()
await websocket.send_json({
"type": MessageType.OBSTACLES_CLEARED,
"count": count
})
return True
return False
# =============================================================================
# WebSocket Endpoint
# =============================================================================
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""
WebSocket endpoint for simulation streaming.
"""
manager = await connection_manager.connect(websocket)
frame_interval = 1.0 / TARGET_FPS
running = True
async def send_frames():
"""Continuously send frame data."""
nonlocal running
try:
while running:
frame_start = asyncio.get_event_loop().time()
manager.update()
frame_data = manager.get_frame_data()
await websocket.send_json(frame_data.model_dump())
frame_end = asyncio.get_event_loop().time()
elapsed = frame_end - frame_start
sleep_time = frame_interval - elapsed
if sleep_time > 0:
await asyncio.sleep(sleep_time)
except Exception as e:
print(f"Send frames error: {e}")
running = False
async def receive_messages():
"""Receive and handle client messages."""
nonlocal running
try:
while running:
data = await websocket.receive_json()
await handle_message(websocket, manager, data)
except WebSocketDisconnect:
running = False
except Exception as e:
print(f"Receive messages error: {e}")
running = False
try:
# Send initial params sync
sync = ParamsSyncMessage(params=manager.get_params_dict())
await websocket.send_json(sync.model_dump())
# Run send and receive concurrently
send_task = asyncio.create_task(send_frames())
receive_task = asyncio.create_task(receive_messages())
done, pending = await asyncio.wait(
[send_task, receive_task],
return_when=asyncio.FIRST_COMPLETED
)
running = False
for task in pending:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
except WebSocketDisconnect:
pass
except Exception as e:
print(f"WebSocket error: {e}")
finally:
connection_manager.disconnect(websocket)
# =============================================================================
# Main Entry Point
# =============================================================================
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
You can’t perform that action at this time.
