Skip to content

Commit

Permalink
fixed more issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 21, 2024
1 parent 53223da commit 9ea2f52
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
25 changes: 24 additions & 1 deletion lib/xlpmg/Communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <poll.h>

// time stamp
#include <chrono>
Expand Down Expand Up @@ -222,6 +223,27 @@ namespace xlpmg
return message;
}

/// @brief Checks if the server responded with OK.
/// @return true if server responded
bool checkServerResponse()
{
char readBuffer[BUFF_SIZE_READ];
memset(readBuffer, 0, BUFF_SIZE_READ);
sockValread = read(sockClient_fd, readBuffer,
BUFF_SIZE_READ - 1); // subtract 1 for the null
bool returnValue = true;
if(std::string(readBuffer).compare("OK") != 0)
returnValue = false;

//DONE
sockValread = read(sockClient_fd, readBuffer,
BUFF_SIZE_READ - 1); // subtract 1 for the null
if(std::string(readBuffer).compare("DONE") != 0)
returnValue = false;

return returnValue;
}

/// @brief Sends a message to the server.
/// @param message String to send.
int sendToServer(std::string message)
Expand Down Expand Up @@ -253,7 +275,7 @@ namespace xlpmg
logEvent(bytesSentStr.c_str(), DEBUG, true);
}
}
sleep(1);

send(sockClient_fd, "DONE", strlen("DONE"), MSG_NOSIGNAL);

return 0;
Expand Down Expand Up @@ -409,6 +431,7 @@ namespace xlpmg
logEvent(bytesSentStr.c_str(), DEBUG, true);
}
}

send(new_socket, "DONE", 4, MSG_NOSIGNAL);
}
};
Expand Down
4 changes: 3 additions & 1 deletion src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int main(int i_argc, char *i_argv[])
{
continue;
}

json l_parsedData = json::parse(l_rawData);
xlpmg::Message l_message = xlpmg::jsonToMessage(l_parsedData);
xlpmg::MessageType l_type = l_message.type;
Expand All @@ -89,7 +90,6 @@ int main(int i_argc, char *i_argv[])
else if (l_key == xlpmg::START_SIMULATION.key)
{
std::string l_config = l_parsedData.at(xlpmg::ARGS);
std::cout << m_simulationThread.joinable() << std::endl;
if (!m_isSimulationRunning && !simulator->isPreparing())
{
if (m_simulationThread.joinable())
Expand Down Expand Up @@ -179,13 +179,15 @@ int main(int i_argc, char *i_argv[])
l_writeFile.write((char *)&l_byteVector[0], l_byteVector.size());
l_writeFile.close();
simulator->setBathymetryFilePath(m_bathTempFile);
simulator->setPrepared(false);
}else if (l_key == xlpmg::SET_DISPLACEMENT_DATA.key)
{
std::vector<std::uint8_t> 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();
simulator->setDisplacementFilePath(m_displTempFile);
simulator->setPrepared(false);
}
else if(l_key == xlpmg::PAUSE_SIMULATION.key)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ void tsunami_lab::Simulator::constructSetup()
m_displacementFilePath.c_str(),
l_netCdfCustom,
m_nx);
std::cout << "setup : " <<m_bathymetryFilePath<< std::endl;
}else{
m_setup = new tsunami_lab::setups::TsunamiEvent1d(m_bathymetryFilePath);
}
Expand Down Expand Up @@ -743,6 +742,7 @@ void tsunami_lab::Simulator::prepareForCalculation()
loadStations();
}
m_isPreparing = false;
m_isPrepared = true;
}

void tsunami_lab::Simulator::runCalculation()
Expand Down Expand Up @@ -861,7 +861,6 @@ int tsunami_lab::Simulator::start(std::string i_config)
if (!m_isPrepared)
{
prepareForCalculation();
m_isPrepared = true;
}

// BREAKPOINT
Expand Down
4 changes: 4 additions & 0 deletions src/Simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ class tsunami_lab::Simulator
m_displacementFilePath = i_filePath;
}

void setPrepared(bool i_prepared){
m_isPrepared = i_prepared;
}

//--------------------------------------------//
//--------------PUBLIC DELETERS---------------//
//--------------------------------------------//
Expand Down
4 changes: 2 additions & 2 deletions src/ui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ int tsunami_lab::ui::GUI::launch()
if (ImGui::Button("Check connection"))
{
m_communicator.sendToServer(messageToJsonString(xlpmg::CHECK));
std::string l_response = m_communicator.receiveFromServer();
if (l_response == "OK")
if (m_communicator.checkServerResponse())
{
m_connected = true;
}
Expand All @@ -278,6 +277,7 @@ int tsunami_lab::ui::GUI::launch()
m_connected = false;
}
}

ImGui::BeginDisabled(!m_connected);
ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(1.0f, 0.6f, 0.6f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(1.0f, 0.8f, 0.8f));
Expand Down

0 comments on commit 9ea2f52

Please sign in to comment.