Parkspace_communicator.cpp 22 KB

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