SF.net SVN: fclient: [316] trunk/sandbox/fcp2/message.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <ju...@us...> - 2008-03-06 12:05:18
|
Revision: 316
http://fclient.svn.sourceforge.net/fclient/?rev=316&view=rev
Author: jurner
Date: 2008-03-06 04:05:25 -0800 (Thu, 06 Mar 2008)
Log Message:
-----------
reead, write methods have gone. Use iohandler
Modified Paths:
--------------
trunk/sandbox/fcp2/message.py
Modified: trunk/sandbox/fcp2/message.py
===================================================================
--- trunk/sandbox/fcp2/message.py 2008-03-06 12:01:06 UTC (rev 315)
+++ trunk/sandbox/fcp2/message.py 2008-03-06 12:05:25 UTC (rev 316)
@@ -60,105 +60,6 @@
self.params = params
- @classmethod
- def bytesFromSocket(clss, socketObj, n):
- """Reads n bytes from socket
- @param socketObj: socket to read bytes from
- @param n: (int) number of bytes to read
- @return: (tuple) (error-message, bytes-read). If no error was encountered, error-message will be None
- """
- error = p = None
- try:
- p = socketObj.recv(n)
- if not p:
- p = None
- raise socket.error('Socket shut down by node')
- except socket.timeout, d: # nothing in the queue
- error = clss(consts.Message.ClientSocketTimeout)
- except socket.error, d:
- error = clss(consts.Message.ClientSocketDied, Exception=socket.error, Details=d)
- return error, p
-
-
- @classmethod
- def fromSocket(clss, socketObj):
- """Reads a message from a socket
- @param socketObj: socket to read a message from
- @return: (Message) next message from the socket. If the socket dies
- unexpectedly a L{consts.Message.ClientSocketDied} message is returned containing the parameters
- 'Exception' and 'Details'. If the socket times out a L{consts.Message.ClientSocketTimeout} message is returned.
-
- @note: SocketObj can be any object that supports a sockets recv() and sendall() methods
- and raises the appropriate socket errors
- """
-
- msg = clss(None)
- buf = []
- paramTypes = None
-
- #TODO: to buffer or not to buffer?
- while True:
-
- # get next line from socket
- error, p = clss.bytesFromSocket(socketObj, 1)
- if error:
- return error
-
- if p != '\n':
- buf.append(p)
- continue
- #TODO: check if '\r\n' is allowed in freenet client protocol
- else:
- if buf and buf[-1] == '\r':
- del buf[-1]
-
- line = ''.join(buf)
- buf = []
-
-
- #NOTE: messages carying data may end with 'EndMessage' or 'Data'.
- # This is a bit messed up in Fcp. We assume here all messages from the
- # node end with Data if data is passed. Alternative would be to check for both
- # and rely on the 'DataLength' member to indicate if data is included. This
- # should work for all messages except 'DataFound'
- if line == 'EndMessage':
- break
-
- # first line == message name
- if msg.name is None:
- msg.name = line
- paramTypes = types.MessageParamTypes.get(line, None)
-
-
- # get data member
- elif line == 'Data':
- remaining = int(msg.params['DataLength'])
- msg.data = ''
- while remaining > 0:
- error, p = clss.bytesFromSocket(socketObj, remaining)
- if error:
- return error
- remaining -= len(p)
- msg.data += p
- break
-
- # get next paramater
- else:
- head, sep, tail = line.partition('=')
-
- # covert fcp to python value if necessary
- if paramTypes is not None:
- paramType = paramTypes.get(head, None)
- if paramType is not None:
- tail = paramType.fcpToPython(tail)
-
- msg.params[head] = tail
- # TODO: errorchek params?
- #if not sep: pass
-
- return msg
-
-
def get(self, name, default=None):
"""Returns the message parameter 'name' or 'default' """
return self.params.get(name, default)
@@ -199,13 +100,6 @@
return '\n'.join(out)
- def send(self, socketObj):
- """Dumps the message to a socket
- @param socketObj: socket to dump the message to
- """
- socketObj.sendall(self.toString())
-
-
def toString(self):
"""Returns the message as formated string ready to be send"""
out = [self.name, ]
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|