parkspace_allocator.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈
  3. * @Author: yct
  4. * @Date: 2020-07-10 09:25:56
  5. * @LastEditTime: 2020-07-15 17:30:18
  6. * @LastEditors: yct
  7. */
  8. #ifndef PARKSPACE_ALLOCATOR_HH
  9. #define PARKSPACE_ALLOCATOR_HH
  10. #include "../tool/thread_pool.h"
  11. #include "../tool/singleton.h"
  12. #include "../error_code/error_code.h"
  13. #include "../communication/communication_message.h"
  14. #include "../message/parkspace_allocation_message.pb.h"
  15. class Parkspace_allocator : public Singleton<Parkspace_allocator>
  16. {
  17. friend class Singleton<Parkspace_allocator>;
  18. public:
  19. enum parkspace_allocator_status
  20. {
  21. eParkspace_allocator_unknown,
  22. eParkspace_allocator_normal,
  23. eParkspace_allocator_fault
  24. };
  25. private:
  26. // 父类的构造函数必须保护,子类的构造函数必须私有。
  27. Parkspace_allocator();
  28. public:
  29. // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  30. Parkspace_allocator(const Parkspace_allocator &) = delete;
  31. Parkspace_allocator &operator=(const Parkspace_allocator &) = delete;
  32. ~Parkspace_allocator();
  33. public://API functions
  34. //初始化
  35. Error_manager parkspace_allocator_init(int threads_size);
  36. //反初始化
  37. Error_manager parkspace_allocator_uninit();
  38. //检查执行者的状态, 判断能否处理这条消息,
  39. Error_manager check_executer(Communication_message* p_msg);
  40. //处理消息的执行函数
  41. Error_manager execute_msg(Communication_message* p_msg);
  42. //判断是否为待机,如果已经准备好,则可以执行任务。
  43. bool is_ready();
  44. // 获取模块状态
  45. parkspace_allocator_status get_parkspace_allocator_status();
  46. public://execute_msg创建各线程进行处理
  47. //分配车位线程函数
  48. void execute_for_allocate(message::Car_info car_info, int terminal_id, int command_id);
  49. //查询车位线程函数
  50. void execute_for_search(message::Car_info car_info, int command_id);
  51. //释放车位线程函数
  52. void execute_for_release(message::Parkspace_info space_info, int command_id);
  53. //强制更新车位信息线程函数
  54. void execute_for_force_update(message::Parkspace_info space_info, int command_id);
  55. //确认分配车位线程函数
  56. void execute_for_confirm_alloc(message::Parkspace_info space_info, int command_id);
  57. private:
  58. parkspace_allocator_status m_current_status; //分配器当前状态
  59. Thread_pool m_thread_pool; //执行多任务的线程池
  60. std::mutex m_mutex; //车位变动锁
  61. };
  62. #endif // !PARKSPACE_ALLOCATOR_HH