-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
242 lines (198 loc) · 7.69 KB
/
backend.py
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
# Flex Net Sim Backend API
# A Flask API for running Flex Net Sim network simulations
from flask import Flask, request, Response, stream_with_context
from flask_cors import CORS
from utils.helpers import *
import time
import json
# --- Flask Application Setup ---
app = Flask(__name__)
# Enable CORS only for /run_simulation_stream
CORS(app, resources={r"/run_simulation_stream": {"origins": "*"}})
@app.route("/run_simulation", methods=["POST"])
def run_simulation():
"""
Executes a network simulation with the provided parameters.
Accepts a JSON request with simulation parameters and runs the C++ executable
with those parameters. Returns the simulation results.
Returns:
JSON response: Simulation data or error details
"""
# Validate prerequisites
is_valid, error_response = validate_simulation_prerequisites()
if not is_valid:
compile_simulation(True)
return error_response
try:
data = request.get_json()
# Parse and validate parameters
is_valid, result = parse_simulation_parameters(data)
if not is_valid:
return result
# Build and execute command
command = build_simulation_command(result)
logger.debug(f"Running simulation with command: {' '.join(command)}")
# Execute simulation
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process.communicate()
# Handle execution result
if process.returncode != 0:
logger.error(f"Simulation execution failed. Return code: {process.returncode}, Error: {stderr.strip()}")
return jsonify({
"status": "error",
"message": "Simulation execution failed",
"error": stderr.strip()
}), 500
# Return successful result
return jsonify({
"status": "success",
"data": stdout.strip()
}), 200
except Exception as e:
# Handle unexpected errors
logger.exception("Unexpected error during simulation:")
return jsonify({
"status": "error",
"message": "An unexpected error occurred"
}), 500
@app.route("/run_simulation_stream", methods=["POST"])
def run_simulation_stream():
"""
Executes a network simulation with the provided parameters and streams the output.
Accepts a JSON request with simulation parameters and runs the C++ executable
with those parameters. Returns a streaming response with simulation results
as they become available.
Returns:
Streaming response: Line-by-line simulation data
"""
# Validate prerequisites
is_valid, error_response = validate_simulation_prerequisites()
if not is_valid:
compile_simulation(True)
return error_response
try:
data = request.get_json()
# Parse and validate parameters
is_valid, result = parse_simulation_parameters(data)
if not is_valid:
return result
# Build command
command = build_simulation_command(result)
logger.debug(f"Running streaming simulation with command: {' '.join(command)}")
# Create streaming function
def generate():
# Send initial event
yield f"event: start\n"
yield f"data: {json.dumps({'status': 'started', 'message': 'Simulation started', 'timestamp': time.time()})}\n\n"
# Execute simulation with streaming output
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1 # Line buffered
)
# Stream stdout
for line in iter(process.stdout.readline, ""):
if line:
yield f"event: data\n"
yield f"data: {json.dumps({'status': 'running', 'message': line.strip(), 'timestamp': time.time()})}\n\n"
# Check for errors at the end
process.stdout.close()
return_code = process.wait()
if return_code != 0:
error = process.stderr.read().strip()
yield f"event: error\n"
yield f"data: {json.dumps({'status': 'error', 'message': 'Simulation execution failed', 'error': error, 'timestamp': time.time()})}\n\n"
logger.error(f"Streaming simulation failed. Return code: {return_code}, Error: {error}")
# Close the stream resources
process.stderr.close()
# Send completion event
yield f"event: end\n"
yield f"data: {json.dumps({'status': 'completed', 'message': 'Simulation completed', 'timestamp': time.time()})}\n\n"
return Response(
stream_with_context(generate()),
mimetype="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
"Connection": "keep-alive"
}
)
except Exception as e:
# Handle unexpected errors
logger.exception("Unexpected error during streaming simulation:")
return jsonify({
"status": "error",
"message": "An unexpected error occurred",
"timestamp": time.time()
}), 500
@app.route("/help", methods=["GET"])
def simulation_help():
"""
Provides API documentation in plain text format.
Returns information about available endpoints, parameters,
and example usage for the Flex Net Sim API.
Returns:
Plain text response: API documentation
"""
help_message = """\
Flex Net Sim API Documentation
ENDPOINTS:
- /run_simulation (POST): Returns complete simulation results
- /run_simulation_stream (POST): Streams results in real-time using Server-Sent Events
COMMON PARAMETERS (JSON body, all optional):
algorithm: "FirstFit" or "BestFit" (default: "FirstFit")
networkType: 1 for EON (default: 1)
goalConnections: 1-10000000 (default: 100000)
confidence: 0-1 (default: 0.05)
lambdaParam: > 0 (default: 1.0)
mu: > 0 (default: 10.0)
network: "NSFNet", "Cost239", "EuroCore", "GermanNet", "UKNet" (default: "NSFNet")
bitrate: "fixed-rate" or "flex-rate" (default: "fixed-rate")
K: 1-6 (default: 3)
EXAMPLE - STANDARD REQUEST:
curl -X POST -H "Content-Type: application/json" \\
-d '{"algorithm": "FirstFit", "goalConnections": 1000000, "lambdaParam": 120, "mu": 1}' \\
https://fns-api-cloud-run-787143541358.us-central1.run.app/run_simulation
EXAMPLE - STREAMING REQUEST:
curl -X POST -H "Content-Type: application/json" \\
-d '{"algorithm": "FirstFit", "goalConnections": 1000000, "lambdaParam": 120, "mu": 1}' \\
https://fns-api-cloud-run-787143541358.us-central1.run.app/run_simulation_stream
RESPONSES:
Standard (/run_simulation):
Success (200): {"status": "success", "data": "simulation results..."}
Invalid Parameters (400): {"status": "error", "message": "Invalid parameters", "error": "Details"}
Error (500): {"status": "error", "message": "Error message", "error": "Details"}
Streaming (/run_simulation_stream):
Success (200): Server-Sent Events (text/event-stream)
event: start
data: {"status": "started", "message": "Simulation started"}
event: data
data: {"status": "running", "message": "Line of output"}
event: end
data: {"status": "completed", "message": "Simulation completed"}
Invalid Parameters (400): {"status": "error", "message": "Invalid parameters", "error": "Details"}
Error (500): {"status": "error", "message": "Error message", "error": "Details"}
"""
return app.response_class(
response=help_message,
status=200,
mimetype="text/plain"
)
# --- Application Initialization ---
with app.app_context():
# Compile the simulation on startup
compile_success = compile_simulation()
# --- Main Entry Point ---
if __name__ == "__main__":
if not compile_success:
logger.error("Application startup failed: Compilation error. Check logs for details.")
else:
logger.info("Starting Flex Net Sim API server...")
app.run(host="0.0.0.0")