// // Created by zx on 2020/6/18. // #include "Locate_communicator.h" //#include "locate_message.pb.h" Locate_communicator::Locate_communicator() { } Locate_communicator::~Locate_communicator() { } Error_manager Locate_communicator::encapsulate_msg(Communication_message* message) { //记录请求 if(message->get_message_type()==Communication_message::eLocate_request_msg) { message::Measure_request_msg request; m_response_table[request.command_id()]=message::Measure_response_msg(); } else { return Error_manager(LOCATER_MSG_TABLE_NOT_EXIST,NEGLIGIBLE_ERROR,"message table is not exist"); } //发送请求 return Communication_socket_base::encapsulate_msg(message); } Error_manager Locate_communicator::locate_request(message::Measure_request_msg& request,message::Measure_response_msg& result) { /* * 检查request合法性,以及模块状态 */ if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eMeasurer) return Error_manager(LOCATER_MSG_REQUEST_INVALID,MINOR_ERROR,"measure request invalid"); //设置超时,若没有设置,默认3000 int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():3000; //向测量节点发送测量请求,并记录请求 Error_manager code; Communication_message message; message::Base_info base_msg; base_msg.set_msg_type(message::eLocate_request_msg); base_msg.set_sender(message::eMain); base_msg.set_receiver(message::eMeasurer); base_msg.set_timeout_ms(timeout); message.reset(base_msg,request.SerializeAsString()); code=encapsulate_msg(&message); if(code!=SUCCESS) return code; //循环查询请求是否被处理 auto start_time=std::chrono::system_clock::now(); double time=0; do{ //在请求表中查询结果 message::Measure_response_msg response=m_response_table[request.command_id()]; //判断是否接收到回应,若回应信息被赋值则证明有回应 if(response.has_base_info() && response.has_command_id()) { message::Base_info response_base=response.base_info(); //检查类型是否匹配 if(response_base.msg_type() != message::eLocate_response_msg) { return Error_manager(LOCATER_MSG_RESPONSE_TYPE_ERROR,MAJOR_ERROR,"measure response msg type error"); } //检查基本信息是否匹配 if(response_base.sender()!=message::eMeasurer || response_base.receiver()!=message::eMain|| response.command_id()!=request.command_id()) { return Error_manager(LOCATER_MSG_RESPONSE_INFO_ERROR,MAJOR_ERROR,"measure response msg info error"); } result=response; return SUCCESS; } auto end_time=std::chrono::system_clock::now(); auto duration = std::chrono::duration_cast(end_time - start_time); time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den; std::this_thread::yield(); }while(timeget_message_type()) { ///测量结果反馈消息 case Communication_message::eLocate_response_msg: { message::Measure_response_msg response; response.ParseFromString(p_msg->get_message_buf()); ///查询请求表是否存在 if(m_response_table.find(response.command_id())==m_response_table.end()) { return Error_manager(LOCATER_MSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"measure response without request"); } ////更新表 m_response_table[response.command_id()]=response; break; } ///测量系统状态 case Communication_message::eLocate_status_msg: { m_measure_statu_msg=*p_msg; break; } } }