// // Created by zx on 2020/6/18. // #include "measure_excutor.h" #include "uniq_key.h" Measure_excutor::Measure_excutor() { } Measure_excutor::~Measure_excutor() { } Error_manager Measure_excutor::locate_request(message::Measure_request_msg& request, message::Measure_response_msg& result,Thread_condition& cancel_condition) { /* * 检查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"); request.set_command_key(create_key()); if(m_response_table.find(request.command_key())==true) return Error_manager(LOCATER_MSG_REQUEST_REPEATED,MAJOR_ERROR," measure reques repeated"); //设置超时,若没有设置,默认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()); m_response_table[request.command_key()]=message::Measure_response_msg(); //发送请求 code= Message_communicator::get_instance_pointer()->send_msg(&message); if(code!=SUCCESS) { m_response_table.erase(request.command_key()); return code; } //循环查询请求是否被处理 auto start_time=std::chrono::system_clock::now(); double time=0; do{ //查询到记录 message::Measure_response_msg response; ///查询是否存在,并且删除该记录, if(m_response_table.find(request.command_key(),response)) { //判断是否接收到回应,若回应信息被赋值则证明有回应 if (response.has_base_info() && response.has_command_key()) { 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_key() == request.command_key())) { return Error_manager(LOCATER_MSG_RESPONSE_INFO_ERROR, MAJOR_ERROR, "measure response msg info error"); } result = response; m_response_table.erase(request.command_key()); return SUCCESS; } } else { //未查询到记录,任务已经被提前取消,记录被删除 return Error_manager(TASK_CANCEL,MINOR_ERROR,"measure request canceled"); } 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(); if(time>double(timeout)) { m_response_table.erase(request.command_key()); return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"measure request timeout"); } }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false); m_response_table.erase(request.command_key()); return Error_manager(TASK_CANCEL,MINOR_ERROR,"measure request timeout"); } Error_manager Measure_excutor::check_statu(int terminal_id) { if(m_measure_statu_msg_map.find(terminal_id)==false || m_statu_recv_time_map.find(terminal_id)==false) return Error_manager(FAILED,MINOR_ERROR,"测量节点状态不存在"); std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now(); auto durantion=time_now-m_statu_recv_time_map[terminal_id]; if(m_measure_statu_msg_map[terminal_id].has_base_info()== false || durantion>std::chrono::seconds(5)) { return Error_manager(DISCONNECT,MINOR_ERROR,"测量节点通讯断开"); } if(m_measure_statu_msg_map[terminal_id].laser_manager_status()==message::LASER_MANAGER_FAULT) { return Error_manager(ERROR,MINOR_ERROR,"测量模块扫描控制故障"); } for(int i=0;iget_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_update(response.command_key(),response)==false) { return Error_manager(LOCATER_MSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"measure response without request"); } break; } ///测量系统状态 case Communication_message::eLocate_status_msg: { message::Measure_status_msg statu_msg; if(statu_msg.ParseFromString(p_msg->get_message_buf())==false) return Error_manager(ERROR,CRITICAL_ERROR,"measure statu msg parse failed, CRITICAL_ERROR"); m_measure_statu_msg_map[statu_msg.terminal_id()]=statu_msg; m_statu_recv_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now(); break; } } return SUCCESS; } Error_manager Measure_excutor::cancel_request(message::Measure_request_msg& request) { if(m_response_table.find(request.command_key())==true) m_response_table.erase(request.command_key()); return SUCCESS; }