HCE project C++ developers source code library  1.1.1
HCE project developer library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEInputJsonMessage.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 #include <utility>
9 
10 #include "DRCEError.hpp"
11 #include "DRCEInputJsonMessage.hpp"
13 
14 namespace HCE
15 {
16 namespace drce
17 {
18 //-----------------------------------------------------------------------------
21 {
22  if (!json.empty())
23  unserialize(json);
24 }
25 //-----------------------------------------------------------------------------
27 {
28  sessionOptions = std::move(std::forward<SessionOptions>(sessionOptions_));
29 }
30 //-----------------------------------------------------------------------------
32 {
33  sessionOptions.clear();
34  commandLine.clear();
35  inputStream.clear();
36  files.clear();
37 }
38 //-----------------------------------------------------------------------------
39 bool DRCEInputJsonMessage::serialize(std::string& json)
40 {
42  errorMsg.clear();
43  try
44  {
45  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
46 
47  Poco::JSON::Object::Ptr pSessionOptions = new Poco::JSON::Object();
48  pSessionOptions->set(drce_json_message_const::sessionType, static_cast<unsigned int>(sessionOptions.sessionType));
49  pSessionOptions->set(drce_json_message_const::port, sessionOptions.port);
50  pSessionOptions->set(drce_json_message_const::userName, sessionOptions.userName);
51  pSessionOptions->set(drce_json_message_const::userPassword, sessionOptions.userPassword);
52  pSessionOptions->set(drce_json_message_const::shellName, sessionOptions.shellName);
53  pSessionOptions->set(drce_json_message_const::timeout, sessionOptions.timeout);
54  pSessionOptions->set(drce_json_message_const::tmode, sessionOptions.tmode);
55 
56  Poco::JSON::Array::Ptr pEnviroment = new Poco::JSON::Array();
57  for (size_t i=0;i<sessionOptions.environments.size();++i)
58  {
59  Poco::JSON::Object::Ptr pEnviromentElem = new Poco::JSON::Object();
60  pEnviromentElem->set(sessionOptions.environments[i].first, sessionOptions.environments[i].second);
61  pEnviroment->add(pEnviromentElem);
62  }
63  pSessionOptions->set(drce_json_message_const::environment, pEnviroment);
64 
65  obj->set(drce_json_message_const::sessionOptions, pSessionOptions);
66  obj->set(drce_json_message_const::commandLine, commandLine);
67  obj->set(drce_json_message_const::inputStream, inputStream);
68 
69  Poco::JSON::Array::Ptr pFiles = new Poco::JSON::Array();
70 
71  const size_t filesCount = getFilesCount();
72  for (size_t i=0;i<filesCount;++i)
73  {
74  Poco::JSON::Object::Ptr pFileElem = new Poco::JSON::Object();
75  pFileElem->set(drce_json_message_const::actionTypeMask, getFileItem(i).actionType);
76  pFileElem->set(drce_json_message_const::fileName, getFileItem(i).name);
78  pFiles->add(pFileElem);
79  }
80  obj->set(drce_json_message_const::filesArray, pFiles);
81 
82  std::stringstream ostr;
83  Poco::JSON::Stringifier::stringify(obj, ostr);
84  json = ostr.str();
85 
86  _isError = false;
87  }
88  catch(std::exception& e)
89  {
90  _isError = true;
91  errorMsg = e.what();
93  }
94  return !_isError;
95 }
96 //-----------------------------------------------------------------------------
97 bool DRCEInputJsonMessage::unserialize(const std::string& json)
98 {
99  clear();
101  errorMsg.clear();
102 
103  try
104  {
105  Poco::JSON::Parser parser;
106  Poco::Dynamic::Var res = parser.parse(json);
107  Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
108 
109  Poco::Dynamic::Var tmp;
110  Poco::JSON::Object::Ptr pSessionOptions = obj->getObject(drce_json_message_const::sessionOptions);
111  if (!pSessionOptions.isNull())
112  {
113  tmp = pSessionOptions->get(drce_json_message_const::sessionType);
114  sessionOptions.sessionType = static_cast<SessionOptions::SessionType>(convertVarToNumeric<unsigned int>(tmp, 0));
115 
116  tmp = pSessionOptions->get(drce_json_message_const::port);
117  sessionOptions.port = convertVarToNumeric<unsigned int>(tmp, 0);
118 
119  tmp = pSessionOptions->get(drce_json_message_const::userName);
120  if (tmp.isString())
121  sessionOptions.userName = tmp.convert<std::string>();
122 
123  tmp = pSessionOptions->get(drce_json_message_const::userPassword);
124  if (tmp.isString())
125  sessionOptions.userPassword = tmp.convert<std::string>();
126 
127  tmp = pSessionOptions->get(drce_json_message_const::shellName);
128  if (tmp.isString())
129  sessionOptions.shellName = tmp.convert<std::string>();
130 
131  tmp = pSessionOptions->get(drce_json_message_const::timeout);
132  sessionOptions.timeout = convertVarToNumeric<unsigned int>(tmp, 0);
133 
134  tmp = pSessionOptions->get(drce_json_message_const::tmode);
135  sessionOptions.tmode = convertVarToNumeric<unsigned int>(tmp, 0);
136 
137  Poco::JSON::Array::Ptr pEnviroment = pSessionOptions->getArray(drce_json_message_const::environment);
138  if (!pEnviroment.isNull())
139  {
140  for (size_t j=0;j<pEnviroment->size();++j)
141  {
142  Poco::JSON::Object::Ptr pEnviromentElem = pEnviroment->getObject(j);
143  if (!pEnviromentElem.isNull())
144  {
145  std::vector<std::string> names;
146  pEnviromentElem->getNames(names);
147  for (size_t k=0;k<names.size();++k)
148  {
149  tmp = pEnviromentElem->get(names[k]);
150  if (tmp.isString())
151  sessionOptions.environments.push_back(std::make_pair(names[k], tmp.convert<std::string>()));
152  }
153  }
154  }
155  }
156  }
157 
158  tmp = obj->get(drce_json_message_const::commandLine);
159  if (tmp.isString())
160  commandLine = tmp.convert<std::string>();
161 
162  tmp = obj->get(drce_json_message_const::inputStream);
163  if (tmp.isString())
164  inputStream = tmp.convert<std::string>();
165 
166  Poco::JSON::Array::Ptr pFiles = obj->getArray(drce_json_message_const::filesArray);
167  if (!pFiles.isNull())
168  {
169  for (size_t i=0;i<pFiles->size();++i)
170  {
171  Poco::JSON::Object::Ptr pFileElem = pFiles->getObject(i);
172  if (!pFileElem.isNull())
173  {
174  FileItem fileItem;
175  fileItem.actionType = pFileElem->getValue<unsigned int>(drce_json_message_const::actionTypeMask);
176  fileItem.name = pFileElem->getValue<std::string>(drce_json_message_const::fileName);
177  fileItem.data = pFileElem->getValue<std::string>(drce_json_message_const::fileData);
178  addFileItem(std::forward<FileItem>(fileItem));
179  }
180  }
181  }
182 
183  _isError = false;
184  }
185  catch(Poco::JSON::JSONException& e)
186  {
187  _isError = true;
188  errorMsg = e.message();
190  }
191  catch(Poco::Exception& e)
192  {
193  _isError = true;
194  errorMsg = e.message();
196  }
197  return !_isError;
198 }
199 //-----------------------------------------------------------------------------
200 std::istream& operator>>(std::istream& is, DRCEInputJsonMessage& inputJsonMessage)
201 {
202  std::string json;
203  is.seekg(0, std::ios::end);
204  json.resize(is.tellg());
205  is.seekg(0, std::ios::beg);
206  is.read(const_cast<char*>(json.c_str()), json.size());
207  inputJsonMessage.unserialize(json);
208  return is;
209 }
210 //-----------------------------------------------------------------------------
211 std::ostream& operator<<(std::ostream& os, const DRCEInputJsonMessage& inputJsonMessage)
212 {
213  std::string json;
214  const_cast<DRCEInputJsonMessage&>(inputJsonMessage).serialize(json);
215  return os << json;
216 }
217 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
219 } // namespace drce
220 } // namespace HCE