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;
|
||||
}
|
||||
|
||||
private boolean uploadFile(File file) throws IOException {
|
||||
String url = getUrl("upload");
|
||||
return WebUtils.postFile(url, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CoreService getService(CoreNode node, String serviceName) throws IOException {
|
||||
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
|
||||
public void openSession(File file) throws IOException {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -336,7 +341,13 @@ public class CoreRestClient implements ICoreClient {
|
|||
|
||||
@Override
|
||||
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()));
|
||||
config.setFile(config.getScriptFile().getName());
|
||||
String data = JsonUtils.toString(config);
|
||||
return WebUtils.postJson(url, data);
|
||||
}
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
package com.core.data;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@Data
|
||||
public class MobilityConfig {
|
||||
private String file;
|
||||
@JsonIgnore
|
||||
private File scriptFile;
|
||||
@JsonProperty("refresh_ms")
|
||||
private Integer refresh;
|
||||
private String loop;
|
||||
|
|
|
@ -59,6 +59,7 @@ public class MobilityDialog extends StageDialog {
|
|||
saveButton.setOnAction(event -> {
|
||||
MobilityConfig mobilityConfig = new MobilityConfig();
|
||||
mobilityConfig.setFile(fileTextField.getText());
|
||||
mobilityConfig.setScriptFile(new File(mobilityConfig.getFile()));
|
||||
mobilityConfig.setAutostart(autoStartTextField.getText());
|
||||
String loop = loopToggleButton.isSelected() ? "1" : "";
|
||||
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/*");
|
||||
RequestBody requestBody = new MultipartBody.Builder()
|
||||
.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();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
import os
|
||||
|
||||
from bottle import HTTPError
|
||||
from flask import Flask
|
||||
from flask import jsonify
|
||||
from flask import request
|
||||
|
||||
import core_utils
|
||||
import emane_routes
|
||||
import hook_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)
|
||||
def handle_error(e):
|
||||
return jsonify(message=e.body, status=e.status_code), e.status_code
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import os
|
||||
from functools import wraps
|
||||
from threading import Lock
|
||||
|
||||
from bottle import abort
|
||||
|
||||
save_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "uploads")
|
||||
CORE_LOCK = Lock()
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import os
|
||||
|
||||
from flask import Blueprint
|
||||
from flask import jsonify
|
||||
from flask import request
|
||||
|
||||
import core_utils
|
||||
from core.mobility import Ns2ScriptedMobility
|
||||
|
||||
api = Blueprint("mobility_api", __name__)
|
||||
|
@ -11,45 +14,26 @@ coreemu = None
|
|||
|
||||
@api.route("/sessions/<int:session_id>/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)
|
||||
|
||||
session = core_utils.get_session(coreemu, session_id)
|
||||
node_id = core_utils.get_node_id(node_id)
|
||||
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)
|
||||
|
||||
return jsonify()
|
||||
|
||||
|
||||
@api.route("/sessions/<int:session_id>/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)
|
||||
|
||||
session = core_utils.get_session(coreemu, session_id)
|
||||
node_id = core_utils.get_node_id(node_id)
|
||||
config = session.mobility.get_model_config(node_id, Ns2ScriptedMobility.name)
|
||||
|
||||
return jsonify(config)
|
||||
|
||||
|
||||
@api.route("/sessions/<int:session_id>/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
|
||||
session = core_utils.get_session(coreemu, session_id)
|
||||
node = core_utils.get_node(session, node_id)
|
||||
|
||||
if action == "start":
|
||||
node.mobility.start()
|
||||
|
|
|
@ -35,7 +35,7 @@ def open_xml():
|
|||
|
||||
logger.info("open xml: %s", request.files)
|
||||
_, temp_path = tempfile.mkstemp()
|
||||
session_file = request.files['session']
|
||||
session_file = request.files["file"]
|
||||
session_file.save(temp_path)
|
||||
|
||||
try:
|
||||
|
|
Loading…
Add table
Reference in a new issue