hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEInputJsonMessage.cpp
Go to the documentation of this file.
1 #include <Poco/JSON/JSON.h>
2 #include <Poco/JSON/Stringifier.h>
3 #include <Poco/JSON/Parser.h>
4 #include <Poco/JSON/JSONException.h>
5 #include <Poco/Dynamic/Var.h>
6 #include <utility>
7 #include <limits.h>
8 
9 #include "DRCEError.hpp"
10 #include "DRCEMessageConst.hpp"
11 #include "DRCEInputJsonMessage.hpp"
14 
15 namespace HCE
16 {
17 namespace drce
18 {
19 //-----------------------------------------------------------------------------
21 : IJsonSerializable(), Subtasks(), requestType(DRCETaskRequest::RequestType::rtSetTaskExecute), requestData(""), requestId(0), pTaskRequest(nullptr)
22 {
23  if (!json.empty())
24  unserialize(json);
25 }
26 //-----------------------------------------------------------------------------
28 : IJsonSerializable(), Subtasks(), requestType(DRCETaskRequest::RequestType::rtSetTaskExecute), requestData(""), requestId(0), pTaskRequest(nullptr)
29 {
30  (*this) = rhs;
31 }
32 //-----------------------------------------------------------------------------
34 : IJsonSerializable(), Subtasks(), requestType(DRCETaskRequest::RequestType::rtSetTaskExecute), requestData(""), requestId(0), pTaskRequest(nullptr)
35 {
36  (*this) = std::forward<DRCEInputJsonMessage>(rhs);
37 }
38 //-----------------------------------------------------------------------------
40 {
41  if (this != &rhs)
42  {
43  requestType = rhs.getRequestType();
44  requestData = rhs.getRequestData();
45  requestId = rhs.getRequestId();
46  pTaskRequest = rhs.getTaskRequest();
47  (*dynamic_cast<Subtasks*>(this)) = static_cast<Subtasks>(rhs);
48  }
49  return *this;
50 }
51 //-----------------------------------------------------------------------------
53 {
54  if (this != &rhs)
55  {
56  requestType = std::move(rhs.getRequestType());
57  requestData = std::move(rhs.getRequestData());
58  requestId = std::move(rhs.getRequestId());
59  pTaskRequest = std::move(rhs.getTaskRequest());
60  (*dynamic_cast<Subtasks*>(this)) = std::move(static_cast<Subtasks>(rhs));
61  }
62  return *this;
63 }
64 //-----------------------------------------------------------------------------
65 void DRCEInputJsonMessage::setRequestData(const std::string& requestData_)
66 {
67  requestData = requestData_;
68  if (!requestData.empty())
69  {
70  DRCETaskRequestFactory taskRequestFactory;
71  pTaskRequest = taskRequestFactory.create(requestType);
72  if (!pTaskRequest.isNull())
73  {
74  if (!pTaskRequest->unserialize(requestData))
75  {
76  _isError = pTaskRequest->isError();
77  errorMsg = pTaskRequest->getErrorMsg();
78  errorCode = pTaskRequest->getErrorCode();
79  }
80  }
81  }
82 }
83 //-----------------------------------------------------------------------------
84 // cppcheck-suppress unusedFunction
85 void DRCEInputJsonMessage::setTaskRequest(Poco::SharedPtr<DRCETaskRequest> pTaskRequest_)
86 {
87  pTaskRequest = pTaskRequest_;
88  if (!pTaskRequest.isNull())
89  {
90  requestType = pTaskRequest->getRequestType();
91  if (!pTaskRequest->serialize(requestData))
92  {
93  _isError = pTaskRequest->isError();
94  errorMsg = pTaskRequest->getErrorMsg();
95  errorCode = pTaskRequest->getErrorCode();
96  }
97  }
98 }
99 //-----------------------------------------------------------------------------
101 {
102  requestType = DRCETaskRequest::RequestType::rtSetTaskExecute;
103  requestData.clear();
104  requestId = 0;
105  pTaskRequest = nullptr;
106  Subtasks::clear();
107 }
108 //-----------------------------------------------------------------------------
109 void DRCEInputJsonMessage::packToObject(Poco::JSON::Object& obj) throw (Poco::Exception)
110 {
111  obj.set(drce_json_message_const::requestType, static_cast<unsigned int>(requestType));
114 
115  Poco::JSON::Array::Ptr pSubtasks = new Poco::JSON::Array();
116  if (!pSubtasks.isNull())
117  {
118  packToArray(*pSubtasks);
119  obj.set(drce_json_message_const::subtasksArray, pSubtasks);
120  }
121 }
122 //-----------------------------------------------------------------------------
123 void DRCEInputJsonMessage::packToArray(Poco::JSON::Array& arr) throw (Poco::Exception)
124 {
125  for (size_t i=0;i<getSubtasksCount();++i)
126  {
127  Poco::JSON::Object::Ptr pObj = new Poco::JSON::Object();
128  if (!pObj.isNull())
129  {
130  DRCEInputJsonMessage inputJsonMessage = getSubtaskItem(i);
131  inputJsonMessage.packToObject(*pObj);
132  arr.add(*pObj);
133  }
134  }
135 }
136 //-----------------------------------------------------------------------------
137 void DRCEInputJsonMessage::unpackFromObject(Poco::JSON::Object& obj) throw (Poco::Exception)
138 {
139  Poco::Dynamic::Var tmp = obj.get(drce_json_message_const::requestType);
140  if (tmp.isEmpty())
142 
143  requestType = static_cast<DRCETaskRequest::RequestType>(convertVarToNumeric<unsigned int>(tmp, 0));
144 
146  if (tmp.isEmpty())
148 
149  if (tmp.isString())
150  setRequestData(DRCEDecodeBase64(tmp.convert<std::string>()));
151 
152  if (isError())
153  throw Poco::Exception(errorMsg, errorCode);
154 
155  if (pTaskRequest.isNull())
157 
158  tmp = obj.get(drce_json_message_const::requestId);
159  if (tmp.isEmpty())
161 
162  long long taskId = convertVarToNumeric<long long>(tmp, 0);
163  if (taskId > std::numeric_limits<unsigned int>::max() || taskId < 0)
164  throw Poco::Exception(message_const::dataNotRange+" { 0, "+std::to_string(std::numeric_limits<unsigned int>::max())+" }", ERROR_TASK_ID_NOT_RANGE);
165  requestId = static_cast<unsigned int>(taskId);
166 
167  Poco::JSON::Array::Ptr pSubtasks = obj.getArray(drce_json_message_const::subtasksArray);
168  if (!pSubtasks.isNull())
169  {
170  unpackFromArray(*pSubtasks);
171  }
172 }
173 //-----------------------------------------------------------------------------
174 void DRCEInputJsonMessage::unpackFromArray(Poco::JSON::Array& arr) throw (Poco::Exception)
175 {
176  for (size_t i=0;i<arr.size();++i)
177  {
178  Poco::JSON::Object::Ptr pObj = arr.getObject(i);
179  if (!pObj.isNull())
180  {
181  DRCEInputJsonMessage inputJsonMessage;
182  inputJsonMessage.unpackFromObject(*pObj);
183  addSubtaskItem(std::forward<DRCEInputJsonMessage>(inputJsonMessage));
184  }
185  }
186 }
187 //-----------------------------------------------------------------------------
188 bool DRCEInputJsonMessage::serialize(std::string& json)
189 {
191  errorMsg.clear();
192  try
193  {
194  Poco::JSON::Object::Ptr pObj = new Poco::JSON::Object();
195  if (pObj.isNull())
196  throw Poco::Exception(message_const::objectInstanceWasNotCreated);
197 
198  packToObject(*pObj);
199 
200  std::stringstream ostr;
201  Poco::JSON::Stringifier::stringify(pObj, ostr);
202  json = ostr.str();
203 
204  _isError = false;
205  }
206  catch(Poco::Exception& e)
207  {
208  _isError = true;
209  errorMsg = e.message();
210  errorCode = e.code();
211  }
212  catch(std::exception& e)
213  {
214  _isError = true;
215  errorMsg = e.what();
217  }
218  return !_isError;
219 }
220 //-----------------------------------------------------------------------------
221 bool DRCEInputJsonMessage::unserialize(const std::string& json)
222 {
223  clear();
224  _isError = false;
226  errorMsg.clear();
227 
228  try
229  {
230  Poco::JSON::Parser parser;
231  Poco::Dynamic::Var res = parser.parse(json);
232  Poco::JSON::Object::Ptr pObj = res.extract<Poco::JSON::Object::Ptr>();
233  if (!pObj.isNull())
234  {
235  unpackFromObject(*pObj);
236  _isError = false;
237  }
238  }
239  catch(Poco::JSON::JSONException& e)
240  {
241  _isError = true;
242  errorMsg = e.message();
244  }
245  catch(Poco::Exception& e)
246  {
247  _isError = true;
248  errorMsg = e.message();
249  errorCode = e.code();
250  }
251  catch(std::exception& e)
252  {
253  _isError = true;
254  errorMsg = e.what();
256  }
257  return !_isError;
258 }
259 //-----------------------------------------------------------------------------
260 std::istream& operator>>(std::istream& is, DRCEInputJsonMessage& inputJsonMessage)
261 {
262  std::string json;
263  is.seekg(0, std::ios::end);
264  json.resize(is.tellg());
265  is.seekg(0, std::ios::beg);
266  is.read(const_cast<char*>(json.c_str()), json.size());
267  inputJsonMessage.unserialize(json);
268  return is;
269 }
270 //-----------------------------------------------------------------------------
271 std::ostream& operator<<(std::ostream& os, const DRCEInputJsonMessage& inputJsonMessage)
272 {
273  std::string json;
274  const_cast<DRCEInputJsonMessage&>(inputJsonMessage).serialize(json);
275  return os << json;
276 }
277 //-----------------------------------------------------------------------------
278 //-----------------------------------------------------------------------------
279 } // end namespace drce
280 } // end namespace HCE