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
void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){
void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
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;
}
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);
//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,"*/*");
if(!p_cmd_data->p_cmd_params->GetContent().empty())
request.set_body(p_cmd_data->p_cmd_params->GetContent(),"application/json");
//auto request_task = client.request(*p_cmd_data->p_mtd);
if(!cmdDataPtr->p_cmd_params->GetContent().empty())
request.set_body(cmdDataPtr->p_cmd_params->GetContent(),"application/json");
//auto request_task = client.request(*cmdDataPtr->p_mtd);
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)
{
p_cmd_data->p_command_counters->succeed++;
cmdDataPtr->p_command_counters->succeed++;
}
else
{
std::stringstream ss;
ss << resp.status_code() << " - " << resp.reason_phrase();
p_cmd_data->p_retstat->SetError(ss.str().c_str());
LOG_ERROR(ss.str());
p_cmd_data->p_command_counters->failed++;
}
{
std::stringstream ss;
ss << resp.status_code() << " - " << resp.reason_phrase();
cmdDataPtr->p_retstat->SetError(ss.str().c_str());
LOG_ERROR(ss.str());
cmdDataPtr->p_command_counters->failed++;
}
});
}
else
{
......@@ -123,15 +123,15 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){
utility::string_t content = resp.extract_string().get();
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())
{
p_cmd_data->p_command_counters->succeed++;
cmdDataPtr->p_command_counters->succeed++;
}
else
{
p_cmd_data->p_retstat->SetError(doc.GetParseError());
p_cmd_data->p_command_counters->failed++;
cmdDataPtr->p_retstat->SetError(doc.GetParseError());
cmdDataPtr->p_command_counters->failed++;
}
}
}
......@@ -139,51 +139,51 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){
{
std::stringstream ss;
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());
p_cmd_data->p_command_counters->failed++;
cmdDataPtr->p_command_counters->failed++;
}
}
}
else
{
p_cmd_data->p_retstat->SetError(FAILED_BUILD_URL);
cmdDataPtr->p_retstat->SetError(FAILED_BUILD_URL);
}
}
catch (web::http::http_exception exp)
{
p_cmd_data->p_retstat->SetError(exp.what());
cmdDataPtr->p_retstat->SetError(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 retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::POST),&retstat,&create_counters_);
HandleCommand(&cmd_data);
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::POST),&retstat,&create_counters_));
HandleCommand(cmd_data);
return retstat;
}
MSRetStat MSICommandClientHttpImpl::Read(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::GET),&retstat,&read_counters_);
HandleCommand(&cmd_data);
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::GET),&retstat,&read_counters_));
HandleCommand(cmd_data);
return retstat;
}
MSRetStat MSICommandClientHttpImpl::Update(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::PUT),&retstat,&update_counters_);
HandleCommand(&cmd_data);
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::PUT),&retstat,&update_counters_));
HandleCommand(cmd_data);
return retstat;
}
MSRetStat MSICommandClientHttpImpl::Delete(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat;
HandleCommandData cmd_data(p_cmd_params,p_response,&(methods::DEL),&retstat,&delete_counters_);
HandleCommand(&cmd_data);
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::DEL),&retstat,&delete_counters_));
HandleCommand(cmd_data);
return retstat;
}
......
......@@ -50,6 +50,8 @@ public:
}
};
typedef std::shared_ptr<HandleCommandData> HttpCommandDataPtr;
MSICommandClientHttpImpl();
MSICommandClientHttpImpl(const MSICommandClientHttpImpl& orig);
virtual ~MSICommandClientHttpImpl();
......@@ -81,12 +83,14 @@ private:
* handle all the command flow
* @param p_cmd_data
*/
void HandleCommand(HandleCommandData* p_cmd_data);
void HandleCommand(HttpCommandDataPtr& cmdDataPtr);
void AddCounters(std::map<std::string, long>& metrics_map,
const char* name,
CommandCounters& cmd_counters);
};
#endif /* MSICOMMANDCLIENTHTTPIMPL_H */
......@@ -195,8 +195,8 @@ int main(int argc, char *argv[])
// testCache();
test_timers();
//runNewMS();
// test_timers();
runNewMS();
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