Commit 6baee46e authored by Sebastian Höffner's avatar Sebastian Höffner
Browse files

Updating communication channels to also include the original request inside the reply

parent 2d342dfe
......@@ -5827,99 +5827,6 @@ BoxCollider:
serializedVersion: 2
m_Size: {x: 0.029913172, y: 0.25596744, z: 0.029676154}
m_Center: {x: -0.08489099, y: 0.103138596, z: 0.08492072}
--- !u!1 &1127439233
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1127439237}
- component: {fileID: 1127439236}
- component: {fileID: 1127439235}
- component: {fileID: 1127439234}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!65 &1127439234
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1127439233}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!23 &1127439235
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1127439233}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &1127439236
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1127439233}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1127439237
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1127439233}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -1.55, y: 1.35, z: -1.55}
m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1133262456
GameObject:
m_ObjectHideFlags: 0
......
using UnityEngine;
using System.Collections;
using SimpleJSON;
using System;
namespace dlu
{
......@@ -24,27 +25,57 @@ public abstract class Communication : MonoBehaviour
{
string command = requestObject["Command"];
Debug.Log($"[Communication] Command: '{command}'");
JSONObject error = new JSONObject();
switch(command)
{
case "RESET_SCENE":
StartCoroutine(sceneController.ResetScene(ReplyToRequest));
StartCoroutine(sceneController.ResetScene(ReplyToRequest, requestObject));
break;
case "GET_CONTEXT":
StartCoroutine(sceneController.GetContext(ReplyToRequest));
StartCoroutine(sceneController.GetContext(ReplyToRequest, requestObject));
break;
case "GET_OCCUPANCY":
StartCoroutine(sceneController.GenerateOccupancyGrid(float.Parse(requestObject["resolution"]), requestObject["exclude"] as JSONArray, ReplyToRequest));
float resolution = 0.05f;
try
{
resolution = float.Parse(requestObject["resolution"]);
}
catch(FormatException)
{
error["Error"] = "Invalid resolution value.";
StartCoroutine(ReplyToRequest(error, requestObject));
}
catch(OverflowException)
{
error["Error"] = "Invalid resolution value.";
StartCoroutine(ReplyToRequest(error, requestObject));
}
catch(ArgumentNullException)
{
Debug.Log($"Using default resolution of {resolution}");
}
JSONArray exclude = requestObject["exclude"] != null ? requestObject["exclude"] as JSONArray : new JSONArray();
StartCoroutine(sceneController.GenerateOccupancyGrid(resolution, exclude, ReplyToRequest, requestObject));
break;
case "SIMULATE_SCENE":
StartCoroutine(sceneController.RunSimulation(requestObject["instructions"] as JSONArray, ReplyToRequest));
JSONArray instructions = requestObject["instructions"] != null ? requestObject["instructions"] as JSONArray : new JSONArray();
StartCoroutine(sceneController.RunSimulation(instructions, ReplyToRequest, requestObject));
break;
default:
StartCoroutine(ReplyToRequest("{\"Error\": \"Server does not understand this command.\"}"));
error["Error"] = "Server does not understand this command.";
StartCoroutine(ReplyToRequest(error, requestObject));
break;
}
}
public abstract IEnumerator ReplyToRequest(string response);
protected string ComposeReply(JSONObject response, JSONObject request) {
JSONObject composed = new JSONObject();
composed["request"] = request;
composed["response"] = response;
return composed.ToString();
}
public abstract IEnumerator ReplyToRequest(JSONObject response, JSONObject request);
}
}
\ No newline at end of file
......@@ -31,10 +31,11 @@ public class DirectCommunication : Communication
HandleRequest(requestObject);
}
public override IEnumerator ReplyToRequest(string response)
public override IEnumerator ReplyToRequest(JSONObject response, JSONObject originalRequest)
{
byte[] responseArray = Encoding.UTF8.GetBytes(response);
Reply(responseArray, response.Length);
string responseString = base.ComposeReply(response, originalRequest);
byte[] responseArray = Encoding.UTF8.GetBytes(responseString);
Reply(responseArray, responseString.Length);
yield return null;
}
......
......@@ -78,15 +78,16 @@ public class WebsocketCommunication : Communication
}
}
public void Send(string message)
public void Send(string message)
{
Debug.Log($"[WebSocket] Sending message:\n{message}");
ws.Send(Encoding.UTF8.GetBytes(message));
}
public override IEnumerator ReplyToRequest(string response)
public override IEnumerator ReplyToRequest(JSONObject response, JSONObject originalRequest)
{
Send(response);
string responseString = base.ComposeReply(response, originalRequest);
Send(responseString);
yield return null;
workingOnRequest = false;
}
......
......@@ -58,10 +58,11 @@ public class ZMQServerCommunication : Communication
}
}
public override IEnumerator ReplyToRequest(string response)
public override IEnumerator ReplyToRequest(JSONObject response, JSONObject originalRequest)
{
string responseString = base.ComposeReply(response, originalRequest);
Debug.Log($"[ZMQServer] Sending message:\n{response}");
server.SendFrame(response);
server.SendFrame(responseString);
yield return null;
workingOnRequest = false;
}
......
......@@ -76,7 +76,7 @@ public class JSONHandler
return JSON.Parse(File.ReadAllText(json)) as JSONArray;
}
public JSONNode GenerateContextJSON()
public JSONObject GenerateContextJSON()
{
string now = DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss");
string screenShotPath = $"{Application.persistentDataPath}/context_{now}.png";
......
......@@ -60,7 +60,7 @@ public class SceneController : MonoBehaviour
JSONHandler.instance.RefreshSemanticLabels();
}
public IEnumerator ResetScene(Func<string, IEnumerator> replyToRequest)
public IEnumerator ResetScene(Func<JSONObject, JSONObject, IEnumerator> replyToRequest, JSONObject originalRequest)
{
UnityAction<Scene> reloadScene = null;
reloadScene = (Scene scene) => {
......@@ -72,7 +72,9 @@ public class SceneController : MonoBehaviour
sendReply = (Scene scene, LoadSceneMode lsm) =>
{
SceneManager.sceneLoaded -= sendReply;
StartCoroutine(replyToRequest("{\"Success\": true}"));
JSONObject response = new JSONObject();
response["Success"] = true;
StartCoroutine(replyToRequest(response, originalRequest));
};
SceneManager.sceneUnloaded += reloadScene;
......@@ -81,7 +83,7 @@ public class SceneController : MonoBehaviour
yield return null;
}
public IEnumerator GenerateOccupancyGrid(float resolution, JSONArray exclude, Func<string, IEnumerator> replyToRequest)
public IEnumerator GenerateOccupancyGrid(float resolution, JSONArray exclude, Func<JSONObject, JSONObject, IEnumerator> replyToRequest, JSONObject originalRequest)
{
HashSet<string> excluded = new HashSet<string>();
if (exclude != null) {
......@@ -117,24 +119,24 @@ public class SceneController : MonoBehaviour
}
jsonObject["occupied"] = grid;
yield return replyToRequest(jsonObject.ToString());
yield return replyToRequest(jsonObject, originalRequest);
}
public IEnumerator GetContext(Func<string, IEnumerator> replyRequest)
public IEnumerator GetContext(Func<JSONObject, JSONObject, IEnumerator> replyRequest, JSONObject originalRequest)
{
string response = JSONHandler.instance.GenerateContextJSON().ToString();
JSONObject response = JSONHandler.instance.GenerateContextJSON();
// Should better wait until context screenshot is available, but this works good enough
yield return new WaitForSeconds(3);
yield return replyRequest(response);
yield return replyRequest(response, originalRequest);
}
public IEnumerator RunSimulation(JSONArray instructions, Func<string, IEnumerator> replyToRequest)
public IEnumerator RunSimulation(JSONArray instructions, Func<JSONObject, JSONObject, IEnumerator> replyToRequest, JSONObject originalRequest)
{
JSONObject response = new JSONObject();
response.Add("trajectories.log", GetSchemasimLog());
response.Add("context", JSONHandler.instance.GenerateContextJSON());
response.Add("result", "Success!");
yield return replyToRequest(response.ToString());
yield return replyToRequest(response, originalRequest);
}
private JSONArray GetSchemasimLog()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment