Skip to content

Commit

Permalink
implemented threading and gettimestep function
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 11, 2024
1 parent 17d99de commit 985476b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
20 changes: 7 additions & 13 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,16 @@ int main(int i_argc, char *i_argv[])
{
EXIT = true;
}
else if (strcmp(data.c_str(), "Screate_simulator") == 0)
else if (strcmp(data.c_str(), tsunami_lab::KEY_CREATE_SIMULATOR) == 0)
{
if (simulator != nullptr)
{
delete simulator;
}
simulator = new tsunami_lab::Simulator;
}
else if (strcmp(data.c_str(), "") == 0)
{
if (simulator != nullptr)
{
delete simulator;
}
simulator = new tsunami_lab::Simulator;
}

else if (strcmp(data.c_str(), tsunami_lab::KEY_KILL_SIMULATION) == 0)
{
std::string config = communicator.receiveFromClient();
// simulationThread = std::thread(&tsunami_lab::Simulator::start, &simulator);
}
}
// FUNCTION CALLS
Expand All @@ -67,7 +56,7 @@ int main(int i_argc, char *i_argv[])
{
std::string config = communicator.receiveFromClient();
std::cout << "start" << std::endl;
simulator->start(config);
simulationThread = std::thread(&tsunami_lab::Simulator::start, simulator, config);
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_LOAD_CONFIG_JSON) == 0)
{
Expand All @@ -91,6 +80,11 @@ int main(int i_argc, char *i_argv[])
simulator->toggleFileIO(false);
}
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_GET_TIMESTEP) == 0)
{
std::cout << simulator->getTimeStep() << std::endl;
communicator.sendToClient(std::to_string(simulator->getTimeStep()));
}
}
// CLIENT WILL WAIT FOR RETURN MESSAGE
else if (data[1] == 'R' && simulator != nullptr)
Expand Down
26 changes: 15 additions & 11 deletions src/Simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include <netcdf.h>

#include <string>
#include <atomic>
using json = nlohmann::json;
using Boundary = tsunami_lab::patches::WavePropagation::Boundary;

Expand Down Expand Up @@ -129,13 +128,13 @@ class tsunami_lab::Simulator
//-------------END OF VARIABLES-------------//
//------------------------------------------//

/**
* Determines if a string ends with another string.
*
* @param i_str input string to check
* @param i_suffix possible suffix of i_str
* @return true if i_str ends with i_suffix, otherwise false.
*/#include <atomic>
/** \
* Determines if a string ends with another string. \
* \
* @param i_str input string to check \
* @param i_suffix possible suffix of i_str \
* @return true if i_str ends with i_suffix, otherwise false. \
*/ \
bool endsWith(std::string const &i_str, std::string const &i_suffix);

/**
Expand Down Expand Up @@ -216,8 +215,6 @@ class tsunami_lab::Simulator
void freeMemory();

public:

std::atomic<bool> END = false;
//------------------------------------------//
//-----------------GETTERS------------------//
//------------------------------------------//
Expand All @@ -232,6 +229,11 @@ class tsunami_lab::Simulator
o_setupChoice = m_setupChoice;
}

tsunami_lab::t_idx getTimeStep()
{
return m_timeStep;
}

//------------------------------------------//
//-----------------SETTERS------------------//
//------------------------------------------//
Expand Down Expand Up @@ -301,8 +303,10 @@ class tsunami_lab::Simulator
* @param i_useFileIO true if file I/O should be enabled.
* @return void
*/
void toggleFileIO(bool i_useFileIO){
void toggleFileIO(bool i_useFileIO)
{
m_useFileIO = i_useFileIO;
std::cout << "toggled file I/O" << std::endl;
}

int start(std::string i_config);
Expand Down
10 changes: 5 additions & 5 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ namespace tsunami_lab
//! tells the server to restart
inline const char* KEY_RESTART_SERVER = "Srestart";

//! tells the server to exit the launcher as soon as possible
inline const char* KEY_EXIT_LAUNCHER = "Sexit_launcher";

//! tells the server to revive the launcher
inline const char* KEY_REVIVE_LAUNCHER = "Srevive_launcher";
inline const char* KEY_CREATE_SIMULATOR = "Screate_simulator";

//! launcher will start its main loop
inline const char* KEY_START_SIMULATION = "FV_START";
Expand All @@ -54,6 +50,10 @@ namespace tsunami_lab

//! launcher will toggle file i/o usage to the boolean which is sent immediately after
inline const char* KEY_TOGGLE_FILEIO = "FV_TOGGLE_FILEIO";

inline const char* KEY_GET_TIMESTEP = "FV_GET_TIMESTEP";

inline const char* KEY_GET_MAXTIMESTEPS = "FV_GET_MAXTIMESTEPS";
}

#endif
25 changes: 20 additions & 5 deletions src/ui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ int tsunami_lab::ui::GUI::launch(int i_PORT)

if (ImGui::Button("Create"))
{
m_communicator.sendToServer("Screate_simulator");
m_communicator.sendToServer(tsunami_lab::KEY_CREATE_SIMULATOR);
}
if (ImGui::Button("Run"))
{
Expand All @@ -270,13 +270,28 @@ int tsunami_lab::ui::GUI::launch(int i_PORT)
{
m_communicator.sendToServer(tsunami_lab::KEY_SHUTDOWN_SERVER);
}
if (ImGui::Button("Exit simulation"))
if (ImGui::Button("Get time step"))
{
m_communicator.sendToServer(tsunami_lab::KEY_EXIT_LAUNCHER);
m_communicator.sendToServer(tsunami_lab::KEY_GET_TIMESTEP);
std::cout << m_communicator.receiveFromServer() << std::endl;
}
if (ImGui::Button("Revive simulation"))

if (ImGui::Button("file io true"))
{
if (m_communicator.sendToServer(tsunami_lab::KEY_TOGGLE_FILEIO) == 0)
{
usleep(1000);
m_communicator.sendToServer("true");
}
}

if (ImGui::Button("file io false"))
{
m_communicator.sendToServer(tsunami_lab::KEY_REVIVE_LAUNCHER);
if (m_communicator.sendToServer(tsunami_lab::KEY_TOGGLE_FILEIO) == 0)
{
usleep(1000);
m_communicator.sendToServer("false");
}
}

ImGui::Checkbox("Demo Window", &show_demo_window);
Expand Down

0 comments on commit 985476b

Please sign in to comment.