SF.net SVN: fclient: [33] trunk/fclient/fclient_lib/fcp/fcp2_0.py
Status: Pre-Alpha
Brought to you by:
jurner
From: <jU...@us...> - 2007-11-02 08:27:30
|
Revision: 33 http://fclient.svn.sourceforge.net/fclient/?rev=33&view=rev Author: jUrner Date: 2007-11-02 01:27:25 -0700 (Fri, 02 Nov 2007) Log Message: ----------- combed over events + a minor adjustement in testDDA() 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-11-01 14:58:13 UTC (rev 32) +++ trunk/fclient/fclient_lib/fcp/fcp2_0.py 2007-11-02 08:27:25 UTC (rev 33) @@ -114,56 +114,61 @@ #************************************************************************** #TODO: name as specified in NodeHello seems to be usable to keep jobs alive. Have to test this. #TODO: events should be FcpClient.event.PeerNote not FcpClient.EventPeerNote -class FcpClient(events.Events): +class FcpClient(object): """Fcp client implementation """ + + Version = '2.0' + FcpTrue = 'true' + FcpFalse = 'false' + class DisconnectReason: + """Reason for client disconnect""" + Shutdown = '1' + SocketDied = '2' + - _events_ = ( + class Events(events.Events): + """All events the client supports""" + _events_ = ( - 'EventClientConnected', - 'EventClientDisconnected', + 'ClientConnected', + 'ClientDisconnected', # config related events - 'EventConfigData', - 'EventNodeData', + 'ConfigData', + 'NodeData', #Peer related events - 'EventEndListPeers', - 'EventPeer', - 'EventPeerRemoved', - 'EventUnknownNodeIdentifier', + 'EndListPeers', + 'Peer', + 'PeerRemoved', + 'UnknownNodeIdentifier', - 'EventListPeerNotes', - 'EventEndListPeerNotes', - 'EventPeerNote', + 'ListPeerNotes', + 'EndListPeerNotes', + 'PeerNote', # get / put related events - 'EventTestDDAComplete', - 'EventIdentifierCollision', + 'TestDDAComplete', + 'IdentifierCollision', - 'EventClientGetInfo', - 'EventClientGetInfoProgress', + 'ClientGetInfo', + 'ClientGetInfoProgress', - 'EventDataFound', - 'EventGetFailed', - 'EventSimpleProgress', - 'EventPersistentRequestModified', - 'EventPersistentRequestRemoved', + 'DataFound', + 'GetFailed', + 'SimpleProgress', + 'PersistentRequestModified', + 'PersistentRequestRemoved', # others - 'EventSSKKeypair', + 'SSKKeypair', ) - Version = '2.0' - FcpTrue = 'true' - FcpFalse = 'false' - class DisconnectReason: - """Reason for client disconnect""" - Shutdown = '1' - SocketDied = '2' + class FetchError(Exception): """All fetch errors supported by the client""" @@ -348,6 +353,7 @@ KeyboardInterrupt = 'Keyboard interrupt' SocketDied = 'Socket died' + #TODO: maybe speed up lookup of message name lookup by implementing integer message names class Message(object): """Class wrapping a freenet message""" @@ -666,6 +672,8 @@ @param name: name of the client instance or '' (for debugging) @param conectionName: name of the connection @param verbosity: verbosity level for debugging + + @ivar events: events the client supports """ self._connectionName = connectionName @@ -673,6 +681,8 @@ self._log = logging.getLogger(name) self._socket = None + self.events = self.Events() + self.setVerbosity(verbosity) atexit.register(self.close) @@ -697,7 +707,7 @@ @param port: (int) port of the node @param repeat: (int) how many seconds try to connect before giving up @param timeout: (int) how much time to wait before another attempt to connect - @event: EventConnected(event, params). Triggered as soon as the client is connected. Params + @event: Connected(event, params). Triggered as soon as the client is connected. Params will be the parameters of the NodeHello message. @return: (Message) NodeHello if successful, None otherwise for the next iteration """ @@ -736,7 +746,7 @@ timeElapsed += SocketTimeout yield None elif msg.name == self.Message.NodeHello: - self.EventClientConnected(msg.params) + self.events.ClientConnected(msg.params) yield msg.params raise StopIteration else: @@ -751,9 +761,7 @@ self._log.info(self.LogMessages.ConnectingFailed) raise StopIteration - - - + def handleMessage(self, msg): """Handles a message from the freenet node @param msg: (Message) to handle @@ -768,7 +776,7 @@ code = msg['Code'] if code == self.ProtocolError.ShuttingDown: self.close() - self.EventClientDisconnect({'DisconnectReason': DisconnectReason.Shutdown}) + self.events.ClientDisconnect({'DisconnectReason': DisconnectReason.Shutdown}) return True raise self.ProtocolError(msg) @@ -777,10 +785,6 @@ ## ## TestDDA ## - ## Note: if both, ReadDirectoryAllowed and WriteDirectoryAllowed are - ## set to false, the node sends a ProtocolError (7, 'Invalid message'). - ## Have to handle this! - ## #################################################### elif msg.name == self.Message.TestDDAReply: fpathWrite = msg.params.get('WriteFilename', None) @@ -810,7 +814,7 @@ for fpath in self._ddaTmpFiles: saveRemoveFile(fpath) self._ddaTmpFiles = [] - self.EventTestDDAComplete(msg.params) + self.events.TestDDAComplete(msg.params) return True #################################################### @@ -819,11 +823,11 @@ ## #################################################### elif msg.name == self.Message.ConfigData: - self.EventConfigData(msg.params) + self.events.ConfigData(msg.params) return True elif msg.name == self.Message.NodeData: - self.EventNodeData(msg.params) + self.events.NodeData(msg.params) return True #################################################### @@ -832,15 +836,15 @@ ## #################################################### elif msg.name == self.Message.EndListPeers: - self.EventEndListPeers(msg.params) + self.events.EndListPeers(msg.params) return True elif msg.name == self.Message.EndListPeerNotes: - self.EventEndListPeerNotes(msg.params) + self.events.EndListPeerNotes(msg.params) return True elif msg.name == self.Message.Peer: - self.EventPeer(msg.params) + self.events.Peer(msg.params) return True elif msg.name == self.Message.PeerNote: @@ -848,28 +852,28 @@ if note: note = base64.decodestring(note) msg['NoteText'] = note - self.EventPeerNote(msg.params, note) + self.events.PeerNote(msg.params, note) return True elif msg.name == self.Message.PeerRemoved: - self.EventPeerRemoved(msg.params) + self.events.PeerRemoved(msg.params) return True elif msg.name == self.Message.UnknownNodeIdentifier: - self.EventUnknownNodeIdentifier(msg.params) + self.events.UnknownNodeIdentifier(msg.params) return True #################################################### ## - ## Get related messages + ## ClientGet related messages ## #################################################### elif msg.name == self.Message.DataFound: if msg['Identifier'].startswith(self.IdentifierPrefix.ClientGetInfo): - self.EventClientGetInfo(msg.params) + self.events.ClientGetInfo(msg.params) return True - self.EventDataFound(msg.params) + self.events.DataFound(msg.params) return True elif msg.name == self.Message.GetFailed: @@ -880,29 +884,29 @@ 'Metadata.ContentType': msg.get('ExpectedMetadata.ContentType', ''), 'DataLength': msg.get('ExpectedDataLength', '') } - self.EventClientGetInfo(params) + self.events.ClientGetInfo(params) return True - self.EventGetFailed(msg.params) + self.events.GetFailed(msg.params) return True elif msg.name == self.Message.SimpleProgress: if msg['Identifier'].startswith(self.IdentifierPrefix.ClientGetInfo): - self.EventClientGetInfoProgress(msg.params) + self.events.ClientGetInfoProgress(msg.params) else: - self.EventSimpleProgress(msg.params) + self.events.SimpleProgress(msg.params) return True elif msg.name == self.Message.IdentifierCollision: - self.EventIdentifierCollision(msg.params) + self.events.IdentifierCollision(msg.params) return True elif msg.name == self.Message.PersistentRequestModified: - self.EventPersistentRequestModified(msg.params) + self.events.PersistentRequestModified(msg.params) return True elif msg.name == self.Message.PersistentRequestRemoved: - self.EventPersistentRequestRemoved(msg.params) + self.events.PersistentRequestRemoved(msg.params) return True #################################################### @@ -911,7 +915,7 @@ ## #################################################### elif msg.name == self.Message.SSKKeypair: - self.EventSSKKeypair(msg.params) + self.events.SSKKeypair(msg.params) return True @@ -961,7 +965,7 @@ 'Exception': msg['Exception'], 'Details': msg['Details'] } - self.EventClientDisconnected(params) + self.events.ClientDisconnected(params) raise self.SocketError(msg['Details']) self.handleMessage(msg) return msg @@ -999,7 +1003,7 @@ 'Exception': socket.error, 'Details': d } - self.EventClientDisconnected(params) + self.events.ClientDisconnected(params) raise self.SocketError(d) return msg @@ -1089,9 +1093,9 @@ def listPeerNotes(self, identity): """Lists all text notes associated to a peer @param identifier: peer as returned in a call to L{peerList} - @event: EventListPeerNotes(event). - @event: EventListPeerNote(event, note). - @event: EventEndListPeerNotes(event). + @event: ListPeerNotes(event). + @event: ListPeerNote(event, note). + @event: EndListPeerNotes(event). """ self.sendMessage( self.Message.ListPeerNotes, @@ -1104,8 +1108,8 @@ @param withMetaData: include meta data for each peer? @param withVolantile: include volantile data for each peer? - @event: EvenPeer(event, peer). - @event: EventEndListPeers(event, params). + @event: Peer(event, peer). + @event: EndListPeers(event, params). """ self.sendMessage( self.Message.ListPeers, @@ -1183,7 +1187,7 @@ @param uri: uri of the file to request info about @event: clientGetInfo(event, params). If success, params will contain a key 'Metadata.ContentType' and 'DataLength'. Both may be '' (empty string) - @event: clientGetInfoProgress(event, params). Triggered instead of EventSimpleProgress + @event: GetInfoProgress(event, params). Triggered instead ofSimpleProgress @note: for other events see: L{clientGet} @return: (str) request identifier """ @@ -1201,7 +1205,7 @@ ) return identifier - + def testDDA(self, directory, wantReadDirectory=None, wantWriteDirectory=None): """Tests a directory for read / write access @param directory: directory to test @@ -1213,7 +1217,19 @@ and a directory for read access before uploading content from it @note: the node does not like both parameters being False and will respond with a protocol error in this case. Take care of that. + @note: if a directory is no longer needed, best pratice is to free resources by calling + testDDA() with both parameters set to False. """ + + # if both, ReadDirectoryAllowed and WriteDirectoryAllowed are + # set to false, the node sends a ProtocolError (7, 'Invalid message') + # No idea what the error is good for... so simply ignore the request. + # + # already file a bug report that (False, False) should be interpreted + # by the node to forgett a directory (free resources) + if not wantReadDirectory and not wantWriteDirectory: + return + msg = self.Message( self.Message.TestDDARequest, Directory=directory, @@ -1223,6 +1239,7 @@ if wantWriteDirectory is not None: msg['WantWriteDirectory'] = self.fcpBool(wantWriteDirectory) self.sendMessageEx(msg) + ########################################################## ## ## others @@ -1246,7 +1263,8 @@ #***************************************************************************** if __name__ == '__main__': c = FcpClient(name='test', verbosity=FcpClient.Verbosity.Debug) - nodeHello = c.connect() + + for nodeHello in c.connect(): pass if nodeHello is not None: @@ -1289,13 +1307,13 @@ print '%s=%s' % (prefix, value) print - c.EventConfigData += cb + c.events.ConfigData += cb oldVerbosity = c.verbosity() ##c.setVerbosity(c.Verbosity.Warning) print '\n>> Requesting config\n' c.getConfig() - for i in xrange(1): + for i in xrange(5): c.next() c.setVerbosity(oldVerbosity) @@ -1308,7 +1326,7 @@ def cb(event, params): print params - c.EventSSKKeypair += cb + c.events.SSKKeypair += cb c.generateSSK() for i in xrange(1): c.next() @@ -1319,7 +1337,7 @@ def cb(event, params): print params - c.EventTestDDAComplete += cb + c.events.TestDDAComplete += cb d = os.path.dirname(os.path.abspath(__file__)) c.testDDA(d, True, True) for i in xrange(4): @@ -1333,9 +1351,9 @@ if params['opennet'] == c.FcpFalse: c.listPeerNotes(params['identity']) - c.EventPeer += cb + c.events.Peer += cb c.listPeers() - for i in xrange(100): + for i in xrange(120): c.next() #testListPeerNotes() @@ -1345,7 +1363,7 @@ def cb(event, params): print params - c.EventClientGetInfo += cb + c.events.ClientGetInfo += cb identifier = c.clientGetInfo('CHK@sdNenKGj5mupxaSwo44jcW8dsX7vYTLww~BsRPtur0k,ZNRm9reMjtKEl9e-xFByKXbW6q4f6OQyfg~l9GRSAes,AAIC--8/snow_002%20%281%29.jpg') for i in xrange(20): c.next() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |