From 053c9789bc950fc71c29fcb19ae86f46f1ef9f87 Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Tue, 15 May 2018 13:35:38 -0700 Subject: [PATCH] web app updates to support save/open session xml files --- webapp/app.py | 34 ++++++++++++++++++++++++++++++ webapp/templates/index.html | 42 ++++++++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/webapp/app.py b/webapp/app.py index cb0656bb..888c0e79 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -1,4 +1,5 @@ import os +import tempfile from functools import wraps from threading import Lock @@ -6,6 +7,7 @@ from flask import Flask from flask import jsonify from flask import render_template from flask import request +from flask import send_file from flask_socketio import SocketIO from flask_socketio import emit @@ -92,6 +94,38 @@ def get_ips(): ) +@app.route("/sessions//xml") +def save_xml(session_id): + session = coreemu.sessions.get(session_id) + if not session: + return jsonify(error="session does not exist"), 404 + + _, temp_path = tempfile.mkstemp() + session.save_xml(temp_path, "1.0") + return send_file( + temp_path, + as_attachment=True, + attachment_filename="session-%s.xml" % session_id + ) + + +@app.route("/sessions/xml", methods=["POST"]) +def open_xml(): + session = coreemu.create_session() + logger.info("open xml: %s", request.files) + _, temp_path = tempfile.mkstemp() + session_file = request.files['session'] + session_file.save(temp_path) + + try: + session.open_xml(temp_path, start=True) + return jsonify(id=session.session_id) + except: + logger.exception("error opening session file") + coreemu.delete_session(session.session_id) + return jsonify(error="error opening session file"), 404 + + @app.route("/sessions") def get_sessions(): sessions = [] diff --git a/webapp/templates/index.html b/webapp/templates/index.html index 858b12ae..84ef8fba 100644 --- a/webapp/templates/index.html +++ b/webapp/templates/index.html @@ -28,8 +28,10 @@ File @@ -118,11 +120,14 @@
+ data-option="linkrf">Link All Routers + + data-option="services">Services + + data-option="delete">Delete +
@@ -148,6 +153,8 @@ const $runButton = $('#run-button'); const $nodeSelect = $('#node-select'); const $newSessionButton = $('#new-session-button'); + const $sessionSaveButton = $('#session-save-button'); + const $sessionOpen = $('#session-open'); function formToJson($form) { const formData = {}; @@ -203,6 +210,8 @@ $sessionId.text(`Session: ${session.id}`); const isStartEnabled = session.state !== SessionStates.runtime; setRunButton(isStartEnabled); + $sessionSaveButton.attr('href', `/sessions/${session.id}/xml`); + $sessionSaveButton.attr('download', true); } // handle network clicks @@ -299,6 +308,29 @@ $nodeSelect.attr('src', icon); }); + $sessionOpen.change(function (event) { + const file = event.target.files[0]; + console.log('session open: ', file); + const uri = `/sessions/xml`; + const request = new XMLHttpRequest(); + const formData = new FormData(); + request.open('POST', uri, true); + request.onreadystatechange = function () { + if (request.readyState == 4 && request.status == 200) { + const response = JSON.parse(request.responseText); + coreNetwork.joinSession(response.id) + .then(function (session) { + joinSession(session); + }) + .catch(function (err) { + console.log('join open session error: ', err); + }); + } + }; + formData.append('session', file); + request.send(formData); + }); + console.log('connecting to ws'); const ws = io.connect(); ws.on('connection', function () {