make baseclient work without mlzlog

The used logger can now be configured from caller. This allows
for example to use NicosLogger when imported from NICOS.

+ fix a problem when connections was lost

Change-Id: I8496ba11ad467749493751b49c4e36dd739892ab
Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/21477
Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
zolliker 2019-10-29 11:50:45 +01:00
parent 679559c1e7
commit 98f87f1306
2 changed files with 24 additions and 8 deletions

View File

@ -30,7 +30,10 @@ import threading
from collections import deque
from os import path
import mlzlog
try:
import mlzlog
except ImportError:
pass # has to be fixed in case this file is used again
from secop.protocol.interface import decode_msg, encode_msg_frame, get_msg
from secop.protocol.messages import DESCRIPTIONREQUEST, EVENTREPLY

View File

@ -30,7 +30,11 @@ import time
from collections import OrderedDict
from select import select
import mlzlog
try:
import mlzlog
except ImportError:
pass
import serial
from secop.datatypes import CommandType, EnumType, get_datatype
@ -43,20 +47,24 @@ from secop.protocol.messages import BUFFERREQUEST, COMMANDREQUEST, \
HEARTBEATREQUEST, HELPREQUEST, IDENTREQUEST, READREPLY, \
READREQUEST, REQUEST2REPLY, WRITEREPLY, WRITEREQUEST
class TCPConnection:
# disguise a TCP connection as serial one
def __init__(self, host, port):
self.log = mlzlog.getLogger('TCPConnection')
def __init__(self, host, port, getLogger=None):
if getLogger:
self.log = getLogger('TCPConnection')
else:
mlzlog.getLogger('TCPConnection')
self._host = host
self._port = int(port)
self._thread = None
self.callbacks = [] # called if SEC-node shuts down
self._io = None
self.connect()
def connect(self):
self._readbuffer = queue.Queue(100)
time.sleep(1)
io = socket.create_connection((self._host, self._port))
io.setblocking(False)
self.stopflag = False
@ -126,6 +134,8 @@ class TCPConnection:
return not self._readbuffer.empty()
def write(self, data):
if self._io is None:
self.connect()
self._io.sendall(data.encode('latin-1'))
def writeline(self, line):
@ -171,9 +181,12 @@ class Client:
stopflag = False
connection_established = False
def __init__(self, opts, autoconnect=True):
def __init__(self, opts, autoconnect=True, getLogger=None):
if 'testing' not in opts:
self.log = mlzlog.log.getChild('client', True)
if getLogger:
self.log = getLogger('client')
else:
self.log = mlzlog.getLogger('client', True)
else:
class logStub:
@ -197,7 +210,7 @@ class Client:
host = opts.pop('host', 'localhost')
port = int(opts.pop('port', 10767))
self.contactPoint = "tcp://%s:%d" % (host, port)
self.connection = TCPConnection(host, port)
self.connection = TCPConnection(host, port, getLogger=getLogger)
else:
self.contactPoint = 'testing'
self.connection = opts.pop('testing')