coral
A C++ library for distributed co-simulation
reactor.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_NET_REACTOR_HPP
11 #define CORAL_NET_REACTOR_HPP
12 
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 #include <vector>
17 #include <utility>
18 
19 #include "zmq.hpp"
20 #include "coral/config.h"
21 
22 
23 namespace coral
24 {
25 namespace net
26 {
27 
28 
41 class Reactor
42 {
43 public:
44 #ifdef _WIN32
45  typedef SOCKET NativeSocket;
46 #else
47  typedef int NativeSocket;
48 #endif
49 
50  typedef std::chrono::system_clock::time_point TimePoint;
54 
55  Reactor();
56 
58  void AddSocket(zmq::socket_t& socket, SocketHandler handler);
59 
70  void RemoveSocket(zmq::socket_t& socket) CORAL_NOEXCEPT;
71 
73  void AddNativeSocket(NativeSocket socket, NativeSocketHandler handler);
74 
85  void RemoveNativeSocket(NativeSocket socket) CORAL_NOEXCEPT;
86 
88  static const int invalidTimerID;
89 
105  int AddTimer(
106  std::chrono::milliseconds interval,
107  int count,
108  TimerHandler handler);
109 
114  void RemoveTimer(int id);
115 
124  void RestartTimerInterval(int id);
125 
137  void Run();
138 
145  void Stop();
146 
147 private:
148  struct Timer
149  {
150  Timer(
151  int id,
152  TimePoint nextEventTime,
153  std::chrono::milliseconds interval,
154  int remaining,
156 
157  CORAL_DEFINE_DEFAULT_MOVE(Timer, id, nextEventTime, interval, remaining, handler)
158 
159  int id;
160  TimePoint nextEventTime;
161  std::chrono::milliseconds interval;
162  int remaining;
163  std::unique_ptr<TimerHandler> handler;
164  };
165 
166  void RestartTimerIntervals(
167  std::vector<Timer>::iterator begin,
168  std::vector<Timer>::iterator end);
169  std::chrono::milliseconds TimeToNextEvent() const;
170  void PerformNextEvent();
171 
172  // Rebuilds the list of poll items.
173  void Rebuild();
174 
175  typedef std::pair<zmq::socket_t*, std::unique_ptr<SocketHandler>> SocketHandlerPair;
176  typedef std::pair<NativeSocket, std::unique_ptr<NativeSocketHandler>> NativeSocketHandlerPair;
177  std::vector<SocketHandlerPair> m_sockets;
178  std::vector<NativeSocketHandlerPair> m_nativeSockets;
179  std::vector<zmq::pollitem_t> m_pollItems;
180 
181  int m_nextTimerID;
182  std::vector<Timer> m_timers;
183 
184  bool m_needsRebuild;
185  bool m_continuePolling;
186 };
187 
188 
189 }} // namespace
190 #endif // header guard
void RestartTimerInterval(int id)
Resets the time to the next event for a timer.
STL namespace.
int AddTimer(std::chrono::milliseconds interval, int count, TimerHandler handler)
Adds a timer.
void Stop()
Stops the messaging loop.
void AddSocket(zmq::socket_t &socket, SocketHandler handler)
Adds a handler for the given socket.
static const int invalidTimerID
A number which will never be returned by AddTimer().
Definition: reactor.hpp:88
void RemoveSocket(zmq::socket_t &socket) CORAL_NOEXCEPT
Removes all handlers for the given socket.
Definition: variable_io.hpp:28
void RemoveTimer(int id)
Removes a timer.
STL class.
void Run()
Runs the messaging loop.
void AddNativeSocket(NativeSocket socket, NativeSocketHandler handler)
Adds a handler for the given native socket.
An implementation of the reactor pattern.
Definition: reactor.hpp:41
Definition: variable_io.hpp:25
void RemoveNativeSocket(NativeSocket socket) CORAL_NOEXCEPT
Removes all handlers for the given native socket.