improvements on secop.client.Client

- add unregister_callback
  (this is needed for the SECoP client in nicos)
- improve error handling
- imporve error handling in AsynTcp
- also improve error handling on StringIO

Change-Id: If4f3632a93cbc0e7fbc55a966e09fcc3e69c09b7
Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/22852
Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de>
Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
2020-04-03 07:53:33 +02:00
parent 3d2333f731
commit fbc0270b15
3 changed files with 43 additions and 23 deletions

View File

@ -23,8 +23,9 @@
"""asynchronous connections
generic class for byte oriented communication
includes implementation for TCP connections
support for asynchronous communication, but may be used also for StringIO
includes implementation for TCP and Serial connections
support for asynchronous communication, but may be used also for
synchronous IO (see secop.stringio.StringIO)
"""
import socket
@ -34,7 +35,7 @@ import ast
from serial import Serial
from secop.lib import parseHostPort, tcpSocket, closeSocket
from secop.errors import ConfigError
from secop.errors import ConfigError, CommunicationFailedError
class ConnectionClosed(ConnectionError):
@ -133,7 +134,11 @@ class AsynTcp(AsynConn):
if uri.startswith('tcp://'):
# should be the case always
uri = uri[6:]
self.connection = tcpSocket(uri, self.defaultport, self.timeout)
try:
self.connection = tcpSocket(uri, self.defaultport, self.timeout)
except (ConnectionRefusedError, socket.gaierror) as e:
# indicate that retrying might make sense
raise CommunicationFailedError(str(e))
def disconnect(self):
if self.connection:
@ -215,6 +220,7 @@ class AsynSerial(AsynConn):
self.connection = Serial(dev, **options)
except ValueError as e:
raise ConfigError(e)
# TODO: turn exceptions into ConnectionFailedError, where a retry makes sense
def disconnect(self):
if self.connection: