hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
JsonMessageCover.cpp
Go to the documentation of this file.
1 #include <Poco/JSON/Object.h>
2 #include <Poco/JSON/Array.h>
3 #include <Poco/JSON/JSON.h>
4 #include <Poco/JSON/Stringifier.h>
5 #include <Poco/JSON/Parser.h>
6 #include <Poco/JSON/JSONException.h>
7 #include <Poco/Dynamic/Var.h>
8 
9 #include "EncodeDecodeBase64.hpp"
10 #include "JsonMessageCover.hpp"
11 
12 namespace HCE
13 {
14 //-----------------------------------------------------------------------------
15 JsonMessageCover::JsonMessageCover(const std::string& json)
16 :inherited(), messageType(types::MessageType::mtSphinx), messageBody(""),
17  messageTTL(jsonMessageCoverConst::defaultTTLValue)
18 {
19  if (!json.empty())
20  unserialize(json);
21 }
22 //-----------------------------------------------------------------------------
23 JsonMessageCover::JsonMessageCover(HCE::types::MessageType messageType_, const std::string& messageBody_, unsigned int ttl_)
24 :inherited(), messageType(messageType_), messageBody(messageBody_),
25  messageTTL(ttl_)
26 {
27 }
28 //-----------------------------------------------------------------------------
29 JsonMessageCover::JsonMessageCover(unsigned int messageType_, const std::string& messageBody_, unsigned int ttl_)
30 :inherited(), messageType(static_cast<types::MessageType>(messageType_)), messageBody(messageBody_),
31  messageTTL(ttl_)
32 {
33 }
34 //-----------------------------------------------------------------------------
35 bool JsonMessageCover::serialize(std::string& json)
36 {
37  _isError = false;
38  errorMsg.clear();
40  try
41  {
42  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
43  obj->set(jsonMessageCoverConst::messageType, static_cast<unsigned int>(messageType));
45  obj->set(jsonMessageCoverConst::messageTTL, messageTTL);
46 
47  std::stringstream ostr;
48  Poco::JSON::Stringifier::stringify(obj, ostr);
49  json = ostr.str();
50  }
51  catch(std::exception& e)
52  {
53  errorMsg = e.what();
55  _isError = true;
56  }
57  return !_isError;
58 }
59 //-----------------------------------------------------------------------------
60 bool JsonMessageCover::unserialize(const std::string& json)
61 {
62  _isError = false;
63  errorMsg.clear();
65  messageType = types::MessageType::mtSphinx;
67 
68  try
69  {
70  Poco::JSON::Parser parser;
71  Poco::Dynamic::Var res = parser.parse(json);
72  Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
73 
74  Poco::Dynamic::Var tmp = obj->get(jsonMessageCoverConst::messageType);
75  messageType = static_cast<types::MessageType>(convertVarToNumeric<unsigned long>(tmp, static_cast<unsigned long>(messageType)));
76 
77  tmp = obj->get(jsonMessageCoverConst::messageBody);
78  if (tmp.isString())
79  messageBody = HCE::decodeBase64(tmp.convert<std::string>());
80 
81  if (messageBody.empty())
82  throw Poco::Exception("Invalid body. Empty message.");
83 
84  tmp = obj->get(jsonMessageCoverConst::messageTTL);
85  messageTTL = convertVarToNumeric<unsigned long>(tmp, messageTTL);
86  }
87  catch(Poco::JSON::JSONException& e)
88  {
89  errorMsg = e.message();
91  _isError = true;
92  }
93  catch(Poco::Exception& e)
94  {
95  errorMsg = e.message();
97  _isError = true;
98  }
99  catch(std::exception& e)
100  {
101  errorMsg = e.what();
103  _isError = true;
104  }
105  return !_isError;
106 }
107 //-----------------------------------------------------------------------------
108 std::istream& operator>>(std::istream& is, JsonMessageCover& jsonHandler)
109 {
110  std::string json;
111  is.seekg(0, std::ios::end);
112  json.resize(is.tellg());
113  is.seekg(0, std::ios::beg);
114  is.read(const_cast<char*>(json.c_str()), json.size());
115  jsonHandler.unserialize(json);
116  return is;
117 }
118 //-----------------------------------------------------------------------------
119 std::ostream& operator<<(std::ostream& os, const JsonMessageCover& jsonHandler)
120 {
121  std::string json;
122  const_cast<HCE::JsonMessageCover&>(jsonHandler).serialize(json);
123  return os << json;
124 }
125 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
127 } // end namespace HCE