hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEResultDataSerializator.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 
8 #include "DRCEError.hpp"
9 #include "DRCEMessageConst.hpp"
11 
12 namespace HCE
13 {
14 namespace drce
15 {
16 //-----------------------------------------------------------------------------
18 : inherited(), resultData(resultData_)
19 {
20 }
21 //-----------------------------------------------------------------------------
22 void DRCEResultDataSerializator::packToObject(Poco::JSON::Object& obj, DRCEResultDataItem& resultDataItem) throw (Poco::Exception)
23 {
24  obj.set(drce_json_message_const::errorCode, resultDataItem.getErrorCode());
25  obj.set(drce_json_message_const::errorMessage, resultDataItem.getErrorMessage());
26  obj.set(drce_json_message_const::requestId, resultDataItem.getRequestId());
27  obj.set(drce_json_message_const::requestType, static_cast<unsigned int>(resultDataItem.getRequestType()));
28  obj.set(drce_json_message_const::nodeHost, resultDataItem.getNodeHost());
29  obj.set(drce_json_message_const::nodePort, resultDataItem.getNodePort());
30  obj.set(drce_json_message_const::state, resultDataItem.getState());
31  obj.set(drce_json_message_const::pid, resultDataItem.getPid());
32  obj.set(drce_json_message_const::stdoutStream, DRCEEncodeBase64(resultDataItem.getStdoutStream()));
33  obj.set(drce_json_message_const::stderrStream, DRCEEncodeBase64(resultDataItem.getStderrStream()));
34  obj.set(drce_json_message_const::nodeName, resultDataItem.getNodeName());
35  obj.set(drce_json_message_const::time, resultDataItem.getTime());
36  obj.set(drce_json_message_const::exitStatus, resultDataItem.getExitStatus());
37 
38  Poco::JSON::Object::Ptr pFields = new Poco::JSON::Object();
39  if (!pFields.isNull())
40  {
41  for (std::map<std::string, std::string>::const_iterator iter=resultDataItem.getFields().begin();iter!=resultDataItem.getFields().end();++iter)
42  {
43  pFields->set((*iter).first, (*iter).second);
44  }
45  obj.set(drce_json_message_const::fields, pFields);
46  }
47 
48  Poco::JSON::Array::Ptr pFiles = new Poco::JSON::Array();
49  if (!pFiles.isNull())
50  {
51  const size_t filesCount = resultDataItem.getFilesCount();
52  for (size_t k=0;k<filesCount;++k)
53  {
54  Poco::JSON::Object::Ptr pFileElem = new Poco::JSON::Object();
55  if (!pFileElem.isNull())
56  {
57  pFileElem->set(drce_json_message_const::fileName, resultDataItem.getFileItem(k).name);
58  pFileElem->set(drce_json_message_const::fileData, resultDataItem.getFileItem(k).data);
59  pFileElem->set(drce_json_message_const::actionTypeMask, resultDataItem.getFileItem(k).actionType);
60  pFiles->add(pFileElem);
61  }
62  }
63  obj.set(drce_json_message_const::filesArray, pFiles);
64  }
65 
66  Poco::JSON::Array::Ptr pSubtasks = new Poco::JSON::Array();
67  if (!pSubtasks.isNull())
68  {
69  packToArray(*pSubtasks, resultDataItem);
70  obj.set(drce_json_message_const::subtasksArray, pSubtasks);
71  }
72 }
73 //-----------------------------------------------------------------------------
74 void DRCEResultDataSerializator::packToArray(Poco::JSON::Array& arr, DRCEResultDataItem& resultDataItem) throw (Poco::Exception)
75 {
76  for (size_t i=0;i<resultDataItem.getSubtasksCount();++i)
77  {
78  Poco::JSON::Object::Ptr pObj = new Poco::JSON::Object();
79  if (!pObj.isNull())
80  {
81  DRCEResultDataItem resultItem(resultDataItem.getSubtaskItem(i));
82  packToObject(*pObj, resultItem);
83  arr.add(*pObj);
84  }
85  }
86 }
87 //-----------------------------------------------------------------------------
88 void DRCEResultDataSerializator::unpackFromObject(Poco::JSON::Object& obj, DRCEResultDataItem& resultDataItem) throw (Poco::Exception)
89 {
90  Poco::Dynamic::Var tmp = obj.get(drce_json_message_const::errorCode);
91  resultDataItem.setErrorCode(convertVarToNumeric<unsigned int>(tmp, 0));
92 
94  if (tmp.isString())
95  resultDataItem.setErrorMessage(tmp.convert<std::string>());
96 
98  resultDataItem.setRequestId(convertVarToNumeric<unsigned int>(tmp, 0));
99 
101  resultDataItem.setRequestType(static_cast<DRCETaskRequest::RequestType>(convertVarToNumeric<unsigned int>(tmp, 0)));
102 
103  tmp = obj.get(drce_json_message_const::nodeHost);
104  if (tmp.isString())
105  resultDataItem.setNodeHost(tmp.convert<std::string>());
106 
107  tmp = obj.get(drce_json_message_const::nodePort);
108  if (tmp.isString())
109  resultDataItem.setNodePort(tmp.convert<std::string>());
110 
111  tmp = obj.get(drce_json_message_const::state);
112  resultDataItem.setState(convertVarToNumeric<unsigned int>(tmp, 0));
113 
114  tmp = obj.get(drce_json_message_const::pid);
115  resultDataItem.setPid(convertVarToNumeric<unsigned int>(tmp, 0));
116 
118  if (tmp.isString())
119  resultDataItem.setStdoutStream(DRCEDecodeBase64(tmp.convert<std::string>()));
120 
122  if (tmp.isString())
123  resultDataItem.setStderrStream(DRCEDecodeBase64(tmp.convert<std::string>()));
124 
125  tmp = obj.get(drce_json_message_const::nodeName);
126  if (tmp.isString())
127  resultDataItem.setNodeName(tmp.convert<std::string>());
128 
129  tmp = obj.get(drce_json_message_const::time);
130  resultDataItem.setTime(convertVarToNumeric<unsigned int>(tmp, 0));
131 
133  resultDataItem.setExitStatus(convertVarToNumeric<unsigned int>(tmp, 0));
134 
135  Poco::JSON::Object::Ptr pFields = obj.getObject(drce_json_message_const::fields);
136  if (!pFields.isNull())
137  {
138  std::map<std::string, std::string> fields;
139  std::vector<std::string> names;
140  pFields->getNames(names);
141  for (size_t i=0;i<names.size();++i)
142  {
143  tmp = pFields->get(names[i]);
144  if (tmp.isString())
145  fields[names[i]] = tmp.convert<std::string>();
146  }
147  resultDataItem.setFields(fields);
148  }
149 
150  Poco::JSON::Array::Ptr pFiles = obj.getArray(drce_json_message_const::filesArray);
151  if (!pFiles.isNull())
152  {
153  for (size_t k=0;k<pFiles->size();++k)
154  {
155  Poco::JSON::Object::Ptr pFileElem = pFiles->getObject(k);
156  if (!pFileElem.isNull())
157  {
158  FileItem fileItem;
159 
160  tmp = pFileElem->get(drce_json_message_const::fileName);
161  if (tmp.isString())
162  fileItem.name = tmp.convert<std::string>();
163 
164  tmp = pFileElem->get(drce_json_message_const::fileData);
165  if (tmp.isString())
166  fileItem.data = tmp.convert<std::string>();
167 
168  tmp = pFileElem->get(drce_json_message_const::actionTypeMask);
169  fileItem.actionType = convertVarToNumeric<unsigned int>(tmp, 0);
170 
171  resultDataItem.addFileItem(std::forward<FileItem>(fileItem));
172  }
173  }
174  }
175 
176  Poco::JSON::Array::Ptr pSubtasks = obj.getArray(drce_json_message_const::subtasksArray);
177  if (!pSubtasks.isNull())
178  {
179  unpackFromArray(*pSubtasks, resultDataItem);
180  }
181 }
182 //-----------------------------------------------------------------------------
183 void DRCEResultDataSerializator::unpackFromArray(Poco::JSON::Array& arr, DRCEResultDataItem& resultDataItem) throw (Poco::Exception)
184 {
185  for (size_t i=0;i<arr.size();++i)
186  {
187  Poco::JSON::Object::Ptr pObj = arr.getObject(i);
188  if (!pObj.isNull())
189  {
190  DRCEResultDataItem resultItem;
191  unpackFromObject(*pObj, resultItem);
192  resultDataItem.addSubtaskItem(resultItem);
193  }
194  }
195 }
196 //-----------------------------------------------------------------------------
198 {
199  _isError = false;
201  errorMsg.clear();
202  try
203  {
204  Poco::JSON::Array::Ptr pItems = new Poco::JSON::Array();
205  if (pItems.isNull())
206  throw Poco::Exception(message_const::objectInstanceWasNotCreated);
207 
208  for (size_t i=0;i<resultData.getItemsCount();++i)
209  {
210  Poco::JSON::Object::Ptr pItemElem = new Poco::JSON::Object();
211 
212  if (!pItemElem.isNull())
213  {
214  DRCEResultDataItem resultItem(resultData.getDataItem(i));
215  packToObject(*pItemElem, resultItem);
216  pItems->add(pItemElem);
217  }
218  }
219 
220  std::stringstream ostr;
221  Poco::JSON::Stringifier::stringify(pItems, ostr);
222  json = ostr.str();
223 
224  _isError = false;
225  }
226  catch(Poco::Exception& e)
227  {
228  _isError = true;
229  errorMsg = e.displayText();
231  }
232  catch(std::exception& e)
233  {
234  _isError = true;
235  errorMsg = e.what();
237  }
238  return !_isError;
239 }
240 //-----------------------------------------------------------------------------
241 bool DRCEResultDataSerializator::unserialize(const std::string& json)
242 {
243  _isError = false;
244  resultData.clear();
246  errorMsg.clear();
247 
248  try
249  {
250  Poco::JSON::Parser parser;
251  Poco::Dynamic::Var res = parser.parse(json);
252 
253  Poco::Dynamic::Var tmp;
254  Poco::JSON::Array::Ptr pItems = res.extract<Poco::JSON::Array::Ptr>();
255  if (!pItems.isNull())
256  {
257  for (size_t i=0;i<pItems->size();++i)
258  {
259  Poco::JSON::Object::Ptr pItemElem = pItems->getObject(i);
260  if (!pItemElem.isNull())
261  {
262  DRCEResultDataItem resultDataItem;
263  unpackFromObject(*pItemElem, resultDataItem);
264  resultData.addDataItem(std::forward<DRCEResultDataItem>(resultDataItem));
265  }
266  }
267  }
268  _isError = false;
269  }
270  catch(Poco::JSON::JSONException& e)
271  {
272  _isError = true;
273  errorMsg = e.displayText();
275  }
276  catch(Poco::Exception& e)
277  {
278  _isError = true;
279  errorMsg = e.displayText();
281  }
282  return !_isError;
283 }
284 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
286 } // namespace drce
287 } // namespace HCE