diff --git a/lib/xlpmg/Communicator.hpp b/lib/xlpmg/Communicator.hpp index 02150bca..1852634c 100644 --- a/lib/xlpmg/Communicator.hpp +++ b/lib/xlpmg/Communicator.hpp @@ -86,8 +86,9 @@ namespace xlpmg std::cout << line << "\t\r" << std::flush; size_t position = logData.length() - 2; - while ((logData[position] != '\n') and position > 0) + while ((logData[position] != '\n') && position > 0){ position--; + } logData = logData.substr(0, position); logData.append(line); @@ -183,7 +184,7 @@ namespace xlpmg bool finished = false; unsigned long totalBytes = 0; - logEvent(std::to_string(totalBytes) + " Bytes (" + std::to_string(totalBytes / 1000000) + " MB) received", DEBUG, true); + logEvent(std::to_string(totalBytes) + " Bytes (" + std::to_string(totalBytes / 1000000) + " MB) received", DEBUG); while (!finished) { @@ -196,7 +197,7 @@ namespace xlpmg logEvent("Reading failed or timed out.", ERROR); return "FAIL"; } - totalBytes += sockValread; + totalBytes += strlen(readBuffer); logEvent(std::to_string(totalBytes) + " Bytes (" + std::to_string(totalBytes / 1000000) + " MB) received", DEBUG, true); if (strncmp("DONE", readBuffer, 4) == 0) @@ -216,7 +217,6 @@ namespace xlpmg message += std::string(readBuffer); } } - return message; } @@ -235,17 +235,19 @@ namespace xlpmg send(sockClient_fd, message.c_str(), strlen(message.c_str()), MSG_NOSIGNAL); logEvent(message, SENT); std::string bytesSentStr = "=> " + std::to_string(strlen(message.c_str())) + " Bytes"; - logEvent(bytesSentStr.c_str(), DEBUG, true); + logEvent(bytesSentStr.c_str(), DEBUG); } else { - logEvent("Sending buffered message", INFO); + logEvent("Sending buffered message (" + std::to_string(strlen(message.c_str())) + " Bytes = " + std::to_string((double)strlen(message.c_str()) / 1000000) + " MB)", INFO); + logEvent("0%", DEBUG); unsigned long bytes_total = 0; while (bytes_total < strlen(message.c_str())) { unsigned long bytes_sent = send(sockClient_fd, message.c_str() + bytes_total, BUFF_SIZE_SEND - 1, 0); bytes_total += bytes_sent; - std::string bytesSentStr = std::to_string((double)bytes_total / strlen(message.c_str()) * 100) + "%"; + int percentage = std::max((double) 0, std::min(((double)bytes_total / strlen(message.c_str())) * 100, (double) 100)); + std::string bytesSentStr = std::to_string(percentage) + "%"; logEvent(bytesSentStr.c_str(), DEBUG, true); } } @@ -342,7 +344,7 @@ namespace xlpmg bool finished = false; unsigned long totalBytes = 0; - logEvent(std::to_string(totalBytes) + " Bytes (" + std::to_string(totalBytes / 1000000) + " MB) received", DEBUG, true); + logEvent(std::to_string(totalBytes) + " Bytes (" + std::to_string(totalBytes / 1000000) + " MB) received", DEBUG); while (!finished) { memset(readBuffer, 0, BUFF_SIZE_READ); @@ -354,7 +356,7 @@ namespace xlpmg logEvent("Reading failed or timed out.", ERROR); return "FAIL"; } - totalBytes += sockValread; + totalBytes += strlen(readBuffer); logEvent(std::to_string(totalBytes) + " Bytes (" + std::to_string(totalBytes / 1000000) + " MB) received", DEBUG, true); if (strncmp("DONE", readBuffer, 4) == 0) @@ -374,7 +376,6 @@ namespace xlpmg message += std::string(readBuffer); } } - return message; } @@ -387,17 +388,19 @@ namespace xlpmg send(new_socket, message.c_str(), strlen(message.c_str()), MSG_NOSIGNAL); logEvent(message, SENT); std::string bytesSentStr = "=> " + std::to_string(strlen(message.c_str())) + " Bytes"; - logEvent(bytesSentStr.c_str(), DEBUG, true); + logEvent(bytesSentStr.c_str(), DEBUG); } else { - logEvent("Sending buffered message", INFO); + logEvent("Sending buffered message (" + std::to_string(strlen(message.c_str())) + " Bytes = " + std::to_string((double)strlen(message.c_str()) / 1000000) + " MB)", INFO); + logEvent("0%", DEBUG); unsigned long bytes_total = 0; while (bytes_total < strlen(message.c_str())) { unsigned long bytes_sent = send(new_socket, message.c_str() + bytes_total, BUFF_SIZE_SEND - 1, 0); bytes_total += bytes_sent; - std::string bytesSentStr = std::to_string((double)bytes_total / strlen(message.c_str()) * 100) + "%"; + int percentage = std::max((double) 0, std::min(((double)bytes_total / strlen(message.c_str())) * 100, (double) 100)); + std::string bytesSentStr = std::to_string(percentage) + "%"; logEvent(bytesSentStr.c_str(), DEBUG, true); } } diff --git a/src/Server.cpp b/src/Server.cpp index 9ed97c78..3f11915c 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -12,6 +12,9 @@ tsunami_lab::Simulator *simulator = nullptr; std::thread m_simulationThread; bool m_isSimulationRunning = false; +std::string m_bathTempFile = "bathtemp.nc"; +std::string m_displTempFile = "displtemp.nc"; + int execWithOutput(std::string i_cmd, std::string i_outputFile) { std::string commandString = (i_cmd + " > " + i_outputFile + " 2>&1 &").data(); @@ -165,33 +168,18 @@ int main(int i_argc, char *i_argv[]) { l_communicator.setSendBufferSize(l_args); } - else if (l_key == xlpmg::PREPARE_BATHYMETRY_DATA.key) + else if (l_key == xlpmg::SET_BATHYMETRY_DATA.key) { - std::cout << "Receiving bathymetry data" << std::endl; - // prepare simulator - tsunami_lab::t_idx l_nCellsX = l_args["cellsX"]; - tsunami_lab::t_idx l_nCellsY = l_args["cellsY"]; - simulator->setCellAmount(l_nCellsX, l_nCellsY); - tsunami_lab::t_real l_offsetX = l_args.value("offsetX", 0); - tsunami_lab::t_real l_offsetY = l_args.value("offsetY", 0); - simulator->setOffset(l_offsetX, l_offsetY); - tsunami_lab::patches::WavePropagation *l_waveprop = simulator->getWaveProp(); - - // receive data - tsunami_lab::t_idx l_index = 0; - std::string l_response = l_communicator.receiveFromClient(); - if (json::accept(l_response)) - { - xlpmg::Message msg = xlpmg::jsonToMessage(json::parse(l_response)); - - std::stringstream l_stream(msg.args.dump().substr(1, msg.args.dump().size() - 2)); - std::string l_num; - while (getline(l_stream, l_num, ',')) - { - l_waveprop->setBathymetry(l_index % l_nCellsX, l_index / l_nCellsX, std::stof(l_num)); - l_index++; - } - } + std::vector l_byteVector = l_args["bytes"]; + auto l_writeFile = std::fstream(m_bathTempFile, std::ios::out | std::ios::binary); + l_writeFile.write((char *)&l_byteVector[0], l_byteVector.size()); + l_writeFile.close(); + }else if (l_key == xlpmg::SET_DISPLACEMENT_DATA.key) + { + std::vector l_byteVector = l_args["bytes"]; + auto l_writeFile = std::fstream(m_displTempFile, std::ios::out | std::ios::binary); + l_writeFile.write((char *)&l_byteVector[0], l_byteVector.size()); + l_writeFile.close(); } else if(l_key == xlpmg::PAUSE_SIMULATION.key) { @@ -204,7 +192,6 @@ int main(int i_argc, char *i_argv[]) simulator->setPausingStatus(false); } } - else if (l_type == xlpmg::FUNCTION_CALL) { if (l_key == xlpmg::RESET_SIMULATOR.key && m_simulationThread.joinable()) diff --git a/src/ui/GUI.cpp b/src/ui/GUI.cpp index 561f5f2f..c11cf91a 100644 --- a/src/ui/GUI.cpp +++ b/src/ui/GUI.cpp @@ -250,6 +250,7 @@ int tsunami_lab::ui::GUI::launch() ImGui::SameLine(); + ImGui::BeginDisabled(!m_connected); if (ImGui::Button("Disconnect")) { @@ -257,7 +258,6 @@ int tsunami_lab::ui::GUI::launch() m_connected = false; } ImGui::EndDisabled(); - ImGui::SameLine(); if (ImGui::Button("Check connection")) { @@ -286,12 +286,13 @@ int tsunami_lab::ui::GUI::launch() ImGui::PushID(301); ImGui::InputInt("", &m_clientReadBufferSize, 0); - ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_READ_DEFAULT)).c_str()); + ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_READ_DEFAULT) + std::string(". Max recommended: 8.000.000")).c_str()); ImGui::SameLine(); if (ImGui::Button("Set")) { m_communicator.setReadBufferSize(m_clientReadBufferSize); } + ImGui::SetItemTooltip("Sets the input."); ImGui::SameLine(); ImGui::Text("Buffer size for receiving (client)"); @@ -305,7 +306,7 @@ int tsunami_lab::ui::GUI::launch() ImGui::PushID(302); ImGui::InputInt("", &m_clientSendBufferSize, 0); - ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_SEND_DEFAULT)).c_str()); + ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_SEND_DEFAULT) + std::string(". Max recommended: 8.000.000")).c_str()); ImGui::SameLine(); if (ImGui::Button("Set")) { @@ -324,11 +325,11 @@ int tsunami_lab::ui::GUI::launch() ImGui::PushID(303); ImGui::InputInt("", &m_serverReadBufferSize, 0); - ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_READ_DEFAULT)).c_str()); + ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_READ_DEFAULT) + std::string(". Max recommended: 8.000.000")).c_str()); ImGui::SameLine(); if (ImGui::Button("Set")) { - + xlpmg::Message msg = xlpmg::SET_READ_BUFFER_SIZE; msg.args = m_serverReadBufferSize; m_communicator.sendToServer(messageToJsonString(msg)); @@ -346,7 +347,7 @@ int tsunami_lab::ui::GUI::launch() ImGui::PushID(304); ImGui::InputInt("", &m_serverSendBufferSize, 0); - ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_SEND_DEFAULT)).c_str()); + ImGui::SetItemTooltip("%s", (std::string("in bytes. Default: ") + std::to_string(m_communicator.BUFF_SIZE_SEND_DEFAULT) + std::string(". Max recommended: 8.000.000")).c_str()); ImGui::SameLine(); if (ImGui::Button("Set")) { @@ -486,7 +487,6 @@ int tsunami_lab::ui::GUI::launch() if (fileDialogBath.HasSelected()) { m_bathymetryFilePath = fileDialogBath.GetSelected().string(); - std::cout << "Selected filename" << m_bathymetryFilePath << std::endl; fileDialogBath.ClearSelected(); } @@ -495,36 +495,24 @@ int tsunami_lab::ui::GUI::launch() ImGui::SameLine(); if (ImGui::Button("Send to server")) { - tsunami_lab::t_idx l_cellsX = 0, l_cellsY = 0; - tsunami_lab::io::NetCdf::getDimensionSize(m_bathymetryFilePath.c_str(), "x", l_cellsX); - tsunami_lab::io::NetCdf::getDimensionSize(m_bathymetryFilePath.c_str(), "y", l_cellsY); - - tsunami_lab::t_real *l_dataX = new tsunami_lab::t_real[l_cellsX]; - tsunami_lab::t_real *l_dataY = new tsunami_lab::t_real[l_cellsY]; - tsunami_lab::t_real *l_data = new tsunami_lab::t_real[l_cellsX * l_cellsY]; - tsunami_lab::io::NetCdf::read(m_bathymetryFilePath.c_str(), "z", &l_dataX, &l_dataY, &l_data); - - xlpmg::Message l_prepareMsg = xlpmg::PREPARE_BATHYMETRY_DATA; - json l_prepareMsgArgs; - l_prepareMsgArgs["cellsX"] = l_cellsX; - l_prepareMsgArgs["cellsY"] = l_cellsY; - l_prepareMsgArgs["offsetX"] = l_dataX[0]; - l_prepareMsgArgs["offsetY"] = l_dataY[0]; - l_prepareMsg.args = l_prepareMsgArgs; - - xlpmg::Message l_bathymetryDataMsg = {xlpmg::OTHER, "bath_data", nullptr}; - if (m_communicator.sendToServer(xlpmg::messageToJsonString(l_prepareMsg)) == 0) - { - for (tsunami_lab::t_idx i = 0; i < l_cellsX * l_cellsY; i++) - { - l_bathymetryDataMsg.args.push_back(l_data[i]); - } - m_communicator.sendToServer(xlpmg::messageToJsonString(l_bathymetryDataMsg)); - m_communicator.sendToServer(xlpmg::messageToJsonString(xlpmg::BUFFERED_SEND_FINISHED)); - } + xlpmg::Message l_bathymetryDataMsg = xlpmg::SET_BATHYMETRY_DATA; + std::ifstream l_bathFile(m_bathymetryFilePath, std::ios::binary); + l_bathFile.unsetf(std::ios::skipws); + std::streampos l_fileSize; + l_bathFile.seekg(0, std::ios::end); + l_fileSize = l_bathFile.tellg(); + l_bathFile.seekg(0, std::ios::beg); + std::vector vec; + vec.reserve(l_fileSize); + vec.insert(vec.begin(), + std::istream_iterator(l_bathFile), + std::istream_iterator()); + l_bathymetryDataMsg.args = json::binary(vec); + m_communicator.sendToServer(xlpmg::messageToJsonString(l_bathymetryDataMsg)); } ImGui::PopID(); + if (ImGui::Button("Select Displacement data file")) fileDialogDis.Open(); @@ -533,10 +521,31 @@ int tsunami_lab::ui::GUI::launch() if (fileDialogDis.HasSelected()) { m_displacementFilePath = fileDialogDis.GetSelected().string(); - std::cout << "Selected filename" << m_displacementFilePath << std::endl; fileDialogDis.ClearSelected(); } + // send displacement + ImGui::PushID(439); + ImGui::SameLine(); + if (ImGui::Button("Send to server")) + { + xlpmg::Message l_displacementMsg = xlpmg::SET_DISPLACEMENT_DATA; + std::ifstream l_displFile(m_displacementFilePath, std::ios::binary); + l_displFile.unsetf(std::ios::skipws); + std::streampos l_fileSize; + l_displFile.seekg(0, std::ios::end); + l_fileSize = l_displFile.tellg(); + l_displFile.seekg(0, std::ios::beg); + std::vector vec; + vec.reserve(l_fileSize); + vec.insert(vec.begin(), + std::istream_iterator(l_displFile), + std::istream_iterator()); + l_displacementMsg.args = json::binary(vec); + m_communicator.sendToServer(xlpmg::messageToJsonString(l_displacementMsg)); + } + ImGui::PopID(); + ImGui::EndTabItem(); } ImGui::EndTabBar();