Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSEGroup22SDP/pserver.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
131 lines (115 sloc)
5.21 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, send_file, jsonify, abort | |
import zipfile | |
import json | |
import time | |
import sys | |
import Python.main as main | |
import os | |
app = Flask(__name__) | |
@app.route('/runSimulation/<input1>') # This is the endpoint that the javascript code will call to run simulation on server | |
def runSimulation(input1): | |
start_time = time.time() | |
# Parse simulation parameters. | |
Param = input1.split("&") # Parameters are separated by '&' delimiter. | |
print(Param) | |
# Check parameter count. If simulation never happens then check here! | |
if len(Param)==25: # VERY-IMPORTANT: Modify here if there are changes to total number of requests! | |
ddd_state = (Param[0] == "true") | |
skip_sim = (Param[1] == "true") | |
sumOut = (Param[2] == "true") | |
logOut = (Param[3] == "true") | |
packet_size = int(Param[4]) | |
rtsa = int(Param[5]) | |
max_queue_size = int(Param[6]) | |
ddd_mapping_delay = int(Param[7]) | |
propagation_delay = int(Param[8]) | |
number_of_ddd_ports = int(Param[9]) | |
time_to_next_ddd_port = int(Param[10]) | |
overlap_time_old_ddd = int(Param[11]) | |
durationSim = float(Param[12]) | |
numbSynAckResends = int(Param[13]) | |
clienttoRouter = int(Param[14]) | |
routertoClient = int(Param[15]) | |
routertoReflector = int(Param[16]) | |
routertoNormal = int(Param[17]) | |
normaltoRouter = int(Param[18]) | |
reflectortoRouter = int(Param[19]) | |
attackertoReflector = int(Param[20]) | |
queueLimit = int(Param[21]) | |
attackerType = float(Param[22]) | |
amplification = float(Param[23]) | |
storageFolder = Param[24] | |
if storageFolder=="Python" or storageFolder=="static" or storageFolder=="templates": | |
abort(400) # NEVER ALLOW FILES TO BE GENERATED IN CODE FOLDERS! | |
else: | |
response = main.runSimulation(ddd_state, skip_sim, sumOut, | |
logOut, packet_size, rtsa, | |
max_queue_size, ddd_mapping_delay, | |
propagation_delay, number_of_ddd_ports, | |
time_to_next_ddd_port, overlap_time_old_ddd, | |
durationSim, numbSynAckResends, clienttoRouter, | |
routertoClient, routertoReflector, routertoNormal, | |
normaltoRouter, reflectortoRouter, | |
attackertoReflector, queueLimit, attackerType, | |
amplification, storageFolder) | |
print('Backend Simulation Total Elapsed Time:', time.time() - start_time, 'seconds') | |
# Converting response data structure into json string | |
return jsonify(response) | |
else: | |
abort(400) | |
@app.route('/download/<storageFolder>/<sessionId>/<fileName>') # This is the endpoint that the javascript code will call to run simulation on server | |
def download(storageFolder, sessionId, fileName): | |
try: | |
filePath = os.path.join(storageFolder, sessionId, fileName) | |
if fileName.startswith("log"): | |
return send_file(filePath, as_attachment=True) | |
elif fileName.startswith("summary"): | |
return send_file(filePath, as_attachment=True) | |
elif fileName.endswith(".svg"): | |
return send_file(filePath, as_attachment=True) | |
else: | |
abort(400) | |
except FileNotFoundError: | |
abort(404) | |
@app.route('/download2/<storageFolder>/<sessionId>/<statement>') | |
def download2(storageFolder, sessionId, statement): | |
# We may use split function to process the request. | |
tmp = statement.split("&") | |
fileName = tmp[0] | |
newName = tmp[1] | |
if len(tmp)==2: | |
try: | |
filePath = os.path.join(storageFolder, sessionId, fileName) | |
if fileName.startswith("log"): | |
return send_file(filePath, as_attachment=True, attachment_filename=newName) | |
elif fileName.startswith("summary"): | |
return send_file(filePath, as_attachment=True, attachment_filename=newName) | |
elif fileName.endswith(".svg"): | |
return send_file(filePath, as_attachment=True, attachment_filename=newName) | |
else: | |
abort(400) | |
except FileNotFoundError: | |
abort(404) | |
else: | |
abort(400) | |
@app.route('/render_image/<storageFolder>/<sessionId>/<fileName>') | |
def render_image(storageFolder, sessionId, fileName): | |
try: | |
filePath = os.path.join(storageFolder, sessionId, fileName) | |
if fileName.endswith(".svg"): | |
return send_file(filePath) | |
elif fileName.endswith(".html"): | |
return send_file(filePath) | |
else: | |
abort(400) | |
except FileNotFoundError: | |
abort(404) | |
@app.route('/') | |
def index(): | |
return render_template('temp_index.html') # This renders index.html as the starting screen | |
@app.errorhandler(400) | |
def badRequest(e): | |
return render_template("400.html"), 400 | |
@app.errorhandler(404) | |
def pageNotFound(e): | |
return render_template("404.html"), 404 |