hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCETaskRequestTerminate.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"
12 
13 namespace HCE
14 {
15 namespace drce
16 {
17 //-----------------------------------------------------------------------------
19 : inherited(DRCETaskRequest::RequestType::rtTerminateTask), algorithmType(AlgorithmType::atUninitialized),
20  delayValue(0), repeatValue(0), signalValue(0), cleanup(CleanupFlag::cfNotDelete)
21 {
22  if (!json.empty())
23  unserialize(json);
24 }
25 //-----------------------------------------------------------------------------
27 {
28  algorithmType = AlgorithmType::atUninitialized;
29  delayValue = 0;
30  repeatValue = 0;
31  signalValue = 0;
32  cleanup = CleanupFlag::cfNotDelete;
33 }
34 //-----------------------------------------------------------------------------
35 bool DRCETaskRequestTerminate::serialize(std::string& json)
36 {
38  errorMsg.clear();
39  try
40  {
41  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
42 
43  obj->set(drce_json_message_const::algorithmType, static_cast<unsigned int>(algorithmType));
44  obj->set(drce_json_message_const::delayValue, delayValue);
45  obj->set(drce_json_message_const::repeatValue, repeatValue);
46  obj->set(drce_json_message_const::signalValue, signalValue);
47  obj->set(drce_json_message_const::cleanup, static_cast<unsigned int>(cleanup));
48 
49  std::stringstream ostr;
50  Poco::JSON::Stringifier::stringify(obj, ostr);
51  json = ostr.str();
52 
53  _isError = false;
54  }
55  catch(std::exception& e)
56  {
57  _isError = true;
58  errorMsg = e.what();
60  }
61  return !_isError;}
62 //-----------------------------------------------------------------------------
63 bool DRCETaskRequestTerminate::unserialize(const std::string& json)
64 {
65  clear();
67  errorMsg.clear();
68 
69  try
70  {
71  Poco::JSON::Parser parser;
72  Poco::Dynamic::Var res = parser.parse(json);
73  Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
74 
75  Poco::Dynamic::Var tmp = obj->get(drce_json_message_const::algorithmType);
76  algorithmType = static_cast<AlgorithmType>(convertVarToNumeric<unsigned int>(tmp, 0));
77 
79  delayValue = convertVarToNumeric<unsigned int>(tmp, 0);
80 
82  repeatValue = convertVarToNumeric<unsigned int>(tmp, 0);
83 
85  signalValue = convertVarToNumeric<unsigned int>(tmp, 0);
86 
87  tmp = obj->get(drce_json_message_const::cleanup);
88  cleanup = static_cast<CleanupFlag>(convertVarToNumeric<unsigned int>(tmp, 0));
89 
90  _isError = false;
91  }
92  catch(Poco::JSON::JSONException& e)
93  {
94  _isError = true;
95  errorMsg = e.message();
97  }
98  catch(Poco::Exception& e)
99  {
100  _isError = true;
101  errorMsg = e.message();
103  }
104  return !_isError;
105 }
106 //-----------------------------------------------------------------------------
107 std::istream& operator>>(std::istream& is, DRCETaskRequestTerminate& rhs)
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  rhs.unserialize(json);
115  return is;
116 }
117 //-----------------------------------------------------------------------------
118 std::ostream& operator<<(std::ostream& os, const DRCETaskRequestTerminate& rhs)
119 {
120  std::string json;
121  const_cast<DRCETaskRequestTerminate&>(rhs).serialize(json);
122  return os << json;
123 }
124 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
126 } // end namespace drce
127 } // end namespace HCE