SF.net SVN: fclient: [186] trunk/sandbox/fcp/001.py
Status: Pre-Alpha
Brought to you by:
jurner
From: <ju...@us...> - 2008-02-08 13:45:19
|
Revision: 186 http://fclient.svn.sourceforge.net/fclient/?rev=186&view=rev Author: jurner Date: 2008-02-08 05:45:16 -0800 (Fri, 08 Feb 2008) Log Message: ----------- ups, shoudn't have gotten in here Modified Paths: -------------- trunk/sandbox/fcp/001.py Modified: trunk/sandbox/fcp/001.py =================================================================== --- trunk/sandbox/fcp/001.py 2008-02-08 13:37:01 UTC (rev 185) +++ trunk/sandbox/fcp/001.py 2008-02-08 13:45:16 UTC (rev 186) @@ -1,107 +1 @@ -import random -import uuid -#********************************************************************************************* -# -#********************************************************************************************* -def validateFcpBool(value): - if value in ('true', 'false'): - return value - return None - -def validateFloat(value): - try: - return float(value) - except ValueError: - return None - -def validateInt(value): - try: - return int(value) - except ValueError: - return None - -def validateUuid(value): - result = uuid.UUID_EXACT_MATCH_PAT.match(value) - if result: - return result.group(0) - return None - -#********************************************************************************************* -# -#********************************************************************************************* -FcParamsSep = '\x01' - -FcParams = ( - ('FcSubType', validateInt), - ('FcInitTime', validateFloat), # can not take it from uuid cos requests may be resend multiple times - ('FcHandleCollisions', validateFcpBool), - ('FcCollisionHandled', validateFcpBool), - ('FcRequestIdentifier', validateUuid), - ('FcRandomBytes', validateInt), - ) - - -ISubType = 0 -IRequestIdentifier = 1 -IInitTime = 2 -IHandleCollisions = 3 -ICollisionHandled = 4 -IRandomBytes = 5 - -def paramsFromRequest(msg): - """ - - >>> params = [1, 123.456, 'false', 'false', uuid.uuid_time(), 123456789] - >>> identifier = FcParamsSep.join( [str(i) for i in params] ) - >>> result = paramsFromRequest({'Identifier': identifier}) - >>> result == params - True - - """ - identifier = msg.get('Identifier', None) - if identifier is None: - return None - - params = identifier.split(FcParamsSep) - if len(params) != len(FcParams): - return None - - for i, (paramName, paramValidator) in enumerate(FcParams): - result = paramValidator(params[i]) - if result is None: - return None - params[i] = result - - return params - -def identifierFromRequest(msg): - """ - - >>> msg = {'FcSubType':1, 'FcInitTime':1.234, 'FcHandleCollisions':'false', 'FcCollisionHandled':'flase', 'FcRequestIdentifier':uuid.uuid_time(), 'FcRandomBytes':1234567879} - >>> identifierFromRequest(msg) - - """ - - - params = [] - for paramName, paramValidator in FcParams: - params.append(msg[paramName]) - - params[-1] = random.getrandbits(32) # add some random bits to be able to - # handle IdentifierCollisions(FcRequestIdentifier - # remains the same, identifier kown to node changes) - - return FcParamsSep.join( [str(i) for i in params] ) - - -s = '1\x011.234\x01false\x01flase\x01{fc0125cc-c76f-11dc-9099-fce464f183f6}\x011234567879' -print len(s) -#********************************************************************************* -# -#********************************************************************************* -if __name__ == '__main__2': - import doctest - doctest.testmod() - - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |