Commit 2ccf5855 by amir

# 0.3.0

- Adding RestResponse that can return the headers and status code of the response
  check the ReadSync in the Test file
- Implementing the Add Headers to request
  check the ReadSync in the Test file
parent 456964ba
...@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.12) ...@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.12)
project(Microservice) project(Microservice)
# version stuff # version stuff
set (Microservice_VERSION_MAJOR 0) set (Microservice_VERSION_MAJOR 0)
set (Microservice_VERSION_MINOR 2) set (Microservice_VERSION_MINOR 3)
set (Microservice_VERSION_PATCH 0) set (Microservice_VERSION_PATCH 0)
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})
...@@ -36,7 +36,7 @@ file(GLOB_RECURSE SOURCES "src/*.cpp") ...@@ -36,7 +36,7 @@ file(GLOB_RECURSE SOURCES "src/*.cpp")
set (3PARTY_SOURCES ../3party/mongoose/mongoose.c) set (3PARTY_SOURCES ../3party/mongoose/mongoose.c)
#Generate the shared library from the sources #Generate the shared library from the sources
add_library(Microservice SHARED ${SOURCES} ${3PARTY_SOURCES}) add_library(Microservice SHARED ${SOURCES} ${3PARTY_SOURCES} src/common/Microservice_RestResponse.h)
target_link_libraries(Microservice ${PROJECT_LINK_LIBS} ) target_link_libraries(Microservice ${PROJECT_LINK_LIBS} )
set_target_properties(Microservice PROPERTIES VERSION ${Microservice_VERSION_STRING} set_target_properties(Microservice PROPERTIES VERSION ${Microservice_VERSION_STRING}
SOVERSION ${Microservice_VERSION_MAJOR}) SOVERSION ${Microservice_VERSION_MAJOR})
......
## C++ Microservice Framework
## VERSIONS: ## VERSIONS:
# 0.3.0
- Adding RestResponse that can return the headers and status code of the response
check the ReadSync in the Test file
- Implementing the Add Headers to request
check the ReadSync in the Test file
# 0.2.0 - Add Async client operations # 0.2.0 - Add Async client operations
- Tasks Reference: https://msdn.microsoft.com/en-us/library/dd492427.aspx
#0.1.0 - Add Scheduler #0.1.0 - Add Scheduler
\ No newline at end of file
...@@ -52,6 +52,8 @@ public: ...@@ -52,6 +52,8 @@ public:
if(!mc_ObjectNode.IsNull()) if(!mc_ObjectNode.IsNull())
mc_ObjectNode.Clear(); mc_ObjectNode.Clear();
} }
virtual uint32_t GetTypeHash() { return 0; }
private: private:
bool mb_Success; bool mb_Success;
......
//
// Created by amir on 07/11/16.
//
#ifndef MICROSERVICE_MICROSERVICE_RESTRESPONSE_H
#define MICROSERVICE_MICROSERVICE_RESTRESPONSE_H
#include <Microservice_BaseRestResponse.h>
#include <map>
class Microservice_RestResponse : public cMicroservice_BaseRestResponse{
public:
Microservice_RestResponse():
cMicroservice_BaseRestResponse() {}
Microservice_RestResponse(bool b_Success, std::string &c_Error) :
cMicroservice_BaseRestResponse(b_Success, c_Error) {}
Microservice_RestResponse(bool b_Success, std::string &c_Error, rapidjson::Document &c_ObjectNode):
cMicroservice_BaseRestResponse(b_Success, c_Error, c_ObjectNode) {}
const std::map<std::string, std::string> &getHeaderMap() const {
return headerMap_;
}
void setHeaderMap(const std::map<std::string, std::string> &headerMap_) {
Microservice_RestResponse::headerMap_ = headerMap_;
}
void addHeader(std::string& header, std::string& value) {
headerMap_[header] = value;
}
/**
* please note that if the header does not exist
* returning empty string
* @param header
* @return
*/
std::string& getHeader(std::string& header) {
return headerMap_[header];
}
unsigned short getResponse_code() const {
return response_code_;
}
void setResponse_code(unsigned short response_code_) {
Microservice_RestResponse::response_code_ = response_code_;
}
void Reset(){
cMicroservice_BaseRestResponse::Reset();
response_code_ = 0;
headerMap_.clear();
}
virtual uint32_t GetTypeHash() override {
return TYPE_HASH;
}
public:
static constexpr uint32_t TYPE_HASH = 1478523102; // epoch time of creation
private:
std::map<std::string,std::string> headerMap_;
unsigned short response_code_;
};
#endif //MICROSERVICE_MICROSERVICE_RESTRESPONSE_H
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <cpprest/http_client.h> #include <cpprest/http_client.h>
#include <cpprest/filestream.h> #include <cpprest/filestream.h>
#include <cpprest/base_uri.h> #include <cpprest/base_uri.h>
#include <common/Microservice_RestResponse.h>
using namespace utility; // Common utilities like string conversions using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs. using namespace web; // Common features like URIs.
...@@ -51,9 +52,9 @@ bool MSICommandClientHttpImpl::BuildUrl(MSCommandParams* p_cmd_params, std::stri ...@@ -51,9 +52,9 @@ bool MSICommandClientHttpImpl::BuildUrl(MSCommandParams* p_cmd_params, std::stri
unencoded_url.append(HTTP_SCHEME).append(entity); unencoded_url.append(HTTP_SCHEME).append(entity);
unencoded_url.append(entity); unencoded_url.append(entity);
// params // params
if(p_cmd_params->GetParams()) if(!p_cmd_params->GetParams().empty())
{ {
for(auto param : *p_cmd_params->GetParams()) for(auto param : p_cmd_params->GetParams())
{ {
unencoded_url.append(1,'/') .append(param.c_str()); unencoded_url.append(1,'/') .append(param.c_str());
} }
...@@ -92,9 +93,19 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){ ...@@ -92,9 +93,19 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
http_client client(url); http_client client(url);
//config.set_timeout<std::chrono::seconds>(std::chrono::seconds(2)); //config.set_timeout<std::chrono::seconds>(std::chrono::seconds(2));
http_request request(*cmdDataPtr->p_mtd); http_request request(*cmdDataPtr->p_mtd);
// headers
request.headers().add(header_names::accept,"*/*"); request.headers().add(header_names::accept,"*/*");
auto& headers = cmdDataPtr->p_cmd_params->GetHeadersMap();
if (!headers.empty()){
for (auto header : headers){
request.headers().add(header.first,header.second);
}
}
// content
if(!cmdDataPtr->p_cmd_params->GetContent().empty()) if(!cmdDataPtr->p_cmd_params->GetContent().empty())
request.set_body(cmdDataPtr->p_cmd_params->GetContent(),"application/json"); request.set_body(cmdDataPtr->p_cmd_params->GetContent(),"application/json");
//auto request_task = client.request(*cmdDataPtr->p_mtd); //auto request_task = client.request(*cmdDataPtr->p_mtd);
auto request_task = client.request(request); auto request_task = client.request(request);
if(cmdDataPtr->p_cmd_params->IsAsync_()) if(cmdDataPtr->p_cmd_params->IsAsync_())
...@@ -127,11 +138,16 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){ ...@@ -127,11 +138,16 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
if(!doc.Parse<0>(content.c_str()).HasParseError()) if(!doc.Parse<0>(content.c_str()).HasParseError())
{ {
cmdDataPtr->p_command_counters->succeed++; cmdDataPtr->p_command_counters->succeed++;
// delegate ?
if (cmdDataPtr->p_response->GetTypeHash() == Microservice_RestResponse::TYPE_HASH)
DelegateRestResponse(cmdDataPtr->p_response,resp);
} }
else else
{ {
cmdDataPtr->p_retstat->SetError(doc.GetParseError()); cmdDataPtr->p_retstat->SetError(doc.GetParseError());
cmdDataPtr->p_command_counters->failed++; cmdDataPtr->p_command_counters->failed++;
cmdDataPtr->p_response->SetError(cmdDataPtr->p_retstat->GetError());
} }
} }
} }
...@@ -142,6 +158,11 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){ ...@@ -142,6 +158,11 @@ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
cmdDataPtr->p_retstat->SetError(ss.str().c_str()); cmdDataPtr->p_retstat->SetError(ss.str().c_str());
LOG_ERROR(ss.str()); LOG_ERROR(ss.str());
cmdDataPtr->p_command_counters->failed++; cmdDataPtr->p_command_counters->failed++;
cmdDataPtr->p_response->SetError(cmdDataPtr->p_retstat->GetError());
// delegate ?
if (cmdDataPtr->p_response->GetTypeHash() == Microservice_RestResponse::TYPE_HASH)
DelegateRestResponse(cmdDataPtr->p_response,resp);
} }
} }
...@@ -207,3 +228,17 @@ void MSICommandClientHttpImpl::GetMetrics(std::map<std::string, long>& metrics_m ...@@ -207,3 +228,17 @@ void MSICommandClientHttpImpl::GetMetrics(std::map<std::string, long>& metrics_m
AddCounters(metrics_map, "update", update_counters_); AddCounters(metrics_map, "update", update_counters_);
AddCounters(metrics_map, "delete", delete_counters_); AddCounters(metrics_map, "delete", delete_counters_);
} }
void MSICommandClientHttpImpl::DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse,
web::http::http_response &response) {
Microservice_RestResponse* p_RestResponse = (Microservice_RestResponse*)pResponse;
p_RestResponse->setResponse_code(response.status_code());
auto& headersMap = p_RestResponse->getHeaderMap();
for(auto header : response.headers())
{
std::string key = header.first;
p_RestResponse->addHeader(key,header.second);
}
}
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "../../Microservice_Iface.h" #include "../../Microservice_Iface.h"
#include <atomic> #include <atomic>
#include <cpprest/http_msg.h>
using namespace nsMicroservice_Iface; using namespace nsMicroservice_Iface;
...@@ -88,6 +89,8 @@ private: ...@@ -88,6 +89,8 @@ private:
void AddCounters(std::map<std::string, long>& metrics_map, void AddCounters(std::map<std::string, long>& metrics_map,
const char* name, const char* name,
CommandCounters& cmd_counters); CommandCounters& cmd_counters);
void DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse, web::http::http_response &response);
}; };
......
...@@ -104,7 +104,7 @@ void MSICommandClientRMQImpl::HandleCommand(HandleCommandData* p_cmd_data) ...@@ -104,7 +104,7 @@ void MSICommandClientRMQImpl::HandleCommand(HandleCommandData* p_cmd_data)
// content // content
message.setContent(p_cmd_data->p_cmd_params->GetContent()); message.setContent(p_cmd_data->p_cmd_params->GetContent());
// headers // headers
for (auto header : *p_cmd_data->p_cmd_params->GetHeadersMap()) for (auto header : p_cmd_data->p_cmd_params->GetHeadersMap())
{ {
message.setHeader(cNameValuePair(header.first, header.second)); message.setHeader(cNameValuePair(header.first, header.second));
} }
...@@ -188,9 +188,9 @@ std::string MSICommandClientRMQImpl::BuildPath(MSCommandParams* p_cmd_params) { ...@@ -188,9 +188,9 @@ std::string MSICommandClientRMQImpl::BuildPath(MSCommandParams* p_cmd_params) {
return path; return path;
// params // params
if(p_cmd_params->GetParams()) if(!p_cmd_params->GetParams().empty())
{ {
for(auto param : *p_cmd_params->GetParams()) for(auto param : p_cmd_params->GetParams())
{ {
unencoded_path.append(1,'/') .append(param.c_str()); unencoded_path.append(1,'/') .append(param.c_str());
} }
......
...@@ -22,16 +22,16 @@ class MSCommandParams ...@@ -22,16 +22,16 @@ class MSCommandParams
{ {
private: private:
std::string entity_; std::string entity_;
std::vector<std::string>* p_params_; std::vector<std::string> params_;
std::string params_string_; // params as a continues string "p1/p2/p3" std::string params_string_; // params as a continues string "p1/p2/p3"
std::string request_params_; std::string request_params_;
std::string content_; std::string content_;
std::map<std::string,std::string>* p_headers_map_; std::map<std::string,std::string> headers_map_;
bool async_; bool async_;
public: public:
MSCommandParams():p_params_(nullptr),p_headers_map_(nullptr) { MSCommandParams() {
async_ = false; async_ = false;
} }
...@@ -41,7 +41,7 @@ public: ...@@ -41,7 +41,7 @@ public:
* @param params_string_ * @param params_string_
* @param request_params_ * @param request_params_
* @param content_ * @param content_
* @param p_headers_map_ * @param headers_map_
* @param async * @param async
*/ */
MSCommandParams(std::string entity, MSCommandParams(std::string entity,
...@@ -50,14 +50,13 @@ public: ...@@ -50,14 +50,13 @@ public:
std::string content, std::string content,
std::map<std::string, std::string>* p_headers_map, std::map<std::string, std::string>* p_headers_map,
bool async = false) : bool async = false) :
entity_(entity), params_string_(params_string), request_params_(request_params), content_(content), p_headers_map_(p_headers_map) { entity_(entity), params_string_(params_string), request_params_(request_params), content_(content), headers_map_(*p_headers_map) {
p_params_ = nullptr;
async_ = async; async_ = async;
} }
MSCommandParams(std::string entity_, std::vector<std::string>* p_params_, std::string request_params_, std::string content_, std::map<std::string, std::string>* p_headers_map_, bool async = false) : MSCommandParams(std::string entity_, std::vector<std::string>* p_params_, std::string request_params_, std::string content_, std::map<std::string, std::string>* p_headers_map, bool async = false) :
entity_(entity_), p_params_(p_params_), request_params_(request_params_), content_(content_), p_headers_map_(p_headers_map_) { entity_(entity_), params_(*p_params_), request_params_(request_params_), content_(content_), headers_map_(*p_headers_map) {
params_string_ = nullptr; //params_string_ = nullptr;
async_ = async; async_ = async;
} }
...@@ -69,12 +68,12 @@ public: ...@@ -69,12 +68,12 @@ public:
return entity_; return entity_;
} }
std::map<std::string, std::string>* GetHeadersMap() const { std::map<std::string, std::string>& GetHeadersMap() {
return p_headers_map_; return headers_map_;
} }
std::vector<std::string>* GetParams() const { std::vector<std::string>& GetParams() {
return p_params_; return params_;
} }
std::string& GetParamsString() { std::string& GetParamsString() {
...@@ -96,9 +95,9 @@ public: ...@@ -96,9 +95,9 @@ public:
MSCommandParams& WithContent(std::string& content) { this->content_.assign(content); return *this; } MSCommandParams& WithContent(std::string& content) { this->content_.assign(content); return *this; }
MSCommandParams& WithContent(const char* p_content) { this->content_.assign(p_content); return *this; } MSCommandParams& WithContent(const char* p_content) { this->content_.assign(p_content); return *this; }
MSCommandParams& WithHeadersMap(std::map<std::string, std::string>* p_headers_map) { this->p_headers_map_ = p_headers_map; return *this; } MSCommandParams& WithHeadersMap(std::map<std::string, std::string>* p_headers_map) { this->headers_map_ = *p_headers_map; return *this; }
MSCommandParams& WithParams(std::vector<std::string>* p_params) { this->p_params_ = p_params; return *this; } MSCommandParams& WithParams(std::vector<std::string>* p_params) { this->params_ = *p_params; return *this; }
MSCommandParams& WithParamsString(std::string& params_string) { this->params_string_.assign(params_string); return *this; } MSCommandParams& WithParamsString(std::string& params_string) { this->params_string_.assign(params_string); return *this; }
MSCommandParams& WithParamsString(const char* p_params_string) { this->params_string_.assign(p_params_string); return *this; } MSCommandParams& WithParamsString(const char* p_params_string) { this->params_string_.assign(p_params_string); return *this; }
......
...@@ -21,74 +21,83 @@ ...@@ -21,74 +21,83 @@
#include <iostream> #include <iostream>
#include <utils/ScheduledTimer.h> #include <utils/ScheduledTimer.h>
#include <pplx/pplxtasks.h> #include <pplx/pplxtasks.h>
#include <common/Microservice_RestResponse.h>
class cMicroserviceHandler: public cMicroservice_BaseHandler class cMicroserviceHandler: public cMicroservice_BaseHandler
{ {
char mba_GetReturnedString[1024]; char mba_GetReturnedString[1024];
cMicroservice_Client* p_client_; cMicroservice_Client* p_client_;
nsMicroservice_Iface::IMetricsFactory::IHistogram* p_histo; nsMicroservice_Iface::IMetricsFactory::IHistogram* p_histo;
public: public:
cMicroserviceHandler(const char* pba_GetReturnedString) cMicroserviceHandler(const char* pba_GetReturnedString)
{
strncpy(mba_GetReturnedString,pba_GetReturnedString,1024);
}
void DoCreate(cMicroservice_RequestContext* pc_reqCtx)
{
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");
}
void DoRead(cMicroservice_RequestContext* pc_reqCtx)
{
rapidjson::Document rpj_Doc; // Null
rapidjson::Document::AllocatorType& rpj_Alloc = rpj_Doc.GetAllocator();
rpj_Doc.SetObject();
for ( std::vector<std::string>::iterator it= pc_reqCtx->mc_Params.begin(); it!=pc_reqCtx->mc_Params.end(); ++it)
rpj_Doc.AddMember(it->c_str(),it->c_str(),rpj_Alloc);
rpj_Doc.AddMember("int",200,rpj_Alloc);
for ( DequeStringMap::iterator it = pc_reqCtx->mc_QueryParameters.begin(); it != pc_reqCtx->mc_QueryParameters.end(); ++it)
{ {
for (std::deque<std::string>::iterator dequeIt = it->second.begin(); dequeIt != it->second.end(); ++dequeIt) strncpy(mba_GetReturnedString,pba_GetReturnedString,1024);
{
rpj_Doc.AddMember(it->first.c_str(),dequeIt->c_str(),rpj_Alloc); }
} void DoCreate(cMicroservice_RequestContext* pc_reqCtx)
{
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");
}
void DoRead(cMicroservice_RequestContext* pc_reqCtx)
{
rapidjson::Document rpj_Doc; // Null
rapidjson::Document::AllocatorType& rpj_Alloc = rpj_Doc.GetAllocator();
rpj_Doc.SetObject();
for ( std::vector<std::string>::iterator it= pc_reqCtx->mc_Params.begin(); it!=pc_reqCtx->mc_Params.end(); ++it)
rpj_Doc.AddMember(it->c_str(),it->c_str(),rpj_Alloc);
rpj_Doc.AddMember("int",200,rpj_Alloc);
for ( DequeStringMap::iterator it = pc_reqCtx->mc_QueryParameters.begin(); it != pc_reqCtx->mc_QueryParameters.end(); ++it)
{
for (std::deque<std::string>::iterator dequeIt = it->second.begin(); dequeIt != it->second.end(); ++dequeIt)
{
rpj_Doc.AddMember(it->first.c_str(),dequeIt->c_str(),rpj_Alloc);
}
}
ReadSync(pc_reqCtx);
//ReadAsync2(pc_reqCtx);
//this->WriteObjectToResponse(pc_reqCtx,rpj_Doc);
// add metric
long value = rand() % 1000 + 1;
p_histo->update(value);
} }
//ReadSync(pc_reqCtx);
ReadAsync2(pc_reqCtx);
//this->WriteObjectToResponse(pc_reqCtx,rpj_Doc);
// add metric
long value = rand() % 1000 + 1;
p_histo->update(value);
}
void ReadSync(cMicroservice_RequestContext *pc_reqCtx) { void ReadSync(cMicroservice_RequestContext *pc_reqCtx) {
cMicroservice_BaseRestResponse rest_response; Microservice_RestResponse rest_response;
std::map<std::string,std::string> headers;
headers["X-IPgallery"] = "OK";
MSCommandParams cmd_params; MSCommandParams cmd_params;
cmd_params cmd_params
.WithEntity("http://172.16.1.132:5000/v1") .WithEntity("http://172.16.1.132:5000/v1")
.WithParamsString("search") .WithParamsString("search")
.WithRequestParams("q=base"); .WithRequestParams("q=base")
.WithHeadersMap(&headers);
MSRetStat retstat = p_client_->Read(&cmd_params, &rest_response); MSRetStat retstat = p_client_->Read(&cmd_params, &rest_response);
if(retstat.IsSuccess()) if(retstat.IsSuccess())
WriteObjectToResponse(pc_reqCtx, rest_response); WriteObjectToResponse(pc_reqCtx, rest_response);
else else
SendErrorResp(pc_reqCtx, retstat.GetError()); SendErrorResp(pc_reqCtx, retstat.GetError());
} }
void DoUpdate(cMicroservice_RequestContext* pc_reqCtx) void DoUpdate(cMicroservice_RequestContext* pc_reqCtx)
{ {
} }
void DoDelete(cMicroservice_RequestContext* pc_reqCtx) void DoDelete(cMicroservice_RequestContext* pc_reqCtx)
{ {
} }
/**
* reading async
* @param pc_reqCtx
*/
void ReadAsync(cMicroservice_RequestContext* pc_reqCtx) void ReadAsync(cMicroservice_RequestContext* pc_reqCtx)
{ {
ClientAsyncTaskParamsPtr clientAsyncTaskParamsPtr = std::make_shared<ClientAsyncTaskParams>(pc_reqCtx->mpti_Response,pc_reqCtx->mpti_Container); ClientAsyncTaskParamsPtr clientAsyncTaskParamsPtr = std::make_shared<ClientAsyncTaskParams>(pc_reqCtx->mpti_Response,pc_reqCtx->mpti_Container);
...@@ -96,14 +105,21 @@ public: ...@@ -96,14 +105,21 @@ public:
.WithParamsString("search") .WithParamsString("search")
.WithRequestParams("q=base"); .WithRequestParams("q=base");
auto readTask = p_client_->AsyncRead(clientAsyncTaskParamsPtr); //&cmd_params,&rest_response); try {
readTask.then([clientAsyncTaskParamsPtr](MSRetStat retStat){ auto readTask = p_client_->AsyncRead(clientAsyncTaskParamsPtr); //&cmd_params,&rest_response);
if(retStat.IsSuccess()) readTask.then([clientAsyncTaskParamsPtr](MSRetStat retStat) {
clientAsyncTaskParamsPtr->p_IContainer_->WriteObjectToResponse(clientAsyncTaskParamsPtr->p_IResponse_.get(), if (retStat.IsSuccess())
*clientAsyncTaskParamsPtr->p_baseRestResoonse_); clientAsyncTaskParamsPtr->p_IContainer_->WriteObjectToResponse(
else clientAsyncTaskParamsPtr->p_IResponse_.get(),
clientAsyncTaskParamsPtr->p_IContainer_->SendErrorResp(clientAsyncTaskParamsPtr->p_IResponse_.get(),retStat.GetError()); *clientAsyncTaskParamsPtr->p_baseRestResoonse_);
}); else
clientAsyncTaskParamsPtr->p_IContainer_->SendErrorResp(clientAsyncTaskParamsPtr->p_IResponse_.get(),
retStat.GetError());
});
} catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
std::cout << " after\n"; std::cout << " after\n";
} }
...@@ -114,103 +130,84 @@ public: ...@@ -114,103 +130,84 @@ public:
*/ */
void ReadAsync2(cMicroservice_RequestContext* pc_reqCtx) void ReadAsync2(cMicroservice_RequestContext* pc_reqCtx)
{ {
/** try {
* task 1
*/
ClientAsyncTaskParamsPtr clientAsyncTaskParamsPtr1 = std::make_shared<ClientAsyncTaskParams>(pc_reqCtx->mpti_Response,pc_reqCtx->mpti_Container);
clientAsyncTaskParamsPtr1->p_command_params_->WithEntity("http://172.16.1.132:5000/v1")
.WithParamsString("search")
.WithRequestParams("q=base");
auto readTask1 = p_client_->AsyncRead(clientAsyncTaskParamsPtr1); //&cmd_params,&rest_response); /**
/** * task 1
* task 2 */
*/ ClientAsyncTaskParamsPtr clientAsyncTaskParamsPtr1 = std::make_shared<ClientAsyncTaskParams>(pc_reqCtx->mpti_Response,pc_reqCtx->mpti_Container);
ClientAsyncTaskParamsPtr clientAsyncTaskParamsPtr2 = std::make_shared<ClientAsyncTaskParams>(pc_reqCtx->mpti_Response,pc_reqCtx->mpti_Container); clientAsyncTaskParamsPtr1->p_command_params_->WithEntity("http://172.16.1.132:5000/v1")
clientAsyncTaskParamsPtr2->p_command_params_->WithEntity("http://172.16.1.132:5000/v1") .WithParamsString("search")
.WithParamsString("search") .WithRequestParams("q=base");
.WithRequestParams("q=ipgallery");
auto readTask1 = p_client_->AsyncRead(clientAsyncTaskParamsPtr1); //&cmd_params,&rest_response);
auto readTask2 = p_client_->AsyncRead(clientAsyncTaskParamsPtr2); //&cmd_params,&rest_response); /**
* task 2
auto both = readTask1 && readTask2; */
ClientAsyncTaskParamsPtr clientAsyncTaskParamsPtr2 = std::make_shared<ClientAsyncTaskParams>(pc_reqCtx->mpti_Response,pc_reqCtx->mpti_Container);
/** clientAsyncTaskParamsPtr2->p_command_params_->WithEntity("http://172.16.1.132:5000/v1")
* waiting on both and combining the results .WithParamsString("search")
* to one json .WithRequestParams("q=ipgallery");
*/
both.then([clientAsyncTaskParamsPtr1,clientAsyncTaskParamsPtr2](std::vector<MSRetStat> retStats){ auto readTask2 = p_client_->AsyncRead(clientAsyncTaskParamsPtr2); //&cmd_params,&rest_response);
rapidjson::Document rpj_Doc;
rapidjson::Document::AllocatorType& rpj_Alloc = rpj_Doc.GetAllocator(); auto both = readTask1 && readTask2;
rpj_Doc.SetObject();
for (auto retstat : retStats){ /**
if (!retstat.IsSuccess()){ * waiting on both and combining the results
clientAsyncTaskParamsPtr1->p_IContainer_->SendErrorResp(clientAsyncTaskParamsPtr1->p_IResponse_.get(),retstat.GetError()); * to one json
return; */
both.then([clientAsyncTaskParamsPtr1,clientAsyncTaskParamsPtr2](std::vector<MSRetStat> retStats){
rapidjson::Document rpj_Doc;
rapidjson::Document::AllocatorType& rpj_Alloc = rpj_Doc.GetAllocator();
rpj_Doc.SetObject();
for (auto retstat : retStats){
if (!retstat.IsSuccess()){
clientAsyncTaskParamsPtr1->p_IContainer_->SendErrorResp(clientAsyncTaskParamsPtr1->p_IResponse_.get(),retstat.GetError());
return;
}
} }
} rpj_Doc.AddMember("first",clientAsyncTaskParamsPtr1->p_baseRestResoonse_->GetObjectNode()["results"],rpj_Alloc);
rpj_Doc.AddMember("first",clientAsyncTaskParamsPtr1->p_baseRestResoonse_->GetObjectNode()["results"],rpj_Alloc); rpj_Doc.AddMember("second",clientAsyncTaskParamsPtr2->p_baseRestResoonse_->GetObjectNode()["results"],rpj_Alloc);
rpj_Doc.AddMember("second",clientAsyncTaskParamsPtr2->p_baseRestResoonse_->GetObjectNode()["results"],rpj_Alloc); clientAsyncTaskParamsPtr1->p_IContainer_->WriteObjectToResponse(clientAsyncTaskParamsPtr1->p_IResponse_.get(),rpj_Doc);
clientAsyncTaskParamsPtr1->p_IContainer_->WriteObjectToResponse(clientAsyncTaskParamsPtr1->p_IResponse_.get(),rpj_Doc); });
}); } catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
std::cout << " after\n"; std::cout << " after\n";
} }
void Init() { void Init() {
std::string other_service("other-service"); std::string other_service("other-service");
auto port = this->mpc_Configuration->GetLong(std::string("server.port"),8000); auto port = this->mpc_Configuration->GetLong(std::string("server.port"),8000);
//printf("port is: %u\n",(unsigned)port); //printf("port is: %u\n",(unsigned)port);
p_client_ = GetApp()->GetMSClient(other_service); p_client_ = GetApp()->GetMSClient(other_service);
srand (time(NULL)); srand (time(NULL));
std::string str("randy.random"); std::string str("randy.random");
p_histo = this->GetApp()->GetMetricsFactory()->createHistogram(str); p_histo = this->GetApp()->GetMetricsFactory()->createHistogram(str);
} }
}; };
void runNewMS(){ void runNewMS(){
cMicroservice_BaseClientParams clientParams("other-service", true, 10, false,"localhost",32010,"localhost:6379"); cMicroservice_BaseClientParams clientParams("other-service", true, 10, false,"localhost",32010,"localhost:6379");
cMicroservice_App msApp("myCppService"); cMicroservice_App msApp("myCppService");
msApp msApp
.withMetrics() .withMetrics()
.withMonitoring() // need to add reload .withMonitoring() // need to add reload
.withPubSub(NULL) .withPubSub(NULL)
.withServiceDiscovery(NULL) .withServiceDiscovery(NULL)
.addMicroserviceClient(new cMicroservice_Client(new MSICommandClientHttpImpl(),&clientParams)) .addMicroserviceClient(new cMicroservice_Client(new MSICommandClientHttpImpl(),&clientParams))
.addRestServer(new cMicroservice_IRestServerMongooseImpl(new cMicroservice_RestServerParams(50010,"",1))) .addRestServer(new cMicroservice_IRestServerMongooseImpl(new cMicroservice_RestServerParams(50010,"",1)))
.addHandler("/xxx",new cMicroserviceHandler("hello")) .addHandler("/xxx",new cMicroserviceHandler("hello"))
.build() .build()
.run(); .run();
} }
//void runOldMS(char** argv){
// int port = atoi(argv[3]);
// std::string host(argv[2]);
// cMicroservice_RestServerParams* pc_RsiParams = new cMicroservice_RestServerParams(port,host,1);
//
//
//
// cMicroservice_RMQServerParams* pc_MbiParams = NULL;
// const char* pba_AppName = argv[1];
// cMicroserviceHandler c_MSH(argv[5]);
//
// cMicroservice_App* pc_App = new cMicroservice_App(pc_RsiParams,pc_MbiParams,pba_AppName);
// pc_App->AddHandler(argv[4],&c_MSH);
//
// // start
// printf("Starting App...\n");
// pc_App->StartApp();
// printf("Started Waiting for CTRL-C...\n");
//
// // pause
// pause();
// printf("Stopping App...\n");
// //
// pc_App->StopApp();
//}
void testCache(){ void testCache(){
using CacheClient = nsMicroservice_Iface::ICacheClient; using CacheClient = nsMicroservice_Iface::ICacheClient;
using Str = std::string; using Str = std::string;
...@@ -265,18 +262,18 @@ void test_timers() ...@@ -265,18 +262,18 @@ void test_timers()
*/ */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// testCache(); // testCache();
// test_timers(); // test_timers();
runNewMS(); runNewMS();
if (argc < 6) if (argc < 6)
{ {
printf("Usage: Test_Microservice app-name host port handler-prefix get-returned-string\n"); printf("Usage: Test_Microservice app-name host port handler-prefix get-returned-string\n");
return 0; return 0;
} }
//runOldMS(argv); //runOldMS(argv);
} }
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