From d2ebdb0468fac2364e475c59df6868b933004fb3 Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Mon, 27 Aug 2018 14:57:36 -0700 Subject: [PATCH] added wlan config set/get to rest, added node terminal command for rest, added hook query to rest --- webapp/app.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/webapp/app.py b/webapp/app.py index fb0c342b..cf759821 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -21,6 +21,7 @@ from core.enumerations import LinkTypes from core.enumerations import NodeTypes from core.misc import nodeutils from core.misc.ipaddress import Ipv4Prefix, Ipv6Prefix +from core.mobility import BasicRangeModel from core.service import ServiceManager CORE_LOCK = Lock() @@ -108,6 +109,17 @@ def convert_link(session, link_data): } +def broadcast_event(event): + socketio.emit("event", { + "node": event.node, + "event_type": event.event_type, + "name": event.name, + "data": event.data, + "time": event.time, + "session": event.session + }) + + @socketio.on("connect") def websocket_connect(): emit("info", {"message": "You are connected!"}) @@ -219,6 +231,9 @@ def create_session(): session.location.setrefgeo(47.57917, -122.13232, 2.0) session.location.refscale = 150.0 + # add handlers + session.event_handlers.append(broadcast_event) + response_data = jsonify( id=session.session_id, state=session.state, @@ -292,6 +307,51 @@ def add_hook(session_id): return jsonify() +@app.route("/sessions//hooks") +def get_hooks(session_id): + session = coreemu.sessions.get(session_id) + if not session: + return jsonify(error="session does not exist"), 404 + + hooks = [] + for state, state_hooks in session._hooks.iteritems(): + for file_name, file_data in state_hooks: + hooks.append({ + "state": state, + "file": file_name, + "data": file_data + }) + + return jsonify(hooks=hooks) + + +@app.route("/sessions//nodes//wlan") +def get_wlan_config(session_id, node_id): + session = coreemu.sessions.get(session_id) + if not session: + return jsonify(error="session does not exist"), 404 + + if node_id.isdigit(): + node_id = int(node_id) + + config = session.mobility.get_model_config(node_id, BasicRangeModel.name) + return jsonify(config) + + +@app.route("/sessions//nodes//wlan", methods=["PUT"]) +def set_wlan_config(session_id, node_id): + session = coreemu.sessions.get(session_id) + if not session: + return jsonify(error="session does not exist"), 404 + + if node_id.isdigit(): + node_id = int(node_id) + + config = request.get_json() or {} + session.mobility.set_model_config(node_id, BasicRangeModel.name, config) + return jsonify() + + @app.route("/sessions//emane/config", methods=["PUT"]) @synchronized def set_emane_config(session_id): @@ -533,9 +593,9 @@ def node_terminal(session_id, node_id): if not node: return jsonify(error="node does not exist"), 404 - node.client.term("bash") + terminal_command = node.termcmdstring("/bin/bash") - return jsonify() + return jsonify(terminal_command) @app.route("/sessions//nodes/", methods=["DELETE"])