/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-07 17:19:16
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-15 11:26:20
 */
#ifndef __TASHCENTER_HPP_
#define __TASHCENTER_HPP_

#include "ins_base.hpp"
#include <condition_variable>
#include <thread>
#include <queue>
#include <vector>
#include <mutex>

using namespace std;

namespace ins
{
    class TaskCenter:public Module
    {
    private:
        TaskCenter();
    public:
        using Ptr = std::shared_ptr<TaskCenter>;

         ~TaskCenter(){this->Destroy();}

        /**
         * @description: 获取实例
         * @param {*}
         * @return {*}
         */        
        static std::shared_ptr<TaskCenter> getPtr();
        
        /**
         * @description: 创建实例
         * @param {*}
         * @return {*}
         */        
        static std::shared_ptr<TaskCenter> CreateNew();

        /**
         * @description: 初始化
         * @param {*}
         * @return {*}
         */        
        int8_t Init() override;

        /**
         * @description: 释放资源
         * @param {*}
         * @return {*}
         */        
        void Destroy() override;

        /**
         * @description: 是否正常
         * @param {*}
         * @return {*}
         */        
        bool isNormally() override{
            return true;
        }

        /**
         * @description: 任务入队
         * @param {Ptr&} task 任务
         * @return {*}
         */        
        int8_t enqueue(ins::InsTask::Ptr& task);

        /**
         * @description: 处理数据包
         * @param {*}
         * @return {*}
         */        
        int8_t ProcessingPacket(InsPacket::Ptr& pack);
        
    private:
        queue<ins::InsTask::Ptr> async_Tasks;
        vector<thread> workers;
        condition_variable condition;
        mutex m_mutex;
        bool stop = false;
    }; 

} // namespace ins











#endif