task_base.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Created by zx on 2019/12/28.
  3. //
  4. #include "task_base.h"
  5. #include "../error_code/error_code.h"
  6. Task_Base::Task_Base()
  7. {
  8. static unsigned int t_task_id = 0;
  9. m_task_id = t_task_id;
  10. t_task_id++;
  11. m_task_type = UNKNOW_TASK;
  12. m_task_statu = TASK_CREATED;
  13. mp_tast_receiver = NULL;
  14. m_task_start_time = std::chrono::system_clock::now(); //获取当前时间
  15. m_task_over_time = std::chrono::milliseconds(TASK_OVER_TIME_DEFAULT); //默认10秒
  16. }
  17. Task_Base::~Task_Base()
  18. {
  19. mp_tast_receiver = NULL;
  20. }
  21. //更新任务单
  22. //task_statu: 任务状态
  23. //statu_information:状态说明
  24. Error_manager Task_Base::update_statu(Task_statu task_statu,std::string statu_information)
  25. {
  26. m_task_statu=task_statu;
  27. m_task_statu_information=statu_information;
  28. return SUCCESS;
  29. }
  30. //判断是否超时。返回true表示任务超时,返回false表示任务没有超时
  31. bool Task_Base::is_over_time()
  32. {
  33. return (std::chrono::system_clock::now() - m_task_start_time) > m_task_over_time;
  34. }
  35. //判断是否结束, TASK_OVER TASK_ERROR TASK_DEAD 都算结束
  36. bool Task_Base::is_task_end()
  37. {
  38. if(m_task_statu == TASK_OVER || m_task_statu == TASK_ERROR || m_task_statu == TASK_DEAD)
  39. {
  40. return true;
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46. }
  47. //获取 任务单id
  48. unsigned int Task_Base::get_task_id()
  49. {
  50. return m_task_id;
  51. }
  52. //获取任务类型
  53. Task_type Task_Base::get_task_type()
  54. {
  55. return m_task_type;
  56. }
  57. //获取任务单状态
  58. Task_statu Task_Base::get_task_statu()
  59. {
  60. return m_task_statu;
  61. }
  62. //设置 任务单状态
  63. void Task_Base::set_task_statu(Task_statu task_statu)
  64. {
  65. m_task_statu = task_statu;
  66. }
  67. //获取状态说明
  68. std::string Task_Base::get_task_statu_information()
  69. {
  70. return m_task_statu_information;
  71. }
  72. //设置 状态说明
  73. void Task_Base::set_task_statu_information(std::string task_statu_information)
  74. {
  75. m_task_statu_information = task_statu_information;
  76. }
  77. //获取 错误码,返回引用。
  78. Error_manager& Task_Base::get_task_error_manager()
  79. {
  80. return m_task_error_manager;
  81. }
  82. //设置 错误码
  83. void Task_Base::set_task_error_manager(Error_manager & error_manager)
  84. {
  85. m_task_error_manager = error_manager;
  86. }
  87. //比较覆盖错误码
  88. void Task_Base::compare_and_cover_task_error_manager(Error_manager & error_manager)
  89. {
  90. m_task_error_manager.compare_and_cover_error(error_manager);
  91. }
  92. //获取任务接收方
  93. void * Task_Base::get_tast_receiver()
  94. {
  95. return mp_tast_receiver;
  96. }
  97. //设置任务接收方
  98. void Task_Base::set_tast_receiver(void * p_tast_receiver)
  99. {
  100. mp_tast_receiver = p_tast_receiver;
  101. }
  102. //获取 任务创建的时间点
  103. std::chrono::system_clock::time_point Task_Base::get_task_start_time()
  104. {
  105. return m_task_start_time;
  106. }
  107. //设置 任务创建的时间点
  108. void Task_Base::set_task_start_time(std::chrono::system_clock::time_point task_start_time)
  109. {
  110. m_task_start_time = task_start_time;
  111. }
  112. //获取 任务超时的时限
  113. std::chrono::milliseconds Task_Base::get_task_over_time()
  114. {
  115. return m_task_over_time;
  116. }
  117. //设置 任务超时的时限
  118. void Task_Base::get_task_over_time(std::chrono::milliseconds task_over_time)
  119. {
  120. m_task_over_time = task_over_time;
  121. }