parkspace_excutor.cpp 19 KB

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