class EngineJSONServer

Overview

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

#include <engine_json_server.h>

class EngineJSONServer {
public:
    // typedefs

    typedef std::timed_mutex mutex_t;
    typedef std::unique_lock<EngineJSONServer::mutex_t> lock_t;

    // construction

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

    EngineJSONServer();
    EngineJSONServer(const EngineJSONServer&);

    // methods

    EngineJSONServer& operator = (const EngineJSONServer&);
    bool isServerRunning() const;
    void startServerAsync();
    void startServer();
    void shutdownServer();
    uint16_t serverPort() const;
    std::string serverAddress() const;

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

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

    virtual SimulationTime runLoopStep(SimulationTime timeStep) = 0;

    virtual nlohmann::json initialize(
        const nlohmann::json& data,
        EngineJSONServer::lock_t& datapackLock
    ) = 0;

    virtual nlohmann::json reset(EngineJSONServer::lock_t& datapackLock) = 0;
    virtual nlohmann::json shutdown(const nlohmann::json& data) = 0;
    bool shutdownFlag();
};

// direct descendants

class NestJSONServer;
class NRPJSONCommunicationController;

Detailed Documentation

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

Construction

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:

engineAddress

Server 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

engineName

Engine Name

clientAddress

Client Address. The server will try to register itself under this address

EngineJSONServer()

No dummy servers without name and address.

Methods

bool isServerRunning() const

Is the server running?

Returns:

Returns true if the server is running

void startServerAsync()

Start the server in asynchronous mode.

void startServer()

Start the server synchronously.

void shutdownServer()

Stop running server.

uint16_t serverPort() const

Get running server port.

Returns:

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

std::string serverAddress() const

Get server address.

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

Registers a datapack.

Parameters:

datapackName

Name of datapack

interface

Pointer to interface

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.

Parameters:

datapackName

Name of datapack

interface

Pointer to interface

virtual SimulationTime runLoopStep(SimulationTime timeStep) = 0

Run a single loop step.

Parameters:

timeStep

Step to take

Returns:

Returns the time registered by this engine at the end of the loop

virtual nlohmann::json initialize(
    const nlohmann::json& data,
    EngineJSONServer::lock_t& datapackLock
) = 0

Engine Initialization routine.

Parameters:

data

Initialization data

datapackLock

DataPack Lock. Prevents access to _datapacksControllers

Returns:

Returns data about initialization status

virtual nlohmann::json reset(EngineJSONServer::lock_t& datapackLock) = 0

Engine reset routine.

Parameters:

datapackLock

DataPack Lock. Prevents access to _datapacksControllers

Returns:

Returns data about initialization status

virtual nlohmann::json shutdown(const nlohmann::json& data) = 0

Engine Shutdown routine.

Parameters:

data

Shutdown data

Returns:

Returns data about shutdown status

bool shutdownFlag()

Has a shutdown command been received?

Returns:

Returns true if a shutdown command has been received