SF.net SVN: fclient: [348] trunk/sandbox/fcp2/iohandler.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-03-09 12:53:43
|
Revision: 348
http://fclient.svn.sourceforge.net/fclient/?rev=348&view=rev
Author: jUrner
Date: 2008-03-09 05:53:45 -0700 (Sun, 09 Mar 2008)
Log Message:
-----------
logging and python like style handling
Modified Paths:
--------------
trunk/sandbox/fcp2/iohandler.py
Modified: trunk/sandbox/fcp2/iohandler.py
===================================================================
--- trunk/sandbox/fcp2/iohandler.py 2008-03-09 12:28:33 UTC (rev 347)
+++ trunk/sandbox/fcp2/iohandler.py 2008-03-09 12:53:45 UTC (rev 348)
@@ -1,9 +1,14 @@
+"""Message io handling
+The module can be used to handle reading and writing of messages io from sockeet, file or whatever
+
+"""
import os, sys
import logging
import socket
import time
+import traceback
#--> rel import hack
@@ -211,15 +216,15 @@
raise ValueError('duration must be >= 0')
if not timeout >= 0:
raise ValueError('timeout must be >= 0')
-
- if self.isOpen():
- self.close()
timeElapsed = 0
while timeElapsed <= duration:
+ if self.isOpen():
+ self.close()
self._log.info(consts.LogMessages.Connecting + ' %r' % kwargs)
- self.io = self._ioPrototype()
+ self.io = self._ioPrototype(**kwargs)
+ 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:
@@ -243,7 +248,7 @@
"""Closes the handler"""
self._log.debug(consts.LogMessages.Closing)
self._receiveBuffer = ''
- if self.io is not None and self.io.isOpen():
+ if self.io is not None:
self.io.close()
self.io = None
@@ -269,12 +274,16 @@
raise ValueError('No bytes received and IO did not raise as expected?!?')
self._receiveBuffer += p
except IOBroken, details:
+ self._log.critical(consts.LogMessages.SocketDied)
self.close()
- self._log.critical(consts.LogMessages.SocketDied)
- return IOBroken, details
+ raise IOBroken(details)
except IOTimeout, details: # nothing in the queue
- return IOTimeout, details
- return None, None
+ raise IOTimeout(details)
+ except Exception, details:
+ self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.traceback.format_exc())
+ self.close()
+ raise Exception(details)
+
def readMessage(self):
@@ -302,10 +311,8 @@
eof += len(self.TerminatorData)
hasData = True
if eof < 0:
- exception, details = self.readBytes(self.io.BufferSize)
- if exception is not None:
- raise exception(details)
-
+ self.readBytes(self.io.BufferSize)
+
# prep message
chunk, self._receiveBuffer = self._receiveBuffer[ :eof], self._receiveBuffer[eof: ]
p = [i for i in chunk.split('\n') if i] # Fcp ignores empty lines, so do we
@@ -362,13 +369,12 @@
if len(self._receiveBuffer) >= n:
msg.data, self._receiveBuffer = self._receiveBuffer[ :n], self._receiveBuffer[n: ]
break
-
- exception, details = self.readBytes(self.io.BufferSize)
- if exception == IOTimeout: # try again later
+ try:
+ self.readBytes(self.io.BufferSize)
+ except IOTimeout, details: # try again later
self._receiveBuffer = chunk + self._receiveBuffer
- elif exception is not None:
- raise exception(details)
-
+ raise IOTimeout(details)
+
self._log.debug(consts.LogMessages.Received + msg.pprint())
return msg
@@ -394,9 +400,13 @@
try:
self.io.write(msg.toString())
except IOBroken, details:
+ self._log.critical(consts.LogMessages.SocketDied)
self.close()
- self._log.critical(consts.LogMessages.SocketDied)
raise IOBroken(details)
+ except Exception, details:
+ self._log.critical(consts.LogMessages.CaughtException + '\n' + traceback.traceback.format_exc())
+ self.close()
+ raise Exception(details)
def setDebugVerbosity(self, debugVerbosity):
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|