hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCETasksQueue.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 
7 #include "DRCEError.hpp"
8 #include "DRCEMessageConst.hpp"
9 #include "DRCETasksQueue.hpp"
10 
11 namespace HCE
12 {
13 namespace drce
14 {
15 //-----------------------------------------------------------------------------
17 : id(0), sdate(0), state(DRCETaskRequest::TaskState::UNDEFINED), tmode(SessionOptions::ThreadMode::tmAsync)
18 {
19 }
20 //-----------------------------------------------------------------------------
22 : id(id_), sdate(sdate_), state(state_), tmode(tmode)
23 {
24 }
25 //-----------------------------------------------------------------------------
27 : id(0), sdate(0), state(DRCETaskRequest::TaskState::UNDEFINED), tmode(SessionOptions::ThreadMode::tmAsync)
28 {
29  (*this) = rhs;
30 }
31 //-----------------------------------------------------------------------------
33 : id(0), sdate(0), state(DRCETaskRequest::TaskState::UNDEFINED), tmode(SessionOptions::ThreadMode::tmAsync)
34 {
35  (*this) = std::forward<TaskStatusData>(rhs);
36 }
37 //-----------------------------------------------------------------------------
39 {
40  if (this!=&rhs)
41  {
42  id = rhs.id;
43  sdate = rhs.sdate;
44  state = rhs.state;
45  tmode = rhs.tmode;
46  }
47  return *this;
48 }
49 //-----------------------------------------------------------------------------
51 {
52  if (this!=&rhs)
53  {
54  id = std::move(rhs.id);
55  sdate = std::move(rhs.sdate);
56  state = std::move(rhs.state);
57  tmode = std::move(rhs.tmode);
58  }
59  return *this;
60 }
61 //-----------------------------------------------------------------------------
63 {
64  id = 0;
65  sdate = 0;
66  state = DRCETaskRequest::TaskState::UNDEFINED;
67  tmode = SessionOptions::ThreadMode::tmAsync;
68 }
69 //-----------------------------------------------------------------------------
70 std::ostream& operator<<(std::ostream& os, const TaskStatusData& rhs)
71 {
72  os << rhs.id << " " << rhs.sdate << " " << static_cast<unsigned int>(rhs.state) << " " << static_cast<unsigned int>(rhs.tmode);
73  return os;
74 }
75 //-----------------------------------------------------------------------------
76 std::istream& operator>>(std::istream& is, TaskStatusData& rhs)
77 {
78  is >> rhs.id;
79  is >> rhs.sdate;
80  unsigned int state(0), tmode(0);
81  is >> state;
82  rhs.state = static_cast<DRCETaskRequest::TaskState>(state);
83  is >> tmode;
84  rhs.tmode = static_cast<SessionOptions::ThreadMode>(tmode);
85  return is;
86 }
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
91 {
92 }
93 //-----------------------------------------------------------------------------
94 bool DRCETasksQueue::serialize(std::string& json)
95 {
96  _isError = false;
98  errorMsg.clear();
99  try
100  {
101  Poco::JSON::Array::Ptr pItems = new Poco::JSON::Array();
102  if (pItems.isNull())
103  throw Poco::Exception(message_const::objectInstanceWasNotCreated);
104 
105  for (size_t i=0;i<getItemsCount();++i)
106  {
107  Poco::JSON::Object::Ptr pItemElem = new Poco::JSON::Object();
108 
109  if (!pItemElem.isNull())
110  {
111  TaskStatusData taskStatusData(getItem(i));
112  pItemElem->set(drce_json_message_const::id, taskStatusData.id);
113  pItemElem->set(drce_json_message_const::sdate, taskStatusData.sdate);
114  pItemElem->set(drce_json_message_const::state, static_cast<unsigned int>(taskStatusData.state));
115  pItemElem->set(drce_json_message_const::tmode, static_cast<unsigned int>(taskStatusData.tmode));
116  pItems->add(pItemElem);
117  }
118  }
119 
120  std::stringstream ostr;
121  Poco::JSON::Stringifier::stringify(pItems, ostr);
122  json = ostr.str();
123 
124  _isError = false;
125  }
126  catch(Poco::Exception& e)
127  {
128  _isError = true;
129  errorMsg = e.displayText();
131  }
132  catch(std::exception& e)
133  {
134  _isError = true;
135  errorMsg = e.what();
137  }
138  return !_isError;
139 }
140 //-----------------------------------------------------------------------------
141 bool DRCETasksQueue::unserialize(const std::string& json)
142 {
143  _isError = false;
144  clear();
146  errorMsg.clear();
147 
148  try
149  {
150  Poco::JSON::Parser parser;
151  Poco::Dynamic::Var res = parser.parse(json);
152 
153  Poco::Dynamic::Var tmp;
154  Poco::JSON::Array::Ptr pItems = res.extract<Poco::JSON::Array::Ptr>();
155  if (!pItems.isNull())
156  {
157  for (size_t i=0;i<pItems->size();++i)
158  {
159  Poco::JSON::Object::Ptr pItemElem = pItems->getObject(i);
160  if (!pItemElem.isNull())
161  {
162  TaskStatusData taskStatusData;
163 
164  Poco::Dynamic::Var tmp = pItemElem->get(drce_json_message_const::id);
165  taskStatusData.id = convertVarToNumeric<unsigned int>(tmp, 0);
166 
167  tmp = pItemElem->get(drce_json_message_const::sdate);
168  taskStatusData.sdate = convertVarToNumeric<size_t>(tmp, 0);
169 
170  tmp = pItemElem->get(drce_json_message_const::state);
171  taskStatusData.state = static_cast<DRCETaskRequest::TaskState>(convertVarToNumeric<unsigned int>(tmp, 0));
172 
173  tmp = pItemElem->get(drce_json_message_const::tmode);
174  taskStatusData.tmode = static_cast<SessionOptions::ThreadMode>(convertVarToNumeric<unsigned int>(tmp, 0));
175 
176  addItem(std::forward<TaskStatusData>(taskStatusData));
177  }
178  }
179  }
180  _isError = false;
181  }
182  catch(Poco::JSON::JSONException& e)
183  {
184  _isError = true;
185  errorMsg = e.displayText();
187  }
188  catch(Poco::Exception& e)
189  {
190  _isError = true;
191  errorMsg = e.displayText();
193  }
194  return !_isError;
195 }
196 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
198 } // end namespace drce
199 } // end namespace HCE