coral
A C++ library for distributed co-simulation
slave_controller.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_BUS_SLAVE_CONTROLLER_HPP
11 #define CORAL_BUS_SLAVE_CONTROLLER_HPP
12 
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 #include <system_error>
17 #include <vector>
18 
19 #include "coral/config.h"
22 #include "coral/net/reactor.hpp"
23 #include "coral/model.hpp"
24 #include "coral/net.hpp"
25 
26 
27 namespace coral
28 {
29 namespace bus
30 {
31 
32 
35 {
36 public:
42 
43 
45  typedef VoidHandler ConnectHandler;
46 
85  coral::net::Reactor& reactor,
86  const coral::net::SlaveLocator& slaveLocator,
87  coral::model::SlaveID slaveID,
88  const std::string& slaveName,
89  const SlaveSetup& setup,
91  ConnectHandler onComplete,
92  int maxConnectionAttempts = 3);
93 
103 
115  void Close();
116 
118  SlaveState State() const CORAL_NOEXCEPT;
119 
121  typedef std::function<void(const std::error_code&, const coral::model::SlaveDescription&)>
123 
132  void GetDescription(
133  std::chrono::milliseconds timeout,
134  GetDescriptionHandler onComplete);
135 
137  typedef VoidHandler SetVariablesHandler;
138 
150  void SetVariables(
151  const std::vector<coral::model::VariableSetting>& settings,
152  std::chrono::milliseconds timeout,
153  SetVariablesHandler onComplete);
154 
156  typedef VoidHandler SetPeersHandler;
157 
169  void SetPeers(
170  const std::vector<coral::net::Endpoint>& peers,
171  std::chrono::milliseconds timeout,
172  SetPeersHandler onComplete);
173 
175  typedef VoidHandler ResendVarsHandler;
176 
192  void ResendVars(
193  std::chrono::milliseconds timeout,
194  ResendVarsHandler onComplete);
195 
197  typedef VoidHandler StepHandler;
198 
213  void Step(
214  coral::model::StepID stepID,
215  coral::model::TimePoint currentT,
216  coral::model::TimeDuration deltaT,
217  std::chrono::milliseconds timeout,
218  StepHandler onComplete);
219 
221  typedef VoidHandler AcceptStepHandler;
222 
232  void AcceptStep(
233  std::chrono::milliseconds timeout,
234  AcceptStepHandler onComplete);
235 
241  void Terminate();
242 
243 private:
244  // Make this class non-movable, since we leak pointers to 'this' in lambda
245  // functions passed to SlaveControlMessenger.
247  SlaveController& operator=(SlaveController&&);
248 
249  // A handle for the pending connection.
250  PendingSlaveControlConnection m_pendingConnection;
251 
252  // The object through which we communicate with the slave.
253  std::unique_ptr<coral::bus::ISlaveControlMessenger> m_messenger;
254 };
255 
256 
257 }} // namespace
258 #endif // header guard
Defines the coral::bus::SlaveSetup class.
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 subscribin...
A class which is used for controlling one slave in an execution.
Definition: slave_controller.hpp:34
Contains the coral::net::Reactor class and related functionality.
A handle for a pending connection to a slave.
Definition: slave_control_messenger.hpp:63
Main module header for coral::net.
STL namespace.
~SlaveController()
Destructor.
void Close()
Closes the connection to the slave and cancels all pending operations.
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 othe...
Configuration data which is sent to each slave as they are added to the simulation.
Definition: slave_setup.hpp:27
VoidHandler ConnectHandler
Completion handler type for the constructor.
Definition: slave_controller.hpp:45
Defines the coral::bus::ISlaveControlMessenger interface and the two related functions ConnectToSlave...
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 vari...
STL class.
std::function< void(const std::error_code &)> VoidHandler
Basic completion handler type without any arguments aside from an error code.
Definition: slave_controller.hpp:41
std::int32_t StepID
A number that uniquely identifies a time step in an execution.
Definition: model.hpp:34
void Terminate()
Terminates the slave and cancels all pending operations.
Definition: variable_io.hpp:28
SlaveState
The various states a slave may be in.
Definition: slave_control_messenger.hpp:41
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.
double TimeDuration
The type used to specify (simulation) time durations.
Definition: model.hpp:56
An interface for classes that implement various versions of the master/slave communication protocol...
Definition: slave_control_messenger.hpp:141
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.
An implementation of the reactor pattern.
Definition: reactor.hpp:41
Class which represents the network location(s) of a slave.
Definition: net.hpp:292
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&#39;s variables.
Main module header for coral::model.
double TimePoint
The type used to specify (simulation) time points.
Definition: model.hpp:42
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.