SF.net SVN: fclient: [358] trunk/sandbox/fcp2/iohandler.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-03-11 01:02:24
|
Revision: 358
http://fclient.svn.sourceforge.net/fclient/?rev=358&view=rev
Author: jUrner
Date: 2008-03-10 18:02:27 -0700 (Mon, 10 Mar 2008)
Log Message:
-----------
moved exceptions ti IOHandler for easier access
Modified Paths:
--------------
trunk/sandbox/fcp2/iohandler.py
Modified: trunk/sandbox/fcp2/iohandler.py
===================================================================
--- trunk/sandbox/fcp2/iohandler.py 2008-03-11 01:01:33 UTC (rev 357)
+++ trunk/sandbox/fcp2/iohandler.py 2008-03-11 01:02:27 UTC (rev 358)
@@ -32,22 +32,6 @@
#*****************************************************************************
#
#*****************************************************************************
-class MessageParseError(Exception):
- """Exception raised when a message could not be parsed succesfuly"""
-
-class IOConnectFailed(Exception):
- """Exception raised if the object can not be connected"""
-
-class IOClosed(Exception):
- """Exception raised if the object is closed"""
-
-class IOBroken(Exception):
- """Exception raised if the IO connection is broken"""
-
-class IOTimeout(Exception):
- """Exception raised when the io connection is closed"""
-
-
class IOObjectBase(object):
"""Base class for io objects
@@ -70,7 +54,7 @@
@return: always None
@raise L{IOConnectFailed}: in case something goes wrong
"""
- raise IOConnectFailed('Failed')
+ raise IOHandler.IOConnectFailed('Failed')
def read(self, n):
"""Should read n bytes from the device
@@ -79,7 +63,7 @@
@raise L{IOBroken}: in case something goes wrong
@raise L{IOTimeout}: if the read operation times out
"""
- raise IOBroken('Broken')
+ raise OHandler.IOBroken('Broken')
def write(self, bytes):
"""Should write bytes to the device
@@ -87,7 +71,7 @@
@return: always None
@raise L{IOBroken}: in case something goes wrong
"""
- raise IOBroken('Broken')
+ raise OHandler.IOBroken('Broken')
def close(self):
"""Should close the io device
@@ -95,7 +79,7 @@
@return: always None
@raise L{IOClosed}: if the device is already closed
"""
- raise IOClosed('Closed')
+ raise OHandler.IOClosed('Closed')
def isOpen(self):
"""Should check if the device is open
@@ -137,7 +121,7 @@
try:
self.socket.connect((host, port))
except socket.error, details:
- raise IOConnectFailed(details)
+ raise IOHandler.IOConnectFailed(details)
def read(self, n):
try:
@@ -145,10 +129,10 @@
if p == '':
raise socket.error('Socket closed by host')
except socket.timeout, details:
- raise IOTimeout(details)
+ raise IOHandler.IOTimeout(details)
except socket.error, details:
self.close()
- raise IOBroken(details)
+ raise IOHandler.IOBroken(details)
else:
return p
@@ -157,11 +141,11 @@
self.socket.sendall(bytes)
except socket.error, details:
self.close()
- raise IOBroken(details)
+ raise IOHandler.IOBroken(details)
def close(self):
if self.socket is None:
- raise IOClosed('Closed')
+ raise IOHandler.IOClosed('Closed')
self.socket.close()
self.socket = None
@@ -178,6 +162,23 @@
"""Handles message io
"""
+ class MessageParseError(Exception):
+ """Exception raised when a message could not be parsed succesfuly"""
+
+ class IOConnectFailed(Exception):
+ """Exception raised if the object can not be connected"""
+
+ class IOClosed(Exception):
+ """Exception raised if the object is closed"""
+
+ class IOBroken(Exception):
+ """Exception raised if the IO connection is broken"""
+
+ class IOTimeout(Exception):
+ """Exception raised when the io connection is closed"""
+
+
+
TerminatorEndMessage = '\nEndMessage\n'
TerminatorData = '\nData\n'
@@ -227,8 +228,8 @@
yield False # have to yield at least once to make unittests work (io has to be created!!)
try:
self.io.connect(**kwargs)
- except IOConnectFailed, details:
- self._log.info(consts.LogMessages.ConnectingFailed + ' %s %s' % (IOConnectFailed, details))
+ except self.IOConnectFailed, details:
+ self._log.info(consts.LogMessages.ConnectingFailed + ' %s %s' % (self.IOConnectFailed, details))
yield False
else:
self.io.setTimeout(self.io.Timeout)
@@ -273,14 +274,14 @@
if not p:
raise ValueError('No bytes received and IO did not raise as expected?!?')
self._receiveBuffer += p
- except IOBroken, details:
+ except self.IOBroken, details:
self._log.critical(consts.LogMessages.SocketDied)
self.close()
- raise IOBroken(details)
- except IOTimeout, details: # nothing in the queue
- raise IOTimeout(details)
+ raise self.IOBroken(details)
+ except self.IOTimeout, details: # nothing in the queue
+ raise self.IOTimeout(details)
except Exception, details:
- self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.traceback.format_exc())
+ self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.format_exc())
self.close()
raise Exception(details)
@@ -317,7 +318,7 @@
p = [i for i in chunk.split('\n') if i] # Fcp ignores empty lines, so do we
p.pop()
if not p:
- raise MessageParseError('No message name present')
+ raise self.MessageParseError('No message name present')
msgName = p.pop(0)
msgClass = message.MessagesAll.get(msgName, None)
if msgClass is None:
@@ -328,7 +329,7 @@
# process param --> value fields
params = dict([line.split('=', 1) for line in p])
if not msg._restoreParams(params):
- raise MessageParseError('Invalid message parameters')
+ raise self.MessageParseError('Invalid message parameters')
# get associated data if necessary
if mayHaveData:
@@ -351,19 +352,8 @@
return msg
- def sendMessage(self, name, data=None, **params):
+ def sendMessage(self, msg):
"""Sends a message to freenet
- @param name: name of the message to send
- @param data: data to atatch to the message
- @param params: {para-name: param-calue, ...} of parameters to pass along
- with the message (see freenet protocol)
- @raise L{IOBroken}: if the connection to the io dies unexpectedly
- """
- return self.sendMessageEx(message.Message(name, data=data, **params))
-
-
- def sendMessageEx(self, msg):
- """Sends a message to freenet
@param msg: (Message) message to send
@return: Message
@raise L{IOBroken}: if the connection to the io dies unexpectedly
@@ -371,12 +361,12 @@
self._log.debug(consts.LogMessages.Sending + msg.pprint())
try:
self.io.write(msg.toString())
- except IOBroken, details:
+ except self.IOBroken, details:
self._log.critical(consts.LogMessages.SocketDied)
self.close()
- raise IOBroken(details)
+ raise self.IOBroken(details)
except Exception, details:
- self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.traceback.format_exc())
+ self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.format_exc())
self.close()
raise Exception(details)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|