123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- //
- // Created by zx on 2020/7/9.
- //
- #include "parkspace_excutor.h"
- #include "uniq_key.h"
- Parkspace_excutor::~Parkspace_excutor(){}
- /*
- * 请求分配车位
- */
- Error_manager Parkspace_excutor::alloc_request(message::Parkspace_allocation_request_msg& request,
- message::Parkspace_allocation_response_msg& result,Thread_condition& cancel_condition)
- {
- /*
- * 检查request合法性,以及模块状态
- */
- if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
- return Error_manager(PARKSPACE_ALLOC_REQUEST_INVALID,MINOR_ERROR,"车位分配请求信息错误!!!");
- request.set_command_key(create_key());
- if(m_alloc_table.find(request.command_key())==true)
- return Error_manager(PARKSPACE_ALLOC_REQUEST_REPEATED,MINOR_ERROR,"车位分配请求已经存在,重复!!!");
- //设置超时,若没有设置,默认1000
- int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():10000;
- //向测量节点发送测量请求,并记录请求
- Error_manager code;
- Communication_message message;
- message::Base_info base_msg;
- base_msg.set_msg_type(message::eParkspace_allocation_request_msg);
- base_msg.set_sender(message::eMain);
- base_msg.set_receiver(message::eParkspace);
- base_msg.set_timeout_ms(timeout);
- message.reset(base_msg,request.SerializeAsString());
- m_alloc_table[request.command_key()]=message::Parkspace_allocation_response_msg();
- //发送请求
- code= Message_communicator::get_instance_pointer()->send_msg(&message);
- if(code!=SUCCESS)
- {
- m_alloc_table.erase(request.command_key());
- return code;
- }
- //循环查询请求是否被处理
- auto start_time=std::chrono::system_clock::now();
- double time=0;
- do{
- //查询到记录
- message::Parkspace_allocation_response_msg response;
- ///查询是否存在,并且删除该记录,
- if(m_alloc_table.find(request.command_key(),response))
- {
- //判断是否接收到回应,若回应信息被赋值则证明有回应
- if (response.has_base_info() && response.has_command_key())
- {
- message::Base_info response_base = response.base_info();
- //检查类型是否匹配
- if (response_base.msg_type() != message::eParkspace_allocation_response_msg) {
- return Error_manager(PARKSPACE_ALLOC_RESPONSE_TYPE_ERROR, MINOR_ERROR,
- "parkspace alloc response msg type error");
- }
- //检查基本信息是否匹配
- if (response_base.sender() != message::eParkspace ||
- response_base.receiver() != message::eMain ||
- response.command_key() != request.command_key()) {
- return Error_manager(PARKSPACE_ALLOC_RESPONSE_INFO_ERROR, MINOR_ERROR,
- "parkspace alloc response msg info error");
- }
- result = response;
- m_alloc_table.erase(request.command_key());
- return SUCCESS;
- }
- }
- else
- {
- //未查询到记录,任务已经被提前取消,记录被删除
- return Error_manager(TASK_CANCEL,MINOR_ERROR,"分配车位请求提前取消!!!");
- }
- auto end_time=std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
- time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
- if(time>double(timeout))
- {
- m_alloc_table.erase(request.command_key());
- return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace alloc request timeout");
- }
- }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(50))==false);
- //超时,删除记录,返回错误
- m_alloc_table.erase(request.command_key());
- return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace alloc request timeout");
- }
- /*
- * 查询车辆所在位置请求
- */
- Error_manager Parkspace_excutor::search_request(message::Parkspace_search_request_msg& request,
- message::Parkspace_search_response_msg& result,Thread_condition& cancel_condition)
- {
- /*
- * 检查request合法性,以及模块状态
- */
- if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
- return Error_manager(PARKSPACE_SEARCH_REQUEST_INVALID,MINOR_ERROR,"parkspace search request invalid");
- request.set_command_key(create_key());
- if(m_search_table.find(request.command_key())==true)
- return Error_manager(PARKSPACE_SEARCH_REQUEST_REPEATED,MAJOR_ERROR," parkspace search request repeated");
- //设置超时,若没有设置,默认1000
- int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():1000;
- //向测量节点发送测量请求,并记录请求
- Error_manager code;
- Communication_message message;
- message::Base_info base_msg;
- base_msg.set_msg_type(message::eParkspace_search_request_msg);
- base_msg.set_sender(message::eMain);
- base_msg.set_receiver(message::eParkspace);
- base_msg.set_timeout_ms(timeout);
- message.reset(base_msg,request.SerializeAsString());
- m_search_table[request.command_key()]=message::Parkspace_search_response_msg();
- //发送请求
- code= Message_communicator::get_instance_pointer()->send_msg(&message);
- if(code!=SUCCESS)
- {
- m_search_table.erase(request.command_key());
- return code;
- }
- //循环查询请求是否被处理
- auto start_time=std::chrono::system_clock::now();
- double time=0;
- do{
- //查询到记录
- message::Parkspace_search_response_msg response;
- ///查询是否存在,并且删除该记录,
- if(m_search_table.find(request.command_key(),response))
- {
- //判断是否接收到回应,若回应信息被赋值则证明有回应
- if (response.has_base_info() && response.has_command_key())
- {
- message::Base_info response_base = response.base_info();
- //检查类型是否匹配
- if (response_base.msg_type() != message::eParkspace_search_response_msg) {
- return Error_manager(PARKSPACE_SEARCH_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
- "parkspace search response msg type error");
- }
- //检查基本信息是否匹配
- if (response_base.sender() != message::eParkspace ||
- response_base.receiver() != message::eMain ||
- response.command_key() != request.command_key()) {
- return Error_manager(PARKSPACE_SEARCH_RESPONSE_INFO_ERROR, MAJOR_ERROR,
- "parkspace search response msg info error");
- }
- result = response;
- m_search_table.erase(request.command_key());
- if(response.has_error_manager())
- {
- if(response.error_manager().error_code()==0)
- return SUCCESS;
- }
- return Error_manager(FAILED,MINOR_ERROR,"车位查询返回错误码");
- }
- }
- else
- {
- //未查询到记录,任务已经被提前取消,记录被删除
- return Error_manager(PARKSPACE_SEARCH_REQUEST_CANCELED,MINOR_ERROR,"parkspace search request canceled");
- }
- auto end_time=std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
- time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
- if(time>double(timeout))
- {
- m_search_table.erase(request.command_key());
- return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace search request timeout");
- }
- }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
- //超时,删除记录,返回错误
- m_search_table.erase(request.command_key());
- return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace search request canceled");
- }
- /*
- * 释放车位请求(停车失败或者取车完成时调用)
- */
- Error_manager Parkspace_excutor::release_request(message::Parkspace_release_request_msg& request,
- message::Parkspace_release_response_msg& result,Thread_condition& cancel_condition)
- {
- /*
- * 检查request合法性,以及模块状态
- */
- if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
- return Error_manager(PARKSPACE_RELEASE_REQUEST_INVALID,MINOR_ERROR,"parkspace release request invalid");
- request.set_command_key(create_key());
- if(m_release_table.find(request.command_key())==true)
- return Error_manager(PARKSPACE_RELEASE_REQUEST_REPEATED,MAJOR_ERROR," parkspace release request repeated");
- //设置超时,若没有设置,默认1000
- int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():3000;
- //向测量节点发送测量请求,并记录请求
- Error_manager code;
- Communication_message message;
- message::Base_info base_msg;
- base_msg.set_msg_type(message::eParkspace_release_request_msg);
- base_msg.set_sender(message::eMain);
- base_msg.set_receiver(message::eParkspace);
- base_msg.set_timeout_ms(timeout);
- message.reset(base_msg,request.SerializeAsString());
- m_release_table[request.command_key()]=message::Parkspace_release_response_msg();
- //发送请求
- code= Message_communicator::get_instance_pointer()->send_msg(&message);
- if(code!=SUCCESS)
- {
- m_release_table.erase(request.command_key());
- return code;
- }
- //循环查询请求是否被处理
- auto start_time=std::chrono::system_clock::now();
- double time=0;
- do{
- //查询到记录
- message::Parkspace_release_response_msg response;
- ///查询是否存在,并且删除该记录,
- if(m_release_table.find(request.command_key(),response))
- {
- //判断是否接收到回应,若回应信息被赋值则证明有回应
- if (response.has_base_info() && response.has_command_key())
- {
- message::Base_info response_base = response.base_info();
- //检查类型是否匹配
- if (response_base.msg_type() != message::eParkspace_release_response_msg) {
- return Error_manager(PARKSPACE_RELEASE_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
- "parkspace release response msg type error");
- }
- //检查基本信息是否匹配
- if (response_base.sender() != message::eParkspace ||
- response_base.receiver() != message::eMain ||
- response.command_key() != request.command_key()) {
- return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
- "parkspace release response msg info error");
- }
- result = response;
- m_release_table.erase(request.command_key());
- return SUCCESS;
- }
- }
- else
- {
- //未查询到记录,任务已经被提前取消,记录被删除
- return Error_manager(PARKSPACE_RELEASE_REQUEST_CANCELED,MINOR_ERROR,"parkspace release request canceled");
- }
- auto end_time=std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
- time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
- if(time>double(timeout))
- {
- m_release_table.erase(request.command_key());
- return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace release request timeout");
- }
- }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
- //超时,删除记录,返回错误
- m_release_table.erase(request.command_key());
- return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace release request canceled");
- }
- /*
- * 确认占用车位消息
- */
- Error_manager Parkspace_excutor::confirm_request(message::Parkspace_confirm_alloc_request_msg& request,
- message::Parkspace_confirm_alloc_response_msg& result,Thread_condition& cancel_condition)
- {
- /*
- * 检查request合法性,以及模块状态
- */
- if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eParkspace)
- return Error_manager(FAILED,MINOR_ERROR,"parkspace confirm request invalid");
- request.set_command_key(create_key());
- if(m_confirm_table.find(request.command_key())==true)
- {
- return Error_manager(FAILED,MAJOR_ERROR," parkspace confirm request repeated");
- }
- //设置超时,若没有设置,默认1000
- int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():1000;
- //向测量节点发送测量请求,并记录请求
- Error_manager code;
- Communication_message message;
- message.reset(request.base_info(),request.SerializeAsString());
- m_confirm_table[request.command_key()]=message::Parkspace_confirm_alloc_response_msg();
- //发送请求
- code= Message_communicator::get_instance_pointer()->send_msg(&message);
- if(code!=SUCCESS)
- {
- m_confirm_table.erase(request.command_key());
- return code;
- }
- //循环查询请求是否被处理
- auto start_time=std::chrono::system_clock::now();
- double time=0;
- do{
- //查询到记录
- message::Parkspace_confirm_alloc_response_msg response;
- ///查询是否存在,并且删除该记录,
- if(m_confirm_table.find(request.command_key(),response))
- {
- //判断是否接收到回应,若回应信息被赋值则证明有回应
- if (response.has_base_info() && response.has_command_key())
- {
- message::Base_info response_base = response.base_info();
- //检查类型是否匹配
- if (response_base.msg_type() != message::eParkspace_confirm_alloc_response_msg) {
- return Error_manager(PARKSPACE_RELEASE_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
- "parkspace confirm response msg type error");
- }
- //检查基本信息是否匹配
- if (response_base.sender() != message::eParkspace ||
- response_base.receiver() != message::eMain ||
- response.command_key() != request.command_key()) {
- return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
- "parkspace confirm response msg info error");
- }
- result = response;
- m_confirm_table.erase(request.command_key());
- return SUCCESS;
- }
- }
- else
- {
- //未查询到记录,任务已经被提前取消,记录被删除
- return Error_manager(FAILED,MINOR_ERROR,"parkspace confirm request canceled");
- }
- auto end_time=std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
- time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
- std::this_thread::yield();
- if(time>double(timeout))
- {
- m_confirm_table.erase(request.command_key());
- return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace confirm request timeout");
- }
- }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
- //超时,删除记录,返回错误
- m_confirm_table.erase(request.command_key());
- return Error_manager(TASK_CANCEL,MINOR_ERROR,"parkspace confirm request canceled");
- }
- Error_manager Parkspace_excutor::check_statu()
- {
- //return SUCCESS;
- std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
- auto durantion=time_now-m_parkspace_statu_time;
- if(m_parkspace_status_msg.has_base_info()== false
- || durantion>std::chrono::seconds(5))
- {
- return Error_manager(ERROR,MINOR_ERROR,"车位管理节点通讯断开");
- }
- return SUCCESS;
- }
- Parkspace_excutor::Parkspace_excutor(){}
- Error_manager Parkspace_excutor::consume_msg(Communication_message* p_msg)
- {
- if(p_msg== nullptr)
- return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"parkspace response msg pointer is null");
- //车位response消息
- switch (p_msg->get_message_type())
- {
- ///测量结果反馈消息
- case Communication_message::eParkspace_allocation_response_msg:
- {
- message::Parkspace_allocation_response_msg response;
- response.ParseFromString(p_msg->get_message_buf());
- ///查询请求表是否存在,并且更新
- if(m_alloc_table.find_update(response.command_key(),response)==false)
- {
- return Error_manager(PARKSPACE_ALLOCMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace alloc response without request");
- }
- break;
- }
- case Communication_message::eParkspace_search_response_msg:
- {
- message::Parkspace_search_response_msg response;
- if(false==response.ParseFromString(p_msg->get_message_buf()))
- {
- return Error_manager(ERROR,CRITICAL_ERROR,"parkspace search response 解析失败");
- }
- ///查询请求表是否存在,并且更新
- if(m_search_table.find_update(response.command_key(),response)==false)
- {
- return Error_manager(PARKSPACE_SEARCHMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace search response without request");
- }
- break;
- }
- case Communication_message::eParkspace_release_response_msg:
- {
- message::Parkspace_release_response_msg response;
- response.ParseFromString(p_msg->get_message_buf());
- ///查询请求表是否存在,并且更新
- if(m_release_table.find_update(response.command_key(),response)==false)
- {
- return Error_manager(PARKSPACE_RELEASEMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace release response without request");
- }
- break;
- }
- case Communication_message::eParkspace_confirm_alloc_response_msg:
- {
- message::Parkspace_confirm_alloc_response_msg response;
- response.ParseFromString(p_msg->get_message_buf());
- ///查询请求表是否存在,并且更新
- if(m_confirm_table.find_update(response.command_key(),response)==true)
- {
- return SUCCESS;
- }
- break;
- }
- case Communication_message::eParkspace_allocation_status_msg:
- {
- if(m_parkspace_status_msg.ParseFromString(p_msg->get_message_buf())==false)
- {
- return Error_manager(ERROR,CRITICAL_ERROR,"车位管理模块状态消息解析失败");
- }
- m_parkspace_statu_time=std::chrono::system_clock::now();
- break;
- }
- }
- return SUCCESS;
- }
|