Commit 99ceca7c by amir

adding MsgArchiversJson to read and write from to request

allowing different types of json parsers
parent a9d3b969
...@@ -3,7 +3,7 @@ project(Microservice) ...@@ -3,7 +3,7 @@ project(Microservice)
# version stuff # version stuff
set (Microservice_VERSION_MAJOR 1) set (Microservice_VERSION_MAJOR 1)
set (Microservice_VERSION_MINOR 4) set (Microservice_VERSION_MINOR 4)
set (Microservice_VERSION_PATCH 0) set (Microservice_VERSION_PATCH 1)
set(Microservice_VERSION_STRING ${Microservice_VERSION_MAJOR}.${Microservice_VERSION_MINOR}.${Microservice_VERSION_PATCH}) set(Microservice_VERSION_STRING ${Microservice_VERSION_MAJOR}.${Microservice_VERSION_MINOR}.${Microservice_VERSION_PATCH})
# type build flags # type build flags
......
...@@ -67,6 +67,7 @@ bool IContainer::ReadObjectFromRequest(nsMicroservice_Iface::IRequest *pti_Reque ...@@ -67,6 +67,7 @@ bool IContainer::ReadObjectFromRequest(nsMicroservice_Iface::IRequest *pti_Reque
return false; return false;
} }
void ICommandClient::GetMetrics(std::map<std::string, long> &metrics_map) { void ICommandClient::GetMetrics(std::map<std::string, long> &metrics_map) {
AddCounters(metrics_map, "create", create_counters_); AddCounters(metrics_map, "create", create_counters_);
AddCounters(metrics_map, "read", read_counters_); AddCounters(metrics_map, "read", read_counters_);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "common/Microservice_Defines.h" #include "common/Microservice_Defines.h"
#include "common/MSTypes.h" #include "common/MSTypes.h"
#include "params/MSCommandParams.h" #include "params/MSCommandParams.h"
#include "json.hpp"
#include <boost/function.hpp> #include <boost/function.hpp>
#include <cereal/archives/json.hpp> #include <cereal/archives/json.hpp>
#include <atomic> #include <atomic>
...@@ -341,6 +342,7 @@ namespace nsMicroservice_Iface ...@@ -341,6 +342,7 @@ namespace nsMicroservice_Iface
}; };
/** /**
* this interface defines the basic operations * this interface defines the basic operations
* required from the container implementor * required from the container implementor
...@@ -374,6 +376,30 @@ namespace nsMicroservice_Iface ...@@ -374,6 +376,30 @@ namespace nsMicroservice_Iface
* @return * @return
*/ */
virtual bool ReadObjectFromRequest(nsMicroservice_Iface::IRequest* pti_Request,rapidjson::Document& t_ObjectDoc); virtual bool ReadObjectFromRequest(nsMicroservice_Iface::IRequest* pti_Request,rapidjson::Document& t_ObjectDoc);
/**
* reading the object from the request body json
* @param req
* @param ObjClass
* @return
*/
template <typename Msg>
bool ReadObjectFromRequest(nsMicroservice_Iface::IRequest* pti_Request, IMsgArchiver<Msg>& archiver, Msg& outMsg){
const char* pba_Content = pti_Request->GetContent();
if (pba_Content)
{
std::string inStr = std::string(pba_Content);
return archiver.parse(inStr,outMsg).IsSuccess();
}
return false;
}
template <typename Msg>
void WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,IMsgArchiver<Msg>& archiver, Msg& inMsg){
std::string outStr;
if (archiver.build(inMsg,outStr).IsSuccess()){
pti_Response->Send(outStr.c_str());
}
}
////////// PUB/SUB //////////////////////////////// ////////// PUB/SUB ////////////////////////////////
/** /**
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <writer.h> //rapidjson writer #include <writer.h> //rapidjson writer
#include <common/Microservice_RequestContext.h> #include <common/Microservice_RequestContext.h>
#include "common/json.hpp"
class cMicroservice_App; class cMicroservice_App;
...@@ -58,6 +59,18 @@ public: ...@@ -58,6 +59,18 @@ public:
return pc_reqCtx->mpti_Container->ReadObjectFromRequest(pc_reqCtx->mpti_Request,t_ObjectDoc); return pc_reqCtx->mpti_Container->ReadObjectFromRequest(pc_reqCtx->mpti_Request,t_ObjectDoc);
} }
template <typename Msg>
bool ReadObjectFromRequest(cMicroservice_RequestContext* pc_reqCtx, nsMicroservice_Iface::IMsgArchiver<Msg>& archiver, Msg& outMsg)
{
return pc_reqCtx->mpti_Container->ReadObjectFromRequest(pc_reqCtx->mpti_Request,archiver, outMsg);
}
template <typename Msg>
void WriteObjectToResponse(cMicroservice_RequestContext* pc_reqCtx, nsMicroservice_Iface::IMsgArchiver<Msg>& archiver, Msg& inMsg)
{
pc_reqCtx->mpti_Container->WriteObjectToResponse(pc_reqCtx->mpti_Response, archiver,inMsg);
}
/** /**
* reload work data * reload work data
*/ */
......
//
// Created by amir on 21/06/17.
//
#ifndef MICROSERVICE_IMSGARCHIVERJSONIMPL_H
#define MICROSERVICE_IMSGARCHIVERJSONIMPL_H
#include "common/json.hpp"
class MsgArchiverJsonLohmann: public nsMicroservice_Iface::IMsgArchiver<nlohmann::json> {
public:
virtual MSRetStat parse(std::string &inStr, nlohmann::json &outMsg) override {
MSRetStat retStat;
try {
outMsg = nlohmann::json::parse(inStr);
if (outMsg.empty())
retStat.SetError("Failed in parsing json");
}
catch (std::exception exp){
retStat.SetError(exp.what());
}
return retStat;
}
virtual MSRetStat build(nlohmann::json &inMsg, std::string &outStr) override {
MSRetStat retStat;
try {
outStr = inMsg.dump();
}
catch (std::exception exp){
retStat.SetError(exp.what());
}
return retStat;
}
};
class MsgArchiverRapidJson: public nsMicroservice_Iface::IMsgArchiver<rapidjson::Document> {
public:
virtual MSRetStat parse(std::string &inStr, rapidjson::Document &outMsg) override {
MSRetStat retStat;
try {
if (outMsg.Parse<0>(inStr.c_str()).HasParseError())
retStat.SetError("Failed in parsing json");
}
catch (std::exception exp){
retStat.SetError(exp.what());
}
return retStat;
}
virtual MSRetStat build(rapidjson::Document &inMsg, std::string &outStr) override {
MSRetStat retStat;
try {
std::ostringstream c_OutputStream;
if(!inMsg.IsNull()) {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
inMsg.Accept(writer);
outStr.assign(buffer.GetString());
} else {
retStat.SetError("Null Object");
}
}
catch (std::exception exp){
retStat.SetError(exp.what());
}
return retStat;
}
};
#endif //MICROSERVICE_IMSGARCHIVERJSONIMPL_H
...@@ -33,7 +33,7 @@ public: ...@@ -33,7 +33,7 @@ public:
void Send(const char* response) override { void Send(const char* response) override {
if (p_respConnection_) { if (p_respConnection_) {
/** /**
* buidling restresponse msg * building restresponse msg
*/ */
respBuilder_.Clear(); respBuilder_.Clear();
auto restResponse = common::context::CreateRestResponseDirect(respBuilder_,rcid_,response); auto restResponse = common::context::CreateRestResponseDirect(respBuilder_,rcid_,response);
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <utils/ServerFactory.h> #include <utils/ServerFactory.h>
#include <impl/servers/Microservice_IRestServerZmqImpl.h> #include <impl/servers/Microservice_IRestServerZmqImpl.h>
#include <common/Microservice_RestResponse.h> #include <common/Microservice_RestResponse.h>
#include <impl/MsgArchiverJsonImpl.h>
static const char *const PUBSUBHOST = "zmqpubsub"; static const char *const PUBSUBHOST = "zmqpubsub";
...@@ -151,7 +152,7 @@ void pubsubtest(cMicroservice_Client *p_Client) { ...@@ -151,7 +152,7 @@ void pubsubtest(cMicroservice_Client *p_Client) {
} }
#include "json.hpp" #include "common/json.hpp"
// for convenience // for convenience
using json = nlohmann::json; using json = nlohmann::json;
...@@ -265,9 +266,28 @@ void testSerializations() { ...@@ -265,9 +266,28 @@ void testSerializations() {
} }
} }
/**
* Last output:
* Testing 1000000 with map serialization json took: 5556msec
* Testing 1000000 with rapid json took: 21233msec
* Testing 1000000 with nlohman json took: 22968msec
*/
void testJsons() void testJsons()
{ {
MsgArchiverJsonLohmann archiverJsonLohmann;
MsgArchiverRapidJson archiverRapidJson;
json json1;
auto inStr = std::string(JSON_CONTENT);
archiverJsonLohmann.parse(inStr, json1);
std::string outStr;
archiverJsonLohmann.build(json1,outStr);
rapidjson::Document rpj_Doc;
archiverRapidJson.parse(inStr, rpj_Doc);
archiverRapidJson.build(rpj_Doc,outStr);
std::cout <<" Testing " << ITERATIONS << " with map serialization json took: " << CommonUtils::measureFunc<>(testSerializations) << "msec" << '\n'; std::cout <<" Testing " << ITERATIONS << " with map serialization json took: " << CommonUtils::measureFunc<>(testSerializations) << "msec" << '\n';
std::cout <<" Testing " << ITERATIONS << " with rapid json took: " << CommonUtils::measureFunc<>(testRapidJson) << "msec" << '\n'; std::cout <<" Testing " << ITERATIONS << " with rapid json took: " << CommonUtils::measureFunc<>(testRapidJson) << "msec" << '\n';
...@@ -410,9 +430,11 @@ void SendZmqRestRequests(const Microservice_App &msApp, cMicroservice_Client *p_ ...@@ -410,9 +430,11 @@ void SendZmqRestRequests(const Microservice_App &msApp, cMicroservice_Client *p_
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
runRestZmqTest(); //runRestZmqTest();
// testCaches(); // testCaches();
// testJsons(); testJsons();
//runTest(); //runTest();
//runPubSubTest(); //runPubSubTest();
......
...@@ -26,7 +26,9 @@ ...@@ -26,7 +26,9 @@
#include <common/Microservice_RestResponse.h> #include <common/Microservice_RestResponse.h>
#include <utils/ServerFactory.h> #include <utils/ServerFactory.h>
#include <utils/ClientFactory.h> #include <utils/ClientFactory.h>
#include <impl/MsgArchiverJsonImpl.h>
#include "Microservice_ZMQTest.cpp" #include "Microservice_ZMQTest.cpp"
#include "common/json.hpp"
static const char *const START = "Start"; static const char *const START = "Start";
static const char *const STOP = "Stop"; static const char *const STOP = "Stop";
...@@ -78,11 +80,15 @@ public: ...@@ -78,11 +80,15 @@ public:
// CreateSync(pc_reqCtx); // CreateSync(pc_reqCtx);
} }
else { else {
rapidjson::Document rpj_Doc; MsgArchiverJsonLohmann archiverJsonLohmann;
if (this->ReadObjectFromRequest(pc_reqCtx,rpj_Doc) ) nlohmann::json outMsg;
this->WriteObjectToResponse(pc_reqCtx,rpj_Doc); if (this->ReadObjectFromRequest<>(pc_reqCtx,archiverJsonLohmann,outMsg))
else this->WriteObjectToResponse<>(pc_reqCtx,archiverJsonLohmann,outMsg);
this->SendErrorResp(pc_reqCtx,"Error in parsing json"); // rapidjson::Document rpj_Doc;
// if (this->ReadObjectFromRequest(pc_reqCtx,rpj_Doc) )
// this->WriteObjectToResponse(pc_reqCtx,rpj_Doc);
// else
// this->SendErrorResp(pc_reqCtx,"Error in parsing json");
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment