From 06a26298d241b3fe8b1e17d29ec4084c5b26ed83 Mon Sep 17 00:00:00 2001 From: Hylke van der Schaaf Date: Tue, 6 Aug 2024 13:13:00 +0200 Subject: [PATCH] Fixed unsubscribe messages reducing counters when not subscribed --- CHANGELOG.md | 1 + .../frostserver/mqtt/subscription/SubscriptionEvent.java | 5 +++++ .../ilt/frostserver/mqtt/moquette/MoquetteMqttServer.java | 8 +++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf93a37a..61039cb0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Fixed #1948: Sanitise user input in error output. * Fixed MultiDatastream not picking up ID type of Thing and Sensor. * Fixed Tomcat Docker images already having UID/GID 1000, FROST now uses 1001. +* Fixed unsubscribe messages reducing counters when not subscribed. ## Release version 2.3.2 diff --git a/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/subscription/SubscriptionEvent.java b/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/subscription/SubscriptionEvent.java index 73e3820bb..9a43e53aa 100644 --- a/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/subscription/SubscriptionEvent.java +++ b/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/subscription/SubscriptionEvent.java @@ -37,4 +37,9 @@ public void setTopic(String topic) { this.topic = topic; } + @Override + public String toString() { + return getTopic(); + } + } diff --git a/FROST-Server.MQTT.Moquette/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/moquette/MoquetteMqttServer.java b/FROST-Server.MQTT.Moquette/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/moquette/MoquetteMqttServer.java index 8b3a6e8af..d6c958123 100644 --- a/FROST-Server.MQTT.Moquette/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/moquette/MoquetteMqttServer.java +++ b/FROST-Server.MQTT.Moquette/src/main/java/de/fraunhofer/iosb/ilt/frostserver/mqtt/moquette/MoquetteMqttServer.java @@ -288,7 +288,7 @@ public void onSubscribe(InterceptSubscribeMessage msg) { final String topicFilter = msg.getTopicFilter(); LOGGER.trace(" Client {} subscribed to {}", clientId, topicFilter); clientSubscriptions - .getOrDefault(clientId, new ArrayList<>()) + .computeIfAbsent(clientId, t -> new ArrayList<>()) .add(topicFilter); fireSubscribe(new SubscriptionEvent(topicFilter)); } @@ -301,9 +301,11 @@ public void onUnsubscribe(InterceptUnsubscribeMessage msg) { } final String topicFilter = msg.getTopicFilter(); LOGGER.trace(" Client {} unsubscribed from {}", clientId, topicFilter); - clientSubscriptions.getOrDefault(clientId, new ArrayList<>()) + boolean removed = clientSubscriptions.getOrDefault(clientId, new ArrayList<>()) .remove(topicFilter); - fireUnsubscribe(new SubscriptionEvent(topicFilter)); + if (removed) { + fireUnsubscribe(new SubscriptionEvent(topicFilter)); + } } @Override