coral
A C++ library for distributed co-simulation
slave_control_messenger_v0.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_BUS_SLAVE_CONTROL_MESSENGER_V0_HPP
11 #define CORAL_BUS_SLAVE_CONTROL_MESSENGER_V0_HPP
12 
13 #include <chrono>
14 #include <memory>
15 
16 #include "coral/config.h"
19 #include "coral/model.hpp"
20 #include "coral/net.hpp"
21 #include "coral/net/reactor.hpp"
22 #include "coral/net/zmqx.hpp"
23 
24 #include "boost/variant.hpp"
25 
26 
27 // Forward declaration to avoid header dependency
28 namespace google { namespace protobuf { class MessageLite; } }
29 
30 
31 namespace coral
32 {
33 namespace bus
34 {
35 
36 
42 {
43 public:
45  coral::net::Reactor& reactor,
47  coral::model::SlaveID slaveID,
48  const std::string& slaveName,
49  const SlaveSetup& setup,
52 
53  ~SlaveControlMessengerV0() CORAL_NOEXCEPT;
54 
55  SlaveState State() const CORAL_NOEXCEPT override;
56 
57  void Close() override;
58 
59  void GetDescription(
61  GetDescriptionHandler onComplete) override;
62 
63  void SetVariables(
66  SetVariablesHandler onComplete) override;
67 
68  void SetPeers(
71  SetPeersHandler onComplete) override;
72 
73  void ResendVars(
75  ResendVarsHandler onComplete) override;
76 
77  void Step(
78  coral::model::StepID stepID,
79  coral::model::TimePoint currentT,
82  StepHandler onComplete) override;
83 
84  void AcceptStep(
86  AcceptStepHandler onComplete) override;
87 
88  void Terminate() override;
89 
90 private:
91  typedef boost::variant<VoidHandler, GetDescriptionHandler> AnyHandler;
92 
93  void Setup(
94  coral::model::SlaveID slaveID,
95  const std::string& slaveName,
96  const SlaveSetup& setup,
98  VoidHandler onComplete);
99 
100  // Helper functions
101  void Reset();
102  void SendCommand(
103  int command,
104  const google::protobuf::MessageLite* data,
106  AnyHandler onComplete);
107  void PostSendCommand(
108  int command,
110  AnyHandler onComplete);
111  void RegisterTimeout(std::chrono::milliseconds timeout);
112  void UnregisterTimeout();
113 
114  // Event handlers
115  void OnReply();
116  void OnReplyTimeout();
117 
118  // Reply parsing/handling
119  void SetupReplyReceived(
120  const std::vector<zmq::message_t>& msg,
121  VoidHandler onComplete);
122  void DescribeReplyReceived(
123  const std::vector<zmq::message_t>& msg,
124  GetDescriptionHandler onComplete);
125  void SetPeersReplyReceived(
126  const std::vector<zmq::message_t>& msg,
127  VoidHandler onComplete);
128  void SetVarsReplyReceived(
129  const std::vector<zmq::message_t>& msg,
130  VoidHandler onComplete);
131  void ResendVarsReplyReceived(
132  const std::vector<zmq::message_t>& msg,
133  VoidHandler onComplete);
134  void StepReplyReceived(
135  const std::vector<zmq::message_t>& msg,
136  VoidHandler onComplete);
137  void AcceptStepReplyReceived(
138  const std::vector<zmq::message_t>& msg,
139  VoidHandler onComplete);
140 
141  // These guys perform the work which is common to several of the above
142  // XyxReplyReceived() functions.
143  void HandleExpectedReadyReply(
144  const std::vector<zmq::message_t>& msg,
145  VoidHandler onComplete);
146  void HandleErrorReply(int reply, AnyHandler onComplete);
147 
148  // Class invariant checker
149  void CheckInvariant() const;
150 
151  coral::net::Reactor& m_reactor;
153 
154  // State information
155  SlaveState m_state;
156  bool m_attachedToReactor;
157  int m_currentCommand;
158  AnyHandler m_onComplete;
159  int m_replyTimeoutTimerId;
160 };
161 
162 
163 }} // namespace
164 #endif // header guard
Defines the coral::bus::SlaveSetup class.
Contains the coral::net::Reactor class and related functionality.
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
Module header for coral::net::zmqx.
Defines the coral::bus::ISlaveControlMessenger interface and the two related functions ConnectToSlave...
STL class.
A client socket for communication with a single server node.
Definition: zmqx.hpp:153
Definition: variable_io.hpp:28
SlaveState
The various states a slave may be in.
Definition: slave_control_messenger.hpp:41
double TimeDuration
The type used to specify (simulation) time durations.
Definition: model.hpp:56
An implementation of ISlaveControlMessenger for version 0 of the master/slave communication protocol...
Definition: slave_control_messenger_v0.hpp:41
An interface for classes that implement various versions of the master/slave communication protocol...
Definition: slave_control_messenger.hpp:141
Definition: slave_control_messenger_v0.hpp:28
An implementation of the reactor pattern.
Definition: reactor.hpp:41
Main module header for coral::model.
double TimePoint
The type used to specify (simulation) time points.
Definition: model.hpp:42