dispatch_excutor.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Created by zx on 2020/7/21.
  3. //
  4. #include "dispatch_excutor.h"
  5. #include "uniq_key.h"
  6. Dispatch_excutor::~Dispatch_excutor(){}
  7. Error_manager Dispatch_excutor::dispatch_request(message::Dispatch_request_msg& request,
  8. message::Dispatch_response_msg& result,Thread_condition& cancel_condition)
  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. //发送请求
  29. m_response_table[request.command_key()]=message::Dispatch_response_msg();
  30. code= Message_communicator::get_instance_pointer()->send_msg(&message);
  31. if(code!=SUCCESS)
  32. {
  33. m_response_table.erase(request.command_key());
  34. return code;
  35. }
  36. //循环查询请求是否被处理
  37. auto start_time=std::chrono::system_clock::now();
  38. double time=0;
  39. do{
  40. //查询到记录
  41. message::Dispatch_response_msg response;
  42. ///查询是否存在,并且删除该记录,
  43. if(m_response_table.find(request.command_key(),response))
  44. {
  45. //判断是否接收到回应,若回应信息被赋值则证明有回应
  46. if (response.has_base_info() && response.has_command_key())
  47. {
  48. message::Base_info response_base = response.base_info();
  49. //检查类型是否匹配
  50. if (response_base.msg_type() != message::eDispatch_response_msg) {
  51. return Error_manager(ERROR, CRITICAL_ERROR,
  52. "dispatch response basemsg type error");
  53. }
  54. //检查基本信息是否匹配
  55. if (response_base.sender() != message::eDispatch ||
  56. response_base.receiver() != message::eMain ||
  57. response.command_key() != request.command_key()) {
  58. return Error_manager(ERROR, MAJOR_ERROR,
  59. "dispatch response basemsg info error");
  60. }
  61. result = response;
  62. m_response_table.erase(request.command_key());
  63. return SUCCESS;
  64. }
  65. }
  66. else
  67. {
  68. //未查询到记录,任务已经被提前取消,记录被删除
  69. return Error_manager(TASK_CANCEL,MINOR_ERROR,"dispatch request canceled");
  70. }
  71. auto end_time=std::chrono::system_clock::now();
  72. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  73. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  74. std::this_thread::yield();
  75. if(time>double(timeout))
  76. {
  77. m_response_table.erase(request.command_key());
  78. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"dispatch request timeout");
  79. }
  80. }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
  81. m_response_table.erase(request.command_key());
  82. return Error_manager(TASK_CANCEL,MINOR_ERROR,"dispatch request task canceled");
  83. }
  84. /*
  85. * 提前取消请求
  86. */
  87. Error_manager Dispatch_excutor::cancel_request(message::Dispatch_request_msg& request)
  88. {
  89. message::Dispatch_response_msg t_response;
  90. if(m_response_table.find(request.command_key(),t_response))
  91. {
  92. m_response_table.erase(request.command_key());
  93. }
  94. return SUCCESS;
  95. }
  96. Error_manager Dispatch_excutor::check_entrance_statu(int terminal_id)
  97. {
  98. if(m_storing_dispatch_statu_msg_map.find(terminal_id)==false || m_storing_dispatch_statu_time_map.find(terminal_id)==false)
  99. return Error_manager(FAILED,MINOR_ERROR,"停车口调度模块状态未找到");
  100. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  101. auto durantion=time_now-m_storing_dispatch_statu_time_map[terminal_id];
  102. if(m_storing_dispatch_statu_msg_map[terminal_id].has_base_info()== false
  103. || durantion>std::chrono::seconds(5))
  104. {
  105. return Error_manager(DISCONNECT,MINOR_ERROR,"调度节点通讯断开");
  106. }
  107. if(m_storing_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_FAULT)
  108. {
  109. return Error_manager(ERROR,MINOR_ERROR,"调度节点故障");
  110. }
  111. if(m_storing_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_UNKNOW)
  112. {
  113. return Error_manager(UNKNOW_STATU,MINOR_ERROR,"调度节点状态未知");
  114. }
  115. return SUCCESS;
  116. }
  117. Error_manager Dispatch_excutor::check_export_statu(int terminal_id)
  118. {
  119. if(m_picking_dispatch_statu_msg_map.find(terminal_id)==false || m_picking_dispatch_statu_time_map.find(terminal_id)==false)
  120. return Error_manager(FAILED,MINOR_ERROR,"停车口调度模块状态未找到");
  121. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  122. auto durantion=time_now-m_picking_dispatch_statu_time_map[terminal_id];
  123. if(m_picking_dispatch_statu_msg_map[terminal_id].has_base_info()== false
  124. || durantion>std::chrono::seconds(5))
  125. {
  126. return Error_manager(ERROR,MINOR_ERROR,"调度节点通讯断开");
  127. }
  128. if(m_picking_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_FAULT)
  129. {
  130. return Error_manager(ERROR,MINOR_ERROR,"调度节点故障");
  131. }
  132. if(m_picking_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_UNKNOW)
  133. {
  134. return Error_manager(ERROR,MINOR_ERROR,"调度节点状态未知");
  135. }
  136. return SUCCESS;
  137. }
  138. Dispatch_excutor::Dispatch_excutor()
  139. {
  140. }
  141. Error_manager Dispatch_excutor::consume_msg(Communication_message* p_msg)
  142. {
  143. if(p_msg== nullptr)
  144. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"dispatch response msg pointer is null");
  145. //测量response消息
  146. switch (p_msg->get_message_type())
  147. {
  148. ///测量结果反馈消息
  149. case Communication_message::eDispatch_response_msg:
  150. {
  151. message::Dispatch_response_msg response;
  152. response.ParseFromString(p_msg->get_message_buf());
  153. ///查询请求表是否存在,并且更新
  154. if(m_response_table.find_update(response.command_key(),response)==false)
  155. {
  156. return Error_manager(ERROR,NEGLIGIBLE_ERROR,"dispatch response without request");
  157. }
  158. break;
  159. }
  160. ///测量系统状态
  161. case Communication_message::eDispatch_status_msg:
  162. {
  163. message::Dispatch_terminal_status_msg statu_msg;
  164. if(statu_msg.ParseFromString(p_msg->get_message_buf())==false)
  165. {
  166. return Error_manager(ERROR,CRITICAL_ERROR,"调度模块状态消息解析失败");
  167. }
  168. if(statu_msg.passageway_direction()==message::E_INLET)
  169. {
  170. m_storing_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  171. m_storing_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  172. }
  173. if(statu_msg.passageway_direction()==message::E_OUTLET)
  174. {
  175. m_picking_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  176. m_picking_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  177. }
  178. if(statu_msg.passageway_direction()==message::E_BILATERAL)
  179. {
  180. m_storing_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  181. m_storing_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  182. m_picking_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  183. m_picking_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  184. }
  185. break;
  186. }
  187. }
  188. return SUCCESS;
  189. }