task_base.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // input:tast_receiver 接受对象
  23. // input:task_over_time 超时时间
  24. Error_manager Task_Base::task_init(void* p_tast_receiver,
  25. std::chrono::milliseconds task_over_time)
  26. {
  27. m_task_statu = TASK_CREATED;
  28. m_task_statu_information = "";
  29. mp_tast_receiver = p_tast_receiver;
  30. m_task_over_time = task_over_time;
  31. m_task_error_manager.error_manager_clear_all();
  32. return Error_code::SUCCESS;
  33. }
  34. //初始化任务单,必须初始化之后才可以使用,
  35. // input:task_statu 任务状态
  36. // input:task_statu_information 状态说明
  37. // input:tast_receiver 接受对象
  38. // input:task_over_time 超时时间
  39. Error_manager Task_Base::task_init(Task_statu task_statu,
  40. std::string task_statu_information,
  41. void* p_tast_receiver,
  42. std::chrono::milliseconds task_over_time)
  43. {
  44. m_task_statu = task_statu;
  45. m_task_statu_information = task_statu_information;
  46. mp_tast_receiver = p_tast_receiver;
  47. m_task_over_time = task_over_time;
  48. m_task_error_manager.error_manager_clear_all();
  49. return Error_code::SUCCESS;
  50. }
  51. //任务单重置, 相当于重新创建, 会重置时间和错误码.
  52. Error_manager Task_Base::task_reset()
  53. {
  54. //m_task_id不变 //m_task_type不变 //因为这个是创建的时候就定好的
  55. m_task_statu = TASK_CREATED;
  56. m_task_statu_information.clear();
  57. mp_tast_receiver = NULL;
  58. m_task_start_time = std::chrono::system_clock::now(); //获取当前时间
  59. m_task_over_time = std::chrono::milliseconds(TASK_OVER_TIME_DEFAULT); //默认10秒
  60. m_task_error_manager.error_manager_clear_all();
  61. return Error_code::SUCCESS;
  62. }
  63. //更新任务单
  64. //task_statu: 任务状态
  65. //statu_information:状态说明
  66. Error_manager Task_Base::update_statu(Task_statu task_statu,std::string statu_information)
  67. {
  68. m_task_statu=task_statu;
  69. m_task_statu_information=statu_information;
  70. return SUCCESS;
  71. }
  72. //判断是否超时。返回true表示任务超时,返回false表示任务没有超时
  73. bool Task_Base::is_over_time()
  74. {
  75. return (std::chrono::system_clock::now() - m_task_start_time) > m_task_over_time;
  76. }
  77. //判断是否结束, TASK_OVER TASK_ERROR TASK_DEAD 都算结束
  78. bool Task_Base::is_task_end()
  79. {
  80. if(m_task_statu == TASK_OVER || m_task_statu == TASK_ERROR || m_task_statu == TASK_DEAD)
  81. {
  82. return true;
  83. }
  84. else
  85. {
  86. return false;
  87. }
  88. }
  89. //获取 任务单id
  90. unsigned int Task_Base::get_task_id()
  91. {
  92. return m_task_id;
  93. }
  94. //获取任务类型
  95. Task_Base::Task_type Task_Base::get_task_type()
  96. {
  97. return m_task_type;
  98. }
  99. //获取任务单状态
  100. Task_Base::Task_statu Task_Base::get_task_statu()
  101. {
  102. return m_task_statu;
  103. }
  104. //设置 任务单状态
  105. void Task_Base::set_task_statu(Task_statu task_statu)
  106. {
  107. m_task_statu = task_statu;
  108. }
  109. //获取状态说明
  110. std::string Task_Base::get_task_statu_information()
  111. {
  112. return m_task_statu_information;
  113. }
  114. //设置 状态说明
  115. void Task_Base::set_task_statu_information(std::string task_statu_information)
  116. {
  117. m_task_statu_information = task_statu_information;
  118. }
  119. //获取 错误码,返回引用。
  120. Error_manager& Task_Base::get_task_error_manager()
  121. {
  122. return m_task_error_manager;
  123. }
  124. //设置 错误码
  125. void Task_Base::set_task_error_manager(Error_manager & error_manager)
  126. {
  127. m_task_error_manager = error_manager;
  128. }
  129. //比较覆盖错误码
  130. void Task_Base::compare_and_cover_task_error_manager(Error_manager & error_manager)
  131. {
  132. m_task_error_manager.compare_and_cover_error(error_manager);
  133. }
  134. //获取任务接收方
  135. void * Task_Base::get_tast_receiver()
  136. {
  137. return mp_tast_receiver;
  138. }
  139. //设置任务接收方
  140. void Task_Base::set_tast_receiver(void * p_tast_receiver)
  141. {
  142. mp_tast_receiver = p_tast_receiver;
  143. }
  144. //获取 任务创建的时间点
  145. std::chrono::system_clock::time_point Task_Base::get_task_start_time()
  146. {
  147. return m_task_start_time;
  148. }
  149. //设置 任务创建的时间点
  150. void Task_Base::set_task_start_time(std::chrono::system_clock::time_point task_start_time)
  151. {
  152. m_task_start_time = task_start_time;
  153. }
  154. //获取 任务超时的时限
  155. std::chrono::milliseconds Task_Base::get_task_over_time()
  156. {
  157. return m_task_over_time;
  158. }
  159. //设置 任务超时的时限
  160. void Task_Base::set_task_over_time(std::chrono::milliseconds task_over_time)
  161. {
  162. m_task_over_time = task_over_time;
  163. }