dispatch_communicator.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // Created by zx on 2020/7/21.
  3. //
  4. #include "dispatch_communicator.h"
  5. Dispatch_communicator::~Dispatch_communicator(){}
  6. Error_manager Dispatch_communicator::dispatch_request(message::Dispatch_request_msg& request,
  7. message::Dispatch_response_msg& result)
  8. {
  9. /*
  10. * 检查request合法性,以及模块状态
  11. */
  12. if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eDispatch)
  13. return Error_manager(ERROR,MINOR_ERROR,"dispatch request invalid");
  14. if(m_response_table.find(request)==true)
  15. return Error_manager(ERROR,MAJOR_ERROR," dispatch request repeated");
  16. //设置超时,若没有设置,默认300000 五分钟
  17. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():300000;
  18. //向测量节点发送测量请求,并记录请求
  19. Error_manager code;
  20. Communication_message message;
  21. message::Base_info base_msg;
  22. base_msg.set_msg_type(message::eDispatch_request_msg);
  23. base_msg.set_sender(message::eMain);
  24. base_msg.set_receiver(message::eDispatch);
  25. base_msg.set_timeout_ms(timeout);
  26. message.reset(base_msg,request.SerializeAsString());
  27. code=encapsulate_msg(&message);
  28. if(code!=SUCCESS)
  29. return code;
  30. //循环查询请求是否被处理
  31. auto start_time=std::chrono::system_clock::now();
  32. double time=0;
  33. do{
  34. //查询到记录
  35. message::Dispatch_response_msg response;
  36. ///查询是否存在,并且删除该记录,
  37. if(m_response_table.find(request,response))
  38. {
  39. //判断是否接收到回应,若回应信息被赋值则证明有回应
  40. if (response.has_base_info() && response.has_command_info())
  41. {
  42. message::Base_info response_base = response.base_info();
  43. //检查类型是否匹配
  44. if (response_base.msg_type() != message::eDispatch_response_msg) {
  45. return Error_manager(ERROR, CRITICAL_ERROR,
  46. "dispatch response basemsg type error");
  47. }
  48. //检查基本信息是否匹配
  49. if (response_base.sender() != message::eDispatch ||
  50. response_base.receiver() != message::eMain ||
  51. response.command_info() != request.command_info()) {
  52. return Error_manager(ERROR, MAJOR_ERROR,
  53. "dispatch response basemsg info error");
  54. }
  55. result = response;
  56. m_response_table.erase(request);
  57. return SUCCESS;
  58. }
  59. }
  60. else
  61. {
  62. //未查询到记录,任务已经被提前取消,记录被删除
  63. return Error_manager(FAILED,MINOR_ERROR,"dispatch request canceled");
  64. }
  65. auto end_time=std::chrono::system_clock::now();
  66. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  67. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  68. std::this_thread::yield();
  69. usleep(1000);
  70. }while(time<double(timeout));
  71. m_response_table.erase(request);
  72. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"dispatch request timeout");
  73. }
  74. /*
  75. * 提前取消请求
  76. */
  77. Error_manager Dispatch_communicator::cancel_request(message::Dispatch_request_msg& request)
  78. {
  79. message::Dispatch_response_msg t_response;
  80. if(m_response_table.find(request,t_response))
  81. {
  82. m_response_table.erase(request);
  83. }
  84. return SUCCESS;
  85. }
  86. Error_manager Dispatch_communicator::check_entrance_statu(int terminal_id)
  87. {
  88. if(m_storing_dispatch_statu_msg_map.find(terminal_id)==false || m_storing_dispatch_statu_time_map.find(terminal_id)==false)
  89. return Error_manager(FAILED,MINOR_ERROR,"停车口调度模块状态未找到");
  90. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  91. auto durantion=time_now-m_storing_dispatch_statu_time_map[terminal_id];
  92. if(m_storing_dispatch_statu_msg_map[terminal_id].has_base_info()== false
  93. || durantion>std::chrono::seconds(5))
  94. {
  95. return Error_manager(ERROR,MINOR_ERROR,"调度节点通讯断开");
  96. }
  97. if(m_storing_dispatch_statu_msg_map[terminal_id].dispatch_manager_status()==message::E_DISPATCH_MANAGER_FAULT)
  98. {
  99. return Error_manager(ERROR,MINOR_ERROR,"调度节点故障");
  100. }
  101. if(m_storing_dispatch_statu_msg_map[terminal_id].dispatch_manager_status()==message::E_DISPATCH_MANAGER_UNKNOW)
  102. {
  103. return Error_manager(ERROR,MINOR_ERROR,"调度节点状态未知");
  104. }
  105. return SUCCESS;
  106. }
  107. Error_manager Dispatch_communicator::check_export_statu(int terminal_id)
  108. {
  109. if(m_picking_dispatch_statu_msg_map.find(terminal_id)==false || m_picking_dispatch_statu_time_map.find(terminal_id)==false)
  110. return Error_manager(FAILED,MINOR_ERROR,"停车口调度模块状态未找到");
  111. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  112. auto durantion=time_now-m_picking_dispatch_statu_time_map[terminal_id];
  113. if(m_picking_dispatch_statu_msg_map[terminal_id].has_base_info()== false
  114. || durantion>std::chrono::seconds(5))
  115. {
  116. return Error_manager(ERROR,MINOR_ERROR,"调度节点通讯断开");
  117. }
  118. if(m_picking_dispatch_statu_msg_map[terminal_id].dispatch_manager_status()==message::E_DISPATCH_MANAGER_FAULT)
  119. {
  120. return Error_manager(ERROR,MINOR_ERROR,"调度节点故障");
  121. }
  122. if(m_picking_dispatch_statu_msg_map[terminal_id].dispatch_manager_status()==message::E_DISPATCH_MANAGER_UNKNOW)
  123. {
  124. return Error_manager(ERROR,MINOR_ERROR,"调度节点状态未知");
  125. }
  126. return SUCCESS;
  127. }
  128. Dispatch_communicator::Dispatch_communicator()
  129. {
  130. }
  131. Error_manager Dispatch_communicator::encapsulate_msg(Communication_message* message)
  132. {
  133. Error_manager code;
  134. //记录请求
  135. switch (message->get_message_type())
  136. {
  137. case Communication_message::eDispatch_request_msg:
  138. {
  139. message::Dispatch_request_msg request;
  140. if(false==request.ParseFromString(message->get_message_buf()))
  141. {
  142. code=Error_manager(ERROR,CRITICAL_ERROR,"request message parse failed");
  143. }
  144. m_response_table[request]=message::Dispatch_response_msg();
  145. //发送请求
  146. code= Communication_socket_base::encapsulate_msg(message);
  147. if(code!=SUCCESS)
  148. {
  149. m_response_table.erase(request);
  150. }
  151. break;
  152. }
  153. default:
  154. code= Error_manager(FAILED,CRITICAL_ERROR," measure发送任务类型不存在");
  155. break;
  156. }
  157. return code;
  158. }
  159. Error_manager Dispatch_communicator::execute_msg(Communication_message* p_msg)
  160. {
  161. if(p_msg== nullptr)
  162. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"dispatch response msg pointer is null");
  163. //测量response消息
  164. switch (p_msg->get_message_type())
  165. {
  166. ///测量结果反馈消息
  167. case Communication_message::eDispatch_response_msg:
  168. {
  169. message::Dispatch_response_msg response;
  170. response.ParseFromString(p_msg->get_message_buf());
  171. message::Dispatch_request_msg request=create_request_by_response(response);
  172. ///查询请求表是否存在,并且更新
  173. if(m_response_table.find_update(request,response)==false)
  174. {
  175. return Error_manager(ERROR,NEGLIGIBLE_ERROR,"dispatch response without request");
  176. }
  177. break;
  178. }
  179. ///测量系统状态
  180. case Communication_message::eDispatch_status_msg:
  181. {
  182. message::Dispatch_status_msg statu_msg;
  183. if(statu_msg.ParseFromString(p_msg->get_message_buf())==false)
  184. {
  185. return Error_manager(ERROR,CRITICAL_ERROR,"调度模块状态消息解析失败");
  186. }
  187. if(statu_msg.dispatch_motion_direction()==message::E_STORE_CAR)
  188. {
  189. m_storing_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  190. m_storing_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  191. }
  192. if(statu_msg.dispatch_motion_direction()==message::E_PICKUP_CAR)
  193. {
  194. m_picking_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  195. m_picking_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  196. }
  197. break;
  198. }
  199. }
  200. return SUCCESS;
  201. }
  202. /*
  203. * 检测消息是否可被处理
  204. */
  205. Error_manager Dispatch_communicator::check_msg(Communication_message* p_msg)
  206. {
  207. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是给我的.
  208. //子类重载时, 增加自己模块的判断逻辑, 以后再写.
  209. if ( (p_msg->get_message_type() == Communication_message::Message_type::eDispatch_response_msg
  210. ||p_msg->get_message_type() == Communication_message::Message_type::eDispatch_status_msg)
  211. && p_msg->get_receiver() == Communication_message::Communicator::eMain )
  212. {
  213. return Error_code::SUCCESS;
  214. }
  215. else
  216. {
  217. //认为接受人
  218. return Error_code::INVALID_MESSAGE;
  219. }
  220. }
  221. /*
  222. * 心跳发送函数,重载
  223. */
  224. Error_manager Dispatch_communicator::encapsulate_send_data()
  225. {
  226. return SUCCESS;
  227. }
  228. //检查消息是否可以被解析, 需要重载
  229. Error_manager Dispatch_communicator::check_executer(Communication_message* p_msg)
  230. {
  231. return SUCCESS;
  232. }
  233. /*
  234. * 根据接收到的response,生成对应的 request
  235. */
  236. message::Dispatch_request_msg Dispatch_communicator::create_request_by_response(message::Dispatch_response_msg& response)
  237. {
  238. message::Dispatch_request_msg request;
  239. message::Base_info baseInfo;
  240. baseInfo.set_msg_type(message::eDispatch_request_msg);
  241. baseInfo.set_sender(response.base_info().receiver());
  242. baseInfo.set_receiver(response.base_info().sender());
  243. request.mutable_base_info()->CopyFrom(baseInfo);
  244. request.mutable_command_info()->CopyFrom(response.command_info());
  245. return request;
  246. }