Skip to content

Commit

Permalink
implemented api
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 11, 2024
1 parent b32092e commit 30d6910
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 207 deletions.
131 changes: 131 additions & 0 deletions include/communicator_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* @author Luca-Philipp Grumbach
*
* # Description
* Communicator api.
**/
#ifndef XLPMG_COMMUNICATOR_API_H
#define XLPMG_COMMUNICATOR_API_H

#include <cstddef>
#include <string>

#include <nlohmann/json.hpp>

namespace xlpmg
{
enum MessagePart
{
MESSAGE_TYPE,
KEY,
ARGS
};

NLOHMANN_JSON_SERIALIZE_ENUM(MessagePart, {{MESSAGE_TYPE, "type"},
{KEY, "key"},
{ARGS, "arguments"}});

enum MessageType
{
SERVER__CALL,
FUNCTION_CALL,
CHECK_CALL
};

NLOHMANN_JSON_SERIALIZE_ENUM(MessageType, {{SERVER__CALL, "server_call"},
{FUNCTION_CALL, "function_call"},
{CHECK_CALL, "check_call"}});

//! should not not induce any functionality and is only used to check if the other side responds
inline const char *KEY_CHECK = "XCHECKX";

// SERVER CALLS

/*
* Tells the server to shutdown.
*
* @MESSAGE_TYPE SERVER__CALL
* @ARGS none
*/
inline const char *KEY_SHUTDOWN_SERVER = "shutdown_server";

/*
* Tells the server to restart.
*
* @MESSAGE_TYPE SERVER__CALL
* @ARGS path to config file or "".
*/
inline const char *KEY_START_SIMULATION = "start_simulation";

/*
* Server will stop the running simulation.
*
* @MESSAGE_TYPE SERVER__CALL
* @ARGS none.
*/
inline const char *KEY_KILL_SIMULATION = "kill_simulation";

/*
* Server will recompile.
*
* @MESSAGE_TYPE SERVER__CALL
* @ARGS as array:
* args[ENV]: environment option for compiler (eg. "CXX=g++-13" or "").
* args[OPT]: compiler options (eg. "omp=gnu opt=-O2" or "")
*/
inline const char *KEY_RECOMPILE = "recompile";

// FUNCTION CALLS: VOID

/*
* Simulator will write a checkpoint.
*
* @MESSAGE_TYPE FUNCTION_CALL
* @ARGS none.
*/
inline const char *KEY_WRITE_CHECKPOINT = "write_checkpoint";

/*
* Simulator will load config from json data.
*
* @MESSAGE_TYPE FUNCTION_CALL
* @ARGS config data in json format.
*/
inline const char *KEY_LOAD_CONFIG_JSON = "load_config_json";

/*
* Simulator will load config from .json config file.
*
* @MESSAGE_TYPE FUNCTION_CALL
* @ARGS path to config file.
*/
inline const char *KEY_LOAD_CONFIG_FILE = "load_config_file";

/*
* Simulator will toggle file i/o usage.
*
* @MESSAGE_TYPE FUNCTION_CALL
* @ARGS boolean to set file i/o to.
*/
inline const char *KEY_TOGGLE_FILEIO = "FV_TOGGLE_FILEIO";

// FUNCTION CALLS: RETURNING

/*
* Returns the current timestep from the simulator.
*
* @MESSAGE_TYPE FUNCTION_CALL
* @ARGS none.
*/
inline const char *KEY_GET_TIMESTEP = "FR_GET_TIMESTEP";

/*
* Returns the max timesteps from the simulator.
*
* @MESSAGE_TYPE FUNCTION_CALL
* @ARGS none.
*/
inline const char *KEY_GET_MAXTIMESTEPS = "FR_GET_MAXTIMESTEPS";
}

#endif
4 changes: 4 additions & 0 deletions runServer.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# wait for 2s to make sure the server stopped
sleep 2

# check if first argument is not empty
# the second argument are the compiler options which will be appended in any case
if [ -n "$1" ]
Expand All @@ -11,4 +14,5 @@ scons $2
fi

# restart the server
echo STARTING SERVER
./build/tsunami_lab
115 changes: 63 additions & 52 deletions src/Server.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
#include "Simulator.h"
#include "Communicator.hpp"
#include "constants.h"
#include "communicator_api.h"
#include <nlohmann/json.hpp>

#include <thread>
#define USEGUI 1

using json = nlohmann::json;

int PORT = 8080;
bool EXIT = false;
std::thread simulationThread;
bool isSimulationRunning = false;

int execWithOutput(std::string i_cmd, std::string i_outputFile)
{
std::string commandString = (i_cmd + " > " + i_outputFile + " 2>&1 &").data();
const char *commandChars = commandString.data();
return system(commandChars);
}

int exec(std::string i_cmd)
{
return system(i_cmd.data());
}

int main(int i_argc, char *i_argv[])
{
int exitCode = 0;
Expand All @@ -23,75 +40,69 @@ int main(int i_argc, char *i_argv[])
communicator.startServer(PORT);
while (!EXIT)
{
std::string data = communicator.receiveFromClient();
// SERVER FUNCTION CALLS
if (data[0] == 'S')
std::string rawData = communicator.receiveFromClient();
// check if client sent valid json, go back to reading if not
if (!json::accept(rawData))
{
communicator.sendToClient("FAIL");
continue;
}

json parsedData = json::parse(rawData);
json type = parsedData.at(xlpmg::MESSAGE_TYPE);
json key = parsedData.at(xlpmg::KEY);
// json data = parsedData.value(xlpmg::DATA, "");

if (type.get<xlpmg::MessageType>() == xlpmg::SERVER__CALL)
{
if (strcmp(data.c_str(), tsunami_lab::KEY_SHUTDOWN_SERVER) == 0)
if (key == xlpmg::KEY_SHUTDOWN_SERVER)
{
EXIT = true;
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_KILL_SIMULATION) == 0)
{
communicator.stopServer();
if (simulationThread.joinable())
{
simulationThread.std::thread::~thread();
isSimulationRunning = false;
}
}
}
// FUNCTION CALLS
else if (data[0] == 'F' && simulator != nullptr)
{
// VOIDS
if (data[1] == 'V')
else if (key == xlpmg::KEY_START_SIMULATION)
{
if (strcmp(data.c_str(), tsunami_lab::KEY_WRITE_CHECKPOINT) == 0)
{
simulator->writeCheckpoint();
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_START_SIMULATION) == 0)
{
std::string config = communicator.receiveFromClient();
if (!isSimulationRunning)
{
simulationThread = std::thread(&tsunami_lab::Simulator::start, simulator, config);
isSimulationRunning = true;
}
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_LOAD_CONFIG_JSON) == 0)
std::string config = parsedData.at(xlpmg::ARGS);
if (!isSimulationRunning)
{
std::string config = communicator.receiveFromClient();
simulator->loadConfigDataJsonString(config);
simulationThread = std::thread(&tsunami_lab::Simulator::start, simulator, config);
isSimulationRunning = true;
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_LOAD_CONFIG_FILE) == 0)
{
std::string configFile = communicator.receiveFromClient();
simulator->loadConfigDataFromFile(configFile);
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_TOGGLE_FILEIO) == 0)
{
std::string toggle = communicator.receiveFromClient();
if (strcmp(toggle.c_str(), "true") == 0)
{
simulator->toggleFileIO(true);
}
else
{
simulator->toggleFileIO(false);
}
}
else if (strcmp(data.c_str(), tsunami_lab::KEY_GET_TIMESTEP) == 0)
}
else if (key == xlpmg::KEY_KILL_SIMULATION)
{
if (simulationThread.joinable())
{
std::cout << simulator->getTimeStep() << std::endl;
communicator.sendToClient(std::to_string(simulator->getTimeStep()));
simulationThread.std::thread::~thread();
isSimulationRunning = false;
}
}
// CLIENT WILL WAIT FOR RETURN MESSAGE
else if (data[1] == 'R' && simulator != nullptr)
else if (key == xlpmg::KEY_RECOMPILE)
{
//Shutdown server
EXIT = true;
communicator.stopServer();
if (simulationThread.joinable())
{
simulationThread.std::thread::~thread();
isSimulationRunning = false;
}
//execute recompilation
std::string env = parsedData.at(xlpmg::ARGS).value("ENV", ""); // environment var
std::string opt = parsedData.at(xlpmg::ARGS).value("OPT", ""); //compiler opt
exec("chmod +x runServer.sh");
exec("./runServer.sh \""+env+"\" \""+opt+"\" &");
}
}
else if (type.get<xlpmg::MessageType>() == xlpmg::FUNCTION_CALL)
{
std::cout << "function call" << std::endl;
}
}

if (simulationThread.joinable())
Expand Down
Loading

0 comments on commit 30d6910

Please sign in to comment.