parkspace_communicator.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "parkspace_communicator.h"
  2. #include "parkspace_manager.h"
  3. Parkspace_communicator::Parkspace_communicator()
  4. {
  5. }
  6. Parkspace_communicator::~Parkspace_communicator()
  7. {
  8. }
  9. //************************************* 公有函数 ****************************************
  10. //外部调用发送分配车位结果
  11. Error_manager Parkspace_communicator::send_response(Communication_message *message)
  12. {
  13. return encapsulate_msg(message);
  14. }
  15. //************************************* 内部函数 ****************************************
  16. //封装反馈消息自动发送
  17. Error_manager Parkspace_communicator::encapsulate_msg(Communication_message *message)
  18. {
  19. if (message == nullptr)
  20. {
  21. return Error_manager(POINTER_IS_NULL, NEGLIGIBLE_ERROR, "Parkspace_communicator::message null pointer");
  22. }
  23. //针对待发送的不同反馈消息,分别进行校核
  24. switch(message->get_message_type())
  25. {
  26. case Communication_message::eParkspace_allocation_response_msg:
  27. {
  28. // message::Parkspace_allocation_response_msg response;
  29. // bool result = response.ParseFromString(message->get_message_buf());
  30. // //可增加待发送分配车位反馈消息校核
  31. break;
  32. }
  33. case Communication_message::eParkspace_search_response_msg:
  34. {
  35. break;
  36. }
  37. case Communication_message::eParkspace_release_response_msg:
  38. {
  39. break;
  40. }
  41. case Communication_message::eParkspace_force_update_response_msg:
  42. {
  43. break;
  44. }
  45. case Communication_message::eParkspace_confirm_alloc_response_msg:
  46. {
  47. break;
  48. }
  49. case Communication_message::eParkspace_allocation_status_msg:
  50. {
  51. break;
  52. }
  53. case Communication_message::eParkspace_allocation_data_response_msg:
  54. {
  55. break;
  56. }
  57. case Communication_message::eParkspace_manual_search_response_msg:
  58. {
  59. break;
  60. }
  61. default:
  62. return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "Parkspace_communicator::message response type error");
  63. }
  64. //发送请求
  65. return Communication_socket_base::encapsulate_msg(message);
  66. }
  67. //封装发送消息
  68. Error_manager Parkspace_communicator::encapsulate_msg(std::string message)
  69. {
  70. return Communication_socket_base::encapsulate_msg(message);
  71. }
  72. //重载执行消息,父类线程通过check_msg接收到车位分配请求后调用,解析内容并进行相应处理
  73. Error_manager Parkspace_communicator::execute_msg(Communication_message *p_msg)
  74. {
  75. return Parkspace_manager::get_instance_references().execute_msg(p_msg);
  76. // return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "parkspace allocation wrong request type");
  77. }
  78. //检查消息是否应由本模块接收
  79. //本模块接收车位分配请求、车位查询请求与车位释放请求
  80. Error_manager Parkspace_communicator::check_msg(Communication_message* p_msg)
  81. {
  82. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是车位请求消息.
  83. if(p_msg->get_receiver() == Communication_message::Communicator::eParkspace)
  84. {
  85. switch(p_msg->get_message_type())
  86. {
  87. case Communication_message::Message_type::eParkspace_allocation_request_msg:
  88. return Error_code::SUCCESS;
  89. case Communication_message::Message_type::eParkspace_search_request_msg:
  90. return Error_code::SUCCESS;
  91. case Communication_message::Message_type::eParkspace_release_request_msg:
  92. return Error_code::SUCCESS;
  93. case Communication_message::Message_type::eParkspace_force_update_request_msg:
  94. return Error_code::SUCCESS;
  95. case Communication_message::Message_type::eParkspace_confirm_alloc_request_msg:
  96. return Error_code::SUCCESS;
  97. case Communication_message::Message_type::eParkspace_refresh_request_msg:
  98. return Error_code::SUCCESS;
  99. case Communication_message::Message_type::eParkspace_manual_search_request_msg:
  100. return Error_code::SUCCESS;
  101. default:
  102. return Error_code::INVALID_MESSAGE;
  103. }
  104. }
  105. return Error_code::INVALID_MESSAGE;
  106. }
  107. Error_manager Parkspace_communicator::check_executer(Communication_message* p_msg)
  108. {
  109. //检查对应模块的状态, 判断是否可以处理这条消息
  110. //同时也要判断是否超时, 超时返回 COMMUNICATION_ANALYSIS_TIME_OUT
  111. //如果处理器正在忙别的, 那么返回 COMMUNICATION_EXCUTER_IS_BUSY
  112. Error_manager t_error;
  113. if ( p_msg->is_over_time() )
  114. { //huli +1
  115. std::cout << "Parkspace_communicator...COMMUNICATION_ANALYSIS_TIME_OUT , " << std::endl;
  116. //超时:接收方不做处理,发送方会进行超时处理
  117. return Error_code::COMMUNICATION_ANALYSIS_TIME_OUT;
  118. }
  119. else
  120. {
  121. Error_manager t_error=Database_controller::get_instance_pointer()->check_status();
  122. if(t_error != SUCCESS)
  123. {
  124. return t_error;
  125. }
  126. t_error=Parkspace_manager::get_instance_pointer()->check_status();
  127. if(t_error != SUCCESS)
  128. {
  129. return t_error;
  130. }
  131. }
  132. return Error_code::SUCCESS;
  133. }
  134. //重载心跳与车位状态发送函数
  135. Error_manager Parkspace_communicator::encapsulate_send_data()
  136. {
  137. //胡力 这里只发送manager状态 不发生车位数据
  138. // parkspace_manager_satus m_current_status;
  139. // Database_controller_status m_database_controller_status;
  140. return Parkspace_manager::get_instance_pointer()->encapsulating_heartbeat_messages();
  141. //
  142. // bool is_push = m_send_data_list.push(&tp_msg);
  143. // if ( is_push == false )
  144. // {
  145. // return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  146. // " Parkspace_communicator::send status error ");
  147. // }
  148. // return Error_code::SUCCESS;
  149. }