coral
A C++ library for distributed co-simulation
reqrep.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_NET_REQREP_HPP
11 #define CORAL_NET_REQREP_HPP
12 
13 #include <chrono>
14 #include <cstdint>
15 #include <map>
16 #include <memory>
17 #include <stdexcept>
18 #include <string>
19 #include <system_error>
20 #include <unordered_map>
21 
22 #include "zmq.hpp"
23 
24 #include "coral/config.h"
25 #include "coral/net/reactor.hpp"
26 #include "coral/net/zmqx.hpp"
27 #include "coral/net.hpp"
28 
29 
30 namespace coral
31 {
32 namespace net
33 {
34 
36 namespace reqrep
37 {
38 
39 
49 class Client
50 {
51 public:
60  Client(
61  coral::net::Reactor& reactor,
62  const std::string& protocolIdentifier,
63  const coral::net::Endpoint& serverEndpoint);
64 
65  // This class is not copyable or movable because it leaks its `this`
66  // pointer to the reactor.
67  Client(const Client&) = delete;
68  Client(Client&&) = delete;
69  Client& operator=(const Client&) = delete;
70  Client& operator=(Client&&) = delete;
71 
72  ~Client() CORAL_NOEXCEPT;
73 
98  typedef std::function<void(
99  const std::error_code& ec,
100  const char* replyHeader, size_t replyHeaderSize,
101  const char* replyBody, size_t replyBodySize)>
103 
144  void Request(
145  std::uint16_t protocolVersion,
146  const char* requestHeader, size_t requestHeaderSize,
147  const char* requestBody, size_t requestBodySize,
149  ReplyHandler onComplete);
150 
165  typedef std::function<void(
166  const std::error_code& ec,
167  std::uint16_t version)>
169 
194  void RequestMaxProtocol(
196  MaxProtocolReplyHandler onComplete);
197 
198 private:
199  void SendRequest(
200  const std::string& protocolIdentifier, std::uint16_t protocolVersion,
201  const char* requestHeader, size_t requestHeaderSize,
202  const char* requestBody, size_t requestBodySize,
203  std::chrono::milliseconds timeout);
204 
205  void ReceiveReply();
206 
207  void CompleteWithError(const std::error_code& ec);
208 
209  void SetTimer(std::chrono::milliseconds timeout);
210 
211  void CancelTimer();
212 
213  coral::net::Reactor& m_reactor;
214  std::string m_protocolIdentifier;
215  coral::net::Endpoint m_serverEndpoint;
217 
218  int m_timeoutTimerID;
219  std::uint16_t m_requestProtocolVersion;
220  ReplyHandler m_onComplete;
221  MaxProtocolReplyHandler m_onMaxProtocolComplete;
222 };
223 
224 
239 {
240 public:
294  virtual bool HandleRequest(
295  const std::string& protocolIdentifier,
296  std::uint16_t protocolVersion,
297  const char* requestHeader, size_t requestHeaderSize,
298  const char* requestBody, size_t requestBodySize,
299  const char*& replyHeader, size_t& replyHeaderSize,
300  const char*& replyBody, size_t& replyBodySize) = 0;
301 
302  virtual ~ServerProtocolHandler() = default;
303 };
304 
305 
325 class Server
326 {
327 public:
333  Server(
334  coral::net::Reactor& reactor,
335  const coral::net::Endpoint& endpoint);
336 
337  ~Server() CORAL_NOEXCEPT;
338 
339  Server(const Server&) = delete;
340  Server& operator=(const Server&) = delete;
341 
342  Server(Server&&) CORAL_NOEXCEPT;
343  Server& operator=(Server&&) CORAL_NOEXCEPT;
344 
352  void AddProtocolHandler(
353  const std::string& protocolIdentifier,
354  std::uint16_t protocolVersion,
356 
369  coral::net::Endpoint BoundEndpoint() const;
370 
371 private:
372  class Private;
373  std::unique_ptr<Private> m_private;
374 };
375 
376 
377 }}} // namespace
378 #endif
Contains the coral::net::Reactor class and related functionality.
std::function< void(const std::error_code &ec, std::uint16_t version)> MaxProtocolReplyHandler
A callback type for RequestMaxProtocol().
Definition: reqrep.hpp:168
Main module header for coral::net.
STL class.
An interface for classes that implement the server side of request-reply protocols, to be used with Server.
Definition: reqrep.hpp:238
Module header for coral::net::zmqx.
STL class.
A backend class for clients that communicate with a Server.
Definition: reqrep.hpp:49
A client socket for communication with a single server node.
Definition: zmqx.hpp:153
A protocol/transport independent endpoint address specification.
Definition: net.hpp:34
Definition: variable_io.hpp:28
void RequestMaxProtocol(std::chrono::milliseconds timeout, MaxProtocolReplyHandler onComplete)
Sends a "meta request" to the server asking for the maximum protocol version it supports.
A generic server class for simple request-reply protocols.
Definition: reqrep.hpp:325
void Request(std::uint16_t protocolVersion, const char *requestHeader, size_t requestHeaderSize, const char *requestBody, size_t requestBodySize, std::chrono::milliseconds timeout, ReplyHandler onComplete)
Sends a request.
std::function< void(const std::error_code &ec, const char *replyHeader, size_t replyHeaderSize, const char *replyBody, size_t replyBodySize)> ReplyHandler
A callback type for Request().
Definition: reqrep.hpp:102
An implementation of the reactor pattern.
Definition: reactor.hpp:41
Client(coral::net::Reactor &reactor, const std::string &protocolIdentifier, const coral::net::Endpoint &serverEndpoint)
Constructs a new client instance connected to the given endpoint, and registers it with the given rea...