lib

namespace xlpmg

Communicator API.

Author

Luca-Philipp Grumbach

Description

Library to easily create a client-server connection and handle its communication and logging.

Author

Luca-Philipp Grumbach

Description

This file provides an interface with pre-defined messages to enable a correct transmission of information between client and server.

Enums

enum MessagePart

Enum representing the different parts of a message.

The MessagePart enum defines the different parts of a message, including the expectation, urgency, key, and arguments.

Values:

enumerator EXPECTATION
enumerator URGENCY
enumerator KEY
enumerator ARGS
enum MessageExpectation

Enum representing the expectation of a message.

The MessageExpectation enum defines the different expectations of a message, such as whether a response is expected or not.

Values:

enumerator NO_RESPONSE
enumerator EXPECT_RESPONSE
enum MessageUrgency

Enum representing the urgency of a message.

The MessageUrgency enum defines the different levels of urgency for a message, such as critical, high, medium, and low.

Values:

enumerator CRITICAL
enumerator HIGH
enumerator MEDIUM
enumerator LOW

Functions

NLOHMANN_JSON_SERIALIZE_ENUM (MessageExpectation, {{NO_RESPONSE, "no_response"}, {EXPECT_RESPONSE, "expect_response"}})

Macro to briefly define a mapping between MessageExpectation enum and JSON.

NLOHMANN_JSON_SERIALIZE_ENUM (MessageUrgency, {{CRITICAL, "critical"}, {HIGH, "high"}, {MEDIUM, "medium"}, {LOW, "low"}})

Macro to briefly define a mapping between MessageUrgency enum and JSON.

json messageToJson(Message i_message)

Converts a Message to json object.

Parameters:

i_message – message

Returns:

message as json object

std::string messageToJsonString(Message message)

Converts a Message to a json string.

Parameters:

i_message – message

Returns:

message as json string

Message jsonToMessage(json i_json)

Converts a json object to a Message.

Parameters:

i_json – json object

Returns:

message

Variables

const Message KILL_SIMULATION = {MessageExpectation::NO_RESPONSE, MessageUrgency::CRITICAL, "kill_simulation"}

Server will stop the running simulation.

const Message WRITE_CHECKPOINT = {MessageExpectation::NO_RESPONSE, MessageUrgency::CRITICAL, "write_checkpoint"}

Tells the Simulator to write a checkpoint.

const Message PAUSE_SIMULATION = {MessageExpectation::NO_RESPONSE, MessageUrgency::CRITICAL, "pause_simulation"}

Pauses a simulation.

const Message SHUTDOWN_SERVER = {MessageExpectation::NO_RESPONSE, MessageUrgency::CRITICAL, "shutdown_server"}

Tells the server to shutdown.

const Message CHECK = {MessageExpectation::NO_RESPONSE, MessageUrgency::HIGH, "CHECK"}

Should not not induce any functionality and is only used to check if the other side responds.

const Message START_SIMULATION = {MessageExpectation::NO_RESPONSE, MessageUrgency::HIGH, "start_simulation"}

Tells the server to start the simulator.

const Message CONTINUE_SIMULATION = {MessageExpectation::NO_RESPONSE, MessageUrgency::HIGH, "continue_simulation"}

Continues a simulation.

const Message RESET_SIMULATOR = {MessageExpectation::NO_RESPONSE, MessageUrgency::HIGH, "reset_simulator"}

Tells the Simulator to reset.

const Message TOGGLE_FILEIO = {MessageExpectation::NO_RESPONSE, MessageUrgency::HIGH, "toggle_fileio"}

Tells the Simulator to toggle file i/o usage to given argument.

const Message COMPILE = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "compile", ""}

Server will recompile with provided arguments.

const Message COMPILE_RUN_BASH = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "compile_run_bash", ""}

Server will recompile with provided arguments and run using a bash script.

const Message COMPILE_RUN_SBATCH = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "compile_run_sbatch", ""}

Server will recompile with provided arguments and run using an sbatch script.

const Message DELETE_CHECKPOINTS = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "delete_checkpoints"}

Deletes checkpoints.

const Message DELETE_STATIONS = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "delete_stations"}

Deletes stations.

const Message SET_OFFSET = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "set_offset"}

Sets the cell amount of the simulation.

const Message SET_CELL_AMOUNT = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "set_cell_amount"}

Sets the offset of the simulation.

const Message SEND_FILE = {MessageExpectation::NO_RESPONSE, MessageUrgency::LOW, "send_file"}

For sending a file to the server.

const Message LOAD_CONFIG_JSON = {MessageExpectation::NO_RESPONSE, MessageUrgency::LOW, "load_config_json"}

Tells the Simulator to load config from json data.

const Message LOAD_CONFIG_FILE = {MessageExpectation::NO_RESPONSE, MessageUrgency::LOW, "load_config_file"}

Tells the Simulator to load config from .json config file.

const Message SET_READ_BUFFER_SIZE = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "set_read_buffer_size"}

Tells the server to change the read buffer size.

const Message SET_SEND_BUFFER_SIZE = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "set_send_buffer_size"}

Tells the server to change the send buffer size.

const Message LOAD_STATIONS = {MessageExpectation::NO_RESPONSE, MessageUrgency::MEDIUM, "load_stations"}

Updates stations.

const Message GET_TIME_VALUES = {MessageExpectation::EXPECT_RESPONSE, MessageUrgency::CRITICAL, "get_time_values"}

Returns the current timestep from the simulator.

const Message GET_SYSTEM_INFORMATION = {MessageExpectation::EXPECT_RESPONSE, MessageUrgency::CRITICAL, "get_system_information"}

Gets system info such as CPU and RAM usage.

const Message GET_SIMULATION_SIZES = {MessageExpectation::EXPECT_RESPONSE, MessageUrgency::CRITICAL, "get_simulation_sizes"}

Returns the current simulation sizes from the simulator.

const Message GET_HEIGHT_DATA = {MessageExpectation::EXPECT_RESPONSE, MessageUrgency::HIGH, "get_height_data"}

Tells the server to start sending height data. (buffered)

const Message GET_BATHYMETRY_DATA = {MessageExpectation::EXPECT_RESPONSE, MessageUrgency::HIGH, "get_bathymetry_data"}

Tells the server to start sending bathymetry data. (buffered)

const Message RECV_FILE = {MessageExpectation::EXPECT_RESPONSE, MessageUrgency::LOW, "recv_file"}

For receiving a file from the server.

const Message BUFFERED_SEND_FINISHED = {MessageExpectation::NO_RESPONSE, MessageUrgency::CRITICAL, "buff_send_finished"}

Tells the client that a buffered sending operation has finished.

const Message SERVER_RESPONSE = {MessageExpectation::NO_RESPONSE, MessageUrgency::CRITICAL, "server_response"}

Server response template.

struct Message
#include <communicator_api.h>

Struct representing a message in the communicator API.

Description

This struct contains information about the urgency, key, and arguments of a message.

Public Members

MessageExpectation expectation = MessageExpectation::NO_RESPONSE
MessageUrgency urgency = MessageUrgency::MEDIUM
std::string key = "NONE"
json args = ""
class Communicator

Public Functions

inline void setReadBufferSize(unsigned int newSize)

Sets the read buffer size.

Parameters:

newSize – The new size of the read buffer.

inline void setSendBufferSize(unsigned int newSize)

Sets the send buffer size.

Parameters:

newSize – The new size of the send buffer.

inline int startClient(char *IPADDRESS, int PORT)

Creates a client socket and searches for a server.

Parameters:

PORT – Port for communication with server.

Returns:

0 on success, -1 for errors.

inline void stopClient()

Stops all connections of the client socket.

inline std::string receiveFromServer(long timeout = TIMEOUT, bool log = true)

Receives a message from the server.

Parameters:
  • timeout – Timeout for the operation in seconds.

  • log – Whether the message should be logged or not.

Returns:

Message as string.

inline int sendToServer(std::string message, long timeout = TIMEOUT, bool log = true)

Sends a message to the server.

Parameters:
  • message – String to send.

  • timeout – Timeout for the operation in seconds.

  • log – Whether the message should be logged or not.

Returns:

0 if successful, 1 otherwise.

inline void getLog(std::string &o_logData)

Gets the log data.

Parameters:

o_logData – Pointer to the string which the log will be written into.

inline void clearLog()

Clears the log data.

inline void startServer(int PORT)

Creates a server socket and waits for connections.

Parameters:

PORT – Port for communication with client.

inline void stopServer()

Stops all connections of the server.

inline std::string receiveFromClient(bool log = true)

Receives a message from a client.

Parameters:

log – Whether the message should be logged or not.

Returns:

Message as string.

inline void sendToClient(std::string message, bool log = true)

Sends a message to a client.

Parameters:
  • messageMessage to send.

  • log – Whether the message should be logged or not.

Public Members

unsigned int BUFF_SIZE_READ_DEFAULT = 8096

default size of the reading buffer

unsigned int BUFF_SIZE_READ = BUFF_SIZE_READ_DEFAULT

actual size of the reading buffer

unsigned int BUFF_SIZE_SEND_DEFAULT = 8096

default size of the sending buffer

unsigned int BUFF_SIZE_SEND = BUFF_SIZE_SEND_DEFAULT

actual size of the sending buffer

bool isConnected = false

true if there is a connection

Public Static Attributes

static const long TIMEOUT = 2