updated set mobility to upload file to a upload directory and mark files used by set mobility config to look for the file within this directory
This commit is contained in:
parent
2815554487
commit
3dc9586817
8 changed files with 63 additions and 30 deletions
|
@ -176,6 +176,11 @@ public class CoreRestClient implements ICoreClient {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean uploadFile(File file) throws IOException {
|
||||||
|
String url = getUrl("upload");
|
||||||
|
return WebUtils.postFile(url, file);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CoreService getService(CoreNode node, String serviceName) throws IOException {
|
public CoreService getService(CoreNode node, String serviceName) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s", sessionId, node.getId(), serviceName));
|
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s", sessionId, node.getId(), serviceName));
|
||||||
|
@ -266,7 +271,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
@Override
|
@Override
|
||||||
public void openSession(File file) throws IOException {
|
public void openSession(File file) throws IOException {
|
||||||
String url = getUrl("sessions/xml");
|
String url = getUrl("sessions/xml");
|
||||||
CreatedSession createdSession = WebUtils.putFile(url, file, CreatedSession.class);
|
CreatedSession createdSession = WebUtils.postFile(url, file, CreatedSession.class);
|
||||||
joinSession(createdSession.getId(), true);
|
joinSession(createdSession.getId(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,7 +341,13 @@ public class CoreRestClient implements ICoreClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setMobilityConfig(CoreNode node, MobilityConfig config) throws IOException {
|
public boolean setMobilityConfig(CoreNode node, MobilityConfig config) throws IOException {
|
||||||
|
boolean uploaded = uploadFile(config.getScriptFile());
|
||||||
|
if (!uploaded) {
|
||||||
|
throw new IOException("failed to upload mobility script");
|
||||||
|
}
|
||||||
|
|
||||||
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility", sessionId, node.getId()));
|
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility", sessionId, node.getId()));
|
||||||
|
config.setFile(config.getScriptFile().getName());
|
||||||
String data = JsonUtils.toString(config);
|
String data = JsonUtils.toString(config);
|
||||||
return WebUtils.postJson(url, data);
|
return WebUtils.postJson(url, data);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
package com.core.data;
|
package com.core.data;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class MobilityConfig {
|
public class MobilityConfig {
|
||||||
private String file;
|
private String file;
|
||||||
|
@JsonIgnore
|
||||||
|
private File scriptFile;
|
||||||
@JsonProperty("refresh_ms")
|
@JsonProperty("refresh_ms")
|
||||||
private Integer refresh;
|
private Integer refresh;
|
||||||
private String loop;
|
private String loop;
|
||||||
|
|
|
@ -59,6 +59,7 @@ public class MobilityDialog extends StageDialog {
|
||||||
saveButton.setOnAction(event -> {
|
saveButton.setOnAction(event -> {
|
||||||
MobilityConfig mobilityConfig = new MobilityConfig();
|
MobilityConfig mobilityConfig = new MobilityConfig();
|
||||||
mobilityConfig.setFile(fileTextField.getText());
|
mobilityConfig.setFile(fileTextField.getText());
|
||||||
|
mobilityConfig.setScriptFile(new File(mobilityConfig.getFile()));
|
||||||
mobilityConfig.setAutostart(autoStartTextField.getText());
|
mobilityConfig.setAutostart(autoStartTextField.getText());
|
||||||
String loop = loopToggleButton.isSelected() ? "1" : "";
|
String loop = loopToggleButton.isSelected() ? "1" : "";
|
||||||
mobilityConfig.setLoop(loop);
|
mobilityConfig.setLoop(loop);
|
||||||
|
|
|
@ -51,11 +51,28 @@ public final class WebUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T putFile(String url, File file, Class<T> clazz) throws IOException {
|
public static boolean postFile(String url, File file) throws IOException {
|
||||||
MediaType mediaType = MediaType.parse("File/*");
|
MediaType mediaType = MediaType.parse("File/*");
|
||||||
RequestBody requestBody = new MultipartBody.Builder()
|
RequestBody requestBody = new MultipartBody.Builder()
|
||||||
.setType(MultipartBody.FORM)
|
.setType(MultipartBody.FORM)
|
||||||
.addFormDataPart("session", file.getName(), RequestBody.create(mediaType, file))
|
.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(requestBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
return response.isSuccessful();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T postFile(String url, File file, Class<T> clazz) throws IOException {
|
||||||
|
MediaType mediaType = MediaType.parse("File/*");
|
||||||
|
RequestBody requestBody = new MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM)
|
||||||
|
.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
|
import os
|
||||||
|
|
||||||
from bottle import HTTPError
|
from bottle import HTTPError
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
from flask import request
|
from flask import request
|
||||||
|
|
||||||
|
import core_utils
|
||||||
import emane_routes
|
import emane_routes
|
||||||
import hook_routes
|
import hook_routes
|
||||||
import link_routes
|
import link_routes
|
||||||
|
@ -65,6 +68,16 @@ def get_ips():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/upload", methods=["POST"])
|
||||||
|
def upload():
|
||||||
|
if not os.path.exists(core_utils.save_dir):
|
||||||
|
os.mkdir(core_utils.save_dir, 755)
|
||||||
|
upload_file = request.files["file"]
|
||||||
|
save_path = os.path.join(core_utils.save_dir, upload_file.filename)
|
||||||
|
upload_file.save(save_path)
|
||||||
|
return jsonify()
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(HTTPError)
|
@app.errorhandler(HTTPError)
|
||||||
def handle_error(e):
|
def handle_error(e):
|
||||||
return jsonify(message=e.body, status=e.status_code), e.status_code
|
return jsonify(message=e.body, status=e.status_code), e.status_code
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
import os
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
from bottle import abort
|
from bottle import abort
|
||||||
|
|
||||||
|
save_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "uploads")
|
||||||
CORE_LOCK = Lock()
|
CORE_LOCK = Lock()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
import os
|
||||||
|
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
from flask import request
|
from flask import request
|
||||||
|
|
||||||
|
import core_utils
|
||||||
from core.mobility import Ns2ScriptedMobility
|
from core.mobility import Ns2ScriptedMobility
|
||||||
|
|
||||||
api = Blueprint("mobility_api", __name__)
|
api = Blueprint("mobility_api", __name__)
|
||||||
|
@ -11,45 +14,26 @@ coreemu = None
|
||||||
|
|
||||||
@api.route("/sessions/<int:session_id>/nodes/<node_id>/mobility", methods=["POST"])
|
@api.route("/sessions/<int:session_id>/nodes/<node_id>/mobility", methods=["POST"])
|
||||||
def set_mobility_config(session_id, node_id):
|
def set_mobility_config(session_id, node_id):
|
||||||
session = coreemu.sessions.get(session_id)
|
session = core_utils.get_session(coreemu, session_id)
|
||||||
if not session:
|
node_id = core_utils.get_node_id(node_id)
|
||||||
return jsonify(error="session does not exist"), 404
|
|
||||||
|
|
||||||
if node_id.isdigit():
|
|
||||||
node_id = int(node_id)
|
|
||||||
|
|
||||||
data = request.get_json() or {}
|
data = request.get_json() or {}
|
||||||
|
data["file"] = os.path.join(core_utils.save_dir, data["file"])
|
||||||
session.mobility.set_model_config(node_id, Ns2ScriptedMobility.name, data)
|
session.mobility.set_model_config(node_id, Ns2ScriptedMobility.name, data)
|
||||||
|
|
||||||
return jsonify()
|
return jsonify()
|
||||||
|
|
||||||
|
|
||||||
@api.route("/sessions/<int:session_id>/nodes/<node_id>/mobility")
|
@api.route("/sessions/<int:session_id>/nodes/<node_id>/mobility")
|
||||||
def get_mobility_config(session_id, node_id):
|
def get_mobility_config(session_id, node_id):
|
||||||
session = coreemu.sessions.get(session_id)
|
session = core_utils.get_session(coreemu, session_id)
|
||||||
if not session:
|
node_id = core_utils.get_node_id(node_id)
|
||||||
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)
|
config = session.mobility.get_model_config(node_id, Ns2ScriptedMobility.name)
|
||||||
|
|
||||||
return jsonify(config)
|
return jsonify(config)
|
||||||
|
|
||||||
|
|
||||||
@api.route("/sessions/<int:session_id>/nodes/<node_id>/mobility/<action>", methods=["PUT"])
|
@api.route("/sessions/<int:session_id>/nodes/<node_id>/mobility/<action>", methods=["PUT"])
|
||||||
def mobility_action(session_id, node_id, action):
|
def mobility_action(session_id, node_id, action):
|
||||||
session = coreemu.sessions.get(session_id)
|
session = core_utils.get_session(coreemu, session_id)
|
||||||
if not session:
|
node = core_utils.get_node(session, node_id)
|
||||||
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":
|
if action == "start":
|
||||||
node.mobility.start()
|
node.mobility.start()
|
||||||
|
|
|
@ -35,7 +35,7 @@ def open_xml():
|
||||||
|
|
||||||
logger.info("open xml: %s", request.files)
|
logger.info("open xml: %s", request.files)
|
||||||
_, temp_path = tempfile.mkstemp()
|
_, temp_path = tempfile.mkstemp()
|
||||||
session_file = request.files['session']
|
session_file = request.files["file"]
|
||||||
session_file.save(temp_path)
|
session_file.save(temp_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Reference in a new issue