From 4c788b91c301fa9e6b13f837805bfc46d90fbcbf Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 19:41:29 +0200 Subject: [PATCH 1/8] CoapClient - Added rolling counter and a lock for it - Count starts at 0 --- source/doap/client/client.d | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/doap/client/client.d b/source/doap/client/client.d index 9659980..c016378 100644 --- a/source/doap/client/client.d +++ b/source/doap/client/client.d @@ -45,6 +45,12 @@ public class CoapClient */ private Condition watcherSignal; + /** + * Rolling Message ID + */ + private ulong rollingMid; + private Mutex rollingLock; + /** * Creates a new CoAP client to the * provided endpoint address @@ -62,6 +68,9 @@ public class CoapClient this.requestsLock = new Mutex(); this.watcherSignal = new Condition(this.requestsLock); + this.rollingMid = 0; + this.rollingLock = new Mutex(); + init(); } From d6eefb3a9b582de6ce2d3b04984afe9d30d5fee6 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 19:43:23 +0200 Subject: [PATCH 2/8] CoapClient - Implemented `newMid()` to provide a unique new mid --- source/doap/client/client.d | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/source/doap/client/client.d b/source/doap/client/client.d index c016378..d94f26a 100644 --- a/source/doap/client/client.d +++ b/source/doap/client/client.d @@ -74,6 +74,22 @@ public class CoapClient init(); } + package ulong newMid() + { + ulong newValue; + + // Lock rolling counter + this.rollingLock.lock(); + + newValue = this.rollingMid; + this.rollingMid++; + + // Unlock rolling counter + this.rollingLock.unlock(); + + return newValue; + } + /** * Constructs a new CoAP client to the * provided endpoint address and port. From 23260860f51b4e2441cf437f75dd132bba9abac6 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 20:00:11 +0200 Subject: [PATCH 3/8] CoapClient - `doRequest(CoapRequestBuilder)` now sets the message id to the result from `newMid()` --- source/doap/client/client.d | 1 + 1 file changed, 1 insertion(+) diff --git a/source/doap/client/client.d b/source/doap/client/client.d index d94f26a..a83f62f 100644 --- a/source/doap/client/client.d +++ b/source/doap/client/client.d @@ -173,6 +173,7 @@ public class CoapClient requestPacket.setCode(requestBuilder.requestCode); requestPacket.setPayload(requestBuilder.pyld); requestPacket.setToken(requestBuilder.tkn); + requestPacket.setMessageId(newMid()); // Create the future CoapRequestFuture future = new CoapRequestFuture(); From 5b67894b325a32715c3d26374fa3d107f0ba8b40 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 20:00:54 +0200 Subject: [PATCH 4/8] CoapClient - Made the `rollingMid` a `ushort` and fixed the `newMid()` method to return that --- source/doap/client/client.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/doap/client/client.d b/source/doap/client/client.d index a83f62f..04f9e0b 100644 --- a/source/doap/client/client.d +++ b/source/doap/client/client.d @@ -48,7 +48,7 @@ public class CoapClient /** * Rolling Message ID */ - private ulong rollingMid; + private ushort rollingMid; private Mutex rollingLock; /** @@ -74,9 +74,9 @@ public class CoapClient init(); } - package ulong newMid() + package ushort newMid() { - ulong newValue; + ushort newValue; // Lock rolling counter this.rollingLock.lock(); From 8dad7795adaefe8293cf306ac8d8d7f1adf4656b Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 20:04:48 +0200 Subject: [PATCH 5/8] CoapClient - `yankRequest(ubyte[])` updated to now be `yankRequestCoapPacket)` --- source/doap/client/client.d | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/doap/client/client.d b/source/doap/client/client.d index 04f9e0b..135622d 100644 --- a/source/doap/client/client.d +++ b/source/doap/client/client.d @@ -206,17 +206,17 @@ public class CoapClient } /** - * Given a token this will try and find an active + * Given a packet this will try and find an active * request with a matching token and return it. * * This will also remove it from the requests queue. * * Params: - * token = the token + * packet = the packet received * Returns: the original `CoapRequest` if a match * is found, otherwise `null` */ - package CoapRequest yankRequest(ubyte[] token) + package CoapRequest yankRequest(CoapPacket packet) { CoapRequest foundRequest = null; @@ -224,7 +224,7 @@ public class CoapClient foreach(CoapRequest request; outgoingRequests) { - if(request.getToken() == token) + if(request.getMid() == packet.getMessageId()) { foundRequest = request; outgoingRequests.linearRemoveElement(foundRequest); From 1bc4664e3302660b1bd1b541f0f71c5bfb55c8aa Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 20:05:20 +0200 Subject: [PATCH 6/8] UDPMessaging - `handlePacket(CoapPacket)` now calls `yankRequest()` with the received and decoded `CoapPacket` --- source/doap/client/messaging/udp.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/doap/client/messaging/udp.d b/source/doap/client/messaging/udp.d index d8eafec..9417756 100644 --- a/source/doap/client/messaging/udp.d +++ b/source/doap/client/messaging/udp.d @@ -199,7 +199,7 @@ public class UDPMessaging : CoapMessagingLayer */ private void handlePacket(CoapPacket packet) { - CoapRequest request = getClient().yankRequest(packet.getToken()); + CoapRequest request = getClient().yankRequest(packet); if(request) { writeln("Matched response '"~packet.toString()~"' to request '"~request.toString()~"'"); From 6c20adfe1126586598c206a9762c49e35da95688 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 20:06:37 +0200 Subject: [PATCH 7/8] CoapRequest - Added `getMid()` --- source/doap/client/request.d | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/doap/client/request.d b/source/doap/client/request.d index 0a2da24..a1badc5 100644 --- a/source/doap/client/request.d +++ b/source/doap/client/request.d @@ -75,6 +75,17 @@ package class CoapRequest return this.requestPacket.getToken(); } + /** + * Gets the message ID from the original + * request that was made + * + * Returns: the message id + */ + public ushort getMid() + { + return this.requestPacket.getMessageId(); + } + /** * Starts the timer */ From 9578ad029fda0ede604914f59298cb2ff8acd9bd Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 25 Sep 2023 20:12:41 +0200 Subject: [PATCH 8/8] CoapClient (unit tests) - Added a test for the message IDs --- source/doap/client/client.d | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/source/doap/client/client.d b/source/doap/client/client.d index 135622d..e8fcdef 100644 --- a/source/doap/client/client.d +++ b/source/doap/client/client.d @@ -317,6 +317,48 @@ version(unittest) import std.stdio : writeln; } +/** + * Client testing + * + * Tests the rolling of the message id + */ +unittest +{ + CoapClient client = new CoapClient("coap.me", 5683); + + + CoapRequestFuture future = client.newRequestBuilder() + .payload(cast(ubyte[])"First message") + .token([69]) + .post(); + + + writeln("Future start (first)"); + CoapPacket response = future.get(); + writeln("Future done (first)"); + writeln("Got response (first): ", response); + assert(response.getMessageId() == 0); + + future = client.newRequestBuilder() + .payload(cast(ubyte[])"Second message") + .token([69]) + .post(); + + + writeln("Future start (second)"); + response = future.get(); + writeln("Future done (second)"); + writeln("Got response (second): ", response); + assert(response.getMessageId() == 1); + + + + + client.close(); +} + + + /** * Client testing *