diff --git a/include/communicator_api.h b/include/communicator_api.h index f8dfc048..9391abe9 100644 --- a/include/communicator_api.h +++ b/include/communicator_api.h @@ -120,7 +120,7 @@ namespace xlpmg inline const Message GET_MAX_TIMESTEPS_MESSAGE = {MessageType::FUNCTION_CALL, "get_max_timesteps"}; //! Tells the server to start sending height data. (buffered) - inline const Message GET_HEIGHT_DATA_MESSAGE = {MessageType::SERVER__CALL, "get_height_data"}; + inline const Message GET_HEIGHT_DATA_MESSAGE = {MessageType::FUNCTION_CALL, "get_height_data"}; //! Tells the client that a buffered sending operation has finished. inline const Message BUFFERED_SEND_FINISHED = {MessageType::SERVER_RESPONSE, "buff_send_finished"}; diff --git a/src/Server.cpp b/src/Server.cpp index 93b443a5..0fa01412 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -33,7 +33,7 @@ void exitSimulationThread() // wait until thread has finished m_simulationThread.join(); std::cout << "Thread terminated." << std::endl; - //prepare for next run + // prepare for next run m_isSimulationRunning = false; simulator->shouldExit(false); } @@ -53,56 +53,89 @@ int main(int i_argc, char *i_argv[]) l_communicator.startServer(m_PORT); while (!m_EXIT) { - std::string rawData = l_communicator.receiveFromClient(); + std::string l_rawData = l_communicator.receiveFromClient(); // check if client sent valid json, go back to reading if not - if (!json::accept(rawData)) + if (!json::accept(l_rawData)) { continue; } - json parsedData = json::parse(rawData); - xlpmg::Message message = xlpmg::jsonToMessage(parsedData); - xlpmg::MessageType type = message.type; - std::string key = message.key; - json args = message.args; - if (type == xlpmg::SERVER__CALL) + json l_parsedData = json::parse(l_rawData); + xlpmg::Message l_message = xlpmg::jsonToMessage(l_parsedData); + xlpmg::MessageType l_type = l_message.type; + std::string l_key = l_message.key; + json l_args = l_message.args; + if (l_type == xlpmg::SERVER__CALL) { - if (key == xlpmg::SHUTDOWN_SERVER_MESSAGE.key) + if (l_key == xlpmg::SHUTDOWN_SERVER_MESSAGE.key) { m_EXIT = true; l_communicator.stopServer(); exitSimulationThread(); } - else if (key == xlpmg::START_SIMULATION_MESSAGE.key) + else if (l_key == xlpmg::START_SIMULATION_MESSAGE.key) { - std::string config = parsedData.at(xlpmg::ARGS); + std::string l_config = l_parsedData.at(xlpmg::ARGS); if (!m_isSimulationRunning) { - m_simulationThread = std::thread(&tsunami_lab::Simulator::start, simulator, config); + m_simulationThread = std::thread(&tsunami_lab::Simulator::start, simulator, l_config); m_isSimulationRunning = true; } } - else if (key == xlpmg::KILL_SIMULATION_MESSAGE.key) + else if (l_key == xlpmg::KILL_SIMULATION_MESSAGE.key) { exitSimulationThread(); } - else if (key == xlpmg::RECOMPILE_MESSAGE.key) + else if (l_key == xlpmg::RECOMPILE_MESSAGE.key) { // Shutdown server m_EXIT = true; l_communicator.stopServer(); exitSimulationThread(); // execute recompilation - std::string env = parsedData.at(xlpmg::ARGS).value("ENV", ""); // environment var - std::string opt = parsedData.at(xlpmg::ARGS).value("OPT", ""); // compiler opt + std::string env = l_args.value("ENV", ""); // environment var + std::string opt = l_args.value("OPT", ""); // compiler opt exec("chmod +x runServer.sh"); exec("./runServer.sh \"" + env + "\" \"" + opt + "\" &"); } } - else if (type == xlpmg::FUNCTION_CALL) + else if (l_type == xlpmg::FUNCTION_CALL) { - if(key == xlpmg::GET_TIMESTEP_MESSAGE.key){ - l_communicator.sendToClient(std::to_string(simulator->getTimeStep())); + if (l_key == xlpmg::GET_TIMESTEP_MESSAGE.key) + { + xlpmg::Message response = {xlpmg::SERVER_RESPONSE, "time_step_data", simulator->getTimeStep()}; + l_communicator.sendToClient(xlpmg::messageToJsonString(response)); + } + else if (l_key == xlpmg::GET_HEIGHT_DATA_MESSAGE.key) + { + xlpmg::Message heightDataMsg = {xlpmg::SERVER_RESPONSE, "height_data", nullptr}; + unsigned long l_headerSize = sizeof(heightDataMsg); + // calculate the remaining space left for one message + unsigned long l_sendDataSpace = xlpmg::BUFF_SIZE - l_headerSize; + l_sendDataSpace = 100; + // get data from simulation + if (simulator->getWaveProp() != nullptr) + { + tsunami_lab::patches::WavePropagation *waveprop = simulator->getWaveProp(); + const tsunami_lab::t_real *heightData = waveprop->getHeight(); + // calculate array size + tsunami_lab::t_idx l_ncellsX, l_ncellsY; + simulator->getCellAmount(l_ncellsX, l_ncellsY); + unsigned long totalCells = l_ncellsX * l_ncellsY; + + unsigned long cellCounter = 0; + while (totalCells > 0) + { + while (sizeof(heightDataMsg.args) < l_sendDataSpace && totalCells > 0) + { + heightDataMsg.args.push_back(heightData[cellCounter]); + totalCells--; + } + l_communicator.sendToClient(xlpmg::messageToJsonString(heightDataMsg)); + heightDataMsg.args = ""; + } + l_communicator.sendToClient(xlpmg::messageToJsonString(xlpmg::BUFFERED_SEND_FINISHED)); + } } } } diff --git a/src/Simulator.cpp b/src/Simulator.cpp index 13dae89e..9521d878 100644 --- a/src/Simulator.cpp +++ b/src/Simulator.cpp @@ -649,12 +649,21 @@ void tsunami_lab::Simulator::runCalculation() void tsunami_lab::Simulator::freeMemory() { - delete m_setup; - delete m_waveProp; + if (m_setup != nullptr) + { + delete m_setup; + } + if (m_waveProp != nullptr) + { + delete m_waveProp; + } if (m_useFileIO) { std::filesystem::remove(m_checkPointFilePathString); - delete m_netCdf; + if (m_netCdf != nullptr) + { + delete m_netCdf; + } for (tsunami_lab::io::Station *l_s : m_stations) { delete l_s; diff --git a/src/Simulator.h b/src/Simulator.h index 29605256..21f49ddd 100644 --- a/src/Simulator.h +++ b/src/Simulator.h @@ -236,6 +236,13 @@ class tsunami_lab::Simulator return m_timeStep; } + void getCellAmount(tsunami_lab::t_idx &o_ncellsX, + tsunami_lab::t_idx &o_ncellsY) + { + o_ncellsX = m_nx; + o_ncellsY = m_ny; + } + //------------------------------------------// //-----------------SETTERS------------------// //------------------------------------------// diff --git a/src/ui/GUI.cpp b/src/ui/GUI.cpp index 94959cfa..2bc3df98 100644 --- a/src/ui/GUI.cpp +++ b/src/ui/GUI.cpp @@ -10,9 +10,9 @@ #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include "GUI.h" -#include -#include -#include +#include "Communicator.hpp" +#include "communicator_api.h" +#include "../constants.h" #define GL_SILENCE_DEPRECATION #if defined(IMGUI_IMPL_OPENGL_ES2) @@ -25,13 +25,14 @@ #endif // c headers +#include +#include +#include #include #include -#include "communicator_api.h" #include #include #include -#include "Communicator.hpp" // ui components #include "RTCustWindow.h" @@ -313,6 +314,7 @@ int tsunami_lab::ui::GUI::launch(int i_PORT) { m_communicator.sendToServer(messageToJsonString(xlpmg::SHUTDOWN_SERVER_MESSAGE)); } + if (ImGui::Button("Recompile")) { xlpmg::Message recompileMsg = xlpmg::RECOMPILE_MESSAGE; @@ -324,10 +326,38 @@ int tsunami_lab::ui::GUI::launch(int i_PORT) recompileMsg.args = compileArgs; m_communicator.sendToServer(messageToJsonString(recompileMsg)); } + + if (ImGui::Button("get height data")) + { + m_communicator.sendToServer(messageToJsonString(xlpmg::GET_HEIGHT_DATA_MESSAGE)); + bool l_finished = false; + std::string heightArrayString = ""; + while (!l_finished) + { + std::string data = m_communicator.receiveFromServer(); + if (json::accept(data)) + { + xlpmg::Message msg = xlpmg::jsonToMessage(json::parse(data)); + if (msg.key == xlpmg::BUFFERED_SEND_FINISHED.key) + { + l_finished = true; + } + else + { + heightArrayString += msg.args; + } + } + } + } if (ImGui::Button("Get time step")) { m_communicator.sendToServer(messageToJsonString(xlpmg::GET_TIMESTEP_MESSAGE)); - std::cout << m_communicator.receiveFromServer() << std::endl; + std::string response = m_communicator.receiveFromServer(); + if (json::accept(response)) + { + xlpmg::Message responseMessage = xlpmg::jsonToMessage(json::parse(response)); + std::cout << responseMessage.args << std::endl; + } } // if (ImGui::Button("file io true")) @@ -368,7 +398,6 @@ int tsunami_lab::ui::GUI::launch(int i_PORT) // getHeightsArraySizeMsg[xlpmg::MESSAGE_TYPE] = xlpmg::FUNCTION_CALL; // getHeightsArraySizeMsg[xlpmg::KEY] = xlpmg::KEY_GET_HEIGHTS; - // json getHeightsMsg; // getHeightsMsg[xlpmg::MESSAGE_TYPE] = xlpmg::FUNCTION_CALL; // getHeightsMsg[xlpmg::KEY] = xlpmg::KEY_GET_HEIGHTS; @@ -381,7 +410,7 @@ int tsunami_lab::ui::GUI::launch(int i_PORT) // if(parsedData.at(xlpmg::KEY) ==xlpmg::KEY_END_OF_HEIGHTS){ // l_finishedReading = true; // }else{ - + // } // } // }