Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
HttpServer Class Reference

HTTPS REST API server. More...

#include <http_server.h>

Classes

struct  Config
 Server configuration. More...
 

Public Types

using InitDeviceManagerFn = std::function< std::expected< void, std::string >(std::string driver, std::string adapter)>
 Callback type for POST /api/init.
 
using GetLogFn = std::function< std::vector< std::string >()>
 Callback type for GET /api/log.
 
using RefreshCertFn = std::function< std::expected< void, std::string >()>
 Callback type for POST /api/cert/refresh.
 
using GetGameLoopHealthFn = std::function< GameLoopHealth()>
 Callback type for GET /api/game-loop.
 
using SetGameLoopPeriodFn = std::function< std::expected< void, std::string >(uint32_t periodUs)>
 Callback type for PUT /api/game-loop.
 

Public Member Functions

 HttpServer (Config config, mm::node::DeviceManager &deviceManager, mm::node::MonitoringManager &monitoringManager)
 Constructs the server with the given configuration.
 
 ~HttpServer ()
 Destructor. Calls stop() if the server is still running.
 
bool start ()
 Starts the server background thread and begins accepting connections.
 
void stop ()
 Stops the server, closes the listen socket, and joins the thread.
 
void addRoutes (mm::api::RegisterRoutesFn module)
 Registers a route plug-in to be wired in when the server starts.
 

Detailed Description

HTTPS REST API server.

Hosts the REST API (version, cert, CORS preflight, device/bus/monitoring routes) on its own TLS port, event loop, and thread. The monitoring/control WebSocket lives in a separate WebSocketServer on its own port and loop, so a slow or blocking HTTP handler here (FoE transfer, SDO, cert fetch) can never stall the WebSocket.

All public methods are thread-safe. start() spawns the background thread; stop() tears it down.

Member Typedef Documentation

◆ GetGameLoopHealthFn

Callback type for GET /api/game-loop.

Returns a snapshot of the RT game-loop's health (cycle counters, achieved rate, task-execution timing, RT-scheduling flags). Wired to GameLoop::health() in main.cc so the server never references the loop itself — the same composition-root pattern as GetLogFn.

◆ GetLogFn

using HttpServer::GetLogFn = std::function<std::vector<std::string>()>

Callback type for GET /api/log.

Returns a snapshot of all log entries collected since startup in chronological order. Wired to RingLogSinkMt::entries() in main.cc.

◆ InitDeviceManagerFn

using HttpServer::InitDeviceManagerFn = std::function<std::expected<void, std::string>(std::string driver, std::string adapter)>

Callback type for POST /api/init.

Receives the requested driver name (e.g. "soem") and adapter string (interface name or MAC; may be empty for auto-detect). The callback is responsible for constructing the concrete FieldbusDriver and calling DeviceManager::init(). Lives in the composition root (main.cc) so that concrete driver types are never referenced inside the server.

◆ RefreshCertFn

using HttpServer::RefreshCertFn = std::function<std::expected<void, std::string>()>

Callback type for POST /api/cert/refresh.

Fetches a fresh TLS certificate/key from the configured source and installs them, returning an error string on failure. Lives in main.cc so the server never references the fetch URLs or cert paths. The newly installed cert only takes effect after a restart (uSockets loads the cert once at listen time).

◆ SetGameLoopPeriodFn

using HttpServer::SetGameLoopPeriodFn = std::function<std::expected<void, std::string>(uint32_t periodUs)>

Callback type for PUT /api/game-loop.

Applies a new RT cycle period (microseconds). Validates the value and, on success, retimes the running loop and updates the period recorded for the recorder dumps. Lives in main.cc so the server references neither the GameLoop nor the DeviceManager for this — the same composition-root pattern as GetGameLoopHealthFn. Returns an error string on an invalid value.

Constructor & Destructor Documentation

◆ HttpServer()

HttpServer::HttpServer ( Config  config,
mm::node::DeviceManager deviceManager,
mm::node::MonitoringManager monitoringManager 
)

Constructs the server with the given configuration.

Parameters
configServer parameters. The config is copied internally.
deviceManagerDevice list source; lifetime must exceed that of this object.
monitoringManagerMonitoring registry backing the /api/monitorings routes; lifetime must exceed that of this object.

◆ ~HttpServer()

HttpServer::~HttpServer ( )

Destructor. Calls stop() if the server is still running.

Member Function Documentation

◆ addRoutes()

void HttpServer::addRoutes ( mm::api::RegisterRoutesFn  module)

Registers a route plug-in to be wired in when the server starts.

Each registered module is invoked once on the server's event-loop thread, after the built-in routes and before the CORS preflight / catch-all 404 / listen(), with a mm::api::RouteContext bound to this server's device and monitoring managers. This is the extension point for application-specific C++ endpoints (e.g. /api/example/...).

Must be called before start(); modules added after the thread is running are ignored (a warning is logged). Not thread-safe with respect to a concurrent start().

◆ start()

bool HttpServer::start ( )

Starts the server background thread and begins accepting connections.

Blocks until the listen attempt completes, so the caller learns synchronously whether the port was bound. The port is claimed exclusively (LIBUS_LISTEN_EXCLUSIVE_PORT), so a port already in use fails here instead of silently sharing it via SO_REUSEPORT.

Idempotent: a second call while already running is a no-op that returns true.

Returns
true if the server is listening; false if the port could not be bound.

◆ stop()

void HttpServer::stop ( )

Stops the server, closes the listen socket, and joins the thread.

Idempotent: a second call after the server has already stopped is a no-op.


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