Commit 9dd7e10b by amir

change cmd data to shared ptr

parent 59b0e000
...@@ -77,43 +77,43 @@ bool MSICommandClientHttpImpl::BuildUrl(MSCommandParams* p_cmd_params, std::stri ...@@ -77,43 +77,43 @@ bool MSICommandClientHttpImpl::BuildUrl(MSCommandParams* p_cmd_params, std::stri
void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){ void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
std::string url; std::string url;
if(p_cmd_data->p_response == nullptr) if(cmdDataPtr->p_response == nullptr)
{ {
p_cmd_data->p_retstat->SetError(NULL_REST_RESPONSE_OBJECT); cmdDataPtr->p_retstat->SetError(NULL_REST_RESPONSE_OBJECT);
return; return;
} }
try try
{ {
if(BuildUrl(p_cmd_data->p_cmd_params,url)) if(BuildUrl(cmdDataPtr->p_cmd_params,url))
{ {
p_cmd_data->p_response->Reset(); cmdDataPtr->p_response->Reset();
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(*p_cmd_data->p_mtd); http_request request(*cmdDataPtr->p_mtd);
request.headers().add(header_names::accept,"*/*"); request.headers().add(header_names::accept,"*/*");
if(!p_cmd_data->p_cmd_params->GetContent().empty()) if(!cmdDataPtr->p_cmd_params->GetContent().empty())
request.set_body(p_cmd_data->p_cmd_params->GetContent(),"application/json"); request.set_body(cmdDataPtr->p_cmd_params->GetContent(),"application/json");
//auto request_task = client.request(*p_cmd_data->p_mtd); //auto request_task = client.request(*cmdDataPtr->p_mtd);
auto request_task = client.request(request); auto request_task = client.request(request);
if(p_cmd_data->p_cmd_params->IsAsync_()) if(cmdDataPtr->p_cmd_params->IsAsync_())
{ {
request_task.then([&](http_response resp){ request_task.then([this,cmdDataPtr](http_response resp){
if(resp.status_code() == status_codes::OK) if(resp.status_code() == status_codes::OK)
{ {
p_cmd_data->p_command_counters->succeed++; cmdDataPtr->p_command_counters->succeed++;
} }
else else
{ {
std::stringstream ss; std::stringstream ss;
ss << resp.status_code() << " - " << resp.reason_phrase(); ss << resp.status_code() << " - " << resp.reason_phrase();
p_cmd_data->p_retstat->SetError(ss.str().c_str()); cmdDataPtr->p_retstat->SetError(ss.str().c_str());
LOG_ERROR(ss.str()); LOG_ERROR(ss.str());
p_cmd_data->p_command_counters->failed++; cmdDataPtr->p_command_counters->failed++;
} }
}); });
} }
else else
{ {
...@@ -123,15 +123,15 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){ ...@@ -123,15 +123,15 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){
utility::string_t content = resp.extract_string().get(); utility::string_t content = resp.extract_string().get();
if(!content.empty()) if(!content.empty())
{ {
rapidjson::Document& doc = p_cmd_data->p_response->GetObjectNode(); rapidjson::Document& doc = cmdDataPtr->p_response->GetObjectNode();
if(!doc.Parse<0>(content.c_str()).HasParseError()) if(!doc.Parse<0>(content.c_str()).HasParseError())
{ {
p_cmd_data->p_command_counters->succeed++; cmdDataPtr->p_command_counters->succeed++;
} }
else else
{ {
p_cmd_data->p_retstat->SetError(doc.GetParseError()); cmdDataPtr->p_retstat->SetError(doc.GetParseError());
p_cmd_data->p_command_counters->failed++; cmdDataPtr->p_command_counters->failed++;
} }
} }
} }
...@@ -139,51 +139,51 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){ ...@@ -139,51 +139,51 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){
{ {
std::stringstream ss; std::stringstream ss;
ss << resp.status_code() << " - " << resp.reason_phrase(); ss << resp.status_code() << " - " << resp.reason_phrase();
p_cmd_data->p_retstat->SetError(ss.str().c_str()); cmdDataPtr->p_retstat->SetError(ss.str().c_str());
LOG_ERROR(ss.str()); LOG_ERROR(ss.str());
p_cmd_data->p_command_counters->failed++; cmdDataPtr->p_command_counters->failed++;
} }
} }
} }
else else
{ {
p_cmd_data->p_retstat->SetError(FAILED_BUILD_URL); cmdDataPtr->p_retstat->SetError(FAILED_BUILD_URL);
} }
} }
catch (web::http::http_exception exp) catch (web::http::http_exception exp)
{ {
p_cmd_data->p_retstat->SetError(exp.what()); cmdDataPtr->p_retstat->SetError(exp.what());
LOG_ERROR(exp.what()); LOG_ERROR(exp.what());
p_cmd_data->p_command_counters->failed++; cmdDataPtr->p_command_counters->failed++;
} }
} }
MSRetStat MSICommandClientHttpImpl::Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) { MSRetStat MSICommandClientHttpImpl::Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat; MSRetStat retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::POST),&retstat,&create_counters_); auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::POST),&retstat,&create_counters_));
HandleCommand(&cmd_data); HandleCommand(cmd_data);
return retstat; return retstat;
} }
MSRetStat MSICommandClientHttpImpl::Read(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) { MSRetStat MSICommandClientHttpImpl::Read(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat; MSRetStat retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::GET),&retstat,&read_counters_); auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::GET),&retstat,&read_counters_));
HandleCommand(&cmd_data); HandleCommand(cmd_data);
return retstat; return retstat;
} }
MSRetStat MSICommandClientHttpImpl::Update(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) { MSRetStat MSICommandClientHttpImpl::Update(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat; MSRetStat retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::PUT),&retstat,&update_counters_); auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::PUT),&retstat,&update_counters_));
HandleCommand(&cmd_data); HandleCommand(cmd_data);
return retstat; return retstat;
} }
MSRetStat MSICommandClientHttpImpl::Delete(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) { MSRetStat MSICommandClientHttpImpl::Delete(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat; MSRetStat retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::DEL),&retstat,&delete_counters_); auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::DEL),&retstat,&delete_counters_));
HandleCommand(&cmd_data); HandleCommand(cmd_data);
return retstat; return retstat;
} }
......
...@@ -50,6 +50,8 @@ public: ...@@ -50,6 +50,8 @@ public:
} }
}; };
typedef std::shared_ptr<HandleCommandData> HttpCommandDataPtr;
MSICommandClientHttpImpl(); MSICommandClientHttpImpl();
MSICommandClientHttpImpl(const MSICommandClientHttpImpl& orig); MSICommandClientHttpImpl(const MSICommandClientHttpImpl& orig);
virtual ~MSICommandClientHttpImpl(); virtual ~MSICommandClientHttpImpl();
...@@ -81,12 +83,14 @@ private: ...@@ -81,12 +83,14 @@ private:
* handle all the command flow * handle all the command flow
* @param p_cmd_data * @param p_cmd_data
*/ */
void HandleCommand(HandleCommandData* p_cmd_data); void HandleCommand(HttpCommandDataPtr& cmdDataPtr);
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);
}; };
#endif /* MSICOMMANDCLIENTHTTPIMPL_H */ #endif /* MSICOMMANDCLIENTHTTPIMPL_H */
...@@ -195,8 +195,8 @@ int main(int argc, char *argv[]) ...@@ -195,8 +195,8 @@ int main(int argc, char *argv[])
// testCache(); // testCache();
test_timers(); // test_timers();
//runNewMS(); runNewMS();
if (argc < 6) if (argc < 6)
{ {
......
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