Motion Master 6.0.0-alpha.86
Next-generation motion control software
Loading...
Searching...
No Matches
http_server.h
Go to the documentation of this file.
1#pragma once
2
3#include <uwebsockets/App.h>
4
5#include <BS_thread_pool.hpp>
6#include <atomic>
7#include <chrono>
8#include <expected>
9#include <functional>
10#include <future>
11#include <memory>
12#include <string>
13#include <string_view>
14#include <thread>
15#include <utility>
16#include <vector>
17
18#include "api/router.h"
19#include "api/web_api.h"
20#include "auto_tuning/status.h" // Status (returned by the GET /api/auto-tuning callback)
21#include "game_loop.h" // GameLoopHealth (returned by the GET /api/game-loop callback)
22#include "net/http_client.h" // net::Response (an auto-tuning reply, passed through verbatim)
23#include "node/eni_collector.h" // EniCollectorOptions (returned by the GET /api/eni callback)
24
25namespace mm::core {
26class UserCache;
27} // namespace mm::core
28
29namespace mm::node {
30class DeviceManager;
31class MonitoringManager;
32class ProcedureManager;
33} // namespace mm::node
34
44 public:
53 std::function<std::expected<void, std::string>(std::string driver, std::string adapter)>;
54
59 using GetLogFn = std::function<std::vector<std::string>()>;
60
67 using RefreshCertFn = std::function<std::expected<void, std::string>()>;
68
75 using GetGameLoopHealthFn = std::function<GameLoopHealth()>;
76
85 using SetGameLoopPeriodFn = std::function<std::expected<void, std::string>(uint32_t periodUs)>;
86
94 std::function<std::expected<mm::net::Response, std::string>(const std::string& body)>;
95
102
107 using AutoTuningSpecFn = std::function<std::expected<mm::net::Response, std::string>()>;
108
117
144
154 HttpServer(Config config, mm::node::DeviceManager& deviceManager,
155 mm::node::MonitoringManager& monitoringManager,
156 mm::node::ProcedureManager& procedureManager, mm::core::UserCache& userCache);
157
159 ~HttpServer();
160
169 bool start();
170
174 void stop();
175
186
187 private:
188 void run();
189
190 Config config_;
191 mm::node::DeviceManager& deviceManager_;
192 mm::node::MonitoringManager& monitoringManager_;
193 mm::node::ProcedureManager& procedureManager_;
194 mm::core::UserCache& userCache_;
195 std::vector<mm::api::RegisterRoutesFn> routeModules_;
196 std::atomic<bool> running_{false};
197 std::thread thread_;
198 std::atomic<uWS::Loop*> loop_{nullptr};
199 std::atomic<uWS::SSLApp*> app_{nullptr};
201 std::promise<bool> listenResult_;
205 std::atomic<bool> stopping_{false};
242 BS::light_thread_pool pool_{32};
243};
HTTPS REST API server.
Definition http_server.h:43
std::function< std::expected< void, std::string >(uint32_t periodUs)> SetGameLoopPeriodFn
Callback type for PUT /api/game-loop.
Definition http_server.h:85
std::function< mm::auto_tuning::Status()> AutoTuningStatusFn
Callback type for GET /api/auto-tuning.
Definition http_server.h:101
~HttpServer()
Destructor. Calls stop() if the server is still running.
Definition http_server.cc:383
std::function< std::expected< mm::net::Response, std::string >(const std::string &body)> AutoTuningRunFn
Callback type for POST /api/auto-tuning/run and GET /api/auto-tuning/swagger.yml.
Definition http_server.h:94
std::function< std::expected< void, std::string >(std::string driver, std::string adapter)> InitDeviceManagerFn
Callback type for POST /api/init.
Definition http_server.h:53
void addRoutes(mm::api::RegisterRoutesFn module)
Registers a route plug-in to be wired in when the server starts.
Definition http_server.cc:391
std::function< mm::node::EniCollectorOptions()> EniOptionsFn
Supplies the three things an ENI needs that no device can answer for: the master's name,...
Definition http_server.h:116
std::function< std::vector< std::string >()> GetLogFn
Callback type for GET /api/log.
Definition http_server.h:59
std::function< std::expected< void, std::string >()> RefreshCertFn
Callback type for POST /api/cert/refresh.
Definition http_server.h:67
void stop()
Stops the server, closes the listen socket, and joins the thread.
Definition http_server.cc:410
std::function< GameLoopHealth()> GetGameLoopHealthFn
Callback type for GET /api/game-loop.
Definition http_server.h:75
std::function< std::expected< mm::net::Response, std::string >()> AutoTuningSpecFn
Callback type for GET /api/auto-tuning/swagger.yml.
Definition http_server.h:107
bool start()
Starts the server background thread and begins accepting connections.
Definition http_server.cc:399
A flat, user-writable file store rooted at Motion Master's per-user cache directory.
Definition user_cache.h:65
Owns the fieldbus driver and node collection, and drives PDO exchange.
Definition device_manager.h:287
Owns the active monitorings and turns each into a lossless stream of recorded rows.
Definition monitoring_manager.h:62
Runs off-RT command-and-wait procedures and remembers how each one went.
Definition procedure_manager.h:121
A blocking HTTP client for the calls Motion Master makes out.
std::function< void(Router &router, const RouteContext &ctx)> RegisterRoutesFn
A route plug-in: registers its routes on the HTTP app.
Definition web_api.h:77
Definition http_server.h:25
Definition bus_health_source.h:7
Routes whose handlers cannot block the event loop, because they do not run on it.
What Motion Master knows about its auto-tuning process, as a client can read it.
The whole config file. Top-level keys map to these members.
Definition config.h:197
Snapshot of game-loop real-time health for the GET /api/game-loop diagnostic endpoint....
Definition game_loop.h:15
Server configuration.
Definition http_server.h:119
AutoTuningRunFn runAutoTuning
Handler for POST /api/auto-tuning/run; forwards a call.
Definition http_server.h:136
AutoTuningStatusFn autoTuningStatus
Handler for GET /api/auto-tuning; the startup snapshot.
Definition http_server.h:138
RefreshCertFn refreshCert
Handler for POST /api/cert/refresh; fetches+installs a cert.
Definition http_server.h:131
GetGameLoopHealthFn getGameLoopHealth
Handler for GET /api/game-loop; RT loop health snapshot.
Definition http_server.h:133
GetLogFn getLog
Handler for GET /api/log; returns buffered log entries.
Definition http_server.h:130
std::string startedConfig
Serialized JSON of the boot config; served at GET /api/config.
Definition http_server.h:127
std::string version
Application version string served at GET /api/version.
Definition http_server.h:125
AutoTuningSpecFn autoTuningSpec
Handler for GET /api/auto-tuning/swagger.yml.
Definition http_server.h:139
std::string bindAddress
Local address to bind. Loopback serves only this machine; "0.0.0.0" serves the network.
Definition http_server.h:121
InitDeviceManagerFn initDeviceManager
Handler for POST /api/init; required for API-driven init.
Definition http_server.h:129
uint16_t port
TCP port to listen on (TLS).
Definition http_server.h:122
std::string certFile
Path to the TLS certificate (PEM).
Definition http_server.h:123
EniOptionsFn eniOptions
Definition http_server.h:140
std::string keyFile
Path to the TLS private key (PEM).
Definition http_server.h:124
std::string corsOrigin
Value sent in Access-Control-Allow-Origin. Defaults to the production PWA origin.
Definition http_server.h:142
SetGameLoopPeriodFn setGameLoopPeriod
Handler for PUT /api/game-loop; retimes the RT loop.
Definition http_server.h:135
The state of the auto-tuning process, reported by GET /api/auto-tuning.
Definition status.h:22
What an ENI needs that no device can answer for.
Definition eni_collector.h:19