ifw  0.0.1-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MsgHandler.hpp
Go to the documentation of this file.
1 
9 #ifndef RAD_MSG_HANDLER_HPP
10 #define RAD_MSG_HANDLER_HPP
11 
12 #include <rad/Logger.hpp>
13 #include <rad/Exceptions.hpp>
14 
15 #include <memory>
16 #include <string>
17 #include <unordered_map>
18 
19 namespace rad {
20 
24 class MsgHandler {
25 public:
26  virtual ~MsgHandler() {}
37  virtual void handle(const std::string& identity,
38  const std::string& msgTypeId,
39  const void* pData,
40  size_t dataSize) = 0;
41 };
42 
51 template<typename EventType>
52 EventType ParseRequestAndCreateEvent(const std::string& identity, const std::string& requestName, const void* pMsg, size_t msgSize)
53 {
54  RAD_LOG_TRACE();
55 
56  // Check that requestId matches the event)
57 #if 0
58  if (strcmp(requestName.c_str(), EventType::id) != 0) {
59  std::string errMsg("Error requestName <" + requestName +
60  "> payload type, does not match eventId <" + EventType::id + ">.");
61  RAD_LOG_ERROR() << errMsg;
62  throw rad::Exception(errMsg);
63  }
64 #endif
65 
66  // EventType = rad::MsgRequest<ProtoBuf_Type>
67  // msg_payload_t = ProtoBuf_Type
68  using msg_payload_t = typename EventType::payload_t::payload_t;
69  msg_payload_t reqPayload;
70 
71  if (reqPayload.ParseFromArray(pMsg, msgSize) == false) {
72  std::string errMsg("Error parsing <" + requestName + "> payload.");
73  RAD_LOG_ERROR() << errMsg;
74  throw rad::Exception(errMsg);
75  }
76 
77  return EventType(identity.c_str(), reqPayload);
78 }
79 
80 
81 
82 
83 } // namespace rad
84 
85 #endif /* #ifndef RAD_MSG_HANDLER_HPP */
#define RAD_LOG_ERROR()
Definition: Logger.hpp:266
Definition: MsgHandler.hpp:24
EventType ParseRequestAndCreateEvent(const std::string &identity, const std::string &requestName, const void *pMsg, size_t msgSize)
Definition: MsgHandler.hpp:52
string id
Definition: requests.proto:15
Definition: Exceptions.hpp:44
virtual ~MsgHandler()
Definition: MsgHandler.hpp:26
#define RAD_LOG_TRACE()
Definition: Logger.hpp:319
virtual void handle(const std::string &identity, const std::string &msgTypeId, const void *pData, size_t dataSize)=0