Skip to content

Commit 5b237ef

Browse files
committed
[broker-5] add max client id length, extract available client id chars to a property
1 parent e83b5aa commit 5b237ef

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

src/main/java/com/ss/mqtt/broker/config/MqttBrokerConfig.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ private interface ChannelFactory extends
5555

5656
@NotNull
5757
@Bean ClientIdRegistry clientIdRegistry() {
58-
return new SimpleClientIdRegistry();
58+
return new SimpleClientIdRegistry(
59+
env.getProperty(
60+
"client.id.available.chars",
61+
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
62+
),
63+
env.getProperty("client.id.max.length", int.class, 36)
64+
);
5965
}
6066

6167
@Bean

src/main/java/com/ss/mqtt/broker/service/SubscriptionService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public interface SubscriptionService {
1616
* Adds MQTT client to topic filter subscribers
1717
*
1818
* @param mqttClient MQTT client to be added
19-
* @param topicFilters topic filters
19+
* @param topicNames topic names
2020
* @return array of subscribe ack reason codes
2121
*/
2222
@NotNull Array<SubscribeAckReasonCode> subscribe(
2323
@NotNull MqttClient mqttClient,
24-
@NotNull Array<SubscribeTopicFilter> topicFilters
24+
@NotNull Array<SubscribeTopicFilter> topicNames
2525
);
2626

2727
/**

src/main/java/com/ss/mqtt/broker/service/impl/SimpleClientIdRegistry.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,21 @@
1212

1313
public class SimpleClientIdRegistry implements ClientIdRegistry {
1414

15-
private static final String AVAILABLE_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-";
16-
private static final BitSet AVAILABLE_CHAR_SET;
1715
private static final Object CLIENT_ID_VALUE = new Object();
1816

19-
static {
20-
21-
var bitSet = new BitSet();
22-
23-
for (char ch : AVAILABLE_CHARS.toCharArray()) {
24-
bitSet.set(ch, true);
25-
}
26-
27-
AVAILABLE_CHAR_SET = bitSet;
28-
}
29-
3017
private final @NotNull ConcurrentObjectDictionary<String, Object> clientIdRegistry;
18+
private final @NotNull BitSet availableCharSet;
3119

32-
public SimpleClientIdRegistry() {
20+
private final int maxClientIdLength;
21+
22+
public SimpleClientIdRegistry(@NotNull String availableChars, int maxClientIdLength) {
23+
this.maxClientIdLength = maxClientIdLength;
3324
this.clientIdRegistry = DictionaryFactory.newConcurrentStampedLockObjectDictionary();
25+
this.availableCharSet = new BitSet();
26+
27+
for (char ch : availableChars.toCharArray()) {
28+
availableCharSet.set(ch, true);
29+
}
3430
}
3531

3632
@Override
@@ -62,15 +58,18 @@ public SimpleClientIdRegistry() {
6258

6359
@Override
6460
public @NotNull Mono<Boolean> unregister(@NotNull String clientId) {
65-
var value = clientIdRegistry.getInWriteLock(clientId, ObjectDictionary::remove);
66-
return Mono.just(value != null);
61+
return Mono.just(clientIdRegistry.getInWriteLock(clientId, ObjectDictionary::remove) != null);
6762
}
6863

6964
@Override
7065
public boolean validate(@NotNull String clientId) {
7166

67+
if (clientId.length() > maxClientIdLength) {
68+
return false;
69+
}
70+
7271
for (int i = 0, length = clientId.length(); i < length; i++) {
73-
if (!AVAILABLE_CHAR_SET.get(clientId.charAt(i))) {
72+
if (!availableCharSet.get(clientId.charAt(i))) {
7473
return false;
7574
}
7675
}

src/main/java/com/ss/mqtt/broker/service/impl/SimpleSubscriptionService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public class SimpleSubscriptionService implements SubscriptionService {
2222
@Override
2323
public @NotNull Array<SubscribeAckReasonCode> subscribe(
2424
@NotNull MqttClient mqttClient,
25-
@NotNull Array<SubscribeTopicFilter> topicFilters
25+
@NotNull Array<SubscribeTopicFilter> topicNames
2626
) {
27-
return topicFilters.stream()
27+
return topicNames.stream()
2828
.map(subscribeTopicFilter -> subscriptions.addSubscription(subscribeTopicFilter, mqttClient))
2929
.collect(ArrayCollectors.toArray(SubscribeAckReasonCode.class));
3030
}

0 commit comments

Comments
 (0)