Skip to content

Commit 7e49ee7

Browse files
committed
Add real-time streaming endpoint and improve error handling
- Added new /run_simulation_stream endpoint for SSE streaming - Improved parameter validation with better error messages - Changed error response code from 500 to 400 for invalid parameters - Reorganized test suite into separate files - Updated documentation with streaming endpoint examples - Refactored code to reduce duplication and improve maintainability
1 parent 303cc52 commit 7e49ee7

16 files changed

Lines changed: 760 additions & 282 deletions
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ This guide helps developers set up, test, and deploy the Flex Net Sim Backend AP
4343
# Test help endpoint
4444
curl http://127.0.0.1:5000/help
4545

46-
# Test simulation endpoint
46+
# Test standard simulation endpoint
4747
curl -X POST -H "Content-Type: application/json" -d '{}' http://127.0.0.1:5000/run_simulation
48+
49+
# Test streaming simulation endpoint
50+
curl -N -X POST -H "Content-Type: application/json" -d '{"goalConnections": 50000}' http://127.0.0.1:5000/run_simulation_stream
4851
```
4952

5053
## Testing
@@ -119,7 +122,11 @@ Follow the Google Cloud setup steps from the video, focusing on:
119122
Use the `curl` command with the `ENDPOINT-URL` you obtained from the previous steps to test your deployed API. Replace `YOUR-ENDPOINT-URL` with the actual `ENDPOINT-URL`.
120123

121124
```bash
125+
# Test standard endpoint
122126
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "fixed-rate"}' <YOUR-ENDPOINT-URL>/run_simulation
127+
128+
# Test streaming endpoint
129+
curl -N -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "goalConnections": 5000000}' <YOUR-ENDPOINT-URL>/run_simulation_stream
123130
```
124131

125132
Remember that depending on the authetification preferences you might need to authetificate to send request to the Endpoint just created.

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ simulation.out
1010
# Test files
1111
.coverage
1212
htmlcov
13-
.pytest_cache
13+
.pytest_cache
14+
15+
# Claude Code
16+
CLAUDE.md

CHANGELOG.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,32 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [Unreleased]
9-
108
### ROADMAP
119
- Switch to Flex Net Sim v0.8.2.
1210
- Enhance previous algorithms.
1311
- Add algorithm BestFit.
1412
- Switch to new domain.
1513

16-
## [1.1.1] - 2023-03-01
14+
## [2.0.0] - 2025-03-xx
15+
16+
### Added
17+
- New `/run_simulation_stream` endpoint for streaming simulation results in real-time
18+
- Refactored backend code to reduce duplication and improve maintainability
19+
- Added unit tests for the streaming endpoint
20+
- Modified `simulator.hpp` to flush stdout after each line for better streaming support (see `src/README.md`)
21+
- Improved help endpoint with more concise documentation
22+
23+
### Changed
24+
- Standardized API response format for better consistency:
25+
- Success responses now use `status: "success"` with data in a `data` field
26+
- Error responses now use `status: "error"` with a `message` and detailed `error` fields
27+
- Streaming responses include event types and structured JSON data
28+
- Reorganized test suite into separate files for better maintainability
29+
- Improved parameter validation with descriptive error messages
30+
- Changed error response code from 500 to 400 for invalid parameters
31+
- Moved parameter validation from C++ to the API layer for better user experience
32+
33+
## [1.1.1] - 2025-03-01
1734

1835
### Added
1936
- Deployed coverage to pages
@@ -60,5 +77,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6077
- API allocated in cloud run.
6178
- Documentation README for the process of develop/deployment located in [workflows](https://github.com/MirkoZETA/FlexNetSim-API/tree/master/.github/workflows/README_DEV.md).
6279

63-
[1.1.0]: https://github.com/MirkoZETA/FlexNetSim-API/releases/tag/v1.1.0
6480
[1.0.0]: https://github.com/MirkoZETA/FlexNetSim-API/releases/tag/v1.0.0
81+
[1.1.0]: https://github.com/MirkoZETA/FlexNetSim-API/releases/tag/v1.1.0
82+
[2.0.0]: https://github.com/MirkoZETA/FlexNetSim-API/releases/tag/v2.0.0

README.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ For development and deployment instructions for this API, refer to [README_DEV.m
3434

3535
### `/run_simulation` (POST)
3636

37-
Runs a network simulation with the provided parameters.
37+
Runs a network simulation with the provided parameters and returns complete results.
3838

3939
#### Request Parameters
4040

@@ -74,9 +74,84 @@ curl -X POST -H "Content-Type: application/json" \
7474
https://fns-api-cloud-run-787143541358.us-central1.run.app/run_simulation
7575
```
7676

77+
#### Response Format
78+
79+
```json
80+
{
81+
"status": "success",
82+
"data": "simulation results as text..."
83+
}
84+
```
85+
86+
#### Response Codes
87+
88+
- `200 OK`: Success
89+
- `400 Bad Request`: Invalid parameters
90+
- `500 Internal Server Error`: Server-side error
91+
92+
### `/run_simulation_stream` (POST)
93+
94+
Runs a network simulation with the provided parameters and streams results in real-time using Server-Sent Events (SSE).
95+
96+
#### Request Parameters
97+
98+
Same parameters as `/run_simulation` endpoint.
99+
100+
#### Example: Streaming with Default Parameters
101+
102+
```bash
103+
curl -N -X POST -H "Content-Type: application/json" -d '{}' \
104+
http://localhost:5000/run_simulation_stream
105+
```
106+
107+
#### Example: Streaming with Custom Parameters
108+
109+
```bash
110+
curl -N -X POST -H "Content-Type: application/json" \
111+
-d '{
112+
"algorithm": "FirstFit",
113+
"networkType": 1,
114+
"goalConnections": 5000000,
115+
"confidence": 0.05,
116+
"lambdaParam": 120,
117+
"mu": 1,
118+
"network": "NSFNet",
119+
"bitrate": "fixed-rate"
120+
}' \
121+
http://localhost:5000/run_simulation_stream
122+
```
123+
124+
#### Response Format
125+
126+
The endpoint returns a stream of Server-Sent Events with the following event types:
127+
128+
1. **Start Event**:
129+
```
130+
event: start
131+
data: {"status": "started", "message": "Simulation started"}
132+
```
133+
134+
2. **Data Events** (multiple events, one per line of output):
135+
```
136+
event: data
137+
data: {"status": "running", "data": "Line of simulation output"}
138+
```
139+
140+
3. **End Event**:
141+
```
142+
event: end
143+
data: {"status": "completed", "message": "Simulation completed"}
144+
```
145+
146+
4. **Error Event** (only if an error occurs):
147+
```
148+
event: error
149+
data: {"status": "error", "message": "Error description", "error": "Detailed error"}
150+
```
151+
77152
#### Response Codes
78153

79-
- `200 OK`: Success. Returns `{"output": "...", "error": ""}`
154+
- `200 OK`: Success (stream starts)
80155
- `400 Bad Request`: Invalid parameters
81156
- `500 Internal Server Error`: Server-side error
82157

0 commit comments

Comments
 (0)