NRP Core  1.4.1
EngineJSONServer Class Referenceabstract

Manages communication with the NRP. Uses a REST server to send/receive data. Singleton class. More...

#include <engine_json_server.h>

Inheritance diagram for EngineJSONServer:
NestJSONServer NRPJSONCommunicationController

Public Types

using mutex_t = std::timed_mutex
 
using lock_t = std::unique_lock< EngineJSONServer::mutex_t >
 

Public Member Functions

 EngineJSONServer (const std::string &engineAddress, const std::string &engineName, const std::string &clientAddress)
 Constructor. Tries to bind to a port and register itself with clientAddress. More...
 
 EngineJSONServer ()=delete
 No dummy servers without name and address. More...
 
virtual ~EngineJSONServer ()
 
 EngineJSONServer (const EngineJSONServer &)=delete
 
EngineJSONServeroperator= (const EngineJSONServer &)=delete
 
bool isServerRunning () const
 Is the server running? More...
 
void startServerAsync ()
 Start the server in asynchronous mode. More...
 
void startServer ()
 Start the server synchronously. More...
 
void shutdownServer ()
 Stop running server. More...
 
uint16_t serverPort () const
 Get running server port. More...
 
std::string serverAddress () const
 Get server address. More...
 
void registerDataPack (const std::string &datapackName, JsonDataPackController *interface)
 Registers a datapack. More...
 
void registerDataPackNoLock (const std::string &datapackName, JsonDataPackController *interface)
 Registers a datapack. Skips locking the mutex. Should only be used if thread-safe access to _datapacksControllers can be guaranteed. More...
 
virtual SimulationTime runLoopStep (SimulationTime timeStep)=0
 Run a single loop step. More...
 
virtual nlohmann::json initialize (const nlohmann::json &data, EngineJSONServer::lock_t &datapackLock)=0
 Engine Initialization routine. More...
 
virtual nlohmann::json reset (EngineJSONServer::lock_t &datapackLock)=0
 Engine reset routine. More...
 
virtual nlohmann::json shutdown (const nlohmann::json &data)=0
 Engine Shutdown routine. More...
 
bool shutdownFlag ()
 Has a shutdown command been received? More...
 

Protected Member Functions

void clearRegisteredDataPacks ()
 Remove all registered datapacks. More...
 
virtual nlohmann::json getDataPackData (const nlohmann::json &reqData)
 Retrieves datapack data. Takes an array of datapack names for which to get data. More...
 
virtual void setDataPackData (const nlohmann::json &reqData)
 Set datapack data. More...
 
const std::string & getEngineName ()
 Get the Engine name. More...
 

Protected Attributes

mutex_t _datapackLock
 Lock access to _datapacks to make execution thread-safe. More...
 

Static Protected Attributes

static constexpr std::string_view GetDataPackInformationRoute = EngineJSONConfigConst::EngineServerGetDataPacksRoute
 
static constexpr std::string_view SetDataPackRoute = EngineJSONConfigConst::EngineServerSetDataPacksRoute
 
static constexpr std::string_view RunLoopStepRoute = EngineJSONConfigConst::EngineServerRunLoopStepRoute
 
static constexpr std::string_view InitializeRoute = EngineJSONConfigConst::EngineServerInitializeRoute
 
static constexpr std::string_view ResetRoute = EngineJSONConfigConst::EngineServerResetRoute
 
static constexpr std::string_view ShutdownRoute = EngineJSONConfigConst::EngineServerShutdownRoute
 

Friends

class EngineJSONServerTest_Functions_Test
 
class TestEngineJSONServer
 

Detailed Description

Manages communication with the NRP. Uses a REST server to send/receive data. Singleton class.

Member Typedef Documentation

◆ lock_t

◆ mutex_t

using EngineJSONServer::mutex_t = std::timed_mutex

Constructor & Destructor Documentation

◆ EngineJSONServer() [1/3]

EngineJSONServer::EngineJSONServer ( const std::string &  engineAddress,
const std::string &  engineName,
const std::string &  clientAddress 
)

Constructor. Tries to bind to a port and register itself with clientAddress.

Parameters
engineAddressServer Address. If it contains a port, will try to bind to said port. If that fails, will increment port number and try again. This will continue for at most EngineJSONConfigConst::MaxAddrBindTries times
engineNameEngine Name
clientAddressClient Address. The server will try to register itself under this address

◆ EngineJSONServer() [2/3]

EngineJSONServer::EngineJSONServer ( )
delete

No dummy servers without name and address.

◆ ~EngineJSONServer()

EngineJSONServer::~EngineJSONServer ( )
virtual

◆ EngineJSONServer() [3/3]

EngineJSONServer::EngineJSONServer ( const EngineJSONServer )
delete

Member Function Documentation

◆ clearRegisteredDataPacks()

void EngineJSONServer::clearRegisteredDataPacks ( )
protected

Remove all registered datapacks.

◆ getDataPackData()

nlohmann::json EngineJSONServer::getDataPackData ( const nlohmann::json reqData)
protectedvirtual

Retrieves datapack data. Takes an array of datapack names for which to get data.

Parameters
reqDataA JSON array containing datapack names.
Returns
DataPack data, formatted as a JSON array

◆ getEngineName()

const std::string& EngineJSONServer::getEngineName ( )
inlineprotected

Get the Engine name.

◆ initialize()

virtual nlohmann::json EngineJSONServer::initialize ( const nlohmann::json data,
EngineJSONServer::lock_t datapackLock 
)
pure virtual

Engine Initialization routine.

Parameters
dataInitialization data
datapackLockDataPack Lock. Prevents access to _datapacksControllers
Returns
Returns data about initialization status

Implemented in NestJSONServer.

◆ isServerRunning()

bool EngineJSONServer::isServerRunning ( ) const

Is the server running?

Returns
Returns true if the server is running

◆ operator=()

EngineJSONServer& EngineJSONServer::operator= ( const EngineJSONServer )
delete

◆ registerDataPack()

void EngineJSONServer::registerDataPack ( const std::string &  datapackName,
JsonDataPackController interface 
)

Registers a datapack.

Parameters
datapackNameName of datapack
interfacePointer to interface

◆ registerDataPackNoLock()

void EngineJSONServer::registerDataPackNoLock ( const std::string &  datapackName,
JsonDataPackController interface 
)

Registers a datapack. Skips locking the mutex. Should only be used if thread-safe access to _datapacksControllers can be guaranteed.

Parameters
datapackNameName of datapack
interfacePointer to interface

◆ reset()

virtual nlohmann::json EngineJSONServer::reset ( EngineJSONServer::lock_t datapackLock)
pure virtual

Engine reset routine.

Parameters
datapackLockDataPack Lock. Prevents access to _datapacksControllers
Returns
Returns data about initialization status

Implemented in NestJSONServer.

◆ runLoopStep()

virtual SimulationTime EngineJSONServer::runLoopStep ( SimulationTime  timeStep)
pure virtual

Run a single loop step.

Parameters
timeStepStep to take
Returns
Returns the time registered by this engine at the end of the loop

Implemented in NestJSONServer.

◆ serverAddress()

std::string EngineJSONServer::serverAddress ( ) const

Get server address.

◆ serverPort()

uint16_t EngineJSONServer::serverPort ( ) const

Get running server port.

Returns
Returns port of running server, 0 if server is not running

◆ setDataPackData()

void EngineJSONServer::setDataPackData ( const nlohmann::json reqData)
protectedvirtual

Set datapack data.

Parameters
reqDataA JSON array containing datapack names linked to the individual datapack's data
Returns
Execution result

◆ shutdown()

virtual nlohmann::json EngineJSONServer::shutdown ( const nlohmann::json data)
pure virtual

Engine Shutdown routine.

Parameters
dataShutdown data
Returns
Returns data about shutdown status

Implemented in NestJSONServer.

◆ shutdownFlag()

bool EngineJSONServer::shutdownFlag ( )

Has a shutdown command been received?

Returns
Returns true if a shutdown command has been received

◆ shutdownServer()

void EngineJSONServer::shutdownServer ( )

Stop running server.

◆ startServer()

void EngineJSONServer::startServer ( )

Start the server synchronously.

◆ startServerAsync()

void EngineJSONServer::startServerAsync ( )

Start the server in asynchronous mode.

Friends And Related Function Documentation

◆ EngineJSONServerTest_Functions_Test

friend class EngineJSONServerTest_Functions_Test
friend

◆ TestEngineJSONServer

friend class TestEngineJSONServer
friend

Member Data Documentation

◆ _datapackLock

mutex_t EngineJSONServer::_datapackLock
protected

Lock access to _datapacks to make execution thread-safe.

◆ GetDataPackInformationRoute

constexpr std::string_view EngineJSONServer::GetDataPackInformationRoute = EngineJSONConfigConst::EngineServerGetDataPacksRoute
staticconstexprprotected

◆ InitializeRoute

constexpr std::string_view EngineJSONServer::InitializeRoute = EngineJSONConfigConst::EngineServerInitializeRoute
staticconstexprprotected

◆ ResetRoute

constexpr std::string_view EngineJSONServer::ResetRoute = EngineJSONConfigConst::EngineServerResetRoute
staticconstexprprotected

◆ RunLoopStepRoute

constexpr std::string_view EngineJSONServer::RunLoopStepRoute = EngineJSONConfigConst::EngineServerRunLoopStepRoute
staticconstexprprotected

◆ SetDataPackRoute

constexpr std::string_view EngineJSONServer::SetDataPackRoute = EngineJSONConfigConst::EngineServerSetDataPacksRoute
staticconstexprprotected

◆ ShutdownRoute

constexpr std::string_view EngineJSONServer::ShutdownRoute = EngineJSONConfigConst::EngineServerShutdownRoute
staticconstexprprotected

The documentation for this class was generated from the following files: