parkspace_allocation_communicator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // Created by zx on 2020/6/18.
  3. //
  4. #include "parkspace_allocation_communicator.h"
  5. Parkspace_allocation_communicator::Parkspace_allocation_communicator()
  6. {
  7. }
  8. Parkspace_allocation_communicator::~Parkspace_allocation_communicator()
  9. {
  10. }
  11. //************************************* 公有函数 ****************************************
  12. //外部调用发送分配车位结果
  13. Error_manager Parkspace_allocation_communicator::send_response(Communication_message *message)
  14. {
  15. return encapsulate_msg(message);
  16. }
  17. //外部调用更新所有车位信息/初始化所有车位
  18. Error_manager Parkspace_allocation_communicator::update_parkspace_status(message::Parkspace_allocation_status_msg status_msg)
  19. {
  20. std::lock_guard<std::mutex> lck(m_status_mutex);
  21. m_parkspace_status_msg.CopyFrom(status_msg);
  22. return SUCCESS;
  23. }
  24. //外部调用更新单一车位信息
  25. Error_manager Parkspace_allocation_communicator::update_parkspace_status(int parkspace_id, message::Parkspace_info parkspace_info)
  26. {
  27. std::lock_guard<std::mutex> lck(m_status_mutex);
  28. if(m_parkspace_status_msg.parkspace_info_size() >= parkspace_id)
  29. {
  30. m_parkspace_status_msg.mutable_parkspace_info(parkspace_id-1)->CopyFrom(parkspace_info);
  31. }
  32. return SUCCESS;
  33. }
  34. //外部调用获取当前车位状态消息
  35. message::Parkspace_allocation_status_msg Parkspace_allocation_communicator::get_status()
  36. {
  37. std::lock_guard<std::mutex> lck(m_status_mutex);
  38. message::Parkspace_allocation_status_msg parkspace_status_msg;
  39. parkspace_status_msg.CopyFrom(m_parkspace_status_msg);
  40. return parkspace_status_msg;
  41. }
  42. //************************************* 内部函数 ****************************************
  43. //封装反馈消息自动发送
  44. Error_manager Parkspace_allocation_communicator::encapsulate_msg(Communication_message *message)
  45. {
  46. if (message == nullptr)
  47. {
  48. return Error_manager(POINTER_IS_NULL, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message null pointer");
  49. }
  50. //针对待发送的不同反馈消息,分别进行校核
  51. switch(message->get_message_type())
  52. {
  53. case Communication_message::eParkspace_allocation_response_msg:
  54. {
  55. message::Parkspace_allocation_response_msg response;
  56. bool result = response.ParseFromString(message->get_message_buf());
  57. //可增加待发送分配车位反馈消息校核
  58. break;
  59. }
  60. case Communication_message::eParkspace_search_response_msg:
  61. {
  62. break;
  63. }
  64. case Communication_message::eParkspace_release_response_msg:
  65. {
  66. break;
  67. }
  68. default:
  69. return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message response type error");
  70. }
  71. //发送请求
  72. return Communication_socket_base::encapsulate_msg(message);
  73. }
  74. //重载执行消息,父类线程通过check_msg接收到车位分配请求后调用,解析内容并进行相应处理
  75. Error_manager Parkspace_allocation_communicator::execute_msg(Communication_message *p_msg)
  76. {
  77. if (p_msg == nullptr)
  78. return Error_manager(POINTER_IS_NULL, CRITICAL_ERROR, "parkspace allocation response msg pointer is null");
  79. //车位分配request消息
  80. switch (p_msg->get_message_type())
  81. {
  82. ///测量结果反馈消息
  83. case Communication_message::eParkspace_allocation_request_msg:
  84. {
  85. message::Parkspace_allocation_request_msg request;
  86. request.ParseFromString(p_msg->get_message_buf());
  87. std::cout<<"allocation request, car license: "<<request.car_info().license()<<std::endl;
  88. //根据请求的信息反馈分配的车位,并封装发送
  89. //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
  90. message::Parkspace_allocation_response_msg response_msg;
  91. message::Base_info t_response_header;
  92. message::Error_manager t_error;
  93. message::Parkspace_info t_allocated_space;
  94. t_response_header.set_msg_type(message::Message_type::eParkspace_allocation_response_msg);
  95. t_response_header.set_timeout_ms(1000);
  96. t_response_header.set_sender(message::Communicator::eParkspace_allocator);
  97. t_response_header.set_receiver(message::Communicator::eMain);
  98. if(m_parkspace_status_msg.parkspace_info_size()>0)
  99. {
  100. t_error.set_error_code(0);
  101. t_error.set_error_level(message::Error_level::NORMAL);
  102. t_allocated_space.CopyFrom(m_parkspace_status_msg.parkspace_info(0));
  103. } else{
  104. t_error.set_error_code(1);
  105. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  106. }
  107. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  108. response_msg.set_command_id(request.command_id());
  109. response_msg.mutable_error_manager()->CopyFrom(t_error);
  110. response_msg.mutable_allocated_space_info()->CopyFrom(t_allocated_space);
  111. Communication_message* response=new Communication_message();
  112. response->reset(t_response_header, response_msg.SerializeAsString());
  113. return send_response(response);
  114. // return SUCCESS;
  115. }
  116. case Communication_message::eParkspace_search_request_msg:
  117. {
  118. message::Parkspace_search_request_msg request;
  119. request.ParseFromString(p_msg->get_message_buf());
  120. std::cout<<"search request, car license: "<<request.car_info().license()<<std::endl;
  121. //根据车辆凭证信息查询车辆位置
  122. //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
  123. message::Parkspace_search_response_msg response_msg;
  124. message::Base_info t_response_header;
  125. message::Error_manager t_error;
  126. message::Parkspace_info t_car_position;
  127. t_response_header.set_msg_type(message::Message_type::eParkspace_search_response_msg);
  128. t_response_header.set_timeout_ms(1000);
  129. t_response_header.set_sender(message::Communicator::eParkspace_allocator);
  130. t_response_header.set_receiver(message::Communicator::eMain);
  131. if (m_parkspace_status_msg.parkspace_info_size() > 1)
  132. {
  133. t_error.set_error_code(0);
  134. t_error.set_error_level(message::Error_level::NORMAL);
  135. t_car_position.CopyFrom(m_parkspace_status_msg.parkspace_info(1));
  136. }
  137. else
  138. {
  139. t_error.set_error_code(1);
  140. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  141. }
  142. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  143. response_msg.set_command_id(request.command_id());
  144. response_msg.mutable_error_manager()->CopyFrom(t_error);
  145. response_msg.mutable_car_position()->CopyFrom(t_car_position);
  146. Communication_message* response=new Communication_message();
  147. response->reset(t_response_header, response_msg.SerializeAsString());
  148. return send_response(response);
  149. }
  150. case Communication_message::eParkspace_release_request_msg:
  151. {
  152. message::Parkspace_release_request_msg request;
  153. request.ParseFromString(p_msg->get_message_buf());
  154. std::cout<<"release request, parkspace id: "<<request.release_space_info().parkspace_id()<<std::endl;
  155. //根据车位信息定位待释放车位位置,车辆凭证号用于校验
  156. //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
  157. message::Parkspace_release_response_msg response_msg;
  158. message::Base_info t_response_header;
  159. message::Error_manager t_error;
  160. t_response_header.set_msg_type(message::Message_type::eParkspace_release_response_msg);
  161. t_response_header.set_timeout_ms(1000);
  162. t_response_header.set_sender(message::Communicator::eParkspace_allocator);
  163. t_response_header.set_receiver(message::Communicator::eMain);
  164. if (m_parkspace_status_msg.parkspace_info_size() > 0)
  165. {
  166. t_error.set_error_code(0);
  167. t_error.set_error_level(message::Error_level::NORMAL);
  168. }
  169. else
  170. {
  171. t_error.set_error_code(1);
  172. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  173. }
  174. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  175. response_msg.set_command_id(request.command_id());
  176. response_msg.mutable_error_manager()->CopyFrom(t_error);
  177. response_msg.mutable_release_space_info()->CopyFrom(request.release_space_info());
  178. Communication_message* response=new Communication_message();
  179. response->reset(t_response_header, response_msg.SerializeAsString());
  180. return send_response(response);
  181. }
  182. }
  183. return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "parkspace allocation wrong request type");
  184. }
  185. //检查消息是否应由本模块接收
  186. //本模块接收车位分配请求、车位查询请求与车位释放请求
  187. Error_manager Parkspace_allocation_communicator::check_msg(Communication_message* p_msg)
  188. {
  189. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是车位请求消息.
  190. if(p_msg->get_receiver() == Communication_message::Communicator::eParkspace_allocator)
  191. {
  192. switch(p_msg->get_message_type())
  193. {
  194. case Communication_message::Message_type::eParkspace_allocation_request_msg:
  195. return Error_code::SUCCESS;
  196. case Communication_message::Message_type::eParkspace_search_request_msg:
  197. return Error_code::SUCCESS;
  198. case Communication_message::Message_type::eParkspace_release_request_msg:
  199. return Error_code::SUCCESS;
  200. default:
  201. return Error_code::INVALID_MESSAGE;
  202. }
  203. }
  204. }
  205. Error_manager Parkspace_allocation_communicator::check_executer(Communication_message* p_msg)
  206. {
  207. //检查对应模块的状态, 判断是否可以处理这条消息
  208. //同时也要判断是否超时, 超时返回 COMMUNICATION_ANALYSIS_TIME_OUT
  209. //如果处理器正在忙别的, 那么返回 COMMUNICATION_EXCUTER_IS_BUSY
  210. if ( p_msg->is_over_time() )
  211. {
  212. std::cout << "Communication_socket_base::check_msg p_buf = " << p_msg->get_message_buf() << std::endl;
  213. std::cout << "Communication_socket_base::check_msg size = " << p_msg->get_message_buf().size() << std::endl;
  214. std::cout << "COMMUNICATION_ANALYSIS_TIME_OUT , " << std::endl;
  215. return Error_code::COMMUNICATION_ANALYSIS_TIME_OUT;
  216. }
  217. else
  218. {
  219. bool executer_is_ready = true;
  220. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 找到处理模块的实例对象, 查询执行人是否可以处理这条消息
  221. //这里子类重载时, 增加判断逻辑, 以后再写.
  222. // std::cout << "Communication_socket_base::check_msg p_buf = " << p_msg->get_message_buf() << std::endl;
  223. // std::cout << "Communication_socket_base::check_msg size = " << p_msg->get_message_buf().size() << std::endl;
  224. if ( executer_is_ready )
  225. {
  226. // std::cout << "executer_is_ready , " << std::endl;
  227. return Error_code::SUCCESS;
  228. }
  229. else
  230. {
  231. std::cout << "executer_is_busy , " << std::endl;
  232. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  233. }
  234. }
  235. return Error_code::SUCCESS;
  236. }
  237. //重载心跳与车位状态发送函数
  238. Error_manager Parkspace_allocation_communicator::encapsulate_send_data()
  239. {
  240. return Error_code::SUCCESS;
  241. std::lock_guard<std::mutex> lck(m_status_mutex);
  242. //车位信息消息赋值
  243. message::Parkspace_allocation_status_msg t_parkspace_status_msg;
  244. t_parkspace_status_msg.CopyFrom(m_parkspace_status_msg);
  245. message::Base_info t_base_info;
  246. message::Error_manager t_error;
  247. t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_status_msg);
  248. t_base_info.set_timeout_ms(5000);
  249. t_base_info.set_sender(message::Communicator::eParkspace_allocator);
  250. t_base_info.set_receiver(message::Communicator::eMain);
  251. t_error.set_error_code(0);
  252. t_parkspace_status_msg.mutable_base_info()->CopyFrom(t_base_info);
  253. t_parkspace_status_msg.mutable_error_manager()->CopyFrom(t_error);
  254. Communication_message* tp_msg = new Communication_message(t_parkspace_status_msg.SerializeAsString());
  255. //重置消息,填入头信息
  256. tp_msg->reset(t_base_info, tp_msg->get_message_buf());
  257. bool is_push = m_send_data_list.push(tp_msg);
  258. if ( is_push == false )
  259. {
  260. delete(tp_msg);
  261. tp_msg = NULL;
  262. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  263. " Parkspace_allocation_communicator::send status error ");
  264. }
  265. return Error_code::SUCCESS;
  266. }