Channel logging implemented
This commit is contained in:
parent
576494b200
commit
7146692712
61
__init__.py
61
__init__.py
@ -54,7 +54,8 @@ class tcp_base(object):
|
|||||||
|
|
||||||
.. note:: This class is not designed for direct usage.
|
.. note:: This class is not designed for direct usage.
|
||||||
"""
|
"""
|
||||||
LOG_PREFIX = 'TCP_IP:'
|
DEFAULT_CHANNEL_NAME = 'all_others'
|
||||||
|
|
||||||
RX_LENGTH = 0xff
|
RX_LENGTH = 0xff
|
||||||
COM_TIMEOUT = 0.5
|
COM_TIMEOUT = 0.5
|
||||||
IS_CLIENT = False
|
IS_CLIENT = False
|
||||||
@ -62,6 +63,7 @@ class tcp_base(object):
|
|||||||
def __init__(self, host, port, rx_log_lvl=logging.INFO):
|
def __init__(self, host, port, rx_log_lvl=logging.INFO):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.init_channel_name()
|
||||||
self.__socket__ = None
|
self.__socket__ = None
|
||||||
self.__data_available_callback__ = None
|
self.__data_available_callback__ = None
|
||||||
self.__supress_data_available_callback__ = False
|
self.__supress_data_available_callback__ = False
|
||||||
@ -75,6 +77,12 @@ class tcp_base(object):
|
|||||||
self.__queue__.enqueue(5, self.__receive_task__, rx_log_lvl)
|
self.__queue__.enqueue(5, self.__receive_task__, rx_log_lvl)
|
||||||
self.__queue__.run()
|
self.__queue__.run()
|
||||||
|
|
||||||
|
def init_channel_name(self, channel_name=None):
|
||||||
|
if channel_name is None:
|
||||||
|
self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + self.DEFAULT_CHANNEL_NAME)
|
||||||
|
else:
|
||||||
|
self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + channel_name)
|
||||||
|
|
||||||
def is_connected(self):
|
def is_connected(self):
|
||||||
return self.__connection__ is not None
|
return self.__connection__ is not None
|
||||||
|
|
||||||
@ -105,7 +113,7 @@ class tcp_base(object):
|
|||||||
if self.__connection__ is None:
|
if self.__connection__ is None:
|
||||||
return None
|
return None
|
||||||
if time.time() > tm + timeout:
|
if time.time() > tm + timeout:
|
||||||
logger.warning('%s TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.', self.LOG_PREFIX, repr(timeout), repr(num or 'all'), len(self.__receive_buffer__))
|
self.logger.warning('TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.', repr(timeout), repr(num or 'all'), len(self.__receive_buffer__))
|
||||||
return None
|
return None
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
if num is None:
|
if num is None:
|
||||||
@ -129,17 +137,24 @@ class tcp_base(object):
|
|||||||
|
|
||||||
This method sends data via the initiated communication channel.
|
This method sends data via the initiated communication channel.
|
||||||
"""
|
"""
|
||||||
cnt = 0
|
tm = time.time()
|
||||||
while self.__connection__ is None and cnt < int(10 * timeout):
|
while time.time() - tm < timeout:
|
||||||
time.sleep(.1) # give some time to establish the connection
|
if self.__connection__ is not None:
|
||||||
cnt += 1
|
try:
|
||||||
if self.__connection__ is not None:
|
self.__connection__.sendall(data)
|
||||||
self.__connection__.sendall(data)
|
except BlockingIOError:
|
||||||
logger.log(log_lvl, '%s TX -> "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
|
time.sleep(.1) # try again till timeout exceeds
|
||||||
return True
|
except BrokenPipeError:
|
||||||
else:
|
self.logger.exception('Exception while sending data')
|
||||||
logger.warning('%s Cound NOT send -> "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
|
self.__connection_lost__()
|
||||||
return False
|
return False
|
||||||
|
else:
|
||||||
|
self.logger.log(log_lvl, 'TX -> "%s"', stringtools.hexlify(data))
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
time.sleep(.1) # give some time to establish the connection
|
||||||
|
self.logger.warning('Cound NOT send -> "%s"', stringtools.hexlify(data))
|
||||||
|
return False
|
||||||
|
|
||||||
def register_callback(self, callback):
|
def register_callback(self, callback):
|
||||||
"""
|
"""
|
||||||
@ -184,7 +199,7 @@ class tcp_base(object):
|
|||||||
time.sleep(.05)
|
time.sleep(.05)
|
||||||
else:
|
else:
|
||||||
if len(data) > 0:
|
if len(data) > 0:
|
||||||
logger.log(rx_log_lvl, '%s RX <- "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
|
self.logger.log(rx_log_lvl, 'RX <- "%s"', stringtools.hexlify(data))
|
||||||
self.__receive_buffer__ += data
|
self.__receive_buffer__ += data
|
||||||
else:
|
else:
|
||||||
self.__connection_lost__()
|
self.__connection_lost__()
|
||||||
@ -204,12 +219,12 @@ class tcp_base(object):
|
|||||||
self.__connection__.close()
|
self.__connection__.close()
|
||||||
self.__connection__ = None
|
self.__connection__ = None
|
||||||
self.__client_address__ = None
|
self.__client_address__ = None
|
||||||
logger.info('%s Connection lost...', self.LOG_PREFIX)
|
self.logger.info('Connection lost...')
|
||||||
if self.__disconnect_callback__ is not None:
|
if self.__disconnect_callback__ is not None:
|
||||||
self.__disconnect_callback__()
|
self.__disconnect_callback__()
|
||||||
|
|
||||||
def __clean_receive_buffer__(self):
|
def __clean_receive_buffer__(self):
|
||||||
logger.debug("%s Cleaning up receive-buffer", self.LOG_PREFIX)
|
self.logger.debug("Cleaning up receive-buffer")
|
||||||
self.__receive_buffer__ = ""
|
self.__receive_buffer__ = ""
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
@ -258,7 +273,7 @@ class tcp_server(tcp_base):
|
|||||||
self.__socket__.settimeout(self.COM_TIMEOUT)
|
self.__socket__.settimeout(self.COM_TIMEOUT)
|
||||||
self.__socket__.setblocking(False)
|
self.__socket__.setblocking(False)
|
||||||
if not self.__listening_message_displayed__:
|
if not self.__listening_message_displayed__:
|
||||||
logger.info('%s Server listening to %s:%d', self.LOG_PREFIX, self.host, self.port)
|
self.logger.info('Server listening to %s:%d', self.host, self.port)
|
||||||
self.__listening_message_displayed__ = True
|
self.__listening_message_displayed__ = True
|
||||||
try:
|
try:
|
||||||
self.__connection__, self.__client_address__ = self.__socket__.accept()
|
self.__connection__, self.__client_address__ = self.__socket__.accept()
|
||||||
@ -268,7 +283,7 @@ class tcp_server(tcp_base):
|
|||||||
else:
|
else:
|
||||||
time.sleep(.05)
|
time.sleep(.05)
|
||||||
else:
|
else:
|
||||||
logger.info('%s Connection established... (from %s)', self.LOG_PREFIX, self.client_address())
|
self.logger.info('Connection established... (from %s)', self.client_address())
|
||||||
self.__clean_receive_buffer__()
|
self.__clean_receive_buffer__()
|
||||||
self.__connection__.setblocking(False)
|
self.__connection__.setblocking(False)
|
||||||
if self.__connect_callback__ is not None:
|
if self.__connect_callback__ is not None:
|
||||||
@ -313,7 +328,7 @@ class tcp_client(tcp_base):
|
|||||||
self.__connection__ = None
|
self.__connection__ = None
|
||||||
time.sleep(.05)
|
time.sleep(.05)
|
||||||
else:
|
else:
|
||||||
logger.info('%s Connection to %s:%s established', self.LOG_PREFIX, self.host, self.port)
|
self.logger.info('Connection to %s:%s established', self.host, self.port)
|
||||||
self.__clean_receive_buffer__()
|
self.__clean_receive_buffer__()
|
||||||
self.__connection__ = self.__socket__
|
self.__connection__ = self.__socket__
|
||||||
if self.__connect_callback__ is not None:
|
if self.__connect_callback__ is not None:
|
||||||
@ -346,7 +361,7 @@ class tcp_base_stp(tcp_base):
|
|||||||
self.__stp__ = stringtools.stp.stp()
|
self.__stp__ = stringtools.stp.stp()
|
||||||
|
|
||||||
def __clean_receive_buffer__(self):
|
def __clean_receive_buffer__(self):
|
||||||
logger.debug("%s Cleaning up receive-buffer", self.LOG_PREFIX)
|
self.logger.debug("Cleaning up receive-buffer")
|
||||||
self.__receive_buffer__ = []
|
self.__receive_buffer__ = []
|
||||||
|
|
||||||
def receive(self, timeout=1):
|
def receive(self, timeout=1):
|
||||||
@ -377,7 +392,7 @@ class tcp_base_stp(tcp_base):
|
|||||||
This method sends one message via the initiated communication channel.
|
This method sends one message via the initiated communication channel.
|
||||||
"""
|
"""
|
||||||
if tcp_base.send(self, stringtools.stp.build_frame(data), timeout=timeout, log_lvl=logging.DEBUG):
|
if tcp_base.send(self, stringtools.stp.build_frame(data), timeout=timeout, log_lvl=logging.DEBUG):
|
||||||
logger.log(log_lvl, '%s TX -> "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
|
self.logger.log(log_lvl, 'TX -> "%s"', stringtools.hexlify(data))
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -395,10 +410,10 @@ class tcp_base_stp(tcp_base):
|
|||||||
time.sleep(.05)
|
time.sleep(.05)
|
||||||
else:
|
else:
|
||||||
if len(data) > 0:
|
if len(data) > 0:
|
||||||
logger.debug('%s -- <- "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
|
self.logger.debug('-- <- "%s"', stringtools.hexlify(data))
|
||||||
content = self.__stp__.process(data)
|
content = self.__stp__.process(data)
|
||||||
for msg in content:
|
for msg in content:
|
||||||
logger.log(rx_log_lvl, '%s RX <- "%s"', self.LOG_PREFIX, stringtools.hexlify(msg))
|
self.logger.log(rx_log_lvl, 'RX <- "%s"', stringtools.hexlify(msg))
|
||||||
self.__receive_buffer__.append(msg)
|
self.__receive_buffer__.append(msg)
|
||||||
else:
|
else:
|
||||||
self.__connection_lost__()
|
self.__connection_lost__()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user