Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COAP-18. Cancelling all threads and coap state machine when close is called. #9

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions coap/coap.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self,ipAddress='',udpPort=d.DEFAULT_UDP_PORT,testing=False):
self.transmitters = {}
self.ackTimeout = d.DFLT_ACK_TIMEOUT
self.respTimeout = d.DFLT_RESPONSE_TIMEOUT
self.coapTrans = None
if testing:
self.socketUdp = socketUdpDispatcher(
ipAddress = self.ipAddress,
Expand All @@ -55,6 +56,8 @@ def __init__(self,ipAddress='',udpPort=d.DEFAULT_UDP_PORT,testing=False):

def close(self):
self.socketUdp.close()
if self.coapTrans:
self.coapTrans.close()

#===== client

Expand Down Expand Up @@ -121,7 +124,7 @@ def _transmit(self,uri,confirmable,code,options=[],payload=[]):
self._cleanupTransmitter()
messageId = self._getMessageID(destIp,destPort)
token = self._getToken(destIp,destPort)
newTransmitter = coapTransmitter.coapTransmitter(
self.coapTrans = coapTransmitter.coapTransmitter(
sendFunc = self.socketUdp.sendUdp,
srcIp = self.ipAddress,
srcPort = self.udpPort,
Expand All @@ -138,9 +141,9 @@ def _transmit(self,uri,confirmable,code,options=[],payload=[]):
)
key = (destIp,destPort,token,messageId)
assert key not in self.transmitters.keys()
self.transmitters[key] = newTransmitter
self.transmitters[key] = self.coapTrans

return newTransmitter.transmit()
return self.coapTrans.transmit()

def _getMessageID(self,destIp,destPort):
'''
Expand Down
6 changes: 6 additions & 0 deletions coap/coapTransmitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ def receiveMessage(self, timestamp, srcIp, srcPort, message):
# signal reception
self.rxMsgEvent.set()

def close(self):
self.coapError = e.coapException("Request cancelled")
self.fsmGoOn = False
self.rxMsgEvent.set()
#======================= private ==========================================

#===== fsm
Expand Down Expand Up @@ -339,6 +343,8 @@ def _action_WAITFORACK(self):
while True:
waitTimeLeft = startTime+ackMaxWait-time.time()
if self.rxMsgEvent.wait(timeout=waitTimeLeft):
if not self.fsmGoOn:
return
# I got message
with self.dataLock:
(timestamp,srcIp,srcPort,message) = self.LastRxPacket
Expand Down
12 changes: 6 additions & 6 deletions coap/socketUdpReal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def emit(self, record):

import socket
import time

import select
import socketUdp
import threading

Expand Down Expand Up @@ -59,16 +59,16 @@ def sendUdp(self,destIp,destPort,msg):
def close(self):
# declare that this thread has to stop
self.goOn = False

# send some dummy value into the socket to trigger a read
self.socket_handler.sendto( 'stop', ('::1',self.udpPort) )

self.socket_handler.close()
#======================== private =========================================

def run(self):
while self.goOn:
try:
# blocking wait for something from UDP socket
# Blocking wait for something from UDP socket.
# A simple recvfrom() call does not returns when the socket is closed
# so we are using select() for detecting this condition.
i,o,e = select.select([self.socket_handler],[],[])
raw,conn = self.socket_handler.recvfrom(self.BUFSIZE)
except socket.error as err:
log.critical("socket error: {0}".format(err))
Expand Down