📄 stafrespoolservice.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2001, 2004, 2005 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/#include "STAFOSTypes.h"#include <vector>#include <list>#include <map>#include "STAF_fstream.h"#include "STAFString.h"#include "STAFError.h"#include "STAFException.h"#include "STAFRefPtr.h"#include "STAFMutexSem.h"#include "STAFEventSem.h"#include "STAFRWSem.h"#include "STAFCommandParser.h"#include "STAFServiceInterface.h"#include "STAFTimestamp.h"#include "STAFUtil.h"#include "STAFInternalUtil.h"#include "STAFResPoolService.h"#include "STAFFileSystem.h"int debug = 0; // XXXtypedef STAFRefPtr<STAFCommandParser> STAFCommandParserPtr;// Resource Data - contains data for a entry in a resource poolstruct ResourceData{ ResourceData() : owned(0) { /* Do Nothing */ } ResourceData(const STAFString &aEntry) : entry(aEntry), owned(0) { /* Do Nothing */ } STAFString entry; // Entry value unsigned int owned; // 0 means Available; 1 means Owned STAFString orgUUID; // Originating request's STAF UUID STAFString orgMachine; // Originating request's machine name STAFString orgName; // Originating request's handle name STAFHandle_t orgHandle; // Originating request's handle STAFString orgUser; // Originating request's handle user STAFString orgEndpoint; // Originating request's endpoint STAFString requestedTime; // Time made request STAFString acquiredTime; // Time acquired the resource};// ResourceList -- Ordered list of entries in a resource pooltypedef std::vector<ResourceData> ResourceList;// Request Data - contains data for a pending request for a resourcestruct RequestData{ RequestData() : wakeup(new STAFEventSem(), STAFEventSemPtr::INIT), retCode(kSTAFTimeout) { requestedTime = STAFTimestamp::now().asString(); wakeup->reset(); *garbageCollectedPtr = false; } RequestData(const STAFString &aUUID, const STAFString &aMachine, const STAFString &aHandleName, const STAFHandle_t aHandle, const STAFString aUser, const STAFString aEndpoint) : orgUUID(aUUID), orgMachine(aMachine), orgName(aHandleName), orgHandle(aHandle), orgUser(aUser), orgEndpoint(aEndpoint), wakeup(new STAFEventSem(), STAFEventSemPtr::INIT), retCode(kSTAFTimeout), garbageCollectedPtr(new bool, STAFRefPtr<bool>::INIT) { requestedTime = STAFTimestamp::now().asString(); wakeup->reset(); *garbageCollectedPtr = false; } STAFString orgUUID; // Originating request's STAF UUID STAFString orgMachine; // Originating request's machine name STAFString orgName; // Originating request's handle name STAFHandle_t orgHandle; // Originating request's STAF handle STAFString orgUser; // Originating request's user STAFString orgEndpoint; // Originating request's endpoint STAFString requestedTime; // Originating request's date/time STAFEventSemPtr wakeup; // Semaphore to wake up a pending request STAFRC_t retCode; // Return code indicating if request was // successfully satisfied STAFString resultBuffer; // Entry obtained if retCode = 0 STAFRefPtr<bool> garbageCollectedPtr;};typedef STAFRefPtr<RequestData> RequestDataPtr;// RequestList -- Ordered list of pending requests for a pooltypedef std::list<RequestDataPtr> RequestList;static const unsigned int sCurrFileFormat = 1;// Resource Pool Data - contains all data for a resource poolstruct PoolData{ PoolData() : usedResources(0), accessSem(new STAFMutexSem(), STAFMutexSemPtr::INIT) { /* Do nothing */ } PoolData(const STAFString &aPoolName, const STAFString &aPoolDescription) : poolName(aPoolName), poolDescription(aPoolDescription), numResources(0), usedResources(0), accessSem(new STAFMutexSem(), STAFMutexSemPtr::INIT) { fileFormat = sCurrFileFormat; } unsigned int fileFormat; // Format of the pool file // "0" - REXX version in STAF 2.2 and < // 1 - C++ version in STAF 2.3 and later STAFString poolName; // Pool Name STAFString poolDescription; // Pool Description unsigned int numResources; // Total # of entries in ResourceList unsigned int usedResources; // # of entries used in ResourceList ResourceList resourceList; // List of entries in a resource pool RequestList requestList; // List of pending requests STAFMutexSemPtr accessSem; // Semaphore to control access to PoolData};typedef STAFRefPtr<PoolData> PoolDataPtr;// PoolMap -- KEY: Pool name in upper case, // VALUE: Pointer to PoolData informationtypedef std::map<STAFString, PoolDataPtr> PoolMap;// Read/Write File Return Codesenum ReadFileRC{ kReadorWriteOk = 0, kReadEndOfFile = 1, kReadInvalidFormat = 2, kFileOpenError = 3};// RESPOOL Service Datastruct ResPoolServiceData{ unsigned int fDebugMode; // Debug Mode flag STAFString fShortName; // Short service name STAFString fName; // Registered service name STAFString fLocalMachineName; // Logical identifier for the local // machine STAFString fPoolDir; // Pool Directory STAFHandlePtr fHandlePtr; // Respool service's STAF handle STAFCommandParserPtr fCreateParser; // RESPOOL CREATE command parser STAFCommandParserPtr fDeleteParser; // RESPOOL DELETE command parser STAFCommandParserPtr fQueryParser; // RESPOOL QUERY command parser STAFCommandParserPtr fRequestParser; // RESPOOL REQUEST command parser STAFCommandParserPtr fAddParser; // RESPOOL ADD command parser STAFCommandParserPtr fRemoveParser; // RESPOOL REMOVE command parser STAFCommandParserPtr fReleaseParser; // RESPOOL RELEASE command parser STAFCommandParserPtr fListParser; // RESPOOL LIST command parser STAFCommandParserPtr fSTAFCallbackParser; // RESPOOL STAF_CALLBACK command parser STAFCommandParserPtr fHelpParser; // RESPOOL HELP command parser STAFCommandParserPtr fVersionParser; // RESPOOL VERSION command parser STAFCommandParserPtr fParmsParser; // RESPOOL PARMS command parser STAFRWSemPtr fPoolMapRWSem; // Read/Write semaphore to control // access to the PoolMap PoolMap fPoolMap; // Map of all resource pools // Map Class Definitions for marshalled results STAFMapClassDefinitionPtr fPoolClass; STAFMapClassDefinitionPtr fPoolInfoClass; STAFMapClassDefinitionPtr fSettingsClass; STAFMapClassDefinitionPtr fRequestClass; STAFMapClassDefinitionPtr fResourceClass; STAFMapClassDefinitionPtr fResourceOwnerClass;};typedef STAFRefPtr<ResPoolServiceData> ResPoolServiceDataPtr;// Static Variablesstatic STAFString sLineSep;static STAFString sPoolExt("rpl");static const STAFString sVersionInfo("3.1.2");static const STAFString sPool("POOL");static const STAFString sDescription("DESCRIPTION");static const STAFString sFirst("FIRST");static const STAFString sTimeout("TIMEOUT");static const STAFString sEntry("ENTRY");static const STAFString sForce("FORCE");static const STAFString sDirectory("DIRECTORY");static const STAFString sLeftCurlyBrace(kUTF8_LCURLY);static const STAFString sSemiColon(kUTF8_SCOLON);static const STAFString sColon(kUTF8_COLON);static const STAFString sSlash(kUTF8_SLASH);static const STAFString sSpecSeparator(sColon + sSlash + sSlash);static const STAFString sLocal("local");static const STAFString sHelp("help");static const STAFString sVar("var");static const STAFString sResStrResolve("RESOLVE REQUEST ");static const STAFString sString(" STRING ");// Prototypesstatic STAFResultPtr handleCreate(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleDelete(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleQuery(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleRequest(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleRelease(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleAdd(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleRemove(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleSTAFCallback(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleList(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleHelp(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr handleVersion(STAFServiceRequestLevel30 *, ResPoolServiceData *);static STAFResultPtr resolveStr(STAFServiceRequestLevel30 *pInfo, ResPoolServiceData *pData, const STAFString &theString);static STAFResultPtr resolveOp(STAFServiceRequestLevel30 *pInfo, ResPoolServiceData *pData, STAFCommandParseResultPtr &parsedResult, const STAFString &fOption, unsigned int optionIndex = 1);STAFResultPtr resolveOpLocal(ResPoolServiceData *pData, STAFCommandParseResultPtr &parsedResult, const STAFString &fOption, unsigned int optionIndex = 1);static void registerHelpData(ResPoolServiceData *pData, unsigned int errorNumber, const STAFString &shortInfo, const STAFString &longInfo);static void unregisterHelpData(ResPoolServiceData *pData, unsigned int errorNumber);void readUIntFromFile(istream &input, unsigned int &data, unsigned int length = 4);void writeUIntToFile(ostream &output, unsigned int data, unsigned int length = 4);void readStringFromFile(istream &input, STAFString &inString);void writeStringToFile(ostream &output, STAFString &outString);unsigned int readPoolFile(const STAFString &fileName, PoolData &poolData);unsigned int writePoolFile(const STAFString &fileName, PoolData &poolData);// Begin implementationSTAFRC_t STAFServiceGetLevelBounds(unsigned int levelID, unsigned int *minimum, unsigned int *maximum){ switch (levelID) { case kServiceInfo: { *minimum = 30; *maximum = 30; break; } case kServiceInit: { *minimum = 30; *maximum = 30; break; } case kServiceAcceptRequest: { *minimum = 30; *maximum = 30; break; } case kServiceTerm: case kServiceDestruct: { *minimum = 0; *maximum = 0; break; } default: { return kSTAFInvalidAPILevel; } } return kSTAFOk;}STAFRC_t STAFServiceConstruct(STAFServiceHandle_t *pServiceHandle, void *pServiceInfo, unsigned int infoLevel, STAFString_t *pErrorBuffer){ STAFRC_t rc = kSTAFUnknownError; try { if (infoLevel != 30) return kSTAFInvalidAPILevel; STAFServiceInfoLevel30 *pInfo = reinterpret_cast<STAFServiceInfoLevel30 *>(pServiceInfo); ResPoolServiceData data; data.fDebugMode = 0; data.fShortName = pInfo->name; data.fName = "STAF/Service/"; data.fName += pInfo->name; for (unsigned int i = 0; i < pInfo->numOptions; ++i) { if (STAFString(pInfo->pOptionName[i]).upperCase() == "DEBUG") { data.fDebugMode = 1; } else { STAFString optionError(pInfo->pOptionName[i]); *pErrorBuffer = optionError.adoptImpl(); return kSTAFServiceConfigurationError; } } // Set service handle *pServiceHandle = new ResPoolServiceData(data); return kSTAFOk; } catch (STAFException &e) { *pErrorBuffer = getExceptionString(e,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -