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:
parent
679559c1e7
commit
98f87f1306
@ -30,7 +30,10 @@ import threading
|
|||||||
from collections import deque
|
from collections import deque
|
||||||
from os import path
|
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.interface import decode_msg, encode_msg_frame, get_msg
|
||||||
from secop.protocol.messages import DESCRIPTIONREQUEST, EVENTREPLY
|
from secop.protocol.messages import DESCRIPTIONREQUEST, EVENTREPLY
|
||||||
|
@ -30,7 +30,11 @@ import time
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from select import select
|
from select import select
|
||||||
|
|
||||||
import mlzlog
|
try:
|
||||||
|
import mlzlog
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
import serial
|
import serial
|
||||||
|
|
||||||
from secop.datatypes import CommandType, EnumType, get_datatype
|
from secop.datatypes import CommandType, EnumType, get_datatype
|
||||||
@ -43,20 +47,24 @@ from secop.protocol.messages import BUFFERREQUEST, COMMANDREQUEST, \
|
|||||||
HEARTBEATREQUEST, HELPREQUEST, IDENTREQUEST, READREPLY, \
|
HEARTBEATREQUEST, HELPREQUEST, IDENTREQUEST, READREPLY, \
|
||||||
READREQUEST, REQUEST2REPLY, WRITEREPLY, WRITEREQUEST
|
READREQUEST, REQUEST2REPLY, WRITEREPLY, WRITEREQUEST
|
||||||
|
|
||||||
|
|
||||||
class TCPConnection:
|
class TCPConnection:
|
||||||
# disguise a TCP connection as serial one
|
# disguise a TCP connection as serial one
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port, getLogger=None):
|
||||||
self.log = mlzlog.getLogger('TCPConnection')
|
if getLogger:
|
||||||
|
self.log = getLogger('TCPConnection')
|
||||||
|
else:
|
||||||
|
mlzlog.getLogger('TCPConnection')
|
||||||
self._host = host
|
self._host = host
|
||||||
self._port = int(port)
|
self._port = int(port)
|
||||||
self._thread = None
|
self._thread = None
|
||||||
self.callbacks = [] # called if SEC-node shuts down
|
self.callbacks = [] # called if SEC-node shuts down
|
||||||
|
self._io = None
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self._readbuffer = queue.Queue(100)
|
self._readbuffer = queue.Queue(100)
|
||||||
|
time.sleep(1)
|
||||||
io = socket.create_connection((self._host, self._port))
|
io = socket.create_connection((self._host, self._port))
|
||||||
io.setblocking(False)
|
io.setblocking(False)
|
||||||
self.stopflag = False
|
self.stopflag = False
|
||||||
@ -126,6 +134,8 @@ class TCPConnection:
|
|||||||
return not self._readbuffer.empty()
|
return not self._readbuffer.empty()
|
||||||
|
|
||||||
def write(self, data):
|
def write(self, data):
|
||||||
|
if self._io is None:
|
||||||
|
self.connect()
|
||||||
self._io.sendall(data.encode('latin-1'))
|
self._io.sendall(data.encode('latin-1'))
|
||||||
|
|
||||||
def writeline(self, line):
|
def writeline(self, line):
|
||||||
@ -171,9 +181,12 @@ class Client:
|
|||||||
stopflag = False
|
stopflag = False
|
||||||
connection_established = False
|
connection_established = False
|
||||||
|
|
||||||
def __init__(self, opts, autoconnect=True):
|
def __init__(self, opts, autoconnect=True, getLogger=None):
|
||||||
if 'testing' not in opts:
|
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:
|
else:
|
||||||
class logStub:
|
class logStub:
|
||||||
|
|
||||||
@ -197,7 +210,7 @@ class Client:
|
|||||||
host = opts.pop('host', 'localhost')
|
host = opts.pop('host', 'localhost')
|
||||||
port = int(opts.pop('port', 10767))
|
port = int(opts.pop('port', 10767))
|
||||||
self.contactPoint = "tcp://%s:%d" % (host, port)
|
self.contactPoint = "tcp://%s:%d" % (host, port)
|
||||||
self.connection = TCPConnection(host, port)
|
self.connection = TCPConnection(host, port, getLogger=getLogger)
|
||||||
else:
|
else:
|
||||||
self.contactPoint = 'testing'
|
self.contactPoint = 'testing'
|
||||||
self.connection = opts.pop('testing')
|
self.connection = opts.pop('testing')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user