Added purging transactions as client, don't add transaction when no_response is True

This commit is contained in:
Michiel De Witte 2018-11-21 18:09:05 +01:00
parent 17673ebced
commit 697c781833
3 changed files with 18 additions and 5 deletions

View file

@ -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"]:

View file

@ -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:

View file

@ -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]