Skip to content

Commit 939b10d

Browse files
committed
Use sequential ID generation for messages
Some cloud related processes are starting to make some assumptions that we generally submit messages with sequential message IDs, indicating either the order that data was collected in, or the order it perhaps should be used in. So remove any hints of random ID generation, even if custom apps might choose to use it for some reason, we will stick to sequential.
1 parent ad7cfa2 commit 939b10d

File tree

4 files changed

+4
-30
lines changed

4 files changed

+4
-30
lines changed

src/modules/comms/comms.toit

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ...devices as devices
33
import ...messages as messages
44
import ...util.docs show message-bytes-to-docs-url
55
import ...util.resilience show catch-and-restart
6-
import ...util.idgen show IdGenerator RandomIdGenerator SequentialIdGenerator
6+
import ...util.idgen show IdGenerator SequentialIdGenerator
77
import ...util.bytes show stringify-all-bytes byte-array-to-list
88
import .message-handler show MessageHandler
99
import io.reader show Reader
@@ -46,8 +46,8 @@ class Comms:
4646
// Allow passing in handlers to register on creation
4747
--handlers/List?/*<MessageHandler>*/ = []
4848
// Customizable id generator for message ids
49-
// Defaults to a random uint32 generator
50-
--idGenerator/IdGenerator = (RandomIdGenerator --lowerBound=1 --upperBound=4_294_967_295)
49+
// Defaults to a sequential generator starting at 1, max 4_294_967_295 (uint32 max)
50+
--idGenerator/IdGenerator = (SequentialIdGenerator --start=1 --maxId=4_294_967_295)
5151
--startInbound/bool = true // Start the inbound reader (polling the device on I2C for messages)
5252
--open/bool = true // Send Open message and heartbeats to keep connection alive
5353
--reinitOnStart/bool = true // Reinitialize the device on start. Clearing buffers and subscriptions. Primarily for high throughput cases.

src/protocol/message.toit

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ class Message:
4040
msg.header.data.add-data-uint8 Header.TYPE-MESSAGE-METHOD method
4141
return msg
4242

43-
with-random-msg-id -> Message:
44-
randomUint32 := ( random 4_294_967_295) +1
45-
header.data.add-data-uint32 Header.TYPE_MESSAGE_ID randomUint32
46-
return this
47-
4843
msgId -> int?:
4944
if header.data.has-data Header.TYPE_MESSAGE_ID: return header.data.get-data-uint Header.TYPE_MESSAGE_ID
5045
return null

src/util/idgen.toit

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,4 @@ class SequentialIdGenerator implements IdGenerator:
1111

1212
next -> int:
1313
nextId_ = (nextId_ + 1) % maxId_
14-
return nextId_
15-
16-
class RandomIdGenerator implements IdGenerator:
17-
lowerBound_ := 0
18-
upperBound_ := 0
19-
20-
constructor --lowerBound/int --upperBound/int:
21-
lowerBound_ = lowerBound
22-
upperBound_ = upperBound
23-
24-
next -> int:
25-
return random lowerBound_ upperBound_
14+
return nextId_

tests/util/idgen.test.toit

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import lightbug.util.idgen
22

33
main:
44
sequential
5-
random
65

76
sequential:
87
g := idgen.SequentialIdGenerator --start=1 --maxId=3
@@ -13,12 +12,3 @@ sequential:
1312
print "❌ idgen sequential Wanted $it got $i"
1413
else:
1514
print "✅ idgen sequential Got $i"
16-
17-
random:
18-
g := idgen.RandomIdGenerator --lowerBound=1 --upperBound=3
19-
for i := 0; i < 10; i++:
20-
it := g.next
21-
if it < 1 or it > 3:
22-
print "❌ idgen random Out of bounds $it"
23-
else:
24-
print "✅ idgen random Got $it (in bounds)"

0 commit comments

Comments
 (0)