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;
403 template <
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());
std::optional< nlohmann::json > _json
The JSON data, if any.
void setTerminateRequested(const bool value=true)
Set for the control to terminate the solve; used by /terminate in the server.
bool isTerminateRequested() const
Whether or not the client has called /terminate.
void setError(const std::string &error)
Set the error message.
std::atomic< bool > _currently_waiting
Whether or not the Control is currently waiting.
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.
void setClientInfo(const ClientInfo &info)
Set the ClientInfo object received from the client during /initialize.
const std::set< std::string > getRequiredJSONKeys() const
bool isClientInitialized() const
Whether or not the client has called /initialize.
void setRequireWaiting(const bool value, const Moose::PassKey< WebServerControl >)
Set the require waiting flag; only accessible by the WebServerControl.
void addServerActionsInternal()
Adds the internal actions to the server.
Starts a webserver that an external process can connect to in order to send JSON messages to control ...
ClientInfo getClientInfo() const
Get the information sent by the client on initialize.
bool getRequireWaiting() const
ErrorResponse(const std::string &error, const unsigned int status_code=400)
virtual void addServerActions()
Entrypoint for controls derived from this one to add additional actions.
std::set< std::string > _required_json_keys
JSON keys that are required in the data.
const Real _initial_client_timeout
Time in seconds to allow the client to initially communicate before timing out.
void setKillRequested()
Set for the control to kill the solve; used by /kill in the server.
const InputParameters & parameters() const
Get the parameters of the object.
void setCurrentlyWaiting(const bool value=true)
Set that the control is currently waiting; used by the server.
std::optional< ClientInfo > _client_info
Client information received on /initialize by the server.
void setClientInitialized()
Set that the client has called /initialized; used by the server.
void requireJSONKeys(std::initializer_list< std::string > &&keys)
Append keys to be required in JSON in the request data.
std::atomic< bool > _kill_requested
Whether or not the client has called /kill.
Represents an error response to the client from the server.
WebServerControl(const InputParameters ¶meters)
bool _require_initialized
Whether or not to require initialization.
const FileName *const _file_socket
File socket to listen on, if any.
virtual void setControllableValue(WebServerControl &control) override final
Sets the controllable value given the name and type via the controllable interface in control...
void startServer(const Moose::PassKey< StartWebServerControlAction >)
Start the server.
void requireJSONKey(const std::string &key)
Append a key to be required in JSON in the request data.
std::mutex _client_info_lock
Lock for _client_info as it is written by the server thread.
bool isKillRequested() const
Get whether or not the client sent the kill command.
const std::string & type() const
void outputMessage(const std::string &message) const
Output a message with the prefix of this control type and name.
std::optional< nlohmann::json > _json
The underlying JSON data, if any.
std::vector< std::unique_ptr< ControlledValueBase > > _controlled_values
The values received to control; filled on rank 0 from the server and then broadcast.
const nlohmann::json & getJSON() const
const unsigned int *const _port
Port to listen on, if any.
std::optional< std::string > _error
The error message, if any.
void setRequireInitialized(const bool value, const Moose::PassKey< WebServerControl >)
Set the require initialized flag; only accessible by the WebServerControl.
std::string name
Client name.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
T _value
The underlying value.
std::atomic< int64_t > _last_client_poke
The most recent time we've heard from the client.
Options to be passed to addServerAction.
bool getRequireInitialized() const
const std::string & getError() const
virtual void execute() override final
Execute the control.
std::mutex _controlled_values_mutex
Mutex to prevent threaded writes to _controlled_values.
ControlledValue(const std::string &name, const std::string &type, const T &value)
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.
void clientPoke()
Store a client's poke, which is a timing used to determine the client timeout.
std::unique_ptr< std::thread > _server_thread_ptr
The server thread.
bool _require_waiting
Whether or not to require waiting.
The base class for a value that is produced by this registry.
const Real _client_timeout
Time in seconds to allow the client to communicate after init before timing out.
std::weak_ptr< HttpServer > _server_weak_ptr
Weak pointer to the server; the server itself is owned by the server thread.
std::atomic< bool > _terminate_requested
Whether or not the solve should be terminated in the next execute() call.
std::string host
Client host.
nlohmann::json data
Raw data.
ControlledValue(const std::string &name, const std::string &type)
Class containing a value to be controlled.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::string user
Client user.
void setJSON(const nlohmann::json &json, const Moose::PassKey< WebServerControl >)
Set the JSON data in the request.
Base class for Control objects.
T value_type
The underlying type of the value.
bool isCurrentlyWaiting() const
Get whether or not the control is currently waiting.
Stores the information sent by the client on initialize.
Represents a response to the client from the server.
Starts the web server(s) for the WebServerControl objects.
const nlohmann::json & getJSON() const
unsigned int _status_code
The status code.
Represents a request from the client.
static InputParameters validParams()
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.
unsigned int getStatusCode() const
static value_T convertJSON(const nlohmann::json &json_value, const key_T &key)
Helper for converting a value to JSON for the given key.
void stopServer()
Stop the server if it exists and is running.
std::atomic< bool > _client_initialized
Whether or not the client has called /initialize.
RequestMethod
Define the valid methods for a client request.
const std::string & name() const