coral
A C++ library for distributed co-simulation
execution_state.hpp
Go to the documentation of this file.
1 
11 #ifndef CORAL_BUS_EXECUTION_STATE_HPP
12 #define CORAL_BUS_EXECUTION_STATE_HPP
13 
14 // For the sake of maintainability, we can skip the headers which are already
15 // included by execution_manager.hpp, and which are only needed here because
16 // ExecutionState duplicates ExecutionManager's method signatures.
18 #include "coral/config.h"
19 #include "coral/error.hpp"
20 
21 
22 namespace coral
23 {
24 namespace bus
25 {
26 
27 
36 {
37 public:
38  virtual void StateEntered(
40  { }
41 
42  virtual void Reconstitute(
44  const std::vector<AddedSlave>& slavesToAdd,
45  std::chrono::milliseconds commTimeout,
48  { NotAllowed(__FUNCTION__); }
49 
50  virtual void Reconfigure(
52  const std::vector<SlaveConfig>& slaveConfigs,
53  std::chrono::milliseconds commTimeout,
56  { NotAllowed(__FUNCTION__); }
57 
58  virtual void ResendVars(
60  int maxAttempts,
61  std::chrono::milliseconds commTimeout,
62  std::function<void(const std::error_code&)> onComplete)
63  { NotAllowed(__FUNCTION__); }
64 
65  virtual void Step(
70  ExecutionManager::SlaveStepHandler onSlaveStepComplete)
71  { NotAllowed(__FUNCTION__); }
72 
73  virtual void AcceptStep(
77  ExecutionManager::SlaveAcceptStepHandler onSlaveAcceptStepComplete)
78  { NotAllowed(__FUNCTION__); }
79 
80  virtual void Terminate(ExecutionManagerPrivate& self)
81  { NotAllowed(__FUNCTION__); }
82 
83  virtual ~ExecutionState() CORAL_NOEXCEPT { }
84 
85 private:
86  CORAL_NORETURN void NotAllowed(const std::string& func) const
87  {
89  func + ": Method call not allowed in present state");
90  }
91 };
92 
93 
94 class ReadyExecutionState : public ExecutionState
95 {
96  void Reconstitute(
98  const std::vector<AddedSlave>& slavesToAdd,
99  std::chrono::milliseconds commTimeout,
101  ExecutionManager::SlaveReconstituteHandler onSlaveComplete) override;
102 
103  void Reconfigure(
105  const std::vector<SlaveConfig>& slaveConfigs,
106  std::chrono::milliseconds commTimeout,
108  ExecutionManager::SlaveReconstituteHandler onSlaveComplete) override;
109 
110  void ResendVars(
112  int maxAttempts,
113  std::chrono::milliseconds commTimeout,
114  std::function<void(const std::error_code&)> onComplete) override;
115 
116  void Step(
121  ExecutionManager::SlaveStepHandler onSlaveStepComplete) override;
122 
123  void Terminate(ExecutionManagerPrivate& self) override;
124 };
125 
126 
127 class ReconstitutingExecutionState : public ExecutionState
128 {
129 public:
130  ReconstitutingExecutionState(
131  const std::vector<AddedSlave>& slavesToAdd,
132  std::chrono::milliseconds commTimeout,
135 
136 private:
137  void StateEntered(ExecutionManagerPrivate& self) override;
138  void AllSlavesAdded(ExecutionManagerPrivate& self);
139  void Completed(ExecutionManagerPrivate& self);
140  void Failed(ExecutionManagerPrivate& self);
141 
142  // Input parameters to this state
143  const std::vector<AddedSlave> m_slavesToAdd;
144  const std::chrono::milliseconds m_commTimeout;
145  const ExecutionManager::ReconstituteHandler m_onComplete;
146  const ExecutionManager::SlaveReconstituteHandler m_onSlaveComplete;
147 
148  // Local variables of this state
150 };
151 
152 
153 class ReconfiguringExecutionState : public ExecutionState
154 {
155 public:
156  ReconfiguringExecutionState(
157  const std::vector<SlaveConfig>& slaveConfigs,
158  std::chrono::milliseconds commTimeout,
161 
162 private:
163  void StateEntered(ExecutionManagerPrivate& self) override;
164 
165  // Input parameters to this state
166  const std::vector<SlaveConfig> m_slaveConfigs;
167  const std::chrono::milliseconds m_commTimeout;
168  const ExecutionManager::ReconstituteHandler m_onComplete;
169  const ExecutionManager::SlaveReconstituteHandler m_onSlaveComplete;
170 };
171 
172 
173 class PrimingExecutionState: public ExecutionState
174 {
175 public:
176  PrimingExecutionState(
177  int maxAttempts,
178  std::chrono::milliseconds commTimeout,
179  std::function<void(const std::error_code&)> onComplete);
180 
181 private:
182  void StateEntered(ExecutionManagerPrivate& self) override;
183  void Failed(ExecutionManagerPrivate& self);
184 
185  void Try(ExecutionManagerPrivate& self, int attemptsLeft);
186  void Fail(ExecutionManagerPrivate& self, const std::error_code& ec);
187  void Succeed(ExecutionManagerPrivate& self);
188 
189  // Input parameters to this state
190  const int m_maxAttempts;
191  const std::chrono::milliseconds m_commTimeout;
193 };
194 
195 
196 class SteppingExecutionState : public ExecutionState
197 {
198 public:
199  SteppingExecutionState(
203  ExecutionManager::SlaveStepHandler onSlaveStepComplete);
204 
205 private:
206  void StateEntered(ExecutionManagerPrivate& self) override;
207 
208  const coral::model::TimeDuration m_stepSize;
209  std::chrono::milliseconds m_timeout;
210  ExecutionManager::StepHandler m_onComplete;
211  ExecutionManager::SlaveStepHandler m_onSlaveStepComplete;
212 };
213 
214 
215 class StepOkExecutionState : public ExecutionState
216 {
217 public:
218  StepOkExecutionState(coral::model::TimeDuration stepSize);
219 
220 private:
221  void Terminate(ExecutionManagerPrivate& self) override;
222 
223  void AcceptStep(
227  ExecutionManager::SlaveAcceptStepHandler onSlaveAcceptStepComplete)
228  override;
229 
230  const coral::model::TimeDuration m_stepSize;
231 };
232 
233 
234 class AcceptingExecutionState : public ExecutionState
235 {
236 public:
237  AcceptingExecutionState(
240  ExecutionManager::SlaveAcceptStepHandler onSlaveAcceptStepComplete);
241 
242 private:
243  void StateEntered(ExecutionManagerPrivate& self) override;
244 
245  std::chrono::milliseconds m_timeout;
247  ExecutionManager::SlaveAcceptStepHandler m_onSlaveAcceptStepComplete;
248 };
249 
250 
251 class StepFailedExecutionState : public ExecutionState
252 {
253  void Terminate(ExecutionManagerPrivate& self) override;
254 };
255 
256 
257 class FatalErrorExecutionState : public ExecutionState
258 {
259  void Terminate(ExecutionManagerPrivate& self) override;
260 };
261 
262 
263 class TerminatedExecutionState : public ExecutionState
264 {
265  void Terminate(ExecutionManagerPrivate& self) override;
266 };
267 
268 
269 }} // namespace
270 #endif // header guard
The superclass of all classes that represent execution states.
Definition: execution_state.hpp:35
STL class.
STL class.
Defines the coral::bus::ExecutionManager class.
Definition: variable_io.hpp:28
An exception which is used to signal that one or more of a function&#39;s preconditions were not met...
Definition: error.hpp:122
double TimeDuration
The type used to specify (simulation) time durations.
Definition: model.hpp:56
Main header file for coral::error.
STL class.
Implementation class for coral::bus::ExecutionManager.
Definition: execution_manager_private.hpp:50