hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AdminClientsSerializator.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 
10 
11 namespace HCE
12 {
13 namespace handlers
14 {
15 //-----------------------------------------------------------------------------
16 AdminClientsSerializator::AdminClientsSerializator(std::vector<ClientWorkerItem>& clients_)
17 : inherited(), clients(clients_), purgeCounter(0), refreshNotFoundCounter(0)
18 {
19 
20 }
21 //-----------------------------------------------------------------------------
22 AdminClientsSerializator::AdminClientsSerializator(std::vector<ClientWorkerItem>& clients_,
23  unsigned long long purgeCounter_, unsigned long long refreshNotFoundCounter_)
24 : inherited(), clients(clients_), purgeCounter(purgeCounter_), refreshNotFoundCounter(refreshNotFoundCounter_)
25 {
26 
27 }
28 //-----------------------------------------------------------------------------
29 bool AdminClientsSerializator::serialize(std::string& json)
30 {
31  resetError();
32  try
33  {
34  Poco::JSON::Object::Ptr pObj = new Poco::JSON::Object();
35  if (pObj.isNull())
36  throw Poco::Exception("Object instance was not created");
37 
38  pObj->set(PURGE_COUNTER, static_cast<Poco::UInt64>(purgeCounter));
39  pObj->set(REFRESH_NOT_FOUND_COUNTER, static_cast<Poco::UInt64>(refreshNotFoundCounter));
40  pObj->set(CLIENTS_COUNT, clients.size());
41 
42  Poco::JSON::Array::Ptr pClients = new Poco::JSON::Array();
43  if (!pClients.isNull())
44  {
45  for(std::vector<ClientWorkerItem>::iterator iter = clients.begin();iter!=clients.end();++iter)
46  {
47  Poco::JSON::Object::Ptr pClientElem = new Poco::JSON::Object();
48  if (!pClientElem.isNull())
49  {
50  pClientElem->set(IDENTITY, (*iter).identity);
51  pClientElem->set(COUNT, static_cast<Poco::UInt64>((*iter).count));
52  pClientElem->set(FREQUENCY, static_cast<Poco::UInt64>((*iter).frequency));
53  pClientElem->set(EXPIRY, (*iter).expiry);
54  pClientElem->set(CPU, (*iter).cpu);
55  pClientElem->set(IOWAIT, (*iter).iowait);
56  pClients->add(pClientElem);
57  }
58  }
59  pObj->set(CLIENTS, pClients);
60  }
61 
62  std::stringstream ostr;
63  Poco::JSON::Stringifier::stringify(pObj, ostr);
64  json = ostr.str();
65 
66  _isError = false;
67  }
68  catch(Poco::Exception& e)
69  {
70  _isError = true;
71  errorMsg = e.displayText();
73  }
74  catch(std::exception& e)
75  {
76  _isError = true;
77  errorMsg = e.what();
79  }
80  return !_isError;
81 }
82 //-----------------------------------------------------------------------------
83 bool AdminClientsSerializator::unserialize(const std::string& json)
84 {
85  clear();
86  try
87  {
88  Poco::JSON::Parser parser;
89  Poco::Dynamic::Var res = parser.parse(json);
90 
91  Poco::JSON::Object::Ptr pObj = res.extract<Poco::JSON::Object::Ptr>();
92 
93  if (!pObj.isNull())
94  {
95  Poco::Dynamic::Var tmp = pObj->get(PURGE_COUNTER);
96  purgeCounter = convertVarToNumeric<unsigned long long>(tmp, 0);
97 
98  tmp = pObj->get(REFRESH_NOT_FOUND_COUNTER);
99  refreshNotFoundCounter = convertVarToNumeric<unsigned long long>(tmp, 0);
100 
101  Poco::JSON::Array::Ptr pClients = pObj->getArray(CLIENTS);
102  if (!pClients.isNull())
103  {
104  for (size_t i=0;i<pClients->size();++i)
105  {
106  Poco::JSON::Object::Ptr pClientElem = pClients->getObject(i);
107  if (!pClientElem.isNull())
108  {
109  ClientWorkerItem clientWorkerItem;
110  tmp = pClientElem->get(IDENTITY);
111  if (tmp.isString())
112  clientWorkerItem.identity = tmp.convert<std::string>();
113 
114  tmp = pClientElem->get(COUNT);
115  clientWorkerItem.count = convertVarToNumeric<unsigned long long>(tmp, 0);
116 
117  tmp = pClientElem->get(FREQUENCY);
118  clientWorkerItem.frequency = convertVarToNumeric<unsigned long long>(tmp, 0);
119 
120  tmp = pClientElem->get(EXPIRY);
121  clientWorkerItem.expiry = convertVarToNumeric<int64_t>(tmp, 0);
122 
123  tmp = pClientElem->get(CPU);
124  clientWorkerItem.cpu = convertVarToNumeric<double>(tmp, 0.0);
125 
126  tmp = pClientElem->get(IOWAIT);
127  clientWorkerItem.iowait = convertVarToNumeric<double>(tmp, 0.0);
128 
129  clients.push_back(std::forward<ClientWorkerItem>(clientWorkerItem));
130  }
131  }
132  }
133  }
134  _isError = false;
135  }
136  catch(Poco::JSON::JSONException& e)
137  {
138  _isError = true;
139  errorMsg = e.displayText();
141  }
142  catch(Poco::Exception& e)
143  {
144  _isError = true;
145  errorMsg = e.displayText();
147  }
148  catch(std::exception& e)
149  {
150  _isError = true;
151  errorMsg = e.what();
153  }
154  return !_isError;
155 }
156 //-----------------------------------------------------------------------------
158 {
159  resetError();
160  clients.clear();
161  purgeCounter = 0;
162  refreshNotFoundCounter = 0;
163 }
164 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
166 }// end namespace handlers
167 } // end namespace HCE