Parkspace_communicator.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. namespace message
  11. {
  12. bool operator<(const message::Parkspace_allocation_request_msg& msg1,const message::Parkspace_allocation_request_msg& msg2);
  13. bool operator==(const message::Parkspace_allocation_request_msg& msg1,const message::Parkspace_allocation_request_msg& msg2);
  14. bool operator<(const message::Parkspace_search_request_msg& msg1,const message::Parkspace_search_request_msg& msg2);
  15. bool operator==(const message::Parkspace_search_request_msg& msg1,const message::Parkspace_search_request_msg& msg2);
  16. bool operator<(const message::Parkspace_release_request_msg& msg1,const message::Parkspace_release_request_msg& msg2);
  17. bool operator==(const message::Parkspace_release_request_msg& msg1,const message::Parkspace_release_request_msg& msg2);
  18. }
  19. class Parkspace_communicator :public Singleton<Parkspace_communicator>, public Communication_socket_base
  20. {
  21. friend Singleton<Parkspace_communicator>;
  22. public:
  23. virtual ~Parkspace_communicator();
  24. /*
  25. * 请求分配车位
  26. */
  27. Error_manager alloc_request(message::Parkspace_allocation_request_msg& request,message::Parkspace_allocation_response_msg& result);
  28. /*
  29. * 查询车辆所在位置请求
  30. */
  31. Error_manager search_request(message::Parkspace_search_request_msg& request,message::Parkspace_search_response_msg& response);
  32. /*
  33. * 释放车位请求(停车失败或者取车完成时调用)
  34. */
  35. Error_manager release_request(message::Parkspace_release_request_msg& request,message::Parkspace_release_response_msg& response);
  36. Error_manager check_statu();
  37. protected:
  38. Parkspace_communicator();
  39. virtual Error_manager encapsulate_msg(Communication_message* message);
  40. virtual Error_manager execute_msg(Communication_message* p_msg);
  41. /*
  42. * 检测消息是否可被处理
  43. */
  44. virtual Error_manager check_msg(Communication_message* p_msg);
  45. /*
  46. * 心跳发送函数,重载
  47. */
  48. virtual Error_manager encapsulate_send_data();
  49. //检查消息是否可以被解析, 需要重载
  50. virtual Error_manager check_executer(Communication_message* p_msg);
  51. /*
  52. * 根据接收到的response,生成对应的 request
  53. */
  54. message::Parkspace_allocation_request_msg create_request_by_response(message::Parkspace_allocation_response_msg& msg);
  55. message::Parkspace_search_request_msg create_request_by_response(message::Parkspace_search_response_msg& msg);
  56. message::Parkspace_release_request_msg create_request_by_response(message::Parkspace_release_response_msg& msg);
  57. protected:
  58. thread_safe_map<message::Parkspace_allocation_request_msg,message::Parkspace_allocation_response_msg> m_alloc_table;
  59. thread_safe_map<message::Parkspace_search_request_msg,message::Parkspace_search_response_msg> m_search_table;
  60. thread_safe_map<message::Parkspace_release_request_msg,message::Parkspace_release_response_msg> m_release_table;
  61. Communication_message m_parkspace_status_msg;
  62. };
  63. #endif //NNXX_TESTS_PARKSPACE_COMMUNICATOR_H