system_executor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //
  2. // Created by huli on 2020/7/2.
  3. //
  4. #include "system_executor.h"
  5. System_executor::System_executor()
  6. {
  7. }
  8. System_executor::~System_executor()
  9. {
  10. system_executor_uninit();
  11. }
  12. //初始化
  13. Error_manager System_executor::system_executor_init(int threads_size)
  14. {
  15. m_thread_pool.thread_pool_init(threads_size);
  16. m_system_executor_status = SYSTEM_EXECUTOR_READY;
  17. return Error_code::SUCCESS;
  18. }
  19. //反初始化
  20. Error_manager System_executor::system_executor_uninit()
  21. {
  22. m_thread_pool.thread_pool_uninit();
  23. m_system_executor_status = SYSTEM_EXECUTOR_UNKNOW;
  24. return Error_code::SUCCESS;
  25. }
  26. //检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
  27. Error_manager System_executor::check_msg(Communication_message* p_msg)
  28. {
  29. if ( p_msg == NULL )
  30. {
  31. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  32. " POINTER IS NULL ");
  33. }
  34. //检查消息类型
  35. switch ( p_msg->get_message_type() )
  36. {
  37. case Communication_message::Message_type::eDispatch_request_msg:
  38. {
  39. message::Dispatch_request_msg t_dispatch_request_msg;
  40. //针对消息类型, 对消息进行二次解析
  41. if (t_dispatch_request_msg.ParseFromString(p_msg->get_message_buf()))
  42. {
  43. LOG(INFO) << " t_dispatch_request_msg = "<< t_dispatch_request_msg.DebugString() << " -- " << this;
  44. }
  45. break;
  46. }
  47. case Communication_message::Message_type::eDispatch_response_msg:
  48. {
  49. message::Dispatch_response_msg t_dispatch_response_msg;
  50. //针对消息类型, 对消息进行二次解析
  51. if (t_dispatch_response_msg.ParseFromString(p_msg->get_message_buf()))
  52. {
  53. LOG(INFO) << " t_dispatch_response_msg = "<< t_dispatch_response_msg.DebugString() << " -- " << this;
  54. }
  55. break;
  56. }
  57. case Communication_message::Message_type::eDispatch_status_msg:
  58. {
  59. message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
  60. //针对消息类型, 对消息进行二次解析
  61. if (t_dispatch_terminal_status_msg.ParseFromString(p_msg->get_message_buf()))
  62. {
  63. if ( t_dispatch_terminal_status_msg.id_struct().unit_id() == 0 )
  64. {
  65. LOG(INFO) << " t_dispatch_terminal_status_msg = "<< t_dispatch_terminal_status_msg.DebugString() << " -- " << this;
  66. }
  67. }
  68. break;
  69. }
  70. // case Communication_message::Message_type::eParkspace_allocation_status_msg:
  71. // {
  72. // message::Parkspace_allocation_status_msg t_Parkspace_allocation_status_msg;
  73. // //针对消息类型, 对消息进行二次解析
  74. // if (t_Parkspace_allocation_status_msg.ParseFromString(p_msg->get_message_buf()))
  75. // {
  76. // LOG(INFO) << " t_Parkspace_allocation_status_msg = "<< t_Parkspace_allocation_status_msg.DebugString() << " -- " << this;
  77. // }
  78. // break;
  79. // }
  80. case Communication_message::Message_type::eParkspace_allocation_request_msg:
  81. {
  82. message::Parkspace_allocation_request_msg t_Parkspace_allocation_request_msg;
  83. //针对消息类型, 对消息进行二次解析
  84. if (t_Parkspace_allocation_request_msg.ParseFromString(p_msg->get_message_buf()))
  85. {
  86. LOG(INFO) << " t_Parkspace_allocation_request_msg = "<< t_Parkspace_allocation_request_msg.DebugString() << " -- " << this;
  87. }
  88. break;
  89. }
  90. case Communication_message::Message_type::eParkspace_allocation_response_msg:
  91. {
  92. message::Parkspace_allocation_response_msg t_Parkspace_allocation_response_msg;
  93. //针对消息类型, 对消息进行二次解析
  94. if (t_Parkspace_allocation_response_msg.ParseFromString(p_msg->get_message_buf()))
  95. {
  96. LOG(INFO) << " t_Parkspace_allocation_response_msg = "<< t_Parkspace_allocation_response_msg.DebugString() << " -- " << this;
  97. }
  98. break;
  99. }
  100. case Communication_message::Message_type::eParkspace_search_request_msg:
  101. {
  102. message::Parkspace_search_request_msg t_Parkspace_search_request_msg;
  103. //针对消息类型, 对消息进行二次解析
  104. if (t_Parkspace_search_request_msg.ParseFromString(p_msg->get_message_buf()))
  105. {
  106. LOG(INFO) << " t_Parkspace_search_request_msg = "<< t_Parkspace_search_request_msg.DebugString() << " -- " << this;
  107. }
  108. break;
  109. }
  110. case Communication_message::Message_type::eParkspace_search_response_msg:
  111. {
  112. message::Parkspace_search_response_msg t_Parkspace_search_response_msg;
  113. //针对消息类型, 对消息进行二次解析
  114. if (t_Parkspace_search_response_msg.ParseFromString(p_msg->get_message_buf()))
  115. {
  116. LOG(INFO) << " t_Parkspace_search_response_msg = "<< t_Parkspace_search_response_msg.DebugString() << " -- " << this;
  117. }
  118. break;
  119. }
  120. case Communication_message::Message_type::eParkspace_release_request_msg:
  121. {
  122. message::Parkspace_release_request_msg t_Parkspace_release_request_msg;
  123. //针对消息类型, 对消息进行二次解析
  124. if (t_Parkspace_release_request_msg.ParseFromString(p_msg->get_message_buf()))
  125. {
  126. LOG(INFO) << " t_Parkspace_release_request_msg = "<< t_Parkspace_release_request_msg.DebugString() << " -- " << this;
  127. }
  128. break;
  129. }
  130. case Communication_message::Message_type::eParkspace_release_response_msg:
  131. {
  132. message::Parkspace_release_response_msg t_Parkspace_release_response_msg;
  133. //针对消息类型, 对消息进行二次解析
  134. if (t_Parkspace_release_response_msg.ParseFromString(p_msg->get_message_buf()))
  135. {
  136. LOG(INFO) << " t_Parkspace_release_response_msg = "<< t_Parkspace_release_response_msg.DebugString() << " -- " << this;
  137. }
  138. break;
  139. }
  140. case Communication_message::Message_type::eParkspace_force_update_request_msg:
  141. {
  142. message::Parkspace_force_update_request_msg t_Parkspace_force_update_request_msg;
  143. //针对消息类型, 对消息进行二次解析
  144. if (t_Parkspace_force_update_request_msg.ParseFromString(p_msg->get_message_buf()))
  145. {
  146. LOG(INFO) << " t_Parkspace_force_update_request_msg = "<< t_Parkspace_force_update_request_msg.DebugString() << " -- " << this;
  147. }
  148. break;
  149. }
  150. case Communication_message::Message_type::eParkspace_force_update_response_msg:
  151. {
  152. message::Parkspace_force_update_response_msg t_Parkspace_force_update_response_msg;
  153. //针对消息类型, 对消息进行二次解析
  154. if (t_Parkspace_force_update_response_msg.ParseFromString(p_msg->get_message_buf()))
  155. {
  156. LOG(INFO) << " t_Parkspace_force_update_response_msg = "<< t_Parkspace_force_update_response_msg.DebugString() << " -- " << this;
  157. }
  158. break;
  159. }
  160. case Communication_message::Message_type::eParkspace_confirm_alloc_request_msg:
  161. {
  162. message::Parkspace_confirm_alloc_request_msg t_Parkspace_confirm_alloc_request_msg;
  163. //针对消息类型, 对消息进行二次解析
  164. if (t_Parkspace_confirm_alloc_request_msg.ParseFromString(p_msg->get_message_buf()))
  165. {
  166. LOG(INFO) << " t_Parkspace_confirm_alloc_request_msg = "<< t_Parkspace_confirm_alloc_request_msg.DebugString() << " -- " << this;
  167. }
  168. break;
  169. }
  170. case Communication_message::Message_type::eParkspace_confirm_alloc_response_msg:
  171. {
  172. message::Parkspace_confirm_alloc_response_msg t_Parkspace_confirm_alloc_response_msg;
  173. //针对消息类型, 对消息进行二次解析
  174. if (t_Parkspace_confirm_alloc_response_msg.ParseFromString(p_msg->get_message_buf()))
  175. {
  176. LOG(INFO) << " t_Parkspace_confirm_alloc_response_msg = "<< t_Parkspace_confirm_alloc_response_msg.DebugString() << " -- " << this;
  177. }
  178. break;
  179. }
  180. case Communication_message::Message_type::eParkspace_refresh_request_msg:
  181. {
  182. message::Parkspace_refresh_request_msg t_Parkspace_refresh_request_msg;
  183. //针对消息类型, 对消息进行二次解析
  184. if (t_Parkspace_refresh_request_msg.ParseFromString(p_msg->get_message_buf()))
  185. {
  186. // LOG(INFO) << " t_Parkspace_refresh_request_msg = "<< t_Parkspace_refresh_request_msg.DebugString() << " -- " << this;
  187. LOG(INFO) << " t_Parkspace_refresh_request_msg = "<< t_Parkspace_refresh_request_msg.base_info().msg_type() << " -- " << this;
  188. }
  189. break;
  190. }
  191. case Communication_message::Message_type::eParkspace_allocation_data_response_msg:
  192. {
  193. message::Parkspace_allocation_data_msg t_Parkspace_allocation_data_response_msg;
  194. //针对消息类型, 对消息进行二次解析
  195. if (t_Parkspace_allocation_data_response_msg.ParseFromString(p_msg->get_message_buf()))
  196. {
  197. // LOG(INFO) << " t_Parkspace_allocation_data_response_msg = "<< t_Parkspace_allocation_data_response_msg.DebugString() << " -- " << this;
  198. // LOG(INFO) << " t_Parkspace_allocation_data_response_msg = "<< t_Parkspace_allocation_data_response_msg.base_info().msg_type() << " -- " << this;
  199. }
  200. break;
  201. }
  202. case Communication_message::Message_type::eParkspace_manual_search_request_msg:
  203. {
  204. message::Parkspace_manual_search_request_msg t_Parkspace_manual_search_request_msg;
  205. //针对消息类型, 对消息进行二次解析
  206. if (t_Parkspace_manual_search_request_msg.ParseFromString(p_msg->get_message_buf()))
  207. {
  208. LOG(INFO) << " t_Parkspace_manual_search_request_msg = "<< t_Parkspace_manual_search_request_msg.DebugString() << " -- " << this;
  209. }
  210. break;
  211. }
  212. case Communication_message::Message_type::eParkspace_manual_search_response_msg:
  213. {
  214. message::Parkspace_manual_search_response_msg t_Parkspace_manual_search_response_msg;
  215. //针对消息类型, 对消息进行二次解析
  216. if (t_Parkspace_manual_search_response_msg.ParseFromString(p_msg->get_message_buf()))
  217. {
  218. LOG(INFO) << " t_Parkspace_manual_search_response_msg = "<< t_Parkspace_manual_search_response_msg.DebugString() << " -- " << this;
  219. }
  220. break;
  221. }
  222. case Communication_message::Message_type::eStore_command_request_msg:
  223. {
  224. message::Store_command_request_msg t_Store_command_request_msg;
  225. //针对消息类型, 对消息进行二次解析
  226. if (t_Store_command_request_msg.ParseFromString(p_msg->get_message_buf()))
  227. {
  228. LOG(INFO) << " t_Store_command_request_msg = "<< t_Store_command_request_msg.DebugString() << " -- " << this;
  229. }
  230. break;
  231. }
  232. case Communication_message::Message_type::eStore_command_response_msg:
  233. {
  234. message::Store_command_response_msg t_Store_command_response_msg;
  235. //针对消息类型, 对消息进行二次解析
  236. if (t_Store_command_response_msg.ParseFromString(p_msg->get_message_buf()))
  237. {
  238. LOG(INFO) << " t_Store_command_response_msg = "<< t_Store_command_response_msg.DebugString() << " -- " << this;
  239. }
  240. break;
  241. }
  242. case Communication_message::Message_type::ePickup_command_request_msg:
  243. {
  244. message::Pickup_command_request_msg t_Pickup_command_request_msg;
  245. //针对消息类型, 对消息进行二次解析
  246. if (t_Pickup_command_request_msg.ParseFromString(p_msg->get_message_buf()))
  247. {
  248. LOG(INFO) << " t_Pickup_command_request_msg = "<< t_Pickup_command_request_msg.DebugString() << " -- " << this;
  249. }
  250. break;
  251. }
  252. case Communication_message::Message_type::ePickup_command_response_msg:
  253. {
  254. message::Pickup_command_response_msg t_Pickup_command_response_msg;
  255. //针对消息类型, 对消息进行二次解析
  256. if (t_Pickup_command_response_msg.ParseFromString(p_msg->get_message_buf()))
  257. {
  258. LOG(INFO) << " t_Pickup_command_response_msg = "<< t_Pickup_command_response_msg.DebugString() << " -- " << this;
  259. }
  260. break;
  261. }
  262. case Communication_message::Message_type::eEntrance_manual_operation_msg:
  263. {
  264. message::Entrance_manual_operation_msg t_Entrance_manual_operation_msg;
  265. //针对消息类型, 对消息进行二次解析
  266. if (t_Entrance_manual_operation_msg.ParseFromString(p_msg->get_message_buf()))
  267. {
  268. LOG(INFO) << " t_Entrance_manual_operation_msg = "<< t_Entrance_manual_operation_msg.DebugString() << " -- " << this;
  269. }
  270. break;
  271. }
  272. case Communication_message::Message_type::eProcess_manual_operation_msg:
  273. {
  274. message::Process_manual_operation_msg t_Process_manual_operation_msg;
  275. //针对消息类型, 对消息进行二次解析
  276. if (t_Process_manual_operation_msg.ParseFromString(p_msg->get_message_buf()))
  277. {
  278. LOG(INFO) << " t_Process_manual_operation_msg = "<< t_Process_manual_operation_msg.DebugString() << " -- " << this;
  279. }
  280. break;
  281. }
  282. case Communication_message::Message_type::eNotify_request_msg:
  283. {
  284. message::Notify_request_msg t_Notify_request_msg;
  285. //针对消息类型, 对消息进行二次解析
  286. if (t_Notify_request_msg.ParseFromString(p_msg->get_message_buf()))
  287. {
  288. LOG(INFO) << " t_Notify_request_msg = "<< t_Notify_request_msg.DebugString() << " -- " << this;
  289. }
  290. break;
  291. }
  292. case Communication_message::Message_type::eNotify_response_msg:
  293. {
  294. message::Notify_response_msg t_Notify_response_msg;
  295. //针对消息类型, 对消息进行二次解析
  296. if (t_Notify_response_msg.ParseFromString(p_msg->get_message_buf()))
  297. {
  298. LOG(INFO) << " t_Notify_response_msg = "<< t_Notify_response_msg.DebugString() << " -- " << this;
  299. }
  300. break;
  301. }
  302. case Communication_message::Message_type::eGround_detect_request_msg:
  303. {
  304. message::Ground_detect_request_msg t_Ground_detect_request_msg;
  305. //针对消息类型, 对消息进行二次解析
  306. if (t_Ground_detect_request_msg.ParseFromString(p_msg->get_message_buf()))
  307. {
  308. LOG(INFO) << " t_Ground_detect_request_msg = "<< t_Ground_detect_request_msg.DebugString() << " -- " << this;
  309. }
  310. break;
  311. }
  312. case Communication_message::Message_type::eGround_detect_response_msg:
  313. {
  314. message::Ground_detect_response_msg t_Ground_detect_response_msg;
  315. //针对消息类型, 对消息进行二次解析
  316. if (t_Ground_detect_response_msg.ParseFromString(p_msg->get_message_buf()))
  317. {
  318. LOG(INFO) << " t_Ground_detect_response_msg = "<< t_Ground_detect_response_msg.DebugString() << " -- " << this;
  319. }
  320. break;
  321. }
  322. case Communication_message::Message_type::eTerminal_status_msg:
  323. {
  324. message::Terminal_status_msg t_Terminal_status_msg;
  325. //针对消息类型, 对消息进行二次解析
  326. if (t_Terminal_status_msg.ParseFromString(p_msg->get_message_buf()))
  327. {
  328. //LOG(INFO) << " t_Terminal_status_msg = "<< t_Terminal_status_msg.DebugString() << " -- " << this;
  329. }
  330. break;
  331. }
  332. case Communication_message::Message_type::eNotify_status_msg:
  333. {
  334. message::Notify_status_msg t_Notify_status_msg;
  335. //针对消息类型, 对消息进行二次解析
  336. if (t_Notify_status_msg.ParseFromString(p_msg->get_message_buf()))
  337. {
  338. LOG(INFO) << " t_Notify_status_msg = "<< t_Notify_status_msg.DebugString() << " -- " << this;
  339. }
  340. break;
  341. }
  342. default :
  343. ;
  344. break;
  345. }
  346. //无效的消息,
  347. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  348. " INVALID_MESSAGE error ");
  349. }
  350. //检查执行者的状态, 判断能否处理这条消息,
  351. Error_manager System_executor::check_executer(Communication_message* p_msg)
  352. {
  353. if ( p_msg == NULL )
  354. {
  355. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  356. " POINTER IS NULL ");
  357. }
  358. //检查执行线程池
  359. Error_manager t_executor_result = System_executor::get_instance_references().check_status();
  360. if (t_executor_result.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  361. {
  362. std::cout << "executer_is_busy , " << std::endl;
  363. //返回繁忙之后, 通信模块1秒后再次调用check
  364. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  365. }
  366. else if (t_executor_result.get_error_level() > NEGLIGIBLE_ERROR)
  367. {
  368. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_ERROR, Error_level::MINOR_ERROR,
  369. "System_executor::get_instance_references().check_status(); fun error ");
  370. }
  371. return Error_code::SUCCESS;
  372. }
  373. //处理消息的执行函数
  374. Error_manager System_executor::execute_msg(Communication_message* p_msg)
  375. {
  376. if ( p_msg == NULL )
  377. {
  378. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  379. " POINTER IS NULL ");
  380. }
  381. return Error_code::SUCCESS;
  382. }
  383. //检查状态
  384. Error_manager System_executor::check_status()
  385. {
  386. if ( m_system_executor_status == SYSTEM_EXECUTOR_READY )
  387. {
  388. if ( m_thread_pool.thread_is_full_load() == false )
  389. {
  390. return Error_code::SUCCESS;
  391. }
  392. else
  393. {
  394. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  395. " System_executor::check_status error ");
  396. }
  397. }
  398. else
  399. {
  400. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_ERROR, Error_level::MINOR_ERROR,
  401. " System_executor::check_status error ");
  402. }
  403. }
  404. //定时发送状态信息
  405. Error_manager System_executor::encapsulate_send_status()
  406. {
  407. return Error_code::SUCCESS;
  408. }
  409. //定时发送 调度管理的状态
  410. Error_manager System_executor::encapsulate_send_dispatch_manager_status()
  411. {
  412. return Error_code::SUCCESS;
  413. }
  414. //判断是否为待机,如果已经准备好,则可以执行任务。
  415. bool System_executor::is_ready()
  416. {
  417. if ( m_system_executor_status == SYSTEM_EXECUTOR_READY && m_thread_pool.thread_is_full_load() == false )
  418. {
  419. return true;
  420. }
  421. else
  422. {
  423. return false;
  424. }
  425. }
  426. System_executor::System_executor_status System_executor::get_system_executor_status()
  427. {
  428. return m_system_executor_status;
  429. }