From 697c7818337038adc72c09977544f2014730124f Mon Sep 17 00:00:00 2001 From: Michiel De Witte Date: Wed, 21 Nov 2018 18:09:05 +0100 Subject: [PATCH 1/4] Added purging transactions as client, don't add transaction when no_response is True --- coapthon/client/coap.py | 14 +++++++++++++- coapthon/client/helperclient.py | 3 ++- coapthon/layers/messagelayer.py | 6 +++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/coapthon/client/coap.py b/coapthon/client/coap.py index d2bed4c..b74fefa 100644 --- a/coapthon/client/coap.py +++ b/coapthon/client/coap.py @@ -66,6 +66,13 @@ class CoAP(object): self._receiver_thread = None + def purge_transactions(self, timeout_time=defines.EXCHANGE_LIFETIME): + """ + Clean old transactions + + """ + self._messageLayer.purge(timeout_time) + def close(self): """ Stop the client. @@ -97,16 +104,21 @@ class CoAP(object): assert isinstance(c, int) self._currentMID = c - def send_message(self, message): + def send_message(self, message, no_response=False): """ Prepare a message to send on the UDP socket. Eventually set retransmissions. :param message: the message to send + :param no_response: whether to await a response from the request """ if isinstance(message, Request): request = self._requestLayer.send_request(message) request = self._observeLayer.send_request(request) request = self._blockLayer.send_request(request) + if no_response: + # don't add the send message to the message layer transactions + self.send_datagram(request) + return transaction = self._messageLayer.send_request(request) self.send_datagram(transaction.request) if transaction.request.type == defines.Types["CON"]: diff --git a/coapthon/client/helperclient.py b/coapthon/client/helperclient.py index d204b68..7308473 100644 --- a/coapthon/client/helperclient.py +++ b/coapthon/client/helperclient.py @@ -223,13 +223,14 @@ class HelperClient(object): :param request: the request to send :param callback: the callback function to invoke upon response :param timeout: the timeout of the request + :param no_response: whether to await a response from the request :return: the response """ if callback is not None: thread = threading.Thread(target=self._thread_body, args=(request, callback)) thread.start() else: - self.protocol.send_message(request) + self.protocol.send_message(request, no_response=no_response) if no_response: return try: diff --git a/coapthon/layers/messagelayer.py b/coapthon/layers/messagelayer.py index f4cba32..178b480 100644 --- a/coapthon/layers/messagelayer.py +++ b/coapthon/layers/messagelayer.py @@ -48,17 +48,17 @@ class MessageLayer(object): self._current_mid %= 65535 return current_mid - def purge(self): + def purge(self, timeout_time=defines.EXCHANGE_LIFETIME): for k in list(self._transactions.keys()): now = time.time() transaction = self._transactions[k] - if transaction.timestamp + defines.EXCHANGE_LIFETIME < now: + if transaction.timestamp + timeout_time < now: logger.debug("Delete transaction") del self._transactions[k] for k in list(self._transactions_token.keys()): now = time.time() transaction = self._transactions_token[k] - if transaction.timestamp + defines.EXCHANGE_LIFETIME < now: + if transaction.timestamp + timeout_time < now: logger.debug("Delete transaction") del self._transactions_token[k] From 4aaa785e84fce120a9002f070809c52f40b523c6 Mon Sep 17 00:00:00 2001 From: Michiel De Witte Date: Wed, 12 Dec 2018 12:03:44 +0100 Subject: [PATCH 2/4] Changed send_request in helperclient to check response on message id --- coapthon/client/helperclient.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/coapthon/client/helperclient.py b/coapthon/client/helperclient.py index 7308473..4ec0aad 100644 --- a/coapthon/client/helperclient.py +++ b/coapthon/client/helperclient.py @@ -234,7 +234,13 @@ class HelperClient(object): if no_response: return try: - response = self.queue.get(block=True, timeout=timeout) + while True: + response = self.queue.get(block=True, timeout=timeout) + if response is not None: + if response.mid == request.mid: + return response + else: + return response except Empty: #if timeout is set response = None From 6b91e581f6d7d9b8db225dcc6f06c6494f17c8be Mon Sep 17 00:00:00 2001 From: Michiel De Witte Date: Wed, 12 Dec 2018 14:48:49 +0100 Subject: [PATCH 3/4] fix failing test helperclient response type NON --- coapthon/client/helperclient.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/coapthon/client/helperclient.py b/coapthon/client/helperclient.py index 4ec0aad..eb79191 100644 --- a/coapthon/client/helperclient.py +++ b/coapthon/client/helperclient.py @@ -239,6 +239,8 @@ class HelperClient(object): if response is not None: if response.mid == request.mid: return response + if response.type == defines.Types["NON"]: + return response else: return response except Empty: From 4e1c7d2bbafd088398f3ab0df42fa76615bade42 Mon Sep 17 00:00:00 2001 From: Michiel De Witte Date: Wed, 20 Feb 2019 09:53:58 +0100 Subject: [PATCH 4/4] catch UnicodeDecodeError --- coapthon/serializer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/coapthon/serializer.py b/coapthon/serializer.py index f938eeb..5a61adc 100644 --- a/coapthon/serializer.py +++ b/coapthon/serializer.py @@ -127,6 +127,9 @@ class Serializer(object): return defines.Codes.BAD_REQUEST.number except struct.error: return defines.Codes.BAD_REQUEST.number + except UnicodeDecodeError as e: + logger.debug(e) + return defines.Codes.BAD_REQUEST.number @staticmethod def serialize(message):