42 virtual void execute()
override final;
74 control.comm().broadcast(
_value);
75 control.setControllableValueByName<T>(
name(),
_value);
108 const nlohmann::json &
getJSON()
const;
120 std::optional<nlohmann::json>
_json;
133 Response(
const unsigned int status_code);
137 Response(
const unsigned int status_code,
const nlohmann::json & json);
151 const nlohmann::json &
getJSON()
const;
160 const std::string &
getError()
const;
172 std::optional<nlohmann::json>
_json;
182 ErrorResponse(
const std::string & error,
const unsigned int status_code = 400);
248 template <RequestMethod method>
263 template <
class value_T,
class key_T>
264 static value_T
convertJSON(
const nlohmann::json & json_value,
const key_T & key);
351 const std::chrono::time_point<std::chrono::steady_clock> & start)
const;
359 const std::string & timeout_param_name,
360 const std::optional<std::string> & suffix = {})
const;
403template <
class value_T,
class key_T>
409 return json_value[key].template get<value_T>();
411 catch (
const std::exception & e)
413 std::ostringstream message;
414 message <<
"While parsing '" << key <<
"' " << e.what();
415 throw std::runtime_error(message.str());
Base class for Control objects.
const InputParameters & parameters() const
Get the parameters of the object.
The base class for a value that is produced by this registry.
const std::string & type() const
const std::string & name() const
Starts the web server(s) for the WebServerControl objects.
Class containing a value to be controlled.
T value_type
The underlying type of the value.
T _value
The underlying value.
ControlledValue(const std::string &name, const std::string &type)
virtual void setControllableValue(WebServerControl &control) override final
Sets the controllable value given the name and type via the controllable interface in control.
ControlledValue(const std::string &name, const std::string &type, const T &value)
Starts a webserver that an external process can connect to in order to send JSON messages to control ...
std::atomic< bool > _terminate_requested
Whether or not the solve should be terminated in the next execute() call.
const Real _client_timeout
Time in seconds to allow the client to communicate after init before timing out.
void addServerAction(const std::string &path, std::function< Response(const Request &, WebServerControl &)> &&action, const ServerActionOptions &options={})
Adds an action for the server to perform at the given path.
virtual void addServerActions()
Entrypoint for controls derived from this one to add additional actions.
void setClientInitialized()
Set that the client has called /initialized; used by the server.
void setKillRequested()
Set for the control to kill the solve; used by /kill in the server.
void addServerActionsInternal()
Adds the internal actions to the server.
virtual void execute() override final
Execute the control.
bool isKillRequested() const
Get whether or not the client sent the kill command.
static InputParameters validParams()
std::atomic< bool > _currently_waiting
Whether or not the Control is currently waiting.
std::weak_ptr< HttpServer > _server_weak_ptr
Weak pointer to the server; the server itself is owned by the server thread.
std::atomic< int64_t > _last_client_poke
The most recent time we've heard from the client.
bool isTerminateRequested() const
Whether or not the client has called /terminate.
std::string clientTimeoutErrorMessage(const Real timeout, const std::string &timeout_param_name, const std::optional< std::string > &suffix={}) const
Helper for producing an error message about a client timeout.
bool isCurrentlyWaiting() const
Get whether or not the control is currently waiting.
void clientPoke()
Store a client's poke, which is a timing used to determine the client timeout.
std::atomic< bool > _kill_requested
Whether or not the client has called /kill.
std::mutex _client_info_lock
Lock for _client_info as it is written by the server thread.
ClientInfo getClientInfo() const
Get the information sent by the client on initialize.
static value_T convertJSON(const nlohmann::json &json_value, const key_T &key)
Helper for converting a value to JSON for the given key.
RequestMethod
Define the valid methods for a client request.
const unsigned int *const _port
Port to listen on, if any.
const FileName *const _file_socket
File socket to listen on, if any.
std::unique_ptr< std::thread > _server_thread_ptr
The server thread.
std::vector< std::unique_ptr< ControlledValueBase > > _controlled_values
The values received to control; filled on rank 0 from the server and then broadcast.
std::atomic< bool > _client_initialized
Whether or not the client has called /initialize.
void setCurrentlyWaiting(const bool value=true)
Set that the control is currently waiting; used by the server.
void stopServer()
Stop the server if it exists and is running.
void setClientInfo(const ClientInfo &info)
Set the ClientInfo object received from the client during /initialize.
void startServer(const Moose::PassKey< StartWebServerControlAction >)
Start the server.
void outputMessage(const std::string &message) const
Output a message with the prefix of this control type and name.
bool isClientInitialized() const
Whether or not the client has called /initialize.
std::optional< ClientInfo > _client_info
Client information received on /initialize by the server.
const Real _initial_client_timeout
Time in seconds to allow the client to initially communicate before timing out.
std::mutex _controlled_values_mutex
Mutex to prevent threaded writes to _controlled_values.
void setTerminateRequested(const bool value=true)
Set for the control to terminate the solve; used by /terminate in the server.
void outputClientTiming(const std::string &message, const std::chrono::time_point< std::chrono::steady_clock > &start) const
Output a timing message with the prefix of this control.
Stores the information sent by the client on initialize.
std::string host
Client host.
nlohmann::json data
Raw data.
std::string user
Client user.
std::string name
Client name.
Represents an error response to the client from the server.
Represents a request from the client.
std::optional< nlohmann::json > _json
The underlying JSON data, if any.
void setJSON(const nlohmann::json &json, const Moose::PassKey< WebServerControl >)
Set the JSON data in the request.
const nlohmann::json & getJSON() const
Represents a response to the client from the server.
const std::string & getError() const
std::optional< std::string > _error
The error message, if any.
const nlohmann::json & getJSON() const
void setError(const std::string &error)
Set the error message.
unsigned int _status_code
The status code.
unsigned int getStatusCode() const
std::optional< nlohmann::json > _json
The JSON data, if any.
Options to be passed to addServerAction.
bool _require_waiting
Whether or not to require waiting.
void requireJSONKey(const std::string &key)
Append a key to be required in JSON in the request data.
void requireJSONKeys(std::initializer_list< std::string > &&keys)
Append keys to be required in JSON in the request data.
void setRequireInitialized(const bool value, const Moose::PassKey< WebServerControl >)
Set the require initialized flag; only accessible by the WebServerControl.
const std::set< std::string > getRequiredJSONKeys() const
std::set< std::string > _required_json_keys
JSON keys that are required in the data.
bool getRequireInitialized() const
bool _require_initialized
Whether or not to require initialization.
bool getRequireWaiting() const
void setRequireWaiting(const bool value, const Moose::PassKey< WebServerControl >)
Set the require waiting flag; only accessible by the WebServerControl.