/* * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈 * @Author: yct * @Date: 2020-07-10 09:25:56 * @LastEditTime: 2020-08-06 10:18:56 * @LastEditors: yct */ #ifndef PARKSPACE_ALLOCATOR_HH #define PARKSPACE_ALLOCATOR_HH #include "../tool/thread_pool.h" #include "../tool/singleton.h" #include "../error_code/error_code.h" #include "../communication/communication_message.h" #include "../message/parkspace_allocation_message.pb.h" #include "parkspace_db_manager.h" #include class Parkspace_allocator : public Singleton { friend class Singleton; public: enum parkspace_allocator_status { eParkspace_allocator_unknown, eParkspace_allocator_normal, eParkspace_allocator_fault }; private: // 父类的构造函数必须保护,子类的构造函数必须私有。 Parkspace_allocator(); public: // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。 Parkspace_allocator(const Parkspace_allocator &) = delete; Parkspace_allocator &operator=(const Parkspace_allocator &) = delete; ~Parkspace_allocator(); public://API functions //初始化 Error_manager parkspace_allocator_init(int threads_size, parkspace_proto::database_config db_config); //反初始化 Error_manager parkspace_allocator_uninit(); //检查执行者的状态, 判断能否处理这条消息, Error_manager check_executer(Communication_message* p_msg); //处理消息的执行函数 Error_manager execute_msg(Communication_message* p_msg); //判断是否为待机,如果已经准备好,则可以执行任务。 bool is_ready(); // 获取模块状态 parkspace_allocator_status get_parkspace_allocator_status(); // 检查车辆是否已存在,通常分配前调用 bool check_car_existence(std::string license, message::Parkspace_allocation_status_msg status_msg); public://execute_msg创建各线程进行处理 //分配车位线程函数 void execute_for_allocate(message::Car_info car_info, int terminal_id,std::string command_key); //查询车位线程函数 void execute_for_search(message::Car_info car_info,std::string command_key); //释放车位线程函数 void execute_for_release(message::Parkspace_info space_info, std::string command_key); //强制更新车位信息线程函数 void execute_for_force_update(message::Parkspace_info space_info, std::string command_key); //确认分配车位线程函数 void execute_for_confirm_alloc(message::Parkspace_info space_info, std::string command_key); private: parkspace_allocator_status m_current_status; //分配器当前状态 Thread_pool m_thread_pool; //执行多任务的线程池 std::mutex m_mutex; //车位变动锁 Parkspace_db_manager* mp_db_manager; //车位模块数据库管理句柄 }; #endif // !PARKSPACE_ALLOCATOR_HH