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>
17 :
inherited(), clients(clients_), purgeCounter(0), refreshNotFoundCounter(0)
23 unsigned long long purgeCounter_,
unsigned long long refreshNotFoundCounter_)
24 :
inherited(), clients(clients_), purgeCounter(purgeCounter_), refreshNotFoundCounter(refreshNotFoundCounter_)
34 Poco::JSON::Object::Ptr
pObj =
new Poco::JSON::Object();
36 throw Poco::Exception(
"Object instance was not created");
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());
42 Poco::JSON::Array::Ptr pClients =
new Poco::JSON::Array();
43 if (!pClients.isNull())
45 for(std::vector<ClientWorkerItem>::iterator iter = clients.begin();iter!=clients.end();++iter)
47 Poco::JSON::Object::Ptr pClientElem =
new Poco::JSON::Object();
48 if (!pClientElem.isNull())
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);
59 pObj->set(CLIENTS, pClients);
62 std::stringstream ostr;
63 Poco::JSON::Stringifier::stringify(pObj, ostr);
68 catch(Poco::Exception& e)
74 catch(std::exception& e)
88 Poco::JSON::Parser parser;
89 Poco::Dynamic::Var res = parser.parse(json);
91 Poco::JSON::Object::Ptr
pObj = res.extract<Poco::JSON::Object::Ptr>();
95 Poco::Dynamic::Var tmp = pObj->get(PURGE_COUNTER);
96 purgeCounter = convertVarToNumeric<unsigned long long>(tmp, 0);
98 tmp = pObj->get(REFRESH_NOT_FOUND_COUNTER);
99 refreshNotFoundCounter = convertVarToNumeric<unsigned long long>(tmp, 0);
101 Poco::JSON::Array::Ptr pClients = pObj->getArray(CLIENTS);
102 if (!pClients.isNull())
104 for (
size_t i=0;i<pClients->size();++i)
106 Poco::JSON::Object::Ptr pClientElem = pClients->getObject(i);
107 if (!pClientElem.isNull())
110 tmp = pClientElem->get(IDENTITY);
112 clientWorkerItem.
identity = tmp.convert<std::string>();
114 tmp = pClientElem->get(COUNT);
115 clientWorkerItem.
count = convertVarToNumeric<unsigned long long>(tmp, 0);
117 tmp = pClientElem->get(FREQUENCY);
118 clientWorkerItem.
frequency = convertVarToNumeric<unsigned long long>(tmp, 0);
120 tmp = pClientElem->get(EXPIRY);
121 clientWorkerItem.
expiry = convertVarToNumeric<int64_t>(tmp, 0);
123 tmp = pClientElem->get(CPU);
124 clientWorkerItem.
cpu = convertVarToNumeric<double>(tmp, 0.0);
126 tmp = pClientElem->get(IOWAIT);
127 clientWorkerItem.
iowait = convertVarToNumeric<double>(tmp, 0.0);
129 clients.push_back(std::forward<ClientWorkerItem>(clientWorkerItem));
136 catch(Poco::JSON::JSONException& e)
142 catch(Poco::Exception& e)
148 catch(std::exception& e)
162 refreshNotFoundCounter = 0;