// // Created by huli on 2020/7/2. // #include "system_executor.h" #include "../message/measure_message.pb.h" #include "../laser/laser_manager.h" #include "../locate/locate_manager.h" #include "../system/system_communication.h" System_executor::System_executor() { } System_executor::~System_executor() { system_executor_uninit(); } //初始化 Error_manager System_executor::system_executor_init(int threads_size) { m_thread_pool.thread_pool_init(threads_size); m_system_executor_status = SYSTEM_EXECUTOR_READY; return Error_code::SUCCESS; } //反初始化 Error_manager System_executor::system_executor_uninit() { m_thread_pool.thread_pool_uninit(); m_system_executor_status = SYSTEM_EXECUTOR_UNKNOW; return Error_code::SUCCESS; } //检查执行者的状态, 判断能否处理这条消息, Error_manager System_executor::check_executer(Communication_message* p_msg) { if ( p_msg == NULL ) { return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR, " POINTER IS NULL "); } Error_manager t_error; //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 找到处理模块的实例对象, 查询执行人是否可以处理这条消息 switch ( p_msg->get_message_type() ) { case Communication_message::Message_type::eLocate_request_msg: { Error_manager t_laser_result = Laser_manager::get_instance_references().check_status(); Error_manager t_executor_result = System_executor::get_instance_references().check_status(); Error_manager t_locate_result = Locate_manager::get_instance_references().check_status(); if (t_laser_result == SUCCESS && t_executor_result == SUCCESS && t_locate_result == SUCCESS) { return Error_code::SUCCESS; } else { //整合所有的错误码 t_error.compare_and_cover_error(t_laser_result); t_error.compare_and_cover_error(t_executor_result); t_error.compare_and_cover_error(t_locate_result); if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障, { std::cout << "executer_is_busy , " << std::endl; //返回繁忙之后, 通信模块1秒后再次调用check return Error_code::COMMUNICATION_EXCUTER_IS_BUSY; } else//返回二级故障,可以封装一条答复信息, 返回错误码 { message::Measure_request_msg t_measure_request_msg; //针对消息类型, 对消息进行二次解析 if (t_measure_request_msg.ParseFromString(p_msg->get_message_buf())) { //创建一条答复消息 message::Measure_response_msg t_measure_response_msg; t_measure_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eLocate_response_msg); t_measure_response_msg.mutable_base_info()->set_timeout_ms(5000); t_measure_response_msg.mutable_base_info()->set_sender(message::Communicator::eMeasurer); t_measure_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain); t_measure_response_msg.set_command_id(t_measure_request_msg.command_id()); t_measure_response_msg.set_terminal_id(t_measure_request_msg.terminal_id()); t_measure_response_msg.mutable_error_manager()->set_error_code(t_error.get_error_code()); t_measure_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level()); t_measure_response_msg.mutable_error_manager()->set_error_description(t_error.get_error_description()); string t_msg = t_measure_response_msg.SerializeAsString(); System_communication::get_instance_references().encapsulate_msg(t_msg); LOG(INFO) << " System_executor::check_executer executer status error "<< this; return t_error; } else { LOG(INFO) << " System_executor::check_executer second PARSE_ERROR "<< this; return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR, " message::Measure_request_msg ParseFromString error "); } } } break; } default : ; break; } return t_error; } //处理消息的执行函数 Error_manager System_executor::execute_msg(Communication_message* p_msg) { if ( p_msg == NULL ) { return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR, " POINTER IS NULL "); } switch ( p_msg->get_message_type() ) { case Communication_message::eLocate_request_msg: message::Measure_request_msg t_measure_request_msg; //针对消息类型, 对消息进行二次解析 if( t_measure_request_msg.ParseFromString(p_msg->get_message_buf()) ) { //往线程池添加执行任务, 之后会唤醒一个线程去执行他. m_thread_pool.enqueue(&System_executor::execute_for_measure, this, t_measure_request_msg.command_id(), t_measure_request_msg.terminal_id()); } else { return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR, " message::Measure_request_msg ParseFromString error "); } break; } return Error_code::SUCCESS; } //检查状态 Error_manager System_executor::check_status() { if ( m_system_executor_status == SYSTEM_EXECUTOR_READY ) { if ( m_thread_pool.thread_is_full_load() == false ) { return Error_code::SUCCESS; } else { return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR, " System_executor::check_status error "); } } else { return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_ERROR, Error_level::MINOR_ERROR, " System_executor::check_status error "); } } //判断是否为待机,如果已经准备好,则可以执行任务。 bool System_executor::is_ready() { if ( m_system_executor_status == SYSTEM_EXECUTOR_READY && m_thread_pool.thread_is_full_load() == false ) { return true; } else { return false; } } System_executor::System_executor_status System_executor::get_system_executor_status() { return m_system_executor_status; } //雷达感测定位 的处理函数 //input::command_id, 消息指令id, 由主控制系统生成的唯一码 //input::command_id, 终端id, 对应具体的某个车位 //return::void, 没有返回, 执行结果直接生成一条答复消息, 然后通过通信返回 void System_executor::execute_for_measure(int command_id, int terminal_id) { Error_manager t_error; LOG(INFO) << " System_executor::execute_for_measure run "<< this; //这里要处理.......以后再写 //创建一条答复消息 message::Measure_response_msg t_measure_response_msg; t_measure_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eLocate_response_msg); t_measure_response_msg.mutable_base_info()->set_timeout_ms(5000); t_measure_response_msg.mutable_base_info()->set_sender(message::Communicator::eMeasurer); t_measure_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain); t_measure_response_msg.set_command_id(command_id); t_measure_response_msg.set_terminal_id(terminal_id); t_measure_response_msg.mutable_error_manager()->set_error_code(t_error.get_error_code()); t_measure_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level()); t_measure_response_msg.mutable_error_manager()->set_error_description(t_error.get_error_description(), t_error.get_description_length()); t_measure_response_msg.mutable_locate_information()->set_locate_x(0); t_measure_response_msg.mutable_locate_information()->set_locate_y(0); t_measure_response_msg.mutable_locate_information()->set_locate_angle(0); t_measure_response_msg.mutable_locate_information()->set_locate_length(0); t_measure_response_msg.mutable_locate_information()->set_locate_width(0); t_measure_response_msg.mutable_locate_information()->set_locate_height(0); t_measure_response_msg.mutable_locate_information()->set_locate_wheel_base(0); t_measure_response_msg.mutable_locate_information()->set_locate_wheel_width(0); t_measure_response_msg.mutable_locate_information()->set_locate_correct(0); string t_msg = t_measure_response_msg.SerializeAsString(); System_communication::get_instance_references().encapsulate_msg(t_msg); return ; }