class EngineJSONRegistrationServer

Overview

Singleton. Creates an HTTP REST server to register newly created EngineJSONServers and store their addresses. More…

#include <engine_json_registration_server.h>

class EngineJSONRegistrationServer {
public:
    // structs

    struct RequestHandler;

    // fields

    static constexpr std::string_view JSONEngineName = "engine_name";
    static constexpr std::string_view JSONAddress = "address";

    // construction

    EngineJSONRegistrationServer(const EngineJSONRegistrationServer&);
    EngineJSONRegistrationServer(EngineJSONRegistrationServer&&);

    // methods

    static EngineJSONRegistrationServer* getInstance();
    static EngineJSONRegistrationServer* resetInstance(const std::string& serverAddress);
    static void clearInstance();

    static std::string tryInstantiate(
        const std::string& initialAddress,
        const unsigned numRetries
    );

    static bool sendClientEngineRequest(
        const std::string& address,
        const engine_name_t& engineName,
        const std::string& engineAddress,
        const unsigned int numTries = 1,
        const unsigned int waitTime = 0
    );

    EngineJSONRegistrationServer& operator = (const EngineJSONRegistrationServer&);
    EngineJSONRegistrationServer& operator = (EngineJSONRegistrationServer&&);
    void startServerAsync();
    void shutdownServer();
    const std::string serverAddress() const;
    bool isRunning() const;
    size_t getNumWaitingEngines();
    std::string retrieveEngineAddress(const engine_name_t& engineName);
    std::string requestEngine(const engine_name_t& engineName);

    void registerEngineAddress(
        const engine_name_t& engineName,
        const std::string& address
    );
};

Detailed Documentation

Singleton. Creates an HTTP REST server to register newly created EngineJSONServers and store their addresses.

Fields

static constexpr std::string_view JSONEngineName = "engine_name"

JSON Engine Name locator used during registration.

static constexpr std::string_view JSONAddress = "address"

JSON Engine Address locator used during registration.

Methods

static EngineJSONRegistrationServer* getInstance()

Get Instance of EngineJSONRegistrationServer.

Returns:

Returns ptr to EngineJSONRegistrationServer if it exists, nullptr otherwise

static EngineJSONRegistrationServer* resetInstance(const std::string& serverAddress)

Reset EngineJSONRegistrationServer with the given address.

Parameters:

serverAddress

Server Address to bind to

Returns:

Returns pointer to created instance

static void clearInstance()

Delete Instance.

static std::string tryInstantiate(
    const std::string& initialAddress,
    const unsigned numRetries
)

Attempts to instantiate and start the registration server.

The function will try to start the registration server at the given address. If the initial attempt fails, then the function will try again, until numRetries limit is exceeded. On every attempt, except for the first one, the address will be modified. The host will be taken from the initialAddress argument, but the port will be changed - the function will ask the OS to provide an unused port for the server.

Parameters:

initialAddress

Initial address that the server will try to use

numRetries

Maximum number of attempts to start the server

When

numRetires has been exceeded.

Returns:

The actual address of the server. If the first attempt to start was successful, then it will be the initialAddress. Otherwise, the address will consist of the hostname from the initialAddress and a random port.

static bool sendClientEngineRequest(
    const std::string& address,
    const engine_name_t& engineName,
    const std::string& engineAddress,
    const unsigned int numTries = 1,
    const unsigned int waitTime = 0
)

Send Engine Name and address to specified address.

Parameters:

address

Address to send data to

engineName

Name of engine

engineAddress

Address of engine

numTries

Number of times to try and contact the registration server

waitTime

Time (in seconds) to wait between contact attempts

Returns:

Returns true on success, false otherwise

void startServerAsync()

Start the server if it’s not already running.

void shutdownServer()

Stop the Server.

const std::string serverAddress() const

Get server address.

bool isRunning() const

Returns true when server is running, false otherwise.

size_t getNumWaitingEngines()

Get the number of engines that are still waiting for registration.

std::string retrieveEngineAddress(const engine_name_t& engineName)

Retrieve a registered engine address. If available and non-empty, erase it from _registeredAddresses.

Parameters:

engineName

Engine Name for which to find the address

Returns:

If address available, return it. Otherwise return empty string

std::string requestEngine(const engine_name_t& engineName)

Request an engine’s address. If available, erases entry from _registeredAddresses.

Parameters:

engineName

Name of engine to wait for

Returns:

If available, returns name of engine. Else, returns empty string

void registerEngineAddress(
    const engine_name_t& engineName,
    const std::string& address
)

Register an engine’s address.

Parameters:

engineName

Name of engine

address

Address of engine