123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- //
- // Created by zx on 2019/12/28.
- //
- #include "task_base.h"
- #include "../error_code/error_code.h"
- Task_Base::Task_Base()
- {
- static unsigned int t_task_id = 0;
- m_task_id = t_task_id;
- t_task_id++;
- m_task_type = UNKNOW_TASK;
- m_task_statu = TASK_CREATED;
- mp_tast_receiver = NULL;
- m_task_start_time = std::chrono::system_clock::now(); //获取当前时间
- m_task_over_time = std::chrono::milliseconds(TASK_OVER_TIME_DEFAULT); //默认10秒
- }
- Task_Base::~Task_Base()
- {
- mp_tast_receiver = NULL;
- }
- //初始化任务单,必须初始化之后才可以使用,
- // input:tast_receiver 接受对象
- // input:task_over_time 超时时间
- Error_manager Task_Base::task_init(void* p_tast_receiver,
- std::chrono::milliseconds task_over_time)
- {
- m_task_statu = TASK_CREATED;
- m_task_statu_information = "";
- mp_tast_receiver = p_tast_receiver;
- m_task_over_time = task_over_time;
- m_task_error_manager.error_manager_clear_all();
- return Error_code::SUCCESS;
- }
- //初始化任务单,必须初始化之后才可以使用,
- // input:task_statu 任务状态
- // input:task_statu_information 状态说明
- // input:tast_receiver 接受对象
- // input:task_over_time 超时时间
- Error_manager Task_Base::task_init(Task_statu task_statu,
- std::string task_statu_information,
- void* p_tast_receiver,
- std::chrono::milliseconds task_over_time)
- {
- m_task_statu = task_statu;
- m_task_statu_information = task_statu_information;
- mp_tast_receiver = p_tast_receiver;
- m_task_over_time = task_over_time;
- m_task_error_manager.error_manager_clear_all();
- return Error_code::SUCCESS;
- }
- //任务单重置, 相当于重新创建, 会重置时间和错误码.
- Error_manager Task_Base::task_reset()
- {
- //m_task_id不变 //m_task_type不变 //因为这个是创建的时候就定好的
- m_task_statu = TASK_CREATED;
- m_task_statu_information.clear();
- mp_tast_receiver = NULL;
- m_task_start_time = std::chrono::system_clock::now(); //获取当前时间
- m_task_over_time = std::chrono::milliseconds(TASK_OVER_TIME_DEFAULT); //默认10秒
- m_task_error_manager.error_manager_clear_all();
- return Error_code::SUCCESS;
- }
- //更新任务单
- //task_statu: 任务状态
- //statu_information:状态说明
- Error_manager Task_Base::update_statu(Task_statu task_statu,std::string statu_information)
- {
- m_task_statu=task_statu;
- m_task_statu_information=statu_information;
- return SUCCESS;
- }
- //判断是否超时。返回true表示任务超时,返回false表示任务没有超时
- bool Task_Base::is_over_time()
- {
- return (std::chrono::system_clock::now() - m_task_start_time) > m_task_over_time;
- }
- //判断是否结束, TASK_OVER TASK_ERROR TASK_DEAD 都算结束
- bool Task_Base::is_task_end()
- {
- if(m_task_statu == TASK_OVER || m_task_statu == TASK_ERROR || m_task_statu == TASK_DEAD)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //获取 任务单id
- unsigned int Task_Base::get_task_id()
- {
- return m_task_id;
- }
- //获取任务类型
- Task_Base::Task_type Task_Base::get_task_type()
- {
- return m_task_type;
- }
- //获取任务单状态
- Task_Base::Task_statu Task_Base::get_task_statu()
- {
- return m_task_statu;
- }
- //设置 任务单状态
- void Task_Base::set_task_statu(Task_statu task_statu)
- {
- m_task_statu = task_statu;
- }
- //获取状态说明
- std::string Task_Base::get_task_statu_information()
- {
- return m_task_statu_information;
- }
- //设置 状态说明
- void Task_Base::set_task_statu_information(std::string task_statu_information)
- {
- m_task_statu_information = task_statu_information;
- }
- //获取 错误码,返回引用。
- Error_manager& Task_Base::get_task_error_manager()
- {
- return m_task_error_manager;
- }
- //设置 错误码
- void Task_Base::set_task_error_manager(Error_manager & error_manager)
- {
- m_task_error_manager = error_manager;
- }
- //比较覆盖错误码
- void Task_Base::compare_and_cover_task_error_manager(Error_manager & error_manager)
- {
- m_task_error_manager.compare_and_cover_error(error_manager);
- }
- //获取任务接收方
- void * Task_Base::get_tast_receiver()
- {
- return mp_tast_receiver;
- }
- //设置任务接收方
- void Task_Base::set_tast_receiver(void * p_tast_receiver)
- {
- mp_tast_receiver = p_tast_receiver;
- }
- //获取 任务创建的时间点
- std::chrono::system_clock::time_point Task_Base::get_task_start_time()
- {
- return m_task_start_time;
- }
- //设置 任务创建的时间点
- void Task_Base::set_task_start_time(std::chrono::system_clock::time_point task_start_time)
- {
- m_task_start_time = task_start_time;
- }
- //获取 任务超时的时限
- std::chrono::milliseconds Task_Base::get_task_over_time()
- {
- return m_task_over_time;
- }
- //设置 任务超时的时限
- void Task_Base::set_task_over_time(std::chrono::milliseconds task_over_time)
- {
- m_task_over_time = task_over_time;
- }
|