Skip to content

Commit

Permalink
Allow the instance to be client and server. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederic-bonjour authored Feb 3, 2022
1 parent 92b52a1 commit 703f4c7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions microcoapy/microcoapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,17 @@ def handleIncomingRequest(self, requestPacket, sourceIp, sourcePort):
urlCallback = self.callbacks.get(url)

if urlCallback is None:
if self.responseCallback:
# The incoming request may be a response, let the responseCallback handle it.
return False
print('Callback for url [', url, "] not found")
self.sendResponse(sourceIp, sourcePort, requestPacket.messageid,
None, macros.COAP_RESPONSE_CODE.COAP_NOT_FOUND,
macros.COAP_CONTENT_FORMAT.COAP_NONE, requestPacket.token)

else:
urlCallback(requestPacket, sourceIp, sourcePort)
return True

def readBytesFromSocket(self, numOfBytes):
try:
Expand Down Expand Up @@ -236,9 +241,7 @@ def loop(self, blocking=True):
self.lastPacketStr = packet.toString()
####

if self.isServer:
self.handleIncomingRequest(packet, remoteAddress[0], remoteAddress[1])
else:
if not self.isServer or not self.handleIncomingRequest(packet, remoteAddress[0], remoteAddress[1]):
# To handle cases of Separate response (rfc7252 #5.2.2)
if packet.type == macros.COAP_TYPE.COAP_ACK and\
packet.method == macros.COAP_METHOD.COAP_EMPTY_MESSAGE:
Expand Down

0 comments on commit 703f4c7

Please sign in to comment.