dispatch_communicator.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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,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. 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(TASK_CANCEL,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. if(time>double(timeout))
  72. {
  73. m_response_table.erase(request.command_key());
  74. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"dispatch request timeout");
  75. }
  76. }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
  77. m_response_table.erase(request.command_key());
  78. return Error_manager(TASK_CANCEL,MINOR_ERROR,"dispatch request task canceled");
  79. }
  80. /*
  81. * 提前取消请求
  82. */
  83. Error_manager Dispatch_communicator::cancel_request(message::Dispatch_request_msg& request)
  84. {
  85. message::Dispatch_response_msg t_response;
  86. if(m_response_table.find(request.command_key(),t_response))
  87. {
  88. m_response_table.erase(request.command_key());
  89. }
  90. return SUCCESS;
  91. }
  92. Error_manager Dispatch_communicator::check_entrance_statu(int terminal_id)
  93. {
  94. if(m_storing_dispatch_statu_msg_map.find(terminal_id)==false || m_storing_dispatch_statu_time_map.find(terminal_id)==false)
  95. return Error_manager(FAILED,MINOR_ERROR,"停车口调度模块状态未找到");
  96. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  97. auto durantion=time_now-m_storing_dispatch_statu_time_map[terminal_id];
  98. if(m_storing_dispatch_statu_msg_map[terminal_id].has_base_info()== false
  99. || durantion>std::chrono::seconds(5))
  100. {
  101. return Error_manager(ERROR,MINOR_ERROR,"调度节点通讯断开");
  102. }
  103. if(m_storing_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_FAULT)
  104. {
  105. return Error_manager(ERROR,MINOR_ERROR,"调度节点故障");
  106. }
  107. if(m_storing_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_UNKNOW)
  108. {
  109. return Error_manager(ERROR,MINOR_ERROR,"调度节点状态未知");
  110. }
  111. return SUCCESS;
  112. }
  113. Error_manager Dispatch_communicator::check_export_statu(int terminal_id)
  114. {
  115. if(m_picking_dispatch_statu_msg_map.find(terminal_id)==false || m_picking_dispatch_statu_time_map.find(terminal_id)==false)
  116. return Error_manager(FAILED,MINOR_ERROR,"停车口调度模块状态未找到");
  117. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  118. auto durantion=time_now-m_picking_dispatch_statu_time_map[terminal_id];
  119. if(m_picking_dispatch_statu_msg_map[terminal_id].has_base_info()== false
  120. || durantion>std::chrono::seconds(5))
  121. {
  122. return Error_manager(ERROR,MINOR_ERROR,"调度节点通讯断开");
  123. }
  124. if(m_picking_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_FAULT)
  125. {
  126. return Error_manager(ERROR,MINOR_ERROR,"调度节点故障");
  127. }
  128. if(m_picking_dispatch_statu_msg_map[terminal_id].terminal_status()==message::E_TERMINAL_UNKNOW)
  129. {
  130. return Error_manager(ERROR,MINOR_ERROR,"调度节点状态未知");
  131. }
  132. return SUCCESS;
  133. }
  134. Dispatch_communicator::Dispatch_communicator()
  135. {
  136. }
  137. Error_manager Dispatch_communicator::encapsulate_msg(Communication_message* message)
  138. {
  139. Error_manager code;
  140. //记录请求
  141. switch (message->get_message_type())
  142. {
  143. case Communication_message::eDispatch_request_msg:
  144. {
  145. message::Dispatch_request_msg request;
  146. if(false==request.ParseFromString(message->get_message_buf()))
  147. {
  148. code=Error_manager(ERROR,CRITICAL_ERROR,"request message parse failed");
  149. }
  150. //发送请求
  151. code= Communication_socket_base::encapsulate_msg(message);
  152. break;
  153. }
  154. default:
  155. code= Error_manager(FAILED,CRITICAL_ERROR," measure发送任务类型不存在");
  156. break;
  157. }
  158. return code;
  159. }
  160. Error_manager Dispatch_communicator::execute_msg(Communication_message* p_msg)
  161. {
  162. if(p_msg== nullptr)
  163. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"dispatch response msg pointer is null");
  164. //测量response消息
  165. switch (p_msg->get_message_type())
  166. {
  167. ///测量结果反馈消息
  168. case Communication_message::eDispatch_response_msg:
  169. {
  170. message::Dispatch_response_msg response;
  171. response.ParseFromString(p_msg->get_message_buf());
  172. ///查询请求表是否存在,并且更新
  173. if(m_response_table.find_update(response.command_key(),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_terminal_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.passageway_direction()==message::E_INLET)
  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.passageway_direction()==message::E_OUTLET)
  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. if(statu_msg.passageway_direction()==message::E_BILATERAL)
  198. {
  199. m_storing_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  200. m_storing_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  201. m_picking_dispatch_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  202. m_picking_dispatch_statu_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  203. }
  204. break;
  205. }
  206. }
  207. return SUCCESS;
  208. }
  209. /*
  210. * 检测消息是否可被处理
  211. */
  212. Error_manager Dispatch_communicator::check_msg(Communication_message* p_msg)
  213. {
  214. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是给我的.
  215. //子类重载时, 增加自己模块的判断逻辑, 以后再写.
  216. if ( (p_msg->get_message_type() == Communication_message::Message_type::eDispatch_response_msg
  217. ||p_msg->get_message_type() == Communication_message::Message_type::eDispatch_status_msg)
  218. && p_msg->get_receiver() == Communication_message::Communicator::eMain )
  219. {
  220. return Error_code::SUCCESS;
  221. }
  222. else
  223. {
  224. //认为接受人
  225. return Error_code::INVALID_MESSAGE;
  226. }
  227. }
  228. /*
  229. * 心跳发送函数,重载
  230. */
  231. Error_manager Dispatch_communicator::encapsulate_send_data()
  232. {
  233. return SUCCESS;
  234. }
  235. //检查消息是否可以被解析, 需要重载
  236. Error_manager Dispatch_communicator::check_executer(Communication_message* p_msg)
  237. {
  238. return SUCCESS;
  239. }