SF.net SVN: fclient: [26] trunk/fclient/fclient_lib/fcp/fcp2_0.py
Status: Pre-Alpha
Brought to you by:
jurner
From: <jU...@us...> - 2007-11-01 14:54:29
|
Revision: 26 http://fclient.svn.sourceforge.net/fclient/?rev=26&view=rev Author: jUrner Date: 2007-11-01 07:54:34 -0700 (Thu, 01 Nov 2007) Log Message: ----------- bit of refactoring 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:53:13 UTC (rev 25) +++ trunk/fclient/fclient_lib/fcp/fcp2_0.py 2007-11-01 14:54:34 UTC (rev 26) @@ -109,41 +109,34 @@ fp.close() return written -#********************************************************************** -# classes -#********************************************************************** - - #************************************************************************** # fcp client #************************************************************************** -#TODO: no idea what happens on reconnect if socket died. What about running jobs? #TODO: name as specified in NodeHello seems to be usable to keep jobs alive. Have to test this. -#TODO: do not mix directories as identifiers with identifiers (might lead to collisions) -#TODO: how to handle (ProtocolError code 18: Shutting down)? +#TODO: events should be FcpClient.event.PeerNote not FcpClient.EventPeerNote class FcpClient(events.Events): """Fcp client implementation """ _events_ = ( + 'EventClientConnected', + 'EventClientDisconnected', + # config related events 'EventConfigData', 'EventNodeData', #Peer related events - 'EventListPeers', 'EventEndListPeers', 'EventPeer', 'EventPeerRemoved', - 'EventUnknownIdentifier', + 'EventUnknownNodeIdentifier', 'EventListPeerNotes', 'EventEndListPeerNotes', 'EventPeerNote', - 'EventShutdown', - 'EventSocketDied', # get / put related events 'EventTestDDAComplete', @@ -166,6 +159,11 @@ 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""" @@ -692,14 +690,16 @@ saveRemoveFile(fpath) - #TODO: an iterator would be nice to enshure Guis stay responsitive in the call + def connect(self, host=DefaultFcpHost, port=DefaultFcpPort, repeat=20, timeout=0.5): - """Establishes the connection to a freenet node + """Iterator to stablish a connection to a freenet node @param host: (str) host of th node @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 - @return: (Message) NodeHello if successful,None otherwise + @event: EventConnected(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 """ self._clientHello = None self._log.info(self.LogMessages.Connecting) @@ -716,7 +716,7 @@ try: self._socket.connect((host, port)) except Exception, d: - pass + yield None else: self._log.info(self.LogMessages.Connected) @@ -733,9 +733,12 @@ while timeElapsed <= repeat: msg = self.next() if msg.name == self.Message.ClientSocketTimeout: - timeElapsed += SocketTimeout + timeElapsed += SocketTimeout + yield None elif msg.name == self.Message.NodeHello: - return msg.params + self.EventClientConnected(msg.params) + yield msg.params + raise StopIteration else: break break @@ -746,9 +749,11 @@ time.sleep(timeout) self._log.info(self.LogMessages.ConnectingFailed) - return None + raise StopIteration + + - + def handleMessage(self, msg): """Handles a message from the freenet node @param msg: (Message) to handle @@ -763,7 +768,7 @@ code = msg['Code'] if code == self.ProtocolError.ShuttingDown: self.close() - self.EventShutdown(msg.params) + self.EventClientDisconnect({'DisconnectReason': DisconnectReason.Shutdown}) return True raise self.ProtocolError(msg) @@ -851,7 +856,7 @@ return True elif msg.name == self.Message.UnknownNodeIdentifier: - self.EventUnknownIdentifier(msg.params) + self.EventUnknownNodeIdentifier(msg.params) return True #################################################### @@ -951,8 +956,13 @@ """ msg = self.Message.fromSocket(self._socket) if msg.name == self.Message.ClientSocketDied: - self.EventSocketDied(msg['Exception'], msg['Details']) - raise SocketError(msg['Details']) + params = { + 'DisconnectReason': DisconnectReason.SocketDied, + 'Exception': msg['Exception'], + 'Details': msg['Details'] + } + self.EventClientDisconnected(params) + raise self.SocketError(msg['Details']) self.handleMessage(msg) return msg @@ -984,8 +994,13 @@ except socket.error, d: self._log.info(self.LogMessages.SocketDied) self.close() - self.EventSocketDied(socket.error, d) - raise SocketError(d) + params = { + 'DisconnectReason': DisconnectReason.SocketDied, + 'Exception': socket.error, + 'Details': d + } + self.EventClientDisconnected(params) + raise self.SocketError(d) return msg ######################################################### @@ -1001,7 +1016,8 @@ return self.FcpTrue if pythonBool else self.FcpFalse - def newIdentifier(self, prefix=None): + @classmethod + def newIdentifier(clss, prefix=None): """Returns a new unique identifier @return: (str) uuid """ @@ -1009,7 +1025,7 @@ return prefix + str(uuid.uuid4()) return str(uuid.uuid4()) - + def pythonBool(self, fcpBool): """Converts a fcp bool to a python bool @param pythonBool: 'true' or 'false' @@ -1017,6 +1033,15 @@ """ return fcpBool == self.FcpTrue + + def pythonTime(self, fcpTime): + """Converts a fcp time value to a python time value + @param fcpTime: (int, str) time to convert + @return: (int) python time + """ + fcpTime = int(fcpTime) + return fcpTime / 1000 + ######################################################## ## ## Config related methods @@ -1054,14 +1079,14 @@ ## Peer related methods ## ######################################################## - def listPeer(self, identifier): + def listPeer(self, identity): self.jobClient.sendMessage( self.Message.ListPeer, - NodeIdentifier=identifier, + NodeIdentifier=identity, ) - def listPeerNotes(self, identifier): + 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). @@ -1070,7 +1095,7 @@ """ self.sendMessage( self.Message.ListPeerNotes, - NodeIdentifier=identifier + NodeIdentifier=identity ) @@ -1079,9 +1104,8 @@ @param withMetaData: include meta data for each peer? @param withVolantile: include volantile data for each peer? - @event: EventListPeers(event). @event: EvenPeer(event, peer). - @event: EventEndListPeers(event). + @event: EventEndListPeers(event, params). """ self.sendMessage( self.Message.ListPeers, @@ -1090,10 +1114,10 @@ ) - def modifyPeer(self, identifier, allowLocalAddresses=None, isDisabled=None, isListenOnly=None): + def modifyPeer(self, identity, allowLocalAddresses=None, isDisabled=None, isListenOnly=None): msg = Message( self.Message.ModifyPeer, - NodeIdentifier=identifier, + NodeIdentifier=identity, ) if allowLocalAddresses is not None: msg['AllowLocalAddresses'] = self.fcpBool(allowLocalAddresses) @@ -1105,20 +1129,20 @@ self.sendMessageEx(msg) - def modifyPeerNote(self, identifier, note): + def modifyPeerNote(self, identity, note): self.sendMessage( self.Message.ModifyPeerNote, - NodeIdentifier=identifier, + NodeIdentifier=identity, #NOTE: currently fcp supports only this one type PeerNoteType=self.PeerNoteType.Private, NoteText=note ) - def removePeer(self, identifier): + def removePeer(self, identity): self.sendMessage( self.Message.RemovePeer, - NodeIdentifier=identifier, + NodeIdentifier=identity, ) ########################################################## This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |