dispatch_excutor.cpp 8.5 KB

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