Skip to content

Commit

Permalink
Add option to discard retransmissions and option to disable debug mes…
Browse files Browse the repository at this point in the history
…sages
  • Loading branch information
ftylitak committed Mar 24, 2020
1 parent d12d8ba commit 302822b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,28 @@ customSocket = CustomSocket()
client.setCustomSocket(customSocket)
```

# Beta features under implementation or evaluation

## Discard incomming retransmission

If a received message is the same as the previously message received, it can be discarded. In that case, the poll function will not return at the time of retrieval and will continue to listen for futher incomming messages. Finally the defined responseCallback will not be called.

By default this simplistic feature is disabled. To enable:

```python
client = microcoapy.Coap()
client.discardRetransmissions = True
```

## Activate / Deactivate debug messages

By default, debug prints in microcoapy are enabled. Though, the user can deactivate the prints per Coap instance:

```python
client = microcoapy.Coap()
client.debug = False
```

# Future work

* Since this library is quite fresh, the next period will be full of testing.
Expand Down
28 changes: 19 additions & 9 deletions microcoapy/microcoapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,28 @@

import binascii

MICROCOAPY_DEBUG = True

def log(s):
if MICROCOAPY_DEBUG:
print(s)

class Coap:
TRANSMISSION_STATE = macros.enum(
STATE_IDLE = 0,
STATE_SEPARATE_ACK_RECEIVED_WAITING_DATA = 1
)

def __init__(self):
self.debug = False
self.debug = True
self.sock = None
self.callbacks = {}
self.resposeCallback = None
self.port = 0
self.isServer = False
self.state = self.TRANSMISSION_STATE.STATE_IDLE

#beta flags
self.discardRetransmissions = False
self.lastPacketStr = ""

def log(self, s):
if self.debug:
print("[microcoapy]: " + s)

# Create and initialize a new UDP socket to listen to.
# port: the local port to be used.
Expand Down Expand Up @@ -88,7 +89,7 @@ def sendPacket(self, ip, port, coapPacket):
if status > 0:
status = coapPacket.messageid

log('Packet sent. messageid: ' + str(status))
self.log('Packet sent. messageid: ' + str(status))
except Exception as e:
status = 0
print('Exception while sending packet...')
Expand Down Expand Up @@ -208,7 +209,7 @@ def loop(self, blocking=True):

packet = CoapPacket()

log("Packet bytes: " + str(binascii.hexlify(bytearray(buffer))))
self.log("Incoming Packet bytes: " + str(binascii.hexlify(bytearray(buffer))))

parsePacketHeaderInfo(buffer, packet)

Expand All @@ -218,6 +219,15 @@ def loop(self, blocking=True):
if not parsePacketOptionsAndPayload(buffer, packet):
return False

# beta functionality
if self.discardRetransmissions:
if packet.toString() == self.lastPacketStr:
self.log("Discarded retransmission message: " + packet.toString())
return False
else:
self.lastPacketStr = packet.toString()
####

if self.isServer:
self.handleIncomingRequest(packet, remoteAddress[0], remoteAddress[1])
else:
Expand Down

0 comments on commit 302822b

Please sign in to comment.