44from flask import Flask , request , Response , stream_with_context
55from flask_cors import CORS
66from utils .helpers import *
7+ import time
8+ import json
79
810# --- Flask Application Setup ---
911app = Flask (__name__ )
@@ -76,65 +78,69 @@ def run_simulation():
7678def run_simulation_stream ():
7779 """
7880 Executes a network simulation with the provided parameters and streams the output.
79-
81+
8082 Accepts a JSON request with simulation parameters and runs the C++ executable
8183 with those parameters. Returns a streaming response with simulation results
8284 as they become available.
83-
85+
8486 Returns:
8587 Streaming response: Line-by-line simulation data
8688 """
8789 # Validate prerequisites
8890 is_valid , error_response = validate_simulation_prerequisites ()
8991 if not is_valid :
9092 return error_response
91-
93+
9294 try :
9395 data = request .get_json ()
94-
96+
9597 # Parse and validate parameters
9698 is_valid , result = parse_simulation_parameters (data )
9799 if not is_valid :
98100 return result
99-
101+
100102 # Build command
101103 command = build_simulation_command (result )
102104 logger .debug (f"Running streaming simulation with command: { ' ' .join (command )} " )
103105
104106 # Create streaming function
105107 def generate ():
106108 # Send initial event
107- yield f"event: start\n data: {{\n \" status\" : \" started\" ,\n \" message\" : \" Simulation started\" \n }}\n \n "
108-
109+ yield f"event: start\n "
110+ yield f"data: { json .dumps ({'status' : 'started' , 'message' : 'Simulation started' , 'timestamp' : time .time ()})} \n \n "
111+
109112 # Execute simulation with streaming output
110113 process = subprocess .Popen (
111- command ,
112- stdout = subprocess .PIPE ,
113- stderr = subprocess .PIPE ,
114+ command ,
115+ stdout = subprocess .PIPE ,
116+ stderr = subprocess .PIPE ,
114117 text = True ,
115118 bufsize = 1 # Line buffered
116119 )
117-
120+
118121 # Stream stdout
119122 for line in iter (process .stdout .readline , "" ):
120123 if line :
121- yield f"event: data\n data: {{\n \" status\" : \" running\" ,\n \" data\" : \" { line .strip ()} \" \n }}\n \n "
122-
124+ yield f"event: data\n "
125+ yield f"data: { json .dumps ({'status' : 'running' , 'message' : line .strip (), 'timestamp' : time .time ()})} \n \n "
126+
123127 # Check for errors at the end
124128 process .stdout .close ()
125129 return_code = process .wait ()
126-
130+
127131 if return_code != 0 :
128- error = process .stderr .read ()
129- yield f"event: error\n data: {{\n \" status\" : \" error\" ,\n \" message\" : \" Simulation execution failed\" ,\n \" error\" : \" { error .strip ()} \" \n }}\n \n "
130- logger .error (f"Streaming simulation failed. Return code: { return_code } , Error: { error .strip ()} " )
132+ error = process .stderr .read ().strip ()
133+ yield f"event: error\n "
134+ yield f"data: { json .dumps ({'status' : 'error' , 'message' : 'Simulation execution failed' , 'error' : error , 'timestamp' : time .time ()})} \n \n "
135+ logger .error (f"Streaming simulation failed. Return code: { return_code } , Error: { error } " )
131136
132137 # Close the stream resources
133138 process .stderr .close ()
134-
139+
135140 # Send completion event
136- yield f"event: end\n data: {{\n \" status\" : \" completed\" ,\n \" message\" : \" Simulation completed\" \n }}\n \n "
137-
141+ yield f"event: end\n "
142+ yield f"data: { json .dumps ({'status' : 'completed' , 'message' : 'Simulation completed' , 'timestamp' : time .time ()})} \n \n "
143+
138144 return Response (
139145 stream_with_context (generate ()),
140146 mimetype = "text/event-stream" ,
@@ -150,8 +156,9 @@ def generate():
150156 logger .exception ("Unexpected error during streaming simulation:" )
151157 return jsonify ({
152158 "status" : "error" ,
153- "message" : "An unexpected error occurred" ,
154- "error" : str (e )
159+ "message" : "An unexpected error occurred" ,
160+ "error" : str (e ),
161+ "timestamp" : time .time ()
155162 }), 500
156163
157164@app .route ("/help" , methods = ["GET" ])
@@ -174,7 +181,7 @@ def simulation_help():
174181 - /run_simulation_stream (POST): Streams results in real-time using Server-Sent Events
175182
176183 COMMON PARAMETERS (JSON body, all optional):
177- algorithm: "FirstFit" or "ExactFit " (default: "FirstFit")
184+ algorithm: "FirstFit" or "BestFit " (default: "FirstFit")
178185 networkType: 1 for EON (default: 1)
179186 goalConnections: 1-10000000 (default: 100000)
180187 confidence: 0-1 (default: 0.05)
@@ -206,7 +213,7 @@ def simulation_help():
206213 data: {"status": "started", "message": "Simulation started"}
207214
208215 event: data
209- data: {"status": "running", "data ": "Line of output"}
216+ data: {"status": "running", "message ": "Line of output"}
210217
211218 event: end
212219 data: {"status": "completed", "message": "Simulation completed"}
0 commit comments