parkspace_allocation_communicator.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "parkspace_allocation_communicator.h"
  2. #include "parkspace_allocator.h"
  3. Parkspace_allocation_communicator::Parkspace_allocation_communicator()
  4. {
  5. }
  6. Parkspace_allocation_communicator::~Parkspace_allocation_communicator()
  7. {
  8. }
  9. //************************************* 公有函数 ****************************************
  10. //外部调用发送分配车位结果
  11. Error_manager Parkspace_allocation_communicator::send_response(Communication_message *message)
  12. {
  13. return encapsulate_msg(message);
  14. }
  15. //外部调用更新所有车位信息/初始化所有车位
  16. Error_manager Parkspace_allocation_communicator::update_parkspace_status(message::Parkspace_allocation_status_msg status_msg)
  17. {
  18. std::lock_guard<std::mutex> lck(m_status_mutex);
  19. m_parkspace_status_msg.CopyFrom(status_msg);
  20. return SUCCESS;
  21. }
  22. //外部调用更新单一车位信息
  23. Error_manager Parkspace_allocation_communicator::update_parkspace_status(int parkspace_id, message::Parkspace_info parkspace_info)
  24. {
  25. std::lock_guard<std::mutex> lck(m_status_mutex);
  26. if(m_parkspace_status_msg.parkspace_info_size() > parkspace_id)
  27. {
  28. m_parkspace_status_msg.mutable_parkspace_info(parkspace_id)->CopyFrom(parkspace_info);
  29. }
  30. return SUCCESS;
  31. }
  32. //外部调用获取当前车位状态消息
  33. message::Parkspace_allocation_status_msg Parkspace_allocation_communicator::get_status()
  34. {
  35. std::lock_guard<std::mutex> lck(m_status_mutex);
  36. message::Parkspace_allocation_status_msg parkspace_status_msg;
  37. parkspace_status_msg.CopyFrom(m_parkspace_status_msg);
  38. return parkspace_status_msg;
  39. }
  40. //************************************* 内部函数 ****************************************
  41. //封装反馈消息自动发送
  42. Error_manager Parkspace_allocation_communicator::encapsulate_msg(Communication_message *message)
  43. {
  44. if (message == nullptr)
  45. {
  46. return Error_manager(POINTER_IS_NULL, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message null pointer");
  47. }
  48. //针对待发送的不同反馈消息,分别进行校核
  49. switch(message->get_message_type())
  50. {
  51. case Communication_message::eParkspace_allocation_response_msg:
  52. {
  53. // message::Parkspace_allocation_response_msg response;
  54. // bool result = response.ParseFromString(message->get_message_buf());
  55. // //可增加待发送分配车位反馈消息校核
  56. break;
  57. }
  58. case Communication_message::eParkspace_search_response_msg:
  59. {
  60. break;
  61. }
  62. case Communication_message::eParkspace_release_response_msg:
  63. {
  64. break;
  65. }
  66. case Communication_message::eParkspace_force_update_response_msg:
  67. {
  68. break;
  69. }
  70. case Communication_message::eParkspace_confirm_alloc_response_msg:
  71. {
  72. break;
  73. }
  74. default:
  75. return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message response type error");
  76. }
  77. //发送请求
  78. return Communication_socket_base::encapsulate_msg(message);
  79. }
  80. //重载执行消息,父类线程通过check_msg接收到车位分配请求后调用,解析内容并进行相应处理
  81. Error_manager Parkspace_allocation_communicator::execute_msg(Communication_message *p_msg)
  82. {
  83. return Parkspace_allocator::get_instance_references().execute_msg(p_msg);
  84. return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "parkspace allocation wrong request type");
  85. }
  86. //检查消息是否应由本模块接收
  87. //本模块接收车位分配请求、车位查询请求与车位释放请求
  88. Error_manager Parkspace_allocation_communicator::check_msg(Communication_message* p_msg)
  89. {
  90. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是车位请求消息.
  91. if(p_msg->get_receiver() == Communication_message::Communicator::eParkspace)
  92. {
  93. switch(p_msg->get_message_type())
  94. {
  95. case Communication_message::Message_type::eParkspace_allocation_request_msg:
  96. return Error_code::SUCCESS;
  97. case Communication_message::Message_type::eParkspace_search_request_msg:
  98. return Error_code::SUCCESS;
  99. case Communication_message::Message_type::eParkspace_release_request_msg:
  100. return Error_code::SUCCESS;
  101. case Communication_message::Message_type::eParkspace_force_update_request_msg:
  102. return Error_code::SUCCESS;
  103. case Communication_message::Message_type::eParkspace_confirm_alloc_request_msg:
  104. return Error_code::SUCCESS;
  105. default:
  106. return Error_code::INVALID_MESSAGE;
  107. }
  108. }
  109. }
  110. Error_manager Parkspace_allocation_communicator::check_executer(Communication_message* p_msg)
  111. {
  112. //检查对应模块的状态, 判断是否可以处理这条消息
  113. //同时也要判断是否超时, 超时返回 COMMUNICATION_ANALYSIS_TIME_OUT
  114. //如果处理器正在忙别的, 那么返回 COMMUNICATION_EXCUTER_IS_BUSY
  115. Error_manager t_error;
  116. if ( p_msg->is_over_time() )
  117. {
  118. std::cout << "Parkspace_allocation_communicator...COMMUNICATION_ANALYSIS_TIME_OUT , " << std::endl;
  119. //超时:接收方不做处理,发送方会进行超时处理
  120. return Error_code::COMMUNICATION_ANALYSIS_TIME_OUT;
  121. }
  122. else
  123. {
  124. return Parkspace_allocator::get_instance_references().check_executer(p_msg);
  125. }
  126. return Error_code::SUCCESS;
  127. }
  128. //重载心跳与车位状态发送函数
  129. Error_manager Parkspace_allocation_communicator::encapsulate_send_data()
  130. {
  131. return Error_code::SUCCESS;
  132. std::lock_guard<std::mutex> lck(m_status_mutex);
  133. //车位信息消息赋值
  134. message::Parkspace_allocation_status_msg t_parkspace_status_msg;
  135. t_parkspace_status_msg.CopyFrom(m_parkspace_status_msg);
  136. message::Base_info t_base_info;
  137. message::Error_manager t_error;
  138. t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_status_msg);
  139. t_base_info.set_timeout_ms(5000);
  140. t_base_info.set_sender(message::Communicator::eParkspace);
  141. t_base_info.set_receiver(message::Communicator::eMain);
  142. t_error.set_error_code(0);
  143. t_parkspace_status_msg.mutable_base_info()->CopyFrom(t_base_info);
  144. t_parkspace_status_msg.mutable_error_manager()->CopyFrom(t_error);
  145. Communication_message* tp_msg = new Communication_message(t_parkspace_status_msg.SerializeAsString());
  146. //重置消息,填入头信息
  147. tp_msg->reset(t_base_info, tp_msg->get_message_buf());
  148. bool is_push = m_send_data_list.push(tp_msg);
  149. if ( is_push == false )
  150. {
  151. delete(tp_msg);
  152. tp_msg = NULL;
  153. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  154. " Parkspace_allocation_communicator::send status error ");
  155. }
  156. return Error_code::SUCCESS;
  157. }