notify_excutor.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // Created by zx on 2020/7/21.
  3. //
  4. #include "notify_excutor.h"
  5. #include "uniq_key.h"
  6. Notify_excutor::~Notify_excutor(){}
  7. Error_manager Notify_excutor::notify_request(message::Notify_request_msg& request,
  8. message::Notify_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::eNotify)
  14. return Error_manager(ERROR,MINOR_ERROR,"notify 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," notify request repeated");
  18. //设置超时,若没有设置,默认60000000 1000分钟
  19. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():60000000;
  20. //向测量节点发送测量请求,并记录请求
  21. Error_manager code;
  22. Communication_message message;
  23. message::Base_info base_msg;
  24. base_msg.set_msg_type(message::eNotify_request_msg);
  25. base_msg.set_sender(message::eMain);
  26. base_msg.set_receiver(message::eNotify);
  27. base_msg.set_timeout_ms(timeout);
  28. message.reset(base_msg,request.SerializeAsString());
  29. //发送请求
  30. m_response_table[request.command_key()]=message::Notify_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::Notify_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::eNotify_response_msg) {
  52. return Error_manager(ERROR, CRITICAL_ERROR,
  53. "notify response basemsg type error");
  54. }
  55. //检查基本信息是否匹配
  56. if (response_base.sender() != message::eNotify ||
  57. response_base.receiver() != message::eMain ||
  58. response.command_key() != request.command_key()) {
  59. return Error_manager(ERROR, MAJOR_ERROR,
  60. "notify 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,"notify 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,"notify 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,"notify request task canceled");
  84. }
  85. /*
  86. * 提前取消请求
  87. */
  88. Error_manager Notify_excutor::cancel_request(message::Notify_request_msg& request)
  89. {
  90. message::Notify_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 Notify_excutor::check_node_statu()
  98. {
  99. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  100. auto durantion=time_now-m_notify_statu_time;
  101. if(m_notify_statu_msg.has_base_info()== false
  102. || durantion>std::chrono::seconds(5) || m_notify_statu_msg.has_error_manager()==false)
  103. {
  104. return Error_manager(DISCONNECT,MINOR_ERROR,"取车等候区通知节点通讯断开");
  105. }
  106. if(m_notify_statu_msg.error_manager().error_code()!=SUCCESS)
  107. {
  108. return Error_manager(ERROR,MINOR_ERROR,"取车等候区通知节点故障");
  109. }
  110. return SUCCESS;
  111. }
  112. Notify_excutor::Notify_excutor()
  113. {
  114. }
  115. Error_manager Notify_excutor::consume_msg(Communication_message* p_msg)
  116. {
  117. if(p_msg== nullptr)
  118. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"notify response msg pointer is null");
  119. //response消息
  120. switch (p_msg->get_message_type())
  121. {
  122. ///测量结果反馈消息
  123. case Communication_message::eNotify_response_msg:
  124. {
  125. message::Notify_response_msg response;
  126. response.ParseFromString(p_msg->get_message_buf());
  127. ///查询请求表是否存在,并且更新
  128. if(m_response_table.find_update(response.command_key(),response)==false)
  129. {
  130. return Error_manager(ERROR,NEGLIGIBLE_ERROR,"notify response without request");
  131. }
  132. break;
  133. }
  134. ///测量系统状态
  135. case Communication_message::eNotify_status_msg:
  136. {
  137. message::Notify_status_msg statu_msg;
  138. if(statu_msg.ParseFromString(p_msg->get_message_buf())==false)
  139. {
  140. return Error_manager(ERROR,CRITICAL_ERROR,"取车等候区通知节点状态消息解析失败");
  141. }
  142. /*std::cout<<"plc msg:"<<std::endl;
  143. std::cout<<statu_msg.DebugString()<<std::endl;*/
  144. m_notify_statu_msg=statu_msg;
  145. m_notify_statu_time=std::chrono::system_clock::now();
  146. break;
  147. }
  148. }
  149. return SUCCESS;
  150. }