hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxInputJsonMessage.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 <Poco/Base64Encoder.h>
10 #include <Poco/Base64Decoder.h>
11 #include <Poco/StreamCopier.h>
12 
13 #include "SphinxError.hpp"
15 
19 
20 namespace HCE
21 {
22 namespace sphinx
23 {
24 //-----------------------------------------------------------------------------
26 :inherited(), messageType(SphinxInputJsonMessage::MessageType::mtSearch), messageBody(""), messageTTL(input_json_message_const::defaultTTLValue)
27 {
28  if (!json.empty())
29  unserialize(json);
30 }
31 //-----------------------------------------------------------------------------
33 :inherited(), messageType(messageType_), messageBody(""), messageTTL(input_json_message_const::defaultTTLValue)
34 {
35 }
36 //-----------------------------------------------------------------------------
37 bool SphinxInputJsonMessage::serialize(std::string& json)
38 {
39  _isError = false;
40  errorMsg.clear();
41  try
42  {
43  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
44  obj->set(input_json_message_const::messageType, static_cast<unsigned int>(messageType));
45  obj->set(input_json_message_const::messageBody, messageBody);
46  obj->set(input_json_message_const::messageTTL, messageTTL);
47 
48  std::stringstream ostr;
49  Poco::JSON::Stringifier::stringify(obj, ostr);
50  json = ostr.str();
51  }
52  catch(std::exception& e)
53  {
54  errorMsg = e.what();
56  _isError = true;
57  }
58  return !_isError;
59 }
60 //-----------------------------------------------------------------------------
61 bool SphinxInputJsonMessage::unserialize(const std::string& json)
62 {
63  messageType = SphinxInputJsonMessage::MessageType::mtSearch;
65  errorMsg.clear();
66  _isError = false;
67  try
68  {
69  Poco::JSON::Parser parser;
70  Poco::Dynamic::Var res = parser.parse(json);
71  Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
72 
73  Poco::Dynamic::Var tmp = obj->get(input_json_message_const::messageType);
74  messageType = static_cast<MessageType>(convertVarToNumeric<unsigned long>(tmp, static_cast<unsigned long>(messageType)));
75 
77  if (tmp.isString())
78  messageBody = tmp.convert<std::string>();
79 
80  if (messageBody.empty())
81  throw Poco::Exception("Invalid body. Empty message.");
82 
84  messageTTL = convertVarToNumeric<unsigned long>(tmp, messageTTL);
85  }
86  catch(Poco::JSON::JSONException& e)
87  {
88  errorMsg = e.message();
90  _isError = true;
91  }
92  catch(Poco::Exception& e)
93  {
94  errorMsg = e.message();
96  _isError = true;
97  }
98  catch(std::exception& e)
99  {
100  errorMsg = e.what();
102  _isError = true;
103  }
104  return !_isError;
105 }
106 //-----------------------------------------------------------------------------
107 std::istream& operator>>(std::istream& is, SphinxInputJsonMessage& jsonHandler)
108 {
109  std::string json;
110  is.seekg(0, std::ios::end);
111  json.resize(is.tellg());
112  is.seekg(0, std::ios::beg);
113  is.read(const_cast<char*>(json.c_str()), json.size());
114  jsonHandler.unserialize(json);
115  return is;
116 }
117 //-----------------------------------------------------------------------------
118 std::ostream& operator<<(std::ostream& os, const SphinxInputJsonMessage& jsonHandler)
119 {
120  std::string json;
121  if (const_cast<SphinxInputJsonMessage&>(jsonHandler).serialize(json))
122  os << json;
123  return os;
124 }
125 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
127 } // end namespace sphinx
128 } // end namespace HCE