dispatch_communicator.cpp 9.4 KB

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