#include "parkspace_allocation_communicator.h" #include "parkspace_allocator.h" Parkspace_allocation_communicator::Parkspace_allocation_communicator() { } Parkspace_allocation_communicator::~Parkspace_allocation_communicator() { } //************************************* 公有函数 **************************************** //外部调用发送分配车位结果 Error_manager Parkspace_allocation_communicator::send_response(Communication_message *message) { return encapsulate_msg(message); } //外部调用更新所有车位信息/初始化所有车位 Error_manager Parkspace_allocation_communicator::update_parkspace_status(message::Parkspace_allocation_status_msg status_msg) { std::lock_guard lck(m_status_mutex); m_parkspace_status_msg.CopyFrom(status_msg); return SUCCESS; } //外部调用更新单一车位信息 Error_manager Parkspace_allocation_communicator::update_parkspace_status(int parkspace_id, message::Parkspace_info parkspace_info) { std::lock_guard lck(m_status_mutex); if(m_parkspace_status_msg.parkspace_info_size() > parkspace_id) { m_parkspace_status_msg.mutable_parkspace_info(parkspace_id)->CopyFrom(parkspace_info); } return SUCCESS; } //外部调用获取当前车位状态消息 message::Parkspace_allocation_status_msg Parkspace_allocation_communicator::get_status() { std::lock_guard lck(m_status_mutex); message::Parkspace_allocation_status_msg parkspace_status_msg; parkspace_status_msg.CopyFrom(m_parkspace_status_msg); return parkspace_status_msg; } //************************************* 内部函数 **************************************** //封装反馈消息自动发送 Error_manager Parkspace_allocation_communicator::encapsulate_msg(Communication_message *message) { if (message == nullptr) { return Error_manager(POINTER_IS_NULL, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message null pointer"); } //针对待发送的不同反馈消息,分别进行校核 switch(message->get_message_type()) { case Communication_message::eParkspace_allocation_response_msg: { // message::Parkspace_allocation_response_msg response; // bool result = response.ParseFromString(message->get_message_buf()); // //可增加待发送分配车位反馈消息校核 break; } case Communication_message::eParkspace_search_response_msg: { break; } case Communication_message::eParkspace_release_response_msg: { break; } case Communication_message::eParkspace_force_update_response_msg: { break; } case Communication_message::eParkspace_confirm_alloc_response_msg: { break; } default: return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message response type error"); } //发送请求 return Communication_socket_base::encapsulate_msg(message); } //重载执行消息,父类线程通过check_msg接收到车位分配请求后调用,解析内容并进行相应处理 Error_manager Parkspace_allocation_communicator::execute_msg(Communication_message *p_msg) { return Parkspace_allocator::get_instance_references().execute_msg(p_msg); return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "parkspace allocation wrong request type"); } //检查消息是否应由本模块接收 //本模块接收车位分配请求、车位查询请求与车位释放请求 Error_manager Parkspace_allocation_communicator::check_msg(Communication_message* p_msg) { //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是车位请求消息. if(p_msg->get_receiver() == Communication_message::Communicator::eParkspace) { switch(p_msg->get_message_type()) { case Communication_message::Message_type::eParkspace_allocation_request_msg: return Error_code::SUCCESS; case Communication_message::Message_type::eParkspace_search_request_msg: return Error_code::SUCCESS; case Communication_message::Message_type::eParkspace_release_request_msg: return Error_code::SUCCESS; case Communication_message::Message_type::eParkspace_force_update_request_msg: return Error_code::SUCCESS; case Communication_message::Message_type::eParkspace_confirm_alloc_request_msg: return Error_code::SUCCESS; default: return Error_code::INVALID_MESSAGE; } } } Error_manager Parkspace_allocation_communicator::check_executer(Communication_message* p_msg) { //检查对应模块的状态, 判断是否可以处理这条消息 //同时也要判断是否超时, 超时返回 COMMUNICATION_ANALYSIS_TIME_OUT //如果处理器正在忙别的, 那么返回 COMMUNICATION_EXCUTER_IS_BUSY Error_manager t_error; if ( p_msg->is_over_time() ) { std::cout << "Parkspace_allocation_communicator...COMMUNICATION_ANALYSIS_TIME_OUT , " << std::endl; //超时:接收方不做处理,发送方会进行超时处理 return Error_code::COMMUNICATION_ANALYSIS_TIME_OUT; } else { return Parkspace_allocator::get_instance_references().check_executer(p_msg); } return Error_code::SUCCESS; } //重载心跳与车位状态发送函数 Error_manager Parkspace_allocation_communicator::encapsulate_send_data() { return Error_code::SUCCESS; std::lock_guard lck(m_status_mutex); //车位信息消息赋值 message::Parkspace_allocation_status_msg t_parkspace_status_msg; t_parkspace_status_msg.CopyFrom(m_parkspace_status_msg); message::Base_info t_base_info; message::Error_manager t_error; t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_status_msg); t_base_info.set_timeout_ms(5000); t_base_info.set_sender(message::Communicator::eParkspace); t_base_info.set_receiver(message::Communicator::eMain); t_error.set_error_code(0); t_parkspace_status_msg.mutable_base_info()->CopyFrom(t_base_info); t_parkspace_status_msg.mutable_error_manager()->CopyFrom(t_error); Communication_message* tp_msg = new Communication_message(t_parkspace_status_msg.SerializeAsString()); //重置消息,填入头信息 tp_msg->reset(t_base_info, tp_msg->get_message_buf()); bool is_push = m_send_data_list.push(tp_msg); if ( is_push == false ) { delete(tp_msg); tp_msg = NULL; return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR, " Parkspace_allocation_communicator::send status error "); } return Error_code::SUCCESS; }