diff --git a/__init__.py b/__init__.py index 6dff810..2d42a70 100644 --- a/__init__.py +++ b/__init__.py @@ -145,7 +145,7 @@ class continues_statistic(dict): :rtype: :class:`helpers.continues_statistic` or None """ if self.quantifier == 0: - return None + return continues_statistic() else: rv = continues_statistic(self.mean, self.min, self.max, self.quantifier) self.__init_data__(None, None, None, 0) @@ -211,7 +211,7 @@ class continues_statistic_multivalue(dict): """ if key is None: if len(self) == 0: - return None + return continues_statistic_multivalue() else: rv = continues_statistic_multivalue(**self) self.clear() @@ -231,6 +231,7 @@ class direct_socket_base(object): This is the base class for other classes in this module. """ DEFAULT_CHANNEL_NAME = 'all_others' + IS_CLIENT = False def __init__(self, max_len=None, virtual_rate_bps=None): self.__max_length__ = max_len @@ -259,7 +260,7 @@ class direct_socket_base(object): def __clean_buffer__(self): self.__rx_buffer__ = b'' - self.logger.info('%s Cleaning RX-Buffer...', self.__log_prefix__()) + self.logger.debug('%s Cleaning up receive-buffer', self.__log_prefix__()) def __connect__(self, remote_socket): if self.__remote_socket__ is None: @@ -271,16 +272,16 @@ class direct_socket_base(object): remote_socket.__connect__(self) def __log_prefix__(self): - return 'Client:' if self.IS_CLIENT else 'Server:' + return 'comm-client:' if self.IS_CLIENT else 'comm-server:' def __rx__(self, data): self.__rx_buffer__ += data - self.logger.debug('%s RX <- %s', self.__log_prefix__(), stringtools.hexlify(data)) + self.logger.info('%s RX <- %s', self.__log_prefix__(), stringtools.hexlify(data)) if self.__data_callback__ is not None: self.__data_callback__(self) def __tx__(self, q, data): - self.logger.debug('%s TX -> %s', self.__log_prefix__(), stringtools.hexlify(data)) + self.logger.info('%s TX -> %s', self.__log_prefix__(), stringtools.hexlify(data)) if self.__rate_bps__ is not None: time.sleep(len(data) / self.__rate_bps__) self.__remote_socket__.__rx__(data) @@ -334,11 +335,11 @@ class direct_socket_base(object): i += 1 time.sleep(.1) if len(self.__rx_buffer__) < (num or 1): - return b'' + return self.__rx_buffer__[0:0] else: if num is None: rv = self.__rx_buffer__ - self.__rx_buffer__ = b'' + self.__rx_buffer__ = rv[0:0] else: rv = self.__rx_buffer__[:num] self.__rx_buffer__ = self.__rx_buffer__[num:] @@ -380,8 +381,6 @@ class direct_socket_base(object): :type data: bytes :param timeout: The timeout for sending data (e.g. time to establish new connection). :type timeout: float - :param log_lvl: The log level to log outgoing TX-data - :type log_lvl: int :return: True if data had been sent, otherwise False. :rtype: bool """ @@ -397,6 +396,44 @@ class direct_socket_base(object): return True +class direct_socket_stp_base(direct_socket_base): + IS_CLIENT = False + + def __init__(self, *args, **kwargs): + direct_socket_base.__init__(self, *args, **kwargs) + self.__stp_rx__ = stringtools.stp.stp() + + def __chunks__(self, data): + return direct_socket_base.__chunks__(self, stringtools.stp.build_frame(data)) + + def __clean_buffer__(self): + self.__rx_buffer__ = [] + self.logger.debug('%s Cleaning up receive-buffer', self.__log_prefix__()) + + def __rx__(self, data): + self.logger.debug('%s RX <- %s', self.__log_prefix__(), stringtools.hexlify(data)) + msg = self.__stp_rx__.process(data) + if len(msg) > 0: + self.__rx_buffer__.extend(msg) + if len(self.__rx_buffer__) > 0: + if self.__data_callback__ is not None: + self.__data_callback__(self) + + def receive(self, timeout=1): + """ + This method returns one received messages via the initiated communication channel. + + :param timeout: The timeout for receiving data (at least after the timeout the method returns data or None). + :type timeout: float + :return: The received data. + :rtype: bytes + """ + try: + return direct_socket_base.receive(self, timeout=timeout, num=1)[0] + except TypeError: + return None + + class direct_socket_client(direct_socket_base): """ Class to create a direct client socket. See also parent :class:`helpers.direct_socket_base`. @@ -451,6 +488,38 @@ class direct_socket_server(direct_socket_base): self.logger.info('%s Waiting for incomming connection', self.__log_prefix__()) +class direct_socket_stp_client(direct_socket_stp_base): + IS_CLIENT = True + + def connect(self, remote_socket): + """ + Method to create a connection between this client and a :class:`helpers.direct_socket_server` instance. + + :param remote_socket: The remote socket to connect to. + :type remote_socket: :class:`helpers.direct_socket_server` + """ + self.__connect__(remote_socket) + + def reconnect(self): + """ + Method to do a reconnect. + + .. note:: The :const:`remote_socket` of the prefious :func:`connect` call will be used. + """ + if self.__last_remote_socket__ is not None and self.__remote_socket__ is None: + self.connect(self.__last_remote_socket__) + return True + return False + + +class direct_socket_stp_server(direct_socket_stp_base): + IS_CLIENT = False + + def __init__(self, *args, **kwargs): + direct_socket_stp_base.__init__(self, *args, **kwargs) + self.logger.info('%s Waiting for incomming connection', self.__log_prefix__()) + + class ringbuffer(list): """ Class for a list with a limited number of elements. diff --git a/_docs_/genindex.html b/_docs_/genindex.html index 110779d..cab3a4f 100644 --- a/_docs_/genindex.html +++ b/_docs_/genindex.html @@ -173,7 +173,11 @@
|