Parkspace_communicator.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Created by zx on 2020/7/9.
  3. //
  4. #ifndef NNXX_TESTS_PARKSPACE_COMMUNICATOR_H
  5. #define NNXX_TESTS_PARKSPACE_COMMUNICATOR_H
  6. #include <parkspace_allocation_message.pb.h>
  7. #include "communication_socket_base.h"
  8. #include "singleton.h"
  9. #include "thread_safe_map.h"
  10. #include "message_compare.h"
  11. /*
  12. * 注册消息的比较函数,用于重载==与<符号,使其可以用作map中的key
  13. */
  14. RegistoryCompare(message,message::Parkspace_allocation_request_msg);
  15. RegistoryCompare(message,message::Parkspace_search_request_msg)
  16. RegistoryCompare(message,message::Parkspace_release_request_msg)
  17. RegistoryCompare(message,Parkspace_confirm_alloc_request_msg)
  18. class Parkspace_communicator :public Singleton<Parkspace_communicator>, public Communication_socket_base
  19. {
  20. friend Singleton<Parkspace_communicator>;
  21. public:
  22. virtual ~Parkspace_communicator();
  23. /*
  24. * 请求分配车位
  25. */
  26. Error_manager alloc_request(message::Parkspace_allocation_request_msg& request,message::Parkspace_allocation_response_msg& result);
  27. /*
  28. * 查询车辆所在位置请求
  29. */
  30. Error_manager search_request(message::Parkspace_search_request_msg& request,message::Parkspace_search_response_msg& response);
  31. /*
  32. * 释放车位请求(停车失败或者取车完成时调用)
  33. */
  34. Error_manager release_request(message::Parkspace_release_request_msg& request,message::Parkspace_release_response_msg& response);
  35. /*
  36. * 确认占用车位消息
  37. */
  38. Error_manager confirm_request(message::Parkspace_confirm_alloc_request_msg& request,message::Parkspace_confirm_alloc_response_msg& response);
  39. Error_manager check_statu();
  40. protected:
  41. Parkspace_communicator();
  42. virtual Error_manager encapsulate_msg(Communication_message* message);
  43. virtual Error_manager execute_msg(Communication_message* p_msg);
  44. /*
  45. * 检测消息是否可被处理
  46. */
  47. virtual Error_manager check_msg(Communication_message* p_msg);
  48. /*
  49. * 心跳发送函数,重载
  50. */
  51. virtual Error_manager encapsulate_send_data();
  52. //检查消息是否可以被解析, 需要重载
  53. virtual Error_manager check_executer(Communication_message* p_msg);
  54. /*
  55. * 根据接收到的response,生成对应的 request
  56. */
  57. message::Parkspace_allocation_request_msg create_request_by_response(message::Parkspace_allocation_response_msg& msg);
  58. message::Parkspace_search_request_msg create_request_by_response(message::Parkspace_search_response_msg& msg);
  59. message::Parkspace_release_request_msg create_request_by_response(message::Parkspace_release_response_msg& msg);
  60. message::Parkspace_confirm_alloc_request_msg create_request_by_response(message::Parkspace_confirm_alloc_response_msg& msg);
  61. protected:
  62. thread_safe_map<message::Parkspace_allocation_request_msg,message::Parkspace_allocation_response_msg> m_alloc_table;
  63. thread_safe_map<message::Parkspace_search_request_msg,message::Parkspace_search_response_msg> m_search_table;
  64. thread_safe_map<message::Parkspace_release_request_msg,message::Parkspace_release_response_msg> m_release_table;
  65. thread_safe_map<message::Parkspace_confirm_alloc_request_msg,message::Parkspace_confirm_alloc_response_msg> m_confirm_table;
  66. message::Parkspace_allocation_status_msg m_parkspace_status_msg;
  67. std::chrono::system_clock::time_point m_parkspace_statu_time;
  68. };
  69. #endif //NNXX_TESTS_PARKSPACE_COMMUNICATOR_H