coral
A C++ library for distributed co-simulation
execution_manager_private.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_BUS_EXECUTION_MANAGER_PRIVATE_HPP
11 #define CORAL_BUS_EXECUTION_MANAGER_PRIVATE_HPP
12 
13 // For the sake of maintainability, we can skip the headers which are already
14 // included by execution_manager.hpp, and which are only needed here because
15 // ExecutionManagerPrivate duplicates ExecutionManager's method signatures.
16 #include <functional>
17 #include <map>
18 #include <memory>
19 #include <system_error>
20 
21 #include "boost/noncopyable.hpp"
22 
23 #include "coral/config.h"
24 #include "coral/model.hpp"
25 #include "coral/net.hpp"
26 
30 
31 
32 namespace coral
33 {
34 namespace bus
35 {
36 
37 class ExecutionState;
38 
39 
50 class ExecutionManagerPrivate : boost::noncopyable
51 {
52 public:
54  coral::net::Reactor& reactor,
55  const std::string& executionName,
56  const coral::master::ExecutionOptions& options);
57 
59 
60  // External methods, i.e. those that forward to state-specific objects.
61  // =========================================================================
62  void Reconstitute(
63  const std::vector<AddedSlave>& slavesToAdd,
64  std::chrono::milliseconds commTimeout,
67 
68  void Reconfigure(
69  const std::vector<SlaveConfig>& slaveConfigs,
70  std::chrono::milliseconds commTimeout,
73 
74  void Step(
78  ExecutionManager::SlaveStepHandler onSlaveStepComplete);
79 
80  void AcceptStep(
83  ExecutionManager::SlaveAcceptStepHandler onSlaveAcceptStepComplete);
84 
85  void Terminate();
86 
87  // Internal methods, i.e. those that are used by the state-specific objects.
88  // =========================================================================
89 
90  // Performs the termination routine.
91  // Terminatable states should simply forward their Terminate() method to
92  // this function, possibly after doing state-specific cleanup.
93  // This function will enter the TERMINATED state before its return, so
94  // the calling state object (which will now be deleted) should not use its
95  // member variables afterwards.
96  void DoTerminate();
97 
98  // Functions for retrieving and updating the current simulation time and ID.
99  coral::model::StepID NextStepID();
100  coral::model::TimePoint CurrentSimTime() const;
101  void AdvanceSimTime(coral::model::TimeDuration delta);
102 
103  // To be called when a per-slave operation has started and completed,
104  // respectively.
105  void SlaveOpStarted() CORAL_NOEXCEPT;
106  void SlaveOpComplete();
107 
109 
110  /*
111  Specifies an action to take when all ongoing per-slave operations are
112  complete.
113 
114  If no per-slave operations are currently in progress, `handler` is called
115  immediately. If there are operations in progress, `handler` will be stored
116  for later and called when they are all complete or when the execution enters
117  a different state. In the latter case, the handler will be called with
118  error code coral::error::generic_error::aborted. In other words, the handler
119  will only be called while the current state object is the active one.
120 
121  Once a completion handler has been set, this function may not be called
122  again until the handler has been called.
123 
124  `handler` may not throw.
125  */
126  void WhenAllSlaveOpsComplete(AllSlaveOpsCompleteHandler handler);
127 
128  /*
129  Switches to another state, and returns the current state object (for when
130  the object needs to be kept alive a little bit more).
131  */
133 
134  struct Slave
135  {
136  Slave(
138  coral::net::SlaveLocator locator,
139  const coral::model::SlaveDescription& description);
140 
141  Slave(const Slave&) = delete;
142  Slave& operator=(const Slave&) = delete;
143 
144  CORAL_DEFINE_DEFAULT_MOVE(Slave, slave, locator, description)
145 
147  coral::net::SlaveLocator locator;
148  coral::model::SlaveDescription description;
149  };
150 
151  // Data which is available to the state objects
152  coral::net::Reactor& reactor;
153  coral::bus::SlaveSetup slaveSetup;
154  coral::model::SlaveID lastSlaveID;
156 
157 private:
158  // Make class nonmovable in addition to noncopyable, because we leak
159  // pointers to it in lambda functions.
162 
163  // Performs the actual aborting of the "wait for all slave ops" thingy
164  void AbortSlaveOpWaiting() CORAL_NOEXCEPT;
165 
166  // An object that represents, and performs the actions for, the current
167  // execution state.
169 
170  // How many per-slave operations are currently in progress.
171  int m_operationCount;
172 
173  // An action to take when all per-slave operations complete.
174  // This is reset on every state change.
175  AllSlaveOpsCompleteHandler m_allSlaveOpsCompleteHandler;
176 
177  // The ID of the time step currently in progress or just completed.
178  coral::model::StepID m_currentStepID;
179 
180  // Whether a RESEND_VARS is needed before the next STEP.
181  bool m_resendVarsNeeded;
182 };
183 
184 
185 }} // namespace
186 #endif // header guard
Defines the coral::bus::SlaveSetup class.
Main module header for coral::net.
Configuration data which is sent to each slave as they are added to the simulation.
Definition: slave_setup.hpp:27
Defines the coral::bus::SlaveController class.
STL class.
Defines the coral::bus::ExecutionManager class.
Definition: variable_io.hpp:28
double TimeDuration
The type used to specify (simulation) time durations.
Definition: model.hpp:56
STL class.
STL class.
A description of a specific slave.
Definition: model.hpp:215
An implementation of the reactor pattern.
Definition: reactor.hpp:41
Implementation class for coral::bus::ExecutionManager.
Definition: execution_manager_private.hpp:50
Class which represents the network location(s) of a slave.
Definition: net.hpp:292
Main module header for coral::model.
double TimePoint
The type used to specify (simulation) time points.
Definition: model.hpp:42