From 563f3d1db9949fabb83f08419e98d1bf8c9c92da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=85=E6=A2=A6?= <1101766085@qq.com> Date: Wed, 19 Jan 2022 17:29:46 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/mqtt/core/common/TopicFilterType.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/common/TopicFilterType.java b/mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/common/TopicFilterType.java index 607dbf3..2c03fda 100644 --- a/mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/common/TopicFilterType.java +++ b/mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/common/TopicFilterType.java @@ -55,14 +55,8 @@ public enum TopicFilterType { SHARE { @Override public boolean match(String topicFilter, String topicName) { - String shareTopicFilter = topicFilter.substring(TopicFilterType.SHARE_GROUP_PREFIX.length()); - String[] group = shareTopicFilter.split("/"); - String groupName = group[0]; - String shareTopicPrefix = TopicFilterType.SHARE_GROUP_PREFIX + groupName + '/'; - int prefixLen = shareTopicPrefix.length(); - if (topicName.startsWith("/")) { - prefixLen = prefixLen - 1; - } + // 去除前缀 $share// + int prefixLen = TopicFilterType.findShareTopicIndex(topicFilter, topicName.startsWith("/")); return TopicUtil.match(topicFilter.substring(prefixLen), topicName); } }; @@ -98,4 +92,16 @@ public enum TopicFilterType { } } + private static int findShareTopicIndex(String topicFilter, boolean startDelimiter) { + int prefixLength = TopicFilterType.SHARE_GROUP_PREFIX.length(); + int topicFilterLength = topicFilter.length(); + for (int i = prefixLength; i < topicFilterLength; i++) { + char ch = topicFilter.charAt(i); + if ('/' == ch) { + return startDelimiter ? i : i + 1; + } + } + throw new IllegalArgumentException("Share subscription topicFilter: " + topicFilter + " not conform to the $share//xxx"); + } + } -- GitLab