SF.net SVN: fclient: [15] trunk/fclient/fclient_lib/fcp/fcp2_0.py
Status: Pre-Alpha
Brought to you by:
jurner
From: <jU...@us...> - 2007-10-27 17:00:16
|
Revision: 15 http://fclient.svn.sourceforge.net/fclient/?rev=15&view=rev Author: jUrner Date: 2007-10-27 10:00:21 -0700 (Sat, 27 Oct 2007) Log Message: ----------- started implementing public methods and events Modified Paths: -------------- trunk/fclient/fclient_lib/fcp/fcp2_0.py Modified: trunk/fclient/fclient_lib/fcp/fcp2_0.py =================================================================== --- trunk/fclient/fclient_lib/fcp/fcp2_0.py 2007-10-27 16:59:07 UTC (rev 14) +++ trunk/fclient/fclient_lib/fcp/fcp2_0.py 2007-10-27 17:00:21 UTC (rev 15) @@ -21,6 +21,24 @@ import thread import uuid + +#--> rel import hack +def parentdir(n, fpath): + fpath = os.path.abspath(fpath) + for i in xrange(n): + fpath = os.path.dirname(fpath) + return fpath +sys.path.insert(0, parentdir(3, __file__)) + + +from fclient_lib.pyex import events + + +sys.path.pop(0) +del parentdir +#<-- rel import hack + + logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) #************************************************************** # consts @@ -32,6 +50,14 @@ DefaultFcpPort = 9481 SocketTimeout = 0.1 + + +class Verbosity: + Debug = logging.DEBUG + Info = logging.INFO + Warning = logging.WARNING + + class FixedJobIdentifiers: """Fixed job identifiers @note: he client can only handle one job of these at a time @@ -44,6 +70,7 @@ ModifyConfig = 'ModifyConfig' WatchGlobal = 'WatchGlobal' Shutdown = 'Shutdown' + @@ -61,6 +88,23 @@ PriorityMin = Minimum PriorityDefault = Bulk + +class PeerNodeStatus: + Connected = 1 + RoutingBackedOff = 2 + TooNew = 3 + TooOld = 4 + Disconnected = 5 + NeverConnected = 6 + Disabled = 7 + Bursting = 8 + Listening = 9 + ListenOnly = 10 + ClockProblem = 11 + ConnError = 12 + Disconnecting = 13 + + #************************************************************************************ # exceptions #************************************************************************************ @@ -194,11 +238,13 @@ return 'false' -def newIdentifier(): +def newIdentifier(prefix=None): """Returns a new unique identifier @return: (str) uuid """ - return 'fclient::' + str(uuid.uuid4()) + if prefix: + return prefix + str(uuid.uuid4()) + return str(uuid.uuid4()) def pythonBool(fcpBool): @@ -580,7 +626,7 @@ """Lists all known peers of the node """ - def __init__(self, fcpClient, withMetaData=False, withVolantile=False): + def __init__(self, fcpClient, withMetaData=True, withVolantile=True): """ @param withMetaData: include meta data for each peer? @param withVolantile: include volantile data for each peer? @@ -594,6 +640,11 @@ JobBase.__init__(self, fcpClient, FixedJobIdentifiers.ListPeers, message) + def handleStart(self): + JobBase.handleStart(self) + self.jobClient.EventListPeers() + + def handleMessage(self,msg): if msg.name == Message.EndListPeers: return self.handleEndListPeers(msg) @@ -604,14 +655,16 @@ def handlePeer(self, msg): + self.jobClient.EventListNextPeer(msg.params) if self.jobResult is None: - self.jobResult = [msg, ] + self.jobResult = [msg.params, ] else: - self.jobResult.append(msg) + self.jobResult.append(msg.params) return True def handleEndListPeers(self, msg): + self.jobClient.EventEndListPeers() self.jobTimeStop = time.time() if self.jobResult is None: self.jobResult = [] @@ -639,6 +692,11 @@ JobBase.__init__(self, fcpClient, identifier, message) + def handleStart(self): + JobBase.handleStart(self) + self.jobClient.EventListPeerNotes() + + def handleMessage(self,msg): if msg.name == Message.EndListPeerNotes: return self.handleEndListPeerNotes(msg) @@ -649,6 +707,7 @@ def handlePeerNote(self, msg): note = msg.get('NoteText', '') + self.jobClient.EventListNextPeerNote(note) if note: note = base64.decodestring(note) if self.jobResult is None: @@ -659,6 +718,7 @@ def handleEndListPeerNotes(self, msg): + self.jobClient.EventEndListPeerNotes() self.jobTimeStop = time.time() if self.jobResult is None: self.jobResult = [] @@ -812,8 +872,6 @@ @ivar jobResult: when the job is complete this will be set to a tuple(bool readAllowed, bool writeAllowed) """ - - if not os.path.isdir(directory): raise ValueError('No such directory: %r' % directory) @@ -823,6 +881,7 @@ WantReadDirectory=fcpBool(read), WantWriteDirectory=fcpBool(write), ) + JobBase.__init__(self, fcpClient, directory, message) self.jobTmpFile = None @@ -832,10 +891,24 @@ return self.handleTestDDAReply(msg) elif msg.name == Message.TestDDAComplete: return self.handleTestDDAComplete(msg) + elif msg.name == Message.ProtocolError: + return self.handleProtocolError(msg) else: raise ValueError('Unexpected message: %s' % msg.name) + def handleProtocolError(self, msg): + # most likely code 7 here... + # "Both WantReadDirectory and WantWriteDirectory are set to false: what's the point of sending a message?" + # ..a stupid response that is ;-) + self.jobTimeStop = time.time() + self.jobClient.jobRemove(self.jobIdentifier) + if msg['Code'] == ProtocolError.InvalidMessage: + self.jobResult = (False, False) + else: + raise ValueError('Unexpected message: %s' % msg.name) + + def handleTestDDAReply(self, msg): fpathWrite = msg.params.get('WriteFilename', None) fpathRead = msg.params.get('ReadFilename', None) @@ -938,14 +1011,25 @@ #TODO: do not mix directories as identifiers with identifiers (might lead to collisions) #TODO: how to handle (ProtocolError code 18: Shutting down)? -class FcpClient(object): +class FcpClient(events.Events): """Fcp client implementation""" + _events_ = ( + 'EventListPeers', + 'EventListNextPeer', + 'EventEndListPeers', + + 'EventListPeerNotes', + 'EventListNextPeerNote', + 'EventEndListPeerNotes', + + ) + def __init__(self, name='', errorHandler=None, - verbosity=logging.WARNING, + verbosity=Verbosity.Warning, logMessages=LogMessages ): """ @@ -963,7 +1047,7 @@ 'PendingJobs': [], 'RegisteredDirectories': [], } - self._errorHandler = errorHandler #TODO: check! + self._errorHandler = errorHandler #TODO: check if necessary! self._log = logging.getLogger(name) self._logMessages = logMessages self._lock = thread.allocate_lock() # lock when resources are accessed @@ -1065,6 +1149,7 @@ else: # check if the is something like an identifier in the message + #TODO: we run into troubles when using directories and NodeIdentifiers as identifiers if msg.name == Message.TestDDAReply: identifier = msg['Directory'] elif msg.name == Message.TestDDAComplete: @@ -1405,13 +1490,27 @@ finally: self._jobs['PendingJobs'].remove(job) return result - - - + + ################################################# + ## + ## public methods + ## + ################################################# + def peerList(self, synchron=False): + job = JobListPeers(self) + self.jobAdd(job, synchron=synchron) + return job.jobResult + + def peerNotes(self, peer, synchron=False): + if pythonBool(peer['opennet']): + return [] + job = JobListPeerNotes(self, peer['identity']) + self.jobAdd(job, synchron=synchron) + return job.jobResult + - #***************************************************************************** # #***************************************************************************** @@ -1436,7 +1535,7 @@ job = JobGenerateSSK(c) c.jobAdd(job, synchron=True) print job.jobResult - foo() + #foo() @@ -1451,29 +1550,18 @@ #foo() def foo(): - job = JobListPeers(c) - c.jobAdd(job) - c.run() - print '---------------------------' - print job.jobResult - print '---------------------------' + peers = c.peerList(synchron=True) + for peer in peers: + print c.peerNotes(peer, synchron=True) + + foo() - for peer in job.jobResult: - if not pythonBool(peer['opennet']): - job = JobListPeerNotes(c, peer['identity']) - c.jobAdd(job, synchron=True) - print '>>', job.jobResult - #.get('NoteText') - - #foo() - - def foo(): - job = JobGetFileInfo(c, 'USK@IK8rVHfjCz1i1IZwgyafsPNDGODCk~EtBgsKWHmPKoQ,FeDoFZ8YeUsU0vo1-ZI~GRhZjeyXyaGhn-xQkIoGpak,AQACAAE/FreeMulET/15/FreeMulET-0.6.29-lib-jar.zip') + #job = JobGetFileInfo(c, 'USK@IK8rVHfjCz1i1IZwgyafsPNDGODCk~EtBgsKWHmPKoQ,FeDoFZ8YeUsU0vo1-ZI~GRhZjeyXyaGhn-xQkIoGpak,AQACAAE/FreeMulET/15/FreeMulET-0.6.29-lib-jar.zip') - #job = JobGetFileInfo(c, 'CHK@sdNenKGj5mupxaSwo44jcW8dsX7vYTLww~BsRPtur0k,ZNRm9reMjtKEl9e-xFByKXbW6q4f6OQyfg~l9GRSAes,AAIC--8/snow_002%20%281%29.jpg') + job = JobGetFileInfo(c, 'CHK@sdNenKGj5mupxaSwo44jcW8dsX7vYTLww~BsRPtur0k,ZNRm9reMjtKEl9e-xFByKXbW6q4f6OQyfg~l9GRSAes,AAIC--8/snow_002%20%281%29.jpg') #job.jobIdentifier = job.jobMessage['Identifier'] = 1 #job.jobMessage['Identifier'] = 1 #job.jobIdentifier = 1 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |