changes to support mobility from rest and configuring mobility from a wlan context menu
This commit is contained in:
parent
dd9ad5644d
commit
f2f83f247d
12 changed files with 379 additions and 15 deletions
|
@ -11,6 +11,7 @@ from flask import send_file
|
|||
from flask_socketio import SocketIO
|
||||
from flask_socketio import emit
|
||||
|
||||
import mobility_routes
|
||||
from core import logger
|
||||
from core.emulator.coreemu import CoreEmu
|
||||
from core.emulator.emudata import InterfaceData
|
||||
|
@ -25,12 +26,14 @@ from core.mobility import BasicRangeModel
|
|||
from core.service import ServiceManager
|
||||
|
||||
CORE_LOCK = Lock()
|
||||
coreemu = CoreEmu()
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config["SECRET_KEY"] = "core"
|
||||
socketio = SocketIO(app)
|
||||
app.config["SECRET_KEY"] = "core"
|
||||
|
||||
coreemu = CoreEmu()
|
||||
mobility_routes.coreemu = coreemu
|
||||
app.register_blueprint(mobility_routes.mobility_api, url_prefix="/sessions/<int:session_id>")
|
||||
|
||||
|
||||
def synchronized(function):
|
||||
|
@ -120,19 +123,22 @@ def broadcast_event(event):
|
|||
})
|
||||
|
||||
|
||||
def broadcast_node(node):
|
||||
socketio.emit("node", {
|
||||
"id": node.id,
|
||||
"name": node.name,
|
||||
"model": node.model,
|
||||
"position": {
|
||||
"x": node.x_position,
|
||||
"y": node.y_position,
|
||||
},
|
||||
"services": node.services.split("|"),
|
||||
})
|
||||
|
||||
|
||||
@socketio.on("connect")
|
||||
def websocket_connect():
|
||||
emit("info", {"message": "You are connected!"})
|
||||
socketio.emit("node", {
|
||||
"id": 1,
|
||||
"x": 100,
|
||||
"y": 101
|
||||
})
|
||||
socketio.emit("node", {
|
||||
"id": 1,
|
||||
"x": 100,
|
||||
"y": 150
|
||||
})
|
||||
|
||||
|
||||
@socketio.on("disconnect")
|
||||
|
@ -142,7 +148,7 @@ def websocket_disconnect():
|
|||
|
||||
@app.route("/")
|
||||
def home():
|
||||
return render_template('index.html')
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route("/ips", methods=["POST"])
|
||||
|
@ -233,6 +239,7 @@ def create_session():
|
|||
|
||||
# add handlers
|
||||
session.event_handlers.append(broadcast_event)
|
||||
session.node_handlers.append(broadcast_node)
|
||||
|
||||
response_data = jsonify(
|
||||
id=session.session_id,
|
||||
|
|
63
webapp/mobility_routes.py
Normal file
63
webapp/mobility_routes.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
from flask import Blueprint
|
||||
from flask import jsonify
|
||||
from flask import request
|
||||
|
||||
from core.mobility import Ns2ScriptedMobility
|
||||
|
||||
mobility_api = Blueprint("mobility_api", __name__)
|
||||
|
||||
coreemu = None
|
||||
|
||||
|
||||
@mobility_api.route("/nodes/<node_id>/mobility", methods=["POST"])
|
||||
def set_mobility_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)
|
||||
|
||||
data = request.get_json() or {}
|
||||
|
||||
session.mobility.set_model_config(node_id, Ns2ScriptedMobility.name, data)
|
||||
|
||||
return jsonify()
|
||||
|
||||
|
||||
@mobility_api.route("/nodes/<node_id>/mobility")
|
||||
def get_mobility_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, Ns2ScriptedMobility.name)
|
||||
|
||||
return jsonify(config)
|
||||
|
||||
|
||||
@mobility_api.route("/nodes/<node_id>/mobility/<action>", methods=["PUT"])
|
||||
def mobility_action(session_id, node_id, action):
|
||||
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)
|
||||
node = session.objects.get(node_id)
|
||||
if not node:
|
||||
return jsonify(error="node does not exist"), 404
|
||||
|
||||
if action == "start":
|
||||
node.mobility.start()
|
||||
elif action == "pause":
|
||||
node.mobility.pause()
|
||||
elif action == "stop":
|
||||
node.mobility.stop(move_initial=True)
|
||||
else:
|
||||
return jsonify(error="invalid mobility action: %s" % action), 404
|
||||
|
||||
return jsonify()
|
Loading…
Add table
Add a link
Reference in a new issue