coral
A C++ library for distributed co-simulation
|
A class which is used for controlling one slave in an execution. More...
#include "coral/bus/slave_controller.hpp"
Public Types | |
typedef std::function< void(const std::error_code &)> | VoidHandler |
Basic completion handler type without any arguments aside from an error code. | |
typedef VoidHandler | ConnectHandler |
Completion handler type for the constructor. | |
typedef std::function< void(const std::error_code &, const coral::model::SlaveDescription &)> | GetDescriptionHandler |
Completion handler type for GetDescription() | |
typedef VoidHandler | SetVariablesHandler |
Completion handler type for SetVariables() | |
typedef VoidHandler | SetPeersHandler |
Completion handler type for SetPeers() | |
typedef VoidHandler | ResendVarsHandler |
Completion handler type for ResendVars() | |
typedef VoidHandler | StepHandler |
Completion handler type for Step() | |
typedef VoidHandler | AcceptStepHandler |
Completion handler type for AcceptStep() | |
Public Member Functions | |
SlaveController (coral::net::Reactor &reactor, const coral::net::SlaveLocator &slaveLocator, coral::model::SlaveID slaveID, const std::string &slaveName, const SlaveSetup &setup, std::chrono::milliseconds timeout, ConnectHandler onComplete, int maxConnectionAttempts=3) | |
Constructor. More... | |
~SlaveController () | |
Destructor. More... | |
void | Close () |
Closes the connection to the slave and cancels all pending operations. More... | |
SlaveState | State () const CORAL_NOEXCEPT |
Returns the current state of the slave. | |
void | GetDescription (std::chrono::milliseconds timeout, GetDescriptionHandler onComplete) |
Requests a description of the slave. More... | |
void | SetVariables (const std::vector< coral::model::VariableSetting > &settings, std::chrono::milliseconds timeout, SetVariablesHandler onComplete) |
Sets the values of, or connects, one or more of the slave's variables. More... | |
void | SetPeers (const std::vector< coral::net::Endpoint > &peers, std::chrono::milliseconds timeout, SetPeersHandler onComplete) |
Sets or resets the list of peers to which the slave should be connected for the purpose of subscribing to variable data. More... | |
void | ResendVars (std::chrono::milliseconds timeout, ResendVarsHandler onComplete) |
Makes the slave send all variable values and then wait to receive values for all connected input variables. More... | |
void | Step (coral::model::StepID stepID, coral::model::TimePoint currentT, coral::model::TimeDuration deltaT, std::chrono::milliseconds timeout, StepHandler onComplete) |
Makes the slave perform a time step. More... | |
void | AcceptStep (std::chrono::milliseconds timeout, AcceptStepHandler onComplete) |
Tells the slave that the time step is accepted and it should update its inputs with results from other slaves. More... | |
void | Terminate () |
Terminates the slave and cancels all pending operations. More... | |
A class which is used for controlling one slave in an execution.
coral::bus::SlaveController::SlaveController | ( | coral::net::Reactor & | reactor, |
const coral::net::SlaveLocator & | slaveLocator, | ||
coral::model::SlaveID | slaveID, | ||
const std::string & | slaveName, | ||
const SlaveSetup & | setup, | ||
std::chrono::milliseconds | timeout, | ||
ConnectHandler | onComplete, | ||
int | maxConnectionAttempts = 3 |
||
) |
Constructor.
The constructor initiates the connection to the slave and returns immediately. Whether the connection succeeds or fails is reported asynchronously via onComplete
. In the meantime, it is possible to enqueue other commands (e.g. SetVariables()) pending a successful connection. If the connection fails, any pending commands will be canceled and their callbacks will be called with error code coral::error::generic_error::canceled
.
If the initial connection fails (e.g. if the slave is not up and running yet), the connection may be retried automatically. The maximum number of connection attempts is given by maxConnectionAttempts
.
[in] | reactor | The reactor used for the messaging/event loop. |
[in] | slaveLocator | Information about how to connect to the slave. |
[in] | slaveID | The ID number assigned to the slave. |
[in] | slaveName | The name given to the slave. |
[in] | setup | Slave configuration parameters. |
[in] | timeout | Max. allowed time for the slave to reply to each message sent to it. |
[in] | onComplete | Completion handler. |
[in] | maxConnectionAttempts | How many times to try the connection if it fails. This includes the first one, so the value must be at least 1. The default is 3. |
std::invalid_argument | if slaveLocator is empty, if slaveID is invalid, if timeout is less than 1 ms, if onComplete is empty, or if maxConnectionAttempts < 1 . |
coral::bus::SlaveController::~SlaveController | ( | ) |
Destructor.
Note that the destructor does not call Terminate() or Close(). The completion handlers for any ongoing or pending operations will therefore never get called. Use Terminate() or Close() before destruction to ensure that all callbacks are called.
void coral::bus::SlaveController::AcceptStep | ( | std::chrono::milliseconds | timeout, |
AcceptStepHandler | onComplete | ||
) |
Tells the slave that the time step is accepted and it should update its inputs with results from other slaves.
[in] | timeout | Max. allowed time for the operation to complete. Must be at least 1 ms. |
[in] | onComplete | Completion handler. |
void coral::bus::SlaveController::Close | ( | ) |
Closes the connection to the slave and cancels all pending operations.
After this, the SlaveController can no longer be used for anything.
Note that this simply closes the communication channel without notifying the slave first. This means that the slave may stay alive, waiting for a command from the master which never comes, until it times out and shuts itself down. Use Terminate() to ensure a controlled slave termination.
void coral::bus::SlaveController::GetDescription | ( | std::chrono::milliseconds | timeout, |
GetDescriptionHandler | onComplete | ||
) |
Requests a description of the slave.
[in] | timeout | Max. allowed time for the operation to complete. Must be at least 1 ms. |
[in] | onComplete | Completion handler. May not be empty. |
void coral::bus::SlaveController::ResendVars | ( | std::chrono::milliseconds | timeout, |
ResendVarsHandler | onComplete | ||
) |
Makes the slave send all variable values and then wait to receive values for all connected input variables.
[in] | timeout | Max. allowed time for the operation to complete. Must be at least 1 ms. |
[in] | onComplete | Completion handler. |
std::invalid_argument | if timeout is less than 1 ms or if onComplete is empty. |
State() == SLAVE_READY
State() == SLAVE_BUSY
. void coral::bus::SlaveController::SetPeers | ( | const std::vector< coral::net::Endpoint > & | peers, |
std::chrono::milliseconds | timeout, | ||
SetPeersHandler | onComplete | ||
) |
Sets or resets the list of peers to which the slave should be connected for the purpose of subscribing to variable data.
[in] | peers | A list of peer endpoint specifications. |
[in] | timeout | Max. allowed time for the operation to complete. Must be at least 1 ms. |
[in] | onComplete | Completion handler. |
void coral::bus::SlaveController::SetVariables | ( | const std::vector< coral::model::VariableSetting > & | settings, |
std::chrono::milliseconds | timeout, | ||
SetVariablesHandler | onComplete | ||
) |
Sets the values of, or connects, one or more of the slave's variables.
[in] | settings | A list of variable values and connections. May not be empty. |
[in] | timeout | Max. allowed time for the operation to complete. Must be at least 1 ms. |
[in] | onComplete | Completion handler. |
void coral::bus::SlaveController::Step | ( | coral::model::StepID | stepID, |
coral::model::TimePoint | currentT, | ||
coral::model::TimeDuration | deltaT, | ||
std::chrono::milliseconds | timeout, | ||
StepHandler | onComplete | ||
) |
Makes the slave perform a time step.
[in] | stepID | The ID number of the time step to be performed |
[in] | currentT | The current time point. |
[in] | deltaT | The step size. Must be positive. |
[in] | timeout | Max. allowed time for the operation to complete. Must be at least 1 ms. |
[in] | onComplete | Completion handler. |
void coral::bus::SlaveController::Terminate | ( | ) |
Terminates the slave and cancels all pending operations.
After this, the SlaveController can no longer be used for anything.