Commit 4494ebbc by Amir Aharon

add all async methods

parent 3e7b41bd
...@@ -163,6 +163,17 @@ ClientRespAsyncTask cMicroservice_Client::AsyncSend(ClientAsyncTaskParamsPtr &cl ...@@ -163,6 +163,17 @@ ClientRespAsyncTask cMicroservice_Client::AsyncSend(ClientAsyncTaskParamsPtr &cl
} }
void cMicroservice_Client::AsyncCreate(MSCommandParams* p_cmdParams,CrudHandler crudHandler){ void cMicroservice_Client::AsyncCreate(MSCommandParams* p_cmdParams,CrudHandler crudHandler){
p_commandClient_->AsyncCreate(p_cmdParams,crudHandler); p_commandClient_->AsyncCreate(p_cmdParams,crudHandler);
} }
void cMicroservice_Client::AsyncRead(MSCommandParams* p_cmdParams,CrudHandler crudHandler){
p_commandClient_->AsyncRead(p_cmdParams,crudHandler);
}
void cMicroservice_Client::AsyncUpdate(MSCommandParams* p_cmdParams,CrudHandler crudHandler){
p_commandClient_->AsyncUpdate(p_cmdParams,crudHandler);
}
void cMicroservice_Client::AsyncDelete(MSCommandParams* p_cmdParams,CrudHandler crudHandler){
p_commandClient_->AsyncDelete(p_cmdParams,crudHandler);
}
...@@ -146,6 +146,9 @@ public: ...@@ -146,6 +146,9 @@ public:
// async only clients // async only clients
void AsyncCreate(MSCommandParams* p_cmdParams,CrudHandler crudHandler); void AsyncCreate(MSCommandParams* p_cmdParams,CrudHandler crudHandler);
void AsyncRead(MSCommandParams* p_cmdParams,CrudHandler crudHandler);
void AsyncUpdate(MSCommandParams* p_cmdParams,CrudHandler crudHandler);
void AsyncDelete(MSCommandParams* p_cmdParams,CrudHandler crudHandler);
}; };
#endif /* MICROSERVICE_CLIENT_H */ #endif /* MICROSERVICE_CLIENT_H */
......
...@@ -218,6 +218,9 @@ namespace nsMicroservice_Iface ...@@ -218,6 +218,9 @@ namespace nsMicroservice_Iface
* *
*/ */
virtual MSRetStat AsyncCreate(MSCommandParams* p_cmd_params,CrudHandler crudHandler) { return MSRetStat(); } virtual MSRetStat AsyncCreate(MSCommandParams* p_cmd_params,CrudHandler crudHandler) { return MSRetStat(); }
virtual MSRetStat AsyncRead(MSCommandParams* p_cmd_params,CrudHandler crudHandler) { return MSRetStat(); }
virtual MSRetStat AsyncUpdate(MSCommandParams* p_cmd_params,CrudHandler crudHandler) { return MSRetStat(); }
virtual MSRetStat AsyncDelete(MSCommandParams* p_cmd_params,CrudHandler crudHandler) { return MSRetStat(); }
}; };
struct IMetricsFactory struct IMetricsFactory
......
...@@ -80,6 +80,21 @@ MSRetStat MSICommandClientEvppImpl::AsyncCreate(MSCommandParams* p_cmd_params,Cr ...@@ -80,6 +80,21 @@ MSRetStat MSICommandClientEvppImpl::AsyncCreate(MSCommandParams* p_cmd_params,Cr
return HandleAsyncCommandAndCallback<evpp::httpc::PostEvppRequest>(hacdPtr); return HandleAsyncCommandAndCallback<evpp::httpc::PostEvppRequest>(hacdPtr);
} }
MSRetStat MSICommandClientEvppImpl::AsyncRead(MSCommandParams* p_cmd_params,CrudHandler crudHandler){
HandleAsyncCommandDataPtr hacdPtr = std::make_shared<HandleAsyncCommandData>(p_cmd_params,&create_counters_,cMicroservice_Enums::eCrudMethod::eRead,crudHandler);
return HandleAsyncCommandAndCallback<evpp::httpc::GetEvppRequest>(hacdPtr);
}
MSRetStat MSICommandClientEvppImpl::AsyncUpdate(MSCommandParams* p_cmd_params,CrudHandler crudHandler){
HandleAsyncCommandDataPtr hacdPtr = std::make_shared<HandleAsyncCommandData>(p_cmd_params,&create_counters_,cMicroservice_Enums::eCrudMethod::eUpdate,crudHandler);
return HandleAsyncCommandAndCallback<evpp::httpc::PutEvppRequest>(hacdPtr);
}
MSRetStat MSICommandClientEvppImpl::AsyncDelete(MSCommandParams* p_cmd_params,CrudHandler crudHandler){
HandleAsyncCommandDataPtr hacdPtr = std::make_shared<HandleAsyncCommandData>(p_cmd_params,&create_counters_,cMicroservice_Enums::eCrudMethod::eDelete,crudHandler);
return HandleAsyncCommandAndCallback<evpp::httpc::DeleteEvppRequest>(hacdPtr);
}
bool MSICommandClientEvppImpl::BuildUrl(MSCommandParams *p_cmd_params, std::string &url) bool MSICommandClientEvppImpl::BuildUrl(MSCommandParams *p_cmd_params, std::string &url)
{ {
//std::string unencoded_url; //std::string unencoded_url;
...@@ -209,7 +224,8 @@ template <typename Req> ...@@ -209,7 +224,8 @@ template <typename Req>
MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCommandDataPtr& handleAsyncCommandDataPtr){ MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCommandDataPtr& handleAsyncCommandDataPtr){
MSRetStat retstat; MSRetStat retstat;
//evpp::httpc::EvppRequestPtr requestPtr = nullptr; //evpp::httpc::EvppRequestPtr requestPtr = nullptr;
std::shared_ptr<Req> requestPtr = nullptr; // std::shared_ptr<Req> requestPtr = nullptr;
Req* requestPtr = nullptr;
std::string url; std::string url;
if (BuildUrl(handleAsyncCommandDataPtr->p_cmd_params, url)) if (BuildUrl(handleAsyncCommandDataPtr->p_cmd_params, url))
...@@ -217,24 +233,24 @@ MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCom ...@@ -217,24 +233,24 @@ MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCom
switch (handleAsyncCommandDataPtr->method) switch (handleAsyncCommandDataPtr->method)
{ {
case cMicroservice_Enums::eCrudMethod::eCreate: case cMicroservice_Enums::eCrudMethod::eCreate:
requestPtr = std::make_shared<Req>(loopThread_.loop(), requestPtr = new Req(loopThread_.loop(),
url, url,
handleAsyncCommandDataPtr->p_cmd_params->GetContent().c_str(), handleAsyncCommandDataPtr->p_cmd_params->GetContent().c_str(),
evpp::Duration(requestTimeout_)); evpp::Duration(requestTimeout_));
break; break;
case cMicroservice_Enums::eCrudMethod::eRead: case cMicroservice_Enums::eCrudMethod::eRead:
requestPtr = std::make_shared<Req>(loopThread_.loop(), requestPtr = new Req(loopThread_.loop(),
url, url,
evpp::Duration(requestTimeout_)); evpp::Duration(requestTimeout_));
break; break;
case cMicroservice_Enums::eCrudMethod::eUpdate: case cMicroservice_Enums::eCrudMethod::eUpdate:
requestPtr = std::make_shared<Req>(loopThread_.loop(), requestPtr = new Req(loopThread_.loop(),
url, url,
handleAsyncCommandDataPtr->p_cmd_params->GetContent().c_str(), handleAsyncCommandDataPtr->p_cmd_params->GetContent().c_str(),
evpp::Duration(requestTimeout_)); evpp::Duration(requestTimeout_));
break; break;
case cMicroservice_Enums::eCrudMethod::eDelete: case cMicroservice_Enums::eCrudMethod::eDelete:
requestPtr = std::make_shared<Req>(loopThread_.loop(), requestPtr = new Req(loopThread_.loop(),
url, url,
evpp::Duration(requestTimeout_)); evpp::Duration(requestTimeout_));
break; break;
...@@ -242,12 +258,21 @@ MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCom ...@@ -242,12 +258,21 @@ MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCom
if (requestPtr) if (requestPtr)
{ {
/**
* Add Headers
*/
auto& headers = handleAsyncCommandDataPtr->p_cmd_params->GetHeadersMap();
if (!headers.empty()){
requestPtr->add_headers(headers);
}
/** /**
* passing requestPtr here to increment the reference, so that * passing requestPtr here to increment the reference, so that
* the request will be deleted from the loop thread * the request will be deleted from the loop thread
* this is mandatory * this is mandatory
*/ */
requestPtr->Execute([this,handleAsyncCommandDataPtr,requestPtr](const std::shared_ptr<evpp::httpc::EvppResponse> &responsePtr) { requestPtr->Execute([this,handleAsyncCommandDataPtr,requestPtr](const std::shared_ptr<evpp::httpc::EvppResponse> &responsePtr) {
try {
register int resp_code = responsePtr->http_code(); register int resp_code = responsePtr->http_code();
handleAsyncCommandDataPtr->finished = true; handleAsyncCommandDataPtr->finished = true;
if (resp_code == 0){ if (resp_code == 0){
...@@ -257,6 +282,16 @@ MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCom ...@@ -257,6 +282,16 @@ MSRetStat MSICommandClientEvppImpl::HandleAsyncCommandAndCallback(HandleAsyncCom
} else { } else {
this->handleAsyncRequestFailed(handleAsyncCommandDataPtr,nsMicroservice_Constants::REQUEST_ERROR); this->handleAsyncRequestFailed(handleAsyncCommandDataPtr,nsMicroservice_Constants::REQUEST_ERROR);
} }
} catch (const std::exception& e ){
this->handleAsyncRequestFailed(handleAsyncCommandDataPtr,e.what());
}
// FINALLY
/**
* The request MUST BE deleted in EventLoop thread.
* if we use shared_ptr for requestPtr, we could end up
* with a case where this call back executed before the enclosing block.
*/
delete requestPtr;
}); });
} else { } else {
retstat.SetError("Unsupported method"); retstat.SetError("Unsupported method");
...@@ -308,14 +343,16 @@ void MSICommandClientEvppImpl::handleAsyncRequestSuccess(const ICommandClient::H ...@@ -308,14 +343,16 @@ void MSICommandClientEvppImpl::handleAsyncRequestSuccess(const ICommandClient::H
* and parse it * and parse it
*/ */
if (responsePtr->body().size()){ if (responsePtr->body().size()){
const char* respBody = responsePtr->body().data(); std::string respBody = responsePtr->body().ToString(); // must use ToString, since it an overrun buffer
if(!doc.Parse<0>(respBody).HasParseError()) { std::cout << "Parsing body: " << respBody << std::endl;
if(!doc.Parse<0>(respBody.c_str()).HasParseError()) {
asyncCmdDataPtr->p_command_counters->succeed++; asyncCmdDataPtr->p_command_counters->succeed++;
brr.setResponse_code(responsePtr->http_code()); brr.setResponse_code(responsePtr->http_code());
brr.setCommandId(asyncCmdDataPtr->p_cmd_params->GetCommandId()); brr.setCommandId(asyncCmdDataPtr->p_cmd_params->GetCommandId());
asyncCmdDataPtr->crudHandler(&brr); asyncCmdDataPtr->crudHandler(&brr);
} else { } else {
handleAsyncRequestFailed(asyncCmdDataPtr,rapidjson::GetParseError_En(doc.GetParseError())); handleAsyncRequestFailed(asyncCmdDataPtr,rapidjson::GetParseError_En(doc.GetParseError()));
} }
} else { } else {
brr.setResponse_code(responsePtr->http_code()); brr.setResponse_code(responsePtr->http_code());
......
...@@ -47,6 +47,9 @@ class MSICommandClientEvppImpl : public ICommandClient ...@@ -47,6 +47,9 @@ class MSICommandClientEvppImpl : public ICommandClient
MSRetStat Delete(MSCommandParams *p_cmd_params, cMicroservice_BaseRestResponse *p_response) override; MSRetStat Delete(MSCommandParams *p_cmd_params, cMicroservice_BaseRestResponse *p_response) override;
MSRetStat AsyncCreate(MSCommandParams* p_cmd_params,CrudHandler crudHandler) override; MSRetStat AsyncCreate(MSCommandParams* p_cmd_params,CrudHandler crudHandler) override;
MSRetStat AsyncRead(MSCommandParams* p_cmd_params,CrudHandler crudHandler) override;
MSRetStat AsyncUpdate(MSCommandParams* p_cmd_params,CrudHandler crudHandler) override;
MSRetStat AsyncDelete(MSCommandParams* p_cmd_params,CrudHandler crudHandler) override;
}; };
#endif // MSICOMMANDCLIENTEVPPIMPL_H #endif // MSICOMMANDCLIENTEVPPIMPL_H
\ No newline at end of file
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