coral
A C++ library for distributed co-simulation
Public Types | Public Member Functions | List of all members
coral::bus::SlaveController Class Reference

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...
 

Detailed Description

A class which is used for controlling one slave in an execution.

Constructor & Destructor Documentation

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.

Parameters
[in]reactorThe reactor used for the messaging/event loop.
[in]slaveLocatorInformation about how to connect to the slave.
[in]slaveIDThe ID number assigned to the slave.
[in]slaveNameThe name given to the slave.
[in]setupSlave configuration parameters.
[in]timeoutMax. allowed time for the slave to reply to each message sent to it.
[in]onCompleteCompletion handler.
[in]maxConnectionAttemptsHow 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.
Exceptions
std::invalid_argumentif 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.

Member Function Documentation

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.

Parameters
[in]timeoutMax. allowed time for the operation to complete. Must be at least 1 ms.
[in]onCompleteCompletion 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.

Parameters
[in]timeoutMax. allowed time for the operation to complete. Must be at least 1 ms.
[in]onCompleteCompletion 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.

Parameters
[in]timeoutMax. allowed time for the operation to complete. Must be at least 1 ms.
[in]onCompleteCompletion handler.
Exceptions
std::invalid_argumentif timeout is less than 1 ms or if onComplete is empty.
Precondition
State() == SLAVE_READY
Postcondition
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.

Parameters
[in]peersA list of peer endpoint specifications.
[in]timeoutMax. allowed time for the operation to complete. Must be at least 1 ms.
[in]onCompleteCompletion 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.

Parameters
[in]settingsA list of variable values and connections. May not be empty.
[in]timeoutMax. allowed time for the operation to complete. Must be at least 1 ms.
[in]onCompleteCompletion 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.

Parameters
[in]stepIDThe ID number of the time step to be performed
[in]currentTThe current time point.
[in]deltaTThe step size. Must be positive.
[in]timeoutMax. allowed time for the operation to complete. Must be at least 1 ms.
[in]onCompleteCompletion 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.


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