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>
22 void RouteMessage::packToObject(Poco::JSON::Object& obj,
const std::vector<ClientWorkerItem>& workerItem)
24 for (
size_t i=0;i<workerItem.size();++i)
26 Poco::JSON::Object::Ptr pItemElem =
new Poco::JSON::Object();
27 if (!pItemElem.isNull())
29 packToObject(*pItemElem, workerItem[i].route);
30 obj.set(workerItem[i].identity, pItemElem);
40 Poco::JSON::Object::Ptr
pObj =
new Poco::JSON::Object();
42 throw Poco::Exception(
"Object instance was not created");
44 packToObject(*pObj, queue);
46 std::stringstream ostr;
47 Poco::JSON::Stringifier::stringify(pObj, ostr);
52 catch(Poco::Exception& e)
58 catch(std::exception& e)
67 void RouteMessage::unpackFromObject(Poco::JSON::Object& obj, std::vector<ClientWorkerItem>& workerItem)
69 std::vector<std::string> names;
72 for (
size_t i=0;i<names.size();++i)
74 Poco::JSON::Object::Ptr
pObj = obj.getObject(names[i]);
78 unpackFromObject(*pObj, clientWorker.route);
79 workerItem.push_back(std::forward<ClientWorkerItem>(clientWorker));
91 Poco::JSON::Parser parser;
92 Poco::Dynamic::Var res = parser.parse(json);
94 Poco::JSON::Object::Ptr pObj = res.extract<Poco::JSON::Object::Ptr>();
96 unpackFromObject(*pObj, queue);
100 catch(Poco::JSON::JSONException& e)
106 catch(Poco::Exception& e)
112 catch(std::exception& e)
123 std::string result =
"";
138 if (msgBody.compare(0, title.length(), title)!=0)
139 Poco::Exception(
"Title of message not found",
ERROR_TITLE);
142 if (pos==std::string::npos)
143 Poco::Exception(
"Delimiter of title message not found",
ERROR_TITLE);
145 std::string json = msgBody.substr(pos+1);
148 catch(Poco::Exception& e)
163 std::vector<std::string> clients;
164 std::string routes(routeMsg);
167 if (pos!=std::string::npos)
168 routes = routes.substr(0, pos);
172 std::stringstream routesStream(routes);
173 std::istream_iterator <std::string> iit(routesStream);
174 std::istream_iterator <std::string> eos;
178 clients.push_back((*iit));
186 std::vector<std::string> names;
187 for (
size_t i=0;i<clientQueue.size();++i)
189 names.push_back(clientQueue[i].identity);
196 std::stringstream outMsg;
197 for (
size_t i=0;i<names.size();++i)
200 if ((i+1) < names.size())
208 for (
size_t i=0;i<clientQueue.size();++i)
210 routeNames.push_back(clientQueue[i].identity);
217 std::vector<std::string> routeNames;
229 :
inherited(), managerRole(0), nodesNames(), resourcesUsageAlgorithm(0), resourcesUsageAlgorithmWeights(
""), resourcesUsageLimits(
"")
240 Poco::JSON::Object::Ptr pObj =
new Poco::JSON::Object();
242 throw Poco::Exception(
"Object instance was not created");
244 pObj->set(MANAGER_ROLE, managerRole);
246 Poco::JSON::Array::Ptr pNodesNames =
new Poco::JSON::Array();
247 if (pNodesNames.isNull())
248 throw Poco::Exception(
"Object instance was not created");
250 for (
size_t i=0;i<nodesNames.size();++i)
252 pNodesNames->add(nodesNames[i]);
254 pObj->set(NODES_NAMES, pNodesNames);
256 pObj->set(RESOURCES_USAGE_ALGORITHM, resourcesUsageAlgorithm);
257 pObj->set(RESOURCES_USAGE_ALGORITHM_WEIGHTS, resourcesUsageAlgorithmWeights);
258 pObj->set(RESOURCES_USAGE_LIMITS, resourcesUsageLimits);
260 std::stringstream ostr;
261 Poco::JSON::Stringifier::stringify(pObj, ostr);
266 catch(Poco::Exception& e)
272 catch(std::exception& e)
288 Poco::JSON::Parser parser;
289 Poco::Dynamic::Var res = parser.parse(json);
291 Poco::JSON::Object::Ptr pObj = res.extract<Poco::JSON::Object::Ptr>();
293 Poco::Dynamic::Var tmp = pObj->get(MANAGER_ROLE);
294 managerRole = convertVarToNumeric<unsigned int>(tmp, 0);
296 Poco::JSON::Array::Ptr pNodesNames = pObj->getArray(NODES_NAMES);
297 for (
size_t i=0;i<pNodesNames->size();++i)
299 tmp = pNodesNames->get(i);
301 nodesNames.push_back(tmp.convert<std::string>());
304 tmp = pObj->get(RESOURCES_USAGE_ALGORITHM);
305 resourcesUsageAlgorithm = convertVarToNumeric<unsigned int>(tmp, 0);
307 tmp = pObj->get(RESOURCES_USAGE_ALGORITHM_WEIGHTS);
309 resourcesUsageAlgorithmWeights = tmp.convert<std::string>();
311 tmp = pObj->get(RESOURCES_USAGE_LIMITS);
313 resourcesUsageLimits = tmp.convert<std::string>();
317 catch(Poco::JSON::JSONException& e)
323 catch(Poco::Exception& e)
336 resourcesUsageAlgorithm = 0;
337 resourcesUsageAlgorithmWeights.clear();
338 resourcesUsageLimits.clear();
343 :
inherited(), queue(queue_), resources(resources_)
347 void HeartbeatMessage::packToObject(Poco::JSON::Object& obj,
const std::vector<ClientWorkerItem>& workerItem)
349 for (
size_t i=0;i<workerItem.size();++i)
351 Poco::JSON::Object::Ptr pItemElem =
new Poco::JSON::Object();
352 if (!pItemElem.isNull())
354 packToObject(*pItemElem, workerItem[i].route);
355 obj.set(workerItem[i].identity, pItemElem);
365 Poco::JSON::Object::Ptr pObj =
new Poco::JSON::Object();
367 throw Poco::Exception(
"Object instance was not created");
369 Poco::JSON::Object::Ptr pRoutesElem =
new Poco::JSON::Object();
370 if (pRoutesElem.isNull())
371 throw Poco::Exception(
"Object instance was not created");
373 packToObject(*pRoutesElem, queue);
379 std::stringstream ostr;
380 Poco::JSON::Stringifier::stringify(pObj, ostr);
385 catch(Poco::Exception& e)
391 catch(std::exception& e)
400 void HeartbeatMessage::unpackFromObject(Poco::JSON::Object& obj, std::vector<ClientWorkerItem>& workerItem)
402 std::vector<std::string> names;
405 for (
size_t i=0;i<names.size();++i)
407 Poco::JSON::Object::Ptr pObj = obj.getObject(names[i]);
411 unpackFromObject(*pObj, clientWorker.route);
412 workerItem.push_back(std::forward<ClientWorkerItem>(clientWorker));
422 Poco::JSON::Parser parser;
423 Poco::Dynamic::Var res = parser.parse(json);
425 Poco::JSON::Object::Ptr pObj = res.extract<Poco::JSON::Object::Ptr>();
428 if (!pRoutesElem.isNull())
430 unpackFromObject(*pRoutesElem, queue);
435 resources = tmp.convert<std::string>();
439 catch(Poco::JSON::JSONException& e)
445 catch(Poco::Exception& e)
451 catch(std::exception& e)
462 std::string result =
"";
477 if (message.compare(0, title.length(), title)!=0)
478 Poco::Exception(
"Title of message not found",
ERROR_TITLE);
481 if (pos==std::string::npos)
482 Poco::Exception(
"Delimiter of title message not found",
ERROR_TITLE);
484 std::string json = message.substr(pos+1);
487 catch(Poco::Exception& e)
504 :
inherited(), identity(identity_), property(property_)
513 Poco::JSON::Object::Ptr pObj =
new Poco::JSON::Object();
515 throw Poco::Exception(
"Object instance was not created");
517 pObj->set(identity, property);
519 std::stringstream ostr;
520 Poco::JSON::Stringifier::stringify(pObj, ostr);
525 catch(Poco::Exception& e)
531 catch(std::exception& e)
545 Poco::JSON::Parser parser;
546 Poco::Dynamic::Var res = parser.parse(json);
548 Poco::JSON::Object::Ptr pObj = res.extract<Poco::JSON::Object::Ptr>();
552 std::vector<std::string> names;
553 pObj->getNames(names);
555 for (
size_t i=0;i<names.size();++i)
558 Poco::Dynamic::Var tmp = pObj->get(identity);
560 property = tmp.convert<std::string>();
566 catch(Poco::JSON::JSONException& e)
572 catch(Poco::Exception& e)
578 catch(std::exception& e)
589 std::string result =
"";
604 if (message.compare(0, title.length(), title)!=0)
605 Poco::Exception(
"Title of message not found",
ERROR_TITLE);
608 if (pos==std::string::npos)
609 Poco::Exception(
"Delimiter of title message not found",
ERROR_TITLE);
611 std::string json = message.substr(pos+1);
614 catch(Poco::Exception& e)