📄 stafrespoolservice.cpp
字号:
{ return STAFResultPtr(new STAFResult(kSTAFResPoolHasPendingRequests, poolName), STAFResultPtr::INIT); } // Now delete the resource pool file STAFFSPath poolFilePath; poolFilePath.setRoot(pData->fPoolDir); poolFilePath.setName(poolName); poolFilePath.setExtension(sPoolExt); try { poolFilePath.getEntry()->remove(); } catch (STAFException &e) { result = getExceptionString(e, "STAFResPoolSerice.cpp: Delete ").adoptImpl(); return STAFResultPtr(new STAFResult(kSTAFFileDeleteError, result), STAFResultPtr::INIT); } // Remove the resource pool the Pool Map pData->fPoolMap.erase(poolIterator); // If there are any pending requests for this pool, wakeup each pending // requester with no resource specified. if (poolPtr->requestList.size() > 0) { for (RequestList::iterator iter = poolPtr->requestList.begin(); iter != poolPtr->requestList.end(); ++iter) { (*iter)->retCode = kSTAFDoesNotExist; (*iter)->resultBuffer = poolName; (*iter)->wakeup->post(); } } // Return an Ok result return STAFResultPtr(new STAFResult(kSTAFOk, STAFString()), STAFResultPtr::INIT);}// Handles resource pool add entry requestsSTAFResultPtr handleAdd(STAFServiceRequestLevel30 *pInfo, ResPoolServiceData *pData){ STAFString result; STAFRC_t rc = kSTAFOk; // Verify the requester has at least trust level 4 VALIDATE_TRUST(4, pData->fShortName, "ADD", pData->fLocalMachineName); // Parse the result STAFCommandParseResultPtr parsedResult = pData->fAddParser->parse(pInfo->request); if (parsedResult->rc != kSTAFOk) { return STAFResultPtr(new STAFResult(kSTAFInvalidRequestString, parsedResult->errorBuffer), STAFResultPtr::INIT); } // Set the poolName variable (resolve the pool name) STAFResultPtr resultPtr = resolveOp(pInfo, pData, parsedResult, sPool); if (resultPtr->rc != 0) return resultPtr; STAFString poolName = resultPtr->result; // Get a write lock on the Pool Map for the duration of this block STAFRWSemWLock wLock(*pData->fPoolMapRWSem); // Make sure the resource pool is in the Pool Map and get a pointer to it PoolMap::iterator poolIterator; poolIterator = pData->fPoolMap.find(poolName.toUpperCase()); if (poolIterator == pData->fPoolMap.end()) { return STAFResultPtr(new STAFResult(kSTAFDoesNotExist, poolName), STAFResultPtr::INIT); } // Create a copy of the pool to update PoolDataPtr poolPtr = (*poolIterator).second; PoolData newPool = *(poolPtr); // Get each entry to be added and check if entry is already in the pool. // If there are any duplicate entries to be added, return an error and // do not update the resource pool. unsigned int numEntriesToAdd = parsedResult->optionTimes(sEntry); unsigned int i; for (i = 1; i <= numEntriesToAdd; i++) { STAFString thisEntry = parsedResult->optionValue(sEntry, i); for (unsigned int j = 0; j < newPool.resourceList.size(); j++) { if (newPool.resourceList[j].entry == thisEntry) { return STAFResultPtr(new STAFResult(kSTAFAlreadyExists, thisEntry), STAFResultPtr::INIT); } } // Add to the end of the new pool's resource list if not a duplicate ResourceData resourceData(thisEntry); newPool.resourceList.push_back(resourceData); newPool.numResources++; } // Delete the old pool file and write the new pool file STAFFSPath poolFilePath; poolFilePath.setRoot(pData->fPoolDir); poolFilePath.setName(poolName); poolFilePath.setExtension(sPoolExt); try { poolFilePath.getEntry()->remove(); } catch (STAFException &e) { result = getExceptionString(e, "STAFResPoolService.cpp: Add ").adoptImpl(); return STAFResultPtr(new STAFResult(kSTAFFileDeleteError, result), STAFResultPtr::INIT); } STAFString fileName = poolFilePath.asString(); if (writePoolFile(fileName, newPool) != kReadorWriteOk) { return STAFResultPtr(new STAFResult(kSTAFFileWriteError, fileName), STAFResultPtr::INIT); } // Update the pool in memory poolPtr->resourceList = newPool.resourceList; poolPtr->numResources = newPool.numResources; // For each resource entry added, if there are any pending requests, // tell the next requester the resource he can have, wake the requester // up, and remove the satisfied pending request. for (i = poolPtr->numResources - numEntriesToAdd; i < poolPtr->numResources; i++) { if (poolPtr->requestList.size() > 0) { // Get the first request from the pending request list RequestDataPtr requestDataPtr = poolPtr->requestList.front(); // Assign a resource to the request requestDataPtr->retCode = kSTAFOk; requestDataPtr->resultBuffer = poolPtr->resourceList[i].entry; // Update the resource entry's ownership information poolPtr->resourceList[i].owned = 1; poolPtr->usedResources++; poolPtr->resourceList[i].orgUUID = requestDataPtr->orgUUID; poolPtr->resourceList[i].orgMachine = requestDataPtr->orgMachine; poolPtr->resourceList[i].orgName = requestDataPtr->orgName; poolPtr->resourceList[i].orgHandle = requestDataPtr->orgHandle; poolPtr->resourceList[i].orgUser = requestDataPtr->orgUser; poolPtr->resourceList[i].orgEndpoint = requestDataPtr->orgEndpoint; poolPtr->resourceList[i].requestedTime = requestDataPtr->requestedTime; poolPtr->resourceList[i].acquiredTime = STAFTimestamp::now().asString(); // Wakeup the requester requestDataPtr->wakeup->post(); // Remove the request from the pending request list poolPtr->requestList.pop_front(); } else { // No more pending requests break; } } // Return an Ok result return STAFResultPtr(new STAFResult(kSTAFOk, result), STAFResultPtr::INIT);}// Handles resource pool remove entry requestsSTAFResultPtr handleRemove(STAFServiceRequestLevel30 *pInfo, ResPoolServiceData *pData){ STAFString result; STAFRC_t rc = kSTAFOk; // Verify the requester has at least trust level 4 VALIDATE_TRUST(4, pData->fShortName, "REMOVE", pData->fLocalMachineName); // Parse the result STAFCommandParseResultPtr parsedResult = pData->fRemoveParser->parse(pInfo->request); if (parsedResult->rc != kSTAFOk) { return STAFResultPtr(new STAFResult(kSTAFInvalidRequestString, parsedResult->errorBuffer), STAFResultPtr::INIT); } // Set the poolName variable (resolve the pool name) STAFResultPtr resultPtr = resolveOp(pInfo, pData, parsedResult, sPool); if (resultPtr->rc != 0) return resultPtr; STAFString poolName = resultPtr->result; // Get a write lock on the Pool Map for the duration of this block STAFRWSemWLock wLock(*pData->fPoolMapRWSem); // Make sure the specified resource pool exists in Pool Map PoolMap::iterator poolIterator; poolIterator = pData->fPoolMap.find(poolName.toUpperCase()); if (poolIterator == pData->fPoolMap.end()) { return STAFResultPtr(new STAFResult(kSTAFDoesNotExist, poolName), STAFResultPtr::INIT); } PoolDataPtr poolPtr = (*poolIterator).second; if (poolPtr->resourceList.size() == 0) { return STAFResultPtr(new STAFResult(kSTAFResPoolNoEntriesAvailable, poolName), STAFResultPtr::INIT); } unsigned int numEntriesToRemove = parsedResult->optionTimes(sEntry); // Create a copy of the pool to update PoolData newPool = *(poolPtr); // Get each of the entries to be removed; If an invalid entry is found, // return with an appropriate error message and do not remove any other // valid entries specified on the request. for (unsigned int i = 1; i <= numEntriesToRemove; i++) { STAFString thisEntry = parsedResult->optionValue(sEntry, i); int resid = -1; // Check if this entry is in the pool - set resid to its index for (unsigned int j = 0; j < newPool.resourceList.size() && resid == -1; j++) { if (thisEntry == newPool.resourceList[j].entry) resid = j; } // Remove the entry if found and if owned with FORCE specified if ((resid != -1) && (!newPool.resourceList[resid].owned || (parsedResult->optionTimes(sForce) != 0))) { if (newPool.resourceList[resid].owned) { newPool.usedResources--; } newPool.numResources--; newPool.resourceList.erase(newPool.resourceList.begin() + resid); } else if (resid == -1) { return STAFResultPtr(new STAFResult(kSTAFDoesNotExist, thisEntry), STAFResultPtr::INIT); } else { // Cannot remove pool entry if owned and FORCE not specified return STAFResultPtr(new STAFResult( kSTAFResPoolEntryIsOwned, thisEntry), STAFResultPtr::INIT); } } // end for each entry specified // Delete the old pool file and write the new pool file STAFFSPath poolFilePath; poolFilePath.setRoot(pData->fPoolDir); poolFilePath.setName(poolName); poolFilePath.setExtension(sPoolExt); try { poolFilePath.getEntry()->remove(); } catch (STAFException &e) { result = getExceptionString(e, "STAFResPoolService.cpp: Remove ").adoptImpl(); return STAFResultPtr(new STAFResult(kSTAFFileDeleteError, result), STAFResultPtr::INIT); } STAFString fileName = poolFilePath.asString(); if (writePoolFile(fileName, newPool) != kReadorWriteOk) { return STAFResultPtr(new STAFResult(kSTAFFileWriteError, fileName), STAFResultPtr::INIT); } // Update the pool in memory poolPtr->resourceList = newPool.resourceList; poolPtr->numResources = newPool.numResources; poolPtr->usedResources = newPool.usedResources; // Result an Ok result return STAFResultPtr(new STAFResult(kSTAFOk, result), STAFResultPtr::INIT);}// Handles resource pool request requests
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -