void Logic::processCancelResponse(const CancelResponse &cancelResponse) { auto it = checkResponseValid(cancelResponse); if (!it->second.cancelled()) { LOGC("Critical error. CancelRequest not sent: sequenceNumber = [%" PRIu16 "]", cancelResponse.sequenceNumber()); throw UnexpectedErrorException("Unexpected response from cynara service"); } releaseRequest(it); }
void Logic::processCheckResponse(const CheckResponse &checkResponse) { LOGD("checkResponse: policyType = [%" PRIu16 "], metadata = <%s>", checkResponse.m_resultRef.policyType(), checkResponse.m_resultRef.metadata().c_str()); auto it = checkResponseValid(checkResponse); int result = m_cache.update(it->second.session(), it->second.key(), checkResponse.m_resultRef); CheckData checkData(std::move(it->second)); releaseRequest(it); if (!checkData.cancelled()) { bool onAnswerCancel = m_inAnswerCancelResponseCallback; m_inAnswerCancelResponseCallback = true; checkData.callback().onAnswer( static_cast<cynara_check_id>(checkResponse.sequenceNumber()), result); m_inAnswerCancelResponseCallback = onAnswerCancel; } }
void Logic::processSimpleCheckResponse(const SimpleCheckResponse &response) { LOGD("simpleCheckResponse"); LOGD("checkResponse: policyType = [%" PRIu16 "], metadata = <%s>", response.getResult().policyType(), response.getResult().metadata().c_str()); auto it = checkResponseValid(response); int result = response.getReturnValue(); if (result == CYNARA_API_SUCCESS) result = m_cache.update(it->second.session(), it->second.key(), response.getResult()); CheckData checkData(std::move(it->second)); releaseRequest(it); if (!checkData.cancelled()) { bool onAnswerCancel = m_inAnswerCancelResponseCallback; m_inAnswerCancelResponseCallback = true; checkData.callback().onAnswer( static_cast<cynara_check_id>(response.sequenceNumber()), result); m_inAnswerCancelResponseCallback = onAnswerCancel; } }
void obtainOptimalStateValue(multimap<double,cEvent>& _event_multimap,vector<cServer>& _server_vec) { multimap<double,cEvent>::iterator iter_event_multimap; counting = -1; //store the number of accepted requests corresponding to each type of requests map<requesttype,unsigned int> hosted_requests_type_num_map; map<requesttype,unsigned int>::iterator iter_find_hosted_request_num_map; map<ID,cRequest*> hosted_request_map; map<ID,cRequest*>::iterator iter_find_request_map; for (iter_event_multimap = _event_multimap.begin();iter_event_multimap != _event_multimap.end();iter_event_multimap++) { if (iter_event_multimap->second.getEventType() == DEPARTURE) { //set current time current_time = iter_event_multimap->first; //delete the request from the hosting list iter_find_request_map = hosted_request_map.find((iter_event_multimap->second.getRequest())->getID()); if (iter_find_request_map == hosted_request_map.end()) { cout<<"Error!!!Can not locate the request that should exist in the hosting list!"<<endl; exit(0); } hosted_request_map.erase(iter_find_request_map); //minus 1 from the entry corresponding the type of currently departing requests in the counting list of hosting requests iter_find_hosted_request_num_map = hosted_requests_type_num_map.find((iter_event_multimap->second.getRequest())->getRequestType()); if (iter_find_hosted_request_num_map == hosted_requests_type_num_map.end()) { cout<<"Error!!!Can not locate the request that should exist in the counting list of types of the hosting requests!"<<endl; exit(0); } else (iter_find_hosted_request_num_map->second)--; //release the resources allocated to the request releaseRequest(iter_event_multimap->second.getRequest()); //update the value of current system state after any request departure //system_state.first = NONE; //updateStateValueRequDepar(_server_vec,hosted_request_map,hosted_requests_type_num_map); //system_state.second = calculateRequestDepartureStateIndicator(_server_vec,iter_event_multimap->second.getRequest()); } else { //set current time current_time = iter_event_multimap->first; //a request is arriving counting++; //update the system state system_state.first = (iter_event_multimap->second.getRequest())->getRequestType(); //we should determine which action should be take base on the purpose of maximizing the expected profits //if(obtainOptimalAction(&(iter_event_multimap->second),_server_vec,hosted_requests_type_num_map,hosted_request_map)) if(obtainOptimalAction(&(iter_event_multimap->second),_server_vec,hosted_requests_type_num_map,hosted_request_map)) { //if the arriving request is accepted, we should allocate physical resources for it and insert a departure event into the event list (iter_event_multimap->second.getRequest())->setAccepted(true); allocateRequest(iter_event_multimap->second.getRequest()); insertDepartureEvent(iter_event_multimap->second.getRequest(),_event_multimap); accepted_requests_num++; hosted_request_map.insert(make_pair((iter_event_multimap->second.getRequest())->getID(),iter_event_multimap->second.getRequest())); iter_find_hosted_request_num_map = hosted_requests_type_num_map.find((iter_event_multimap->second.getRequest())->getRequestType()); if (iter_find_hosted_request_num_map == hosted_requests_type_num_map.end()) { //there is no request in the system which has the same type as currently arriving request hosted_requests_type_num_map.insert(make_pair((iter_event_multimap->second.getRequest())->getRequestType(),1)); } else (iter_find_hosted_request_num_map->second)++; } else { //do nothing //the arriving request is rejected due to 1) having not enough residual resources, or 2) maximizing profits policy (iter_event_multimap->second.getRequest())->setAccepted(false); //update the value of current system state after any request departure system_state.first = NONE; updateStateValueRequDepar(_server_vec,hosted_request_map,hosted_requests_type_num_map); } } } return ; }