parkspace_excutor.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //
  2. // Created by zx on 2020/7/9.
  3. //
  4. #include "parkspace_excutor.h"
  5. Parkspace_excutor::~Parkspace_excutor(){}
  6. /*
  7. * 请求分配车位
  8. */
  9. Error_manager Parkspace_excutor::alloc_request(message::Parkspace_allocation_request_msg& request,
  10. message::Parkspace_allocation_response_msg& result,Thread_condition& cancel_condition)
  11. {
  12. /*
  13. * 检查request合法性,以及模块状态
  14. */
  15. if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
  16. return Error_manager(PARKSPACE_ALLOC_REQUEST_INVALID,MINOR_ERROR,"车位分配请求信息错误!!!");
  17. if(m_alloc_table.find(request.command_key())==true)
  18. return Error_manager(PARKSPACE_ALLOC_REQUEST_REPEATED,MINOR_ERROR,"车位分配请求已经存在,重复!!!");
  19. //设置超时,若没有设置,默认1000
  20. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():10000;
  21. //向测量节点发送测量请求,并记录请求
  22. Error_manager code;
  23. Communication_message message;
  24. message::Base_info base_msg;
  25. base_msg.set_msg_type(message::eParkspace_allocation_request_msg);
  26. base_msg.set_sender(message::eMain);
  27. base_msg.set_receiver(message::eParkspace);
  28. base_msg.set_timeout_ms(timeout);
  29. message.reset(base_msg,request.SerializeAsString());
  30. m_alloc_table[request.command_key()]=message::Parkspace_allocation_response_msg();
  31. //发送请求
  32. code= Message_communicator::get_instance_pointer()->send_msg(&message);
  33. if(code!=SUCCESS)
  34. {
  35. m_alloc_table.erase(request.command_key());
  36. return code;
  37. }
  38. //循环查询请求是否被处理
  39. auto start_time=std::chrono::system_clock::now();
  40. double time=0;
  41. do{
  42. //查询到记录
  43. message::Parkspace_allocation_response_msg response;
  44. ///查询是否存在,并且删除该记录,
  45. if(m_alloc_table.find(request.command_key(),response))
  46. {
  47. //判断是否接收到回应,若回应信息被赋值则证明有回应
  48. if (response.has_base_info() && response.has_command_key())
  49. {
  50. message::Base_info response_base = response.base_info();
  51. //检查类型是否匹配
  52. if (response_base.msg_type() != message::eParkspace_allocation_response_msg) {
  53. return Error_manager(PARKSPACE_ALLOC_RESPONSE_TYPE_ERROR, MINOR_ERROR,
  54. "parkspace alloc response msg type error");
  55. }
  56. //检查基本信息是否匹配
  57. if (response_base.sender() != message::eParkspace ||
  58. response_base.receiver() != message::eMain ||
  59. response.command_key() != request.command_key()) {
  60. return Error_manager(PARKSPACE_ALLOC_RESPONSE_INFO_ERROR, MINOR_ERROR,
  61. "parkspace alloc response msg info error");
  62. }
  63. result = response;
  64. m_alloc_table.erase(request.command_key());
  65. return SUCCESS;
  66. }
  67. }
  68. else
  69. {
  70. //未查询到记录,任务已经被提前取消,记录被删除
  71. return Error_manager(TASK_CANCEL,MINOR_ERROR,"分配车位请求提前取消!!!");
  72. }
  73. auto end_time=std::chrono::system_clock::now();
  74. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  75. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  76. if(time>double(timeout))
  77. {
  78. m_alloc_table.erase(request.command_key());
  79. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace alloc request timeout");
  80. }
  81. }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(50))==false);
  82. //超时,删除记录,返回错误
  83. m_alloc_table.erase(request.command_key());
  84. return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace alloc request timeout");
  85. }
  86. /*
  87. * 查询车辆所在位置请求
  88. */
  89. Error_manager Parkspace_excutor::search_request(message::Parkspace_search_request_msg& request,
  90. message::Parkspace_search_response_msg& result,Thread_condition& cancel_condition)
  91. {
  92. /*
  93. * 检查request合法性,以及模块状态
  94. */
  95. if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
  96. return Error_manager(PARKSPACE_SEARCH_REQUEST_INVALID,MINOR_ERROR,"parkspace search request invalid");
  97. if(m_search_table.find(request.command_key())==true)
  98. return Error_manager(PARKSPACE_SEARCH_REQUEST_REPEATED,MAJOR_ERROR," parkspace search request repeated");
  99. //设置超时,若没有设置,默认1000
  100. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():1000;
  101. //向测量节点发送测量请求,并记录请求
  102. Error_manager code;
  103. Communication_message message;
  104. message::Base_info base_msg;
  105. base_msg.set_msg_type(message::eParkspace_search_request_msg);
  106. base_msg.set_sender(message::eMain);
  107. base_msg.set_receiver(message::eParkspace);
  108. base_msg.set_timeout_ms(timeout);
  109. message.reset(base_msg,request.SerializeAsString());
  110. m_search_table[request.command_key()]=message::Parkspace_search_response_msg();
  111. //发送请求
  112. code= Message_communicator::get_instance_pointer()->send_msg(&message);
  113. if(code!=SUCCESS)
  114. {
  115. m_search_table.erase(request.command_key());
  116. return code;
  117. }
  118. //循环查询请求是否被处理
  119. auto start_time=std::chrono::system_clock::now();
  120. double time=0;
  121. do{
  122. //查询到记录
  123. message::Parkspace_search_response_msg response;
  124. ///查询是否存在,并且删除该记录,
  125. if(m_search_table.find(request.command_key(),response))
  126. {
  127. //判断是否接收到回应,若回应信息被赋值则证明有回应
  128. if (response.has_base_info() && response.has_command_key())
  129. {
  130. message::Base_info response_base = response.base_info();
  131. //检查类型是否匹配
  132. if (response_base.msg_type() != message::eParkspace_search_response_msg) {
  133. return Error_manager(PARKSPACE_SEARCH_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
  134. "parkspace search response msg type error");
  135. }
  136. //检查基本信息是否匹配
  137. if (response_base.sender() != message::eParkspace ||
  138. response_base.receiver() != message::eMain ||
  139. response.command_key() != request.command_key()) {
  140. return Error_manager(PARKSPACE_SEARCH_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  141. "parkspace search response msg info error");
  142. }
  143. result = response;
  144. m_search_table.erase(request.command_key());
  145. if(response.has_error_manager())
  146. {
  147. if(response.error_manager().error_code()==0)
  148. return SUCCESS;
  149. }
  150. return Error_manager(FAILED,MINOR_ERROR,"车位查询返回错误码");
  151. }
  152. }
  153. else
  154. {
  155. //未查询到记录,任务已经被提前取消,记录被删除
  156. return Error_manager(PARKSPACE_SEARCH_REQUEST_CANCELED,MINOR_ERROR,"parkspace search request canceled");
  157. }
  158. auto end_time=std::chrono::system_clock::now();
  159. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  160. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  161. if(time>double(timeout))
  162. {
  163. m_search_table.erase(request.command_key());
  164. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace search request timeout");
  165. }
  166. }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
  167. //超时,删除记录,返回错误
  168. m_search_table.erase(request.command_key());
  169. return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace search request canceled");
  170. }
  171. /*
  172. * 释放车位请求(停车失败或者取车完成时调用)
  173. */
  174. Error_manager Parkspace_excutor::release_request(message::Parkspace_release_request_msg& request,
  175. message::Parkspace_release_response_msg& result,Thread_condition& cancel_condition)
  176. {
  177. /*
  178. * 检查request合法性,以及模块状态
  179. */
  180. if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
  181. return Error_manager(PARKSPACE_RELEASE_REQUEST_INVALID,MINOR_ERROR,"parkspace release request invalid");
  182. if(m_release_table.find(request.command_key())==true)
  183. return Error_manager(PARKSPACE_RELEASE_REQUEST_REPEATED,MAJOR_ERROR," parkspace release request repeated");
  184. //设置超时,若没有设置,默认1000
  185. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():3000;
  186. //向测量节点发送测量请求,并记录请求
  187. Error_manager code;
  188. Communication_message message;
  189. message::Base_info base_msg;
  190. base_msg.set_msg_type(message::eParkspace_release_request_msg);
  191. base_msg.set_sender(message::eMain);
  192. base_msg.set_receiver(message::eParkspace);
  193. base_msg.set_timeout_ms(timeout);
  194. message.reset(base_msg,request.SerializeAsString());
  195. m_release_table[request.command_key()]=message::Parkspace_release_response_msg();
  196. //发送请求
  197. code= Message_communicator::get_instance_pointer()->send_msg(&message);
  198. if(code!=SUCCESS)
  199. {
  200. m_release_table.erase(request.command_key());
  201. return code;
  202. }
  203. //循环查询请求是否被处理
  204. auto start_time=std::chrono::system_clock::now();
  205. double time=0;
  206. do{
  207. //查询到记录
  208. message::Parkspace_release_response_msg response;
  209. ///查询是否存在,并且删除该记录,
  210. if(m_release_table.find(request.command_key(),response))
  211. {
  212. //判断是否接收到回应,若回应信息被赋值则证明有回应
  213. if (response.has_base_info() && response.has_command_key())
  214. {
  215. message::Base_info response_base = response.base_info();
  216. //检查类型是否匹配
  217. if (response_base.msg_type() != message::eParkspace_release_response_msg) {
  218. return Error_manager(PARKSPACE_RELEASE_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
  219. "parkspace release response msg type error");
  220. }
  221. //检查基本信息是否匹配
  222. if (response_base.sender() != message::eParkspace ||
  223. response_base.receiver() != message::eMain ||
  224. response.command_key() != request.command_key()) {
  225. return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  226. "parkspace release response msg info error");
  227. }
  228. result = response;
  229. m_release_table.erase(request.command_key());
  230. return SUCCESS;
  231. }
  232. }
  233. else
  234. {
  235. //未查询到记录,任务已经被提前取消,记录被删除
  236. return Error_manager(PARKSPACE_RELEASE_REQUEST_CANCELED,MINOR_ERROR,"parkspace release request canceled");
  237. }
  238. auto end_time=std::chrono::system_clock::now();
  239. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  240. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  241. if(time>double(timeout))
  242. {
  243. m_release_table.erase(request.command_key());
  244. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace release request timeout");
  245. }
  246. }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
  247. //超时,删除记录,返回错误
  248. m_release_table.erase(request.command_key());
  249. return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace release request canceled");
  250. }
  251. /*
  252. * 确认占用车位消息
  253. */
  254. Error_manager Parkspace_excutor::confirm_request(message::Parkspace_confirm_alloc_request_msg& request,
  255. message::Parkspace_confirm_alloc_response_msg& result,Thread_condition& cancel_condition)
  256. {
  257. /*
  258. * 检查request合法性,以及模块状态
  259. */
  260. if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
  261. return Error_manager(FAILED,MINOR_ERROR,"parkspace confirm request invalid");
  262. if(m_confirm_table.find(request.command_key())==true)
  263. {
  264. return Error_manager(FAILED,MAJOR_ERROR," parkspace confirm request repeated");
  265. }
  266. //设置超时,若没有设置,默认1000
  267. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():1000;
  268. //向测量节点发送测量请求,并记录请求
  269. Error_manager code;
  270. Communication_message message;
  271. message.reset(request.base_info(),request.SerializeAsString());
  272. m_confirm_table[request.command_key()]=message::Parkspace_confirm_alloc_response_msg();
  273. //发送请求
  274. code= Message_communicator::get_instance_pointer()->send_msg(&message);
  275. if(code!=SUCCESS)
  276. {
  277. m_confirm_table.erase(request.command_key());
  278. return code;
  279. }
  280. //循环查询请求是否被处理
  281. auto start_time=std::chrono::system_clock::now();
  282. double time=0;
  283. do{
  284. //查询到记录
  285. message::Parkspace_confirm_alloc_response_msg response;
  286. ///查询是否存在,并且删除该记录,
  287. if(m_confirm_table.find(request.command_key(),response))
  288. {
  289. //判断是否接收到回应,若回应信息被赋值则证明有回应
  290. if (response.has_base_info() && response.has_command_key())
  291. {
  292. message::Base_info response_base = response.base_info();
  293. //检查类型是否匹配
  294. if (response_base.msg_type() != message::eParkspace_confirm_alloc_response_msg) {
  295. return Error_manager(PARKSPACE_RELEASE_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
  296. "parkspace confirm response msg type error");
  297. }
  298. //检查基本信息是否匹配
  299. if (response_base.sender() != message::eParkspace ||
  300. response_base.receiver() != message::eMain ||
  301. response.command_key() != request.command_key()) {
  302. return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  303. "parkspace confirm response msg info error");
  304. }
  305. result = response;
  306. m_confirm_table.erase(request.command_key());
  307. return SUCCESS;
  308. }
  309. }
  310. else
  311. {
  312. //未查询到记录,任务已经被提前取消,记录被删除
  313. return Error_manager(FAILED,MINOR_ERROR,"parkspace confirm request canceled");
  314. }
  315. auto end_time=std::chrono::system_clock::now();
  316. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  317. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  318. std::this_thread::yield();
  319. if(time>double(timeout))
  320. {
  321. m_confirm_table.erase(request.command_key());
  322. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace confirm request timeout");
  323. }
  324. }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
  325. //超时,删除记录,返回错误
  326. m_confirm_table.erase(request.command_key());
  327. return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace confirm request canceled");
  328. }
  329. Error_manager Parkspace_excutor::check_statu()
  330. {
  331. //return SUCCESS;
  332. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  333. auto durantion=time_now-m_parkspace_statu_time;
  334. if(m_parkspace_status_msg.has_base_info()== false
  335. || durantion>std::chrono::seconds(5))
  336. {
  337. return Error_manager(ERROR,MINOR_ERROR,"车位管理节点通讯断开");
  338. }
  339. return SUCCESS;
  340. }
  341. Parkspace_excutor::Parkspace_excutor(){}
  342. Error_manager Parkspace_excutor::consume_msg(Communication_message* p_msg)
  343. {
  344. if(p_msg== nullptr)
  345. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"parkspace response msg pointer is null");
  346. //车位response消息
  347. switch (p_msg->get_message_type())
  348. {
  349. ///测量结果反馈消息
  350. case Communication_message::eParkspace_allocation_response_msg:
  351. {
  352. message::Parkspace_allocation_response_msg response;
  353. response.ParseFromString(p_msg->get_message_buf());
  354. ///查询请求表是否存在,并且更新
  355. if(m_alloc_table.find_update(response.command_key(),response)==false)
  356. {
  357. return Error_manager(PARKSPACE_ALLOCMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace alloc response without request");
  358. }
  359. break;
  360. }
  361. case Communication_message::eParkspace_search_response_msg:
  362. {
  363. message::Parkspace_search_response_msg response;
  364. if(false==response.ParseFromString(p_msg->get_message_buf()))
  365. {
  366. return Error_manager(ERROR,CRITICAL_ERROR,"parkspace search response 解析失败");
  367. }
  368. ///查询请求表是否存在,并且更新
  369. if(m_search_table.find_update(response.command_key(),response)==false)
  370. {
  371. return Error_manager(PARKSPACE_SEARCHMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace search response without request");
  372. }
  373. break;
  374. }
  375. case Communication_message::eParkspace_release_response_msg:
  376. {
  377. message::Parkspace_release_response_msg response;
  378. response.ParseFromString(p_msg->get_message_buf());
  379. ///查询请求表是否存在,并且更新
  380. if(m_release_table.find_update(response.command_key(),response)==false)
  381. {
  382. return Error_manager(PARKSPACE_RELEASEMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace release response without request");
  383. }
  384. break;
  385. }
  386. case Communication_message::eParkspace_confirm_alloc_response_msg:
  387. {
  388. message::Parkspace_confirm_alloc_response_msg response;
  389. response.ParseFromString(p_msg->get_message_buf());
  390. ///查询请求表是否存在,并且更新
  391. if(m_confirm_table.find_update(response.command_key(),response)==true)
  392. {
  393. return SUCCESS;
  394. }
  395. break;
  396. }
  397. case Communication_message::eParkspace_allocation_status_msg:
  398. {
  399. if(m_parkspace_status_msg.ParseFromString(p_msg->get_message_buf())==false)
  400. {
  401. return Error_manager(ERROR,CRITICAL_ERROR,"车位管理模块状态消息解析失败");
  402. }
  403. m_parkspace_statu_time=std::chrono::system_clock::now();
  404. break;
  405. }
  406. }
  407. return SUCCESS;
  408. }