SF.net SVN: fclient: [182] trunk/sandbox/fcp/fcp2_0_client.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <ju...@us...> - 2008-02-08 13:34:36
|
Revision: 182
http://fclient.svn.sourceforge.net/fclient/?rev=182&view=rev
Author: jurner
Date: 2008-02-08 05:34:41 -0800 (Fri, 08 Feb 2008)
Log Message:
-----------
now all requests are registered via registerReques()
Modified Paths:
--------------
trunk/sandbox/fcp/fcp2_0_client.py
Modified: trunk/sandbox/fcp/fcp2_0_client.py
===================================================================
--- trunk/sandbox/fcp/fcp2_0_client.py 2008-02-08 09:13:54 UTC (rev 181)
+++ trunk/sandbox/fcp/fcp2_0_client.py 2008-02-08 13:34:41 UTC (rev 182)
@@ -10,22 +10,23 @@
should ever do so. Global is evil.
-Sample code, request data of a freenet key::
-
+Sample code. Connect to the freenet node::
client = FcpClient()
nodeHello = client.connect()
if nodeHello is None:
pass
# something went wrong ..could not connect to the freenet node
else:
- # request data associated to a freenet key
- myRequestIdentifier = client.getData('CHK@ABCDE.......')
- myRequest = c.getRequest(myIdentifier)
- c.run()
- print myRequest.data
+ pass
+ # everything went well ..we are connected now
-Usually you would connect handlers to client events to do processing or handle errors::
+Request data associated to a freenet key::
+ myRequestIdentifier = client.getData('CHK@ABCDE.......')
+ myRequest = c.getRequest(myIdentifier)
+ client.run()
+ print myRequest.data
+Usually you would connect handlers to client events to do processing or handle errors::
def handleSuccess(event, request):
print 'Here is the data:', request.data
@@ -40,7 +41,6 @@
Instead of calling run() you may run the client step by step::
-
client.getData('CHK@ABCDE.......')
for i in xrange(50):
client.next()
@@ -132,6 +132,7 @@
#------------------------------------------------------------------------------------------------------------------------------------------------
import atexit
+import copy
import cPickle
import logging
import os
@@ -284,54 +285,6 @@
## private methods
##
###############################################################
- def _addFcParamsToRequest(self,
- msg,
- userData,
- msgType,
- initTime,
- persistentUserData,
- filenameCollision,
- ):
-
- # add additional params to msg
- msg.params.update({
-
- # persistent params that will go into identifier
- 'FcRequestType': msgType, # identifies sub message types
- 'FcInitTime': initTime, # when was the request started?
- 'FcFilenameCollision': filenameCollision, # handle fielanem collisions?
- 'FcPersistentUserData': persistentUserData, # any user defined persistent data
-
- # non persistent params
- 'FcRequestStatus': consts.RequestStatus.Null,
- 'FcErrorMessage': None, # error message in case an error occured
- 'FcUserData': userData, # any user defined runtime data here
-
- # params for SSKKeypair
- 'FcInsertUri': None,
- 'FcRequestUri': None,
-
- # params from DataFound
- 'FcMetadataContentType': '', # contecnt type
- 'FcDataLength': '', # content size
-
- # params from PersistentRequestModified
- 'FcModified': {},
-
- # params for DDA test
- 'FcTestDDA': {},
-
- # params for SimpleProgress
- 'FcProgressTotal': '0',
- 'FcProgressRequired': '0',
- 'FcProgressFailed': '0',
- 'FcProgressFatalyFailed': '0',
- 'FcProgressSucceeeded': '0',
-
- })
- return msg
-
-
def _finalizeRequest(self, msg, request, event):
"""Finalzes a request
@param msg: message that is the reason for finalizing
@@ -362,71 +315,92 @@
def _registerRequest(self,
msg,
- userData,
- msgType,
- initTime,
- persistentUserData,
+ requestType,
+ userData=None,
+ identifier=None,
+ initTime=None,
+ persistentUserData='',
filenameCollision=consts.FilenameCollision.HandleNever,
):
"""Registers a request
@param msg: message to register
- @param userData: any user defined data
- @param msgType: one of the message sub type consts
- @param initTime: (python time)
- @param persistentUserData: (str) user defined persistent data
- @param filenameCollision: (bool)
-
- @return: (str) uuid
+ @param requestType: (L{consts.RequestType}) type of request to register
+ @param filenameCollision: (L{consts.FilenameCollision}) how to handle filename collisions.
+ Default is L{consts.FilenameCollision.HandleNever}
+ @param identifier: (str) identifier of the request or None to create a new one
+ @param initTime: (int) init time of the request or None to set it to now
+ @param persistentUserData: (str) anyuser defined persistent data
+ @param userData: (any) any user defined non persistent data
+
+ @return: (str) identifer of therequest
@note: the identifier returned is unique to the client but may not be unique to the node
"""
- identifier = self.FcParams.newUuid(uuids=self._requests)
+ identifier = self.FcParams.newUuid(uuids=self._requests) if identifier is None else identifier
- # add additional params to msg
- msg = self._addFcParamsToRequest(
- msg,
- userData,
- msgType,
- initTime,
- persistentUserData,
- filenameCollision,
- )
+ if requestType & (consts.RequestType.MaskGet | consts.RequestType.MaskPut):
+
+ msg.params.update({
- msg['ClientToken'] = self.FcParams.messageToParams(msg)
+ # persistent params that will go into identifier
+ 'FcRequestType': requestType, # identifies sub message types
+ 'FcInitTime': time.time() if initTime is None else initTime, # when was the request started?
+ 'FcFilenameCollision': filenameCollision, # handle fielanem collisions?
+ 'FcPersistentUserData': persistentUserData, # any user defined persistent data
+
+ # non persistent params
+ 'FcRequestStatus': consts.RequestStatus.Pending,
+ 'FcErrorMessage': None, # error message in case an error occured
+ 'FcUserData': userData, # any user defined runtime data here
+
+ # params for SSKKeypair
+ 'FcInsertUri': None,
+ 'FcRequestUri': None,
+
+ # params from DataFound
+ 'FcMetadataContentType': '', # contecnt type
+ 'FcDataLength': '', # content size
+
+ # params from PersistentRequestModified
+ 'FcModified': {},
+
+ # params for DDA test
+ 'FcTestDDA': {},
+
+ # params for SimpleProgress
+ 'FcProgressTotal': '0',
+ 'FcProgressRequired': '0',
+ 'FcProgressFailed': '0',
+ 'FcProgressFatalyFailed': '0',
+ 'FcProgressSucceeeded': '0',
+
+ })
+ # equip msg with some persistent pparams
+ if msg.get('ClientToken', None) is None:
+ msg['ClientToken'] = self.FcParams.messageToParams(msg)
+
+ elif requestType & consts.RequestType.MaskGenerateKeypair:
+ msg.params.update({
+ 'FcRequestType': requestType, # identifies sub message types
+ 'FcRequestStatus': consts.RequestStatus.Pending,
+ 'FcInitTime': initTime, # when was the request started?
+ 'FcModified': {},
+
+ 'FcPrivateKey': None,
+ 'FcPublicKey': None,
+ })
+
+ else:
+ msg.params.update({
+ 'FcRequestType': requestType, # identifies sub message types
+ 'FcRequestStatus': consts.RequestStatus.Pending,
+ 'FcInitTime': initTime, # when was the request started?
+ 'FcModified': {},
+ })
+
msg['FcRequestStatus'] |= consts.RequestStatus.Pending
msg['Identifier'] = identifier
self._requests[identifier] = msg
-
-
- def _restorePersistentRequestFromNode(self, msg):
- """Preps a message from node to be restored
- @return: the restored message if everything went well, None otherwise
- """
- fcParams = self.FcParams.paramsFromRequest(msg)
- if fcParams is None:
- return None
-
- # add additional params to msg
- msg = self._addFcParamsToRequest(
- msg,
- None, # userData,
- fcParams[self.FcParams.ISubType],
- fcParams[self.FcParams.IInitTime],
- fcParams[self.FcParams.IPersistentUserData],
- fcParams[self.FcParams.IFilenameCollision],
- )
-
- #FIX: remove Started param from PersistentGet / Put
- if msg.name == consts.Message.PersistentGet:
- del msg.params['Started']
- #FIX: [0001965: Persistence vs PersistenceType]
- if 'PersistenceType' in msg.params:
- msg['Persistence'] = msg.params.pop('PersistenceType')
- elif msg.name == consts.Message.PersistentPut:
- del msg.params['Started']
-
- return msg
-
###############################################################
##
## connection related methods
@@ -914,24 +888,36 @@
# unknown request... try to restore it
if initialRequest is None:
- restoredRequest = self._restorePersistentRequestFromNode(msg)
-
+ fcParams = self.FcParams.paramsFromRequest(msg)
+
# not one of our requests... so cancel it
- if restoredRequest is None or CancelPersistentRequests:
+ if fcParams is None or CancelPersistentRequests:
self.sendMessage(
consts.Message.RemovePersistentRequest,
- Identifier=msg['Identifier'],
+ Identifier=requestIdentifier,
Global=msg['Global'],
)
return True
- # determine initial message name
- restoredRequest.name = consts.Message.ClientGet
-
- # restore request
- self._requests[requestIdentifier] = restoredRequest
- #restoredRequest['FcRequestStatus'] |= consts.RequestStatus.Null
- self.events.RequestRestored(restoredRequest)
+ initialRequest = copy.deepcopy(msg)
+ self._registerRequest(
+ initialRequest,
+ fcParams[self.FcParams.IRequestType],
+ identifier=requestIdentifier,
+ initTime=fcParams[self.FcParams.IInitTime],
+ userData=None,
+ persistentUserData=fcParams[self.FcParams.IPersistentUserData],
+ filenameCollision=fcParams[self.FcParams.IFilenameCollision],
+ )
+
+ initialRequest.name = consts.Message.ClientGet
+ #FIX: remove Started param from PersistentGet / Put
+ del initialRequest.params['Started']
+ #FIX: [0001965: Persistence vs PersistenceType]
+ if 'PersistenceType' in initialRequest.params:
+ initialRequest['Persistence'] = initialRequest.params.pop('PersistenceType')
+ initialRequest['FcRequestStatus'] = consts.RequestStatus.Null
+ self.events.RequestRestored(initialRequest)
return True
# known request ..we don't handle that
@@ -996,29 +982,41 @@
# unknown request... try to restore it
if initialRequest is None:
- restoredRequest = self._restorePersistentRequestFromNode(msg)
-
+ fcParams = self.FcParams.paramsFromRequest(msg)
+
# not one of our requests... so cancel it
- if restoredRequest is None or CancelPersistentRequests:
+ if fcParams is None or CancelPersistentRequests:
self.sendMessage(
consts.Message.RemovePersistentRequest,
- Identifier=msg['Identifier'],
+ Identifier=requestIdentifier,
Global=msg['Global'],
)
return True
- # determine initial message name
- if restoredRequest['FcRequestType'] == consts.RequestType.Put:
- restoredRequest.name = consts.Message.ClientPut
- elif restoredRequest['FcRequestType'] == consts.RequestType.PutDiskDir:
- restoredRequest.name = consts.Message.ClientPutDiskDir
- elif restoredRequest['FcRequestType'] == consts.RequestType.PutComplexDir:
- restoredRequest.name = consts.Message.ClientPutComplexDir
+ initialRequest = copy.deepcopy(msg)
+ requestType = fcParams[self.FcParams.IRequestType]
+ self._registerRequest(
+ initialRequest,
+ requestType,
+ identifier=requestIdentifier,
+ initTime=fcParams[self.FcParams.IInitTime],
+ userData=None,
+ persistentUserData=fcParams[self.FcParams.IPersistentUserData],
+ filenameCollision=fcParams[self.FcParams.IFilenameCollision],
+ )
- # restore request
- self._requests[requestIdentifier] = restoredRequest
- #restoredRequest['FcRequestStatus'] = consts.RequestStatus.Null
- self.events.RequestRestored(restoredRequest)
+ # determine initial message name
+ if requestType == consts.RequestType.Put:
+ initialRequest.name = consts.Message.ClientPut
+ elif requestType == consts.RequestType.PutDiskDir:
+ initialRequest.name = consts.Message.ClientPutDiskDir
+ elif requestType == consts.RequestType.PutComplexDir:
+ initialRequest.name = consts.Message.ClientPutComplexDir
+
+ #FIX: remove Started param from PersistentGet / Put
+ del initialRequest.params['Started']
+ initialRequest['FcRequestStatus'] = consts.RequestStatus.Null
+ self.events.RequestRestored(initialRequest)
return True
# known request ..we don't handle that
@@ -1318,7 +1316,7 @@
########################################################
def clientGet(self,
uri,
- messageSubType,
+ requestType,
userData,
persistentUserData,
filenameCollision,
@@ -1326,7 +1324,7 @@
):
"""Requests a key from the node
@param uri: (str) uri of the file to request (may contain prefixes like 'freenet:' or 'http://')
- @param messageSubType: (L{consts.RequestType}) sub type of the message
+ @param requestType: (L{consts.RequestType}) sub type of the message
@param userData: any non persistent data to associate to the request or None
@param persistentUserData: any string to associate to the request as persistent data or None
@param filenameCollision: what to do if the disk target alreaady exists. One of the FilenameCollision.* consts
@@ -1345,11 +1343,10 @@
self._registerRequest(
msg,
- userData,
- messageSubType,
- time.time(),
- persistentUserData,
+ requestType,
filenameCollision=filenameCollision,
+ persistentUserData=persistentUserData,
+ userData=userData,
)
self.sendMessageEx(msg)
return msg['Identifier']
@@ -1595,11 +1592,9 @@
self._registerRequest(
msg,
- userData,
messageType,
- time.time(),
- persistentUserData,
- #filenameCollision=filenameCollision,
+ persistentUserData=persistentUserData,
+ userData=userData,
)
if upload.keyType in (consts.KeyType.SSK, consts.KeyType.USK):
@@ -1612,7 +1607,7 @@
def clientPut(self,
uri,
- messageSubType,
+ requestType,
userData,
persistentUserData,
data,
@@ -1629,21 +1624,15 @@
self._registerRequest(
msg,
- userData,
- messageSubType,
- time.time(),
- persistentUserData,
- #filenameCollision=filenameCollision,
+ requestType,
+ persistentUserData=persistentUserData,
+ userData=userData,
)
self.sendMessageEx(msg)
return msg['Identifier']
- #TODO: method names
-
#CHK
-
-
def chkPutData(self,
data,
@@ -1897,21 +1886,23 @@
or L{consts.Message.ClientPluginMessages}. All attempts to resend other requests will fail
@note: the request passed is not removed in the call. Use L{removeRequest} if necessary
"""
- if requestMessage.name in consts.Message.ClientKeyRequestMessages:
+ requestType = requestMessage.get('FcRequestType', None)
+ if requestType is None:
+ raise ValueError('Can not resend request: %s' % requestMessage.name)
+
+ if requestType & (consts.RequestType.MaskGet | consts.RequestType.MaskPut):
self._registerRequest(
requestMessage,
- requestMessage['FcUserData'],
- requestMessage['FcMessageSubType'],
- time.time(), # TOSO: reset init time?
- requestMessage['FcPersistentUserData'],
- requestMessage['FcFilenameCollision'] & consts.FilenameCollision.MaskHandle,
+ requestType,
+ filenameCollision=requestMessage['FcFilenameCollision'] & consts.FilenameCollision.MaskHandle,
+ #initTime=time.time(), # TODO: reset init time or not?
+ persistentUserData=requestMessage['FcPersistentUserData'],
+ userData=requestMessage['FcUserData'],
)
- elif requestMessage.name in consts.Message.ClientPluginMessages:
- identifier = self.FcParam.newUuid(uuids=self._requests)
- initialRequest['Identifier'] = identifier
- self._requests[identifier] = initialRequest
+ elif requestType & (consts.RequestType.MaskGenerateKeypair | onsts.RequestType.MaskPlugin):
+ self._registerRequest(requestMessage, requestType)
else:
- raise ValueError('Can not resend request: %s' % requestMessage.name)
+ raise ValueError('Should not have ended here')
self.sendMessageEx(msg)
return requestMessage['Identifier']
@@ -2038,18 +2029,10 @@
"""
identifier = self.FcParam.newUuid(uuids=self._requests)
msg = self.Message(
- consts.Message.GetPluginInfo,
-
- FcErrorMessage=None,
- FcModified={},
- FcRequestType=consts.RequestType.PluginInfo,
- FcRequestStatus=consts.RequestStatus.Pending,
-
- Identifier=identifier,
PluginName=pluginName,
Detailed=detailed,
)
- self._requests[identifier] = msg
+ self._registerRequest(msg, consts.Message.GetPluginInfo)
self.sendMessageEx(msg)
return identifier
@@ -2064,21 +2047,13 @@
identifier = self.FcParam.newUuid(uuids=self._requests)
msg = self.Message(
consts.Message.GetPluginInfo,
-
- FcErrorMessage=None,
- FcModified={},
- FcRequestType=consts.RequestType.PluginMessage,
- FcRequestStatus=consts.RequestStatus.Pending,
-
- Identifier=identifier,
PluginName=pluginName,
**params
)
if data is not None:
msg['DataLength'] = len(data)
msg.data = data
-
- self._requests[identifier] = msg
+ self._registerRequest(msg, onsts.RequestType.PluginMessage)
self.sendMessageEx(msg)
return identifier
@@ -2107,16 +2082,8 @@
requestType = consts.RequestType.GenerateSSKKeypair if keypairType == consts.KeyType.SSK else consts.RequestType.GenerateUSKKeypair
identifier = self.FcParams.newUuid(uuids=self._requests)
- msg = self.Message(
- consts.Message.GenerateSSK,
- FcModified={},
- FcPrivateKey= None,
- FcPublicKey=None,
- FcRequestStatus=consts.RequestStatus.Pending,
- FcRequestType=requestType,
- Identifier=identifier,
- )
- self._requests[identifier] = msg
+ msg = self.Message(consts.Message.GenerateSSK)
+ self._registerRequest(msg, requestType)
self.sendMessageEx(msg)
return identifier
@@ -2344,7 +2311,9 @@
def testGenerateKeypair():
def cb(event, msg):
- print msg.pprint()
+ print '--------------------------------'
+ print '>>>>privateKey:', msg['FcPrivateKey']
+ print '>>>>publicKey:', msg['FcPublicKey']
pass
c.events.KeypairGenerated += cb
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|