forked from moquette-io/moquette
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement topic alias management (moquette-io#873)
Implemented handling of topic alias received by publishers. Doesn't implement the alias remapping in forwarded publishes. Updates CONNACK to set topic alias maximum property if user explicitly configures topic_alias_maximum configuration setting. Adds a topic alias to topic name cache to MQTTConnection, named TopicAliasMapping. Updates PUBLISH processing to handled the incoming topic alias, checking the various error condition (respect to the specification), resolve the alias to topic name and forward processing of publish message with topic name and without topic alias. Added unit tests to MQTTConnection to cover the feature.
- Loading branch information
Showing
11 changed files
with
537 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
broker/src/main/java/io/moquette/broker/TopicAliasMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* | ||
* Copyright (c) 2012-2024 The original author or authors | ||
* ------------------------------------------------------ | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* The Apache License v2.0 is available at | ||
* http://www.opensource.org/licenses/apache2.0.php | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
*/ | ||
|
||
package io.moquette.broker; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Implements a mapping cache so that the binding key -> value is also reversed, | ||
* it maintains the invariant to be a 1:1 mapping. | ||
* */ | ||
class TopicAliasMapping { | ||
|
||
private final Map<String, Integer> direct = new HashMap<>(); | ||
private final Map<Integer, String> reversed = new HashMap<>(); | ||
|
||
void update(String topicName, int topicAlias) { | ||
Integer previousAlias = direct.put(topicName, topicAlias); | ||
if (previousAlias != null) { | ||
reversed.remove(previousAlias); | ||
} | ||
reversed.put(topicAlias, topicName); | ||
} | ||
|
||
Optional<String> topicFromAlias(int topicAlias) { | ||
return Optional.ofNullable(reversed.get(topicAlias)); | ||
} | ||
|
||
int size() { | ||
return reversed.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.