Locate_communicator.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Created by zx on 2020/6/18.
  3. //
  4. #include "Locate_communicator.h"
  5. //#include "locate_message.pb.h"
  6. Locate_communicator::Locate_communicator()
  7. {
  8. }
  9. Locate_communicator::~Locate_communicator()
  10. {
  11. }
  12. Error_manager Locate_communicator::encapsulate_msg(Communication_message* message)
  13. {
  14. //记录请求
  15. if(message->get_message_type()==Communication_message::eLocate_request_msg)
  16. {
  17. message::Measure_request_msg request;
  18. m_response_table[request.command_id()]=message::Measure_response_msg();
  19. }
  20. else
  21. {
  22. return Error_manager(LOCATER_MSG_TABLE_NOT_EXIST,NEGLIGIBLE_ERROR,"message table is not exist");
  23. }
  24. //发送请求
  25. return Communication_socket_base::encapsulate_msg(message);
  26. }
  27. Error_manager Locate_communicator::locate_request(message::Measure_request_msg& request,message::Measure_response_msg& result)
  28. {
  29. /*
  30. * 检查request合法性,以及模块状态
  31. */
  32. if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eMeasurer)
  33. return Error_manager(LOCATER_MSG_REQUEST_INVALID,MINOR_ERROR,"measure request invalid");
  34. //设置超时,若没有设置,默认3000
  35. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():3000;
  36. //向测量节点发送测量请求,并记录请求
  37. Error_manager code;
  38. Communication_message message;
  39. message::Base_info base_msg;
  40. base_msg.set_msg_type(message::eLocate_request_msg);
  41. base_msg.set_sender(message::eMain);
  42. base_msg.set_receiver(message::eMeasurer);
  43. base_msg.set_timeout_ms(timeout);
  44. message.reset(base_msg,request.SerializeAsString());
  45. code=encapsulate_msg(&message);
  46. if(code!=SUCCESS)
  47. return code;
  48. //循环查询请求是否被处理
  49. auto start_time=std::chrono::system_clock::now();
  50. double time=0;
  51. do{
  52. //在请求表中查询结果
  53. message::Measure_response_msg response=m_response_table[request.command_id()];
  54. //判断是否接收到回应,若回应信息被赋值则证明有回应
  55. if(response.has_base_info() && response.has_command_id())
  56. {
  57. message::Base_info response_base=response.base_info();
  58. //检查类型是否匹配
  59. if(response_base.msg_type() != message::eLocate_response_msg)
  60. {
  61. return Error_manager(LOCATER_MSG_RESPONSE_TYPE_ERROR,MAJOR_ERROR,"measure response msg type error");
  62. }
  63. //检查基本信息是否匹配
  64. if(response_base.sender()!=message::eMeasurer ||
  65. response_base.receiver()!=message::eMain||
  66. response.command_id()!=request.command_id())
  67. {
  68. return Error_manager(LOCATER_MSG_RESPONSE_INFO_ERROR,MAJOR_ERROR,"measure response msg info error");
  69. }
  70. result=response;
  71. return SUCCESS;
  72. }
  73. auto end_time=std::chrono::system_clock::now();
  74. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  75. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  76. std::this_thread::yield();
  77. }while(time<double(timeout));
  78. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"measure request timeout");
  79. }
  80. message::Measure_status_msg Locate_communicator::get_statu()
  81. {
  82. }
  83. Error_manager Locate_communicator::execute_msg(Communication_message* p_msg)
  84. {
  85. if(p_msg== nullptr)
  86. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"measure response msg pointer is null");
  87. //测量response消息
  88. switch (p_msg->get_message_type())
  89. {
  90. ///测量结果反馈消息
  91. case Communication_message::eLocate_response_msg:
  92. {
  93. message::Measure_response_msg response;
  94. response.ParseFromString(p_msg->get_message_buf());
  95. ///查询请求表是否存在
  96. if(m_response_table.find(response.command_id())==m_response_table.end())
  97. {
  98. return Error_manager(LOCATER_MSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"measure response without request");
  99. }
  100. ////更新表
  101. m_response_table[response.command_id()]=response;
  102. break;
  103. }
  104. ///测量系统状态
  105. case Communication_message::eLocate_status_msg:
  106. {
  107. m_measure_statu_msg=*p_msg;
  108. break;
  109. }
  110. }
  111. }