Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed NPE due to TNode processing before CNode #564

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion broker/src/main/java/io/moquette/broker/subscriptions/CNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class CNode {

Token token;
private Token token;
private List<INode> children;
Set<Subscription> subscriptions;

Expand All @@ -35,6 +35,14 @@ private CNode(Token token, List<INode> children, Set<Subscription> subscriptions
this.children = new ArrayList<>(children);
}

public Token getToken() {
return token;
}

public void setToken(Token token) {
this.token = token;
}

boolean anyChildrenMatch(Token token) {
for (INode iNode : children) {
final CNode child = iNode.mainNode();
Expand Down Expand Up @@ -75,6 +83,7 @@ CNode copy() {
public void add(INode newINode) {
this.children.add(newINode);
}

public void remove(INode node) {
this.children.remove(node);
}
Expand Down
18 changes: 9 additions & 9 deletions broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private enum Action {

CTrie() {
final CNode mainNode = new CNode();
mainNode.token = ROOT;
mainNode.setToken(ROOT);
this.root = new INode(mainNode);
}

Expand All @@ -48,14 +48,14 @@ enum NavigationAction {
}

private NavigationAction evaluate(Topic topic, CNode cnode) {
if (Token.MULTI.equals(cnode.token)) {
if (Token.MULTI.equals(cnode.getToken())) {
return NavigationAction.MATCH;
}
if (topic.isEmpty()) {
return NavigationAction.STOP;
}
final Token token = topic.headToken();
if (!(Token.SINGLE.equals(cnode.token) || cnode.token.equals(token) || ROOT.equals(cnode.token))) {
if (!(Token.SINGLE.equals(cnode.getToken()) || cnode.getToken().equals(token) || ROOT.equals(cnode.getToken()))) {
return NavigationAction.STOP;
}
return NavigationAction.GODEEP;
Expand All @@ -67,17 +67,17 @@ public Set<Subscription> recursiveMatch(Topic topic) {

private Set<Subscription> recursiveMatch(Topic topic, INode inode) {
CNode cnode = inode.mainNode();
if (cnode instanceof TNode) {
return Collections.emptySet();
}
NavigationAction action = evaluate(topic, cnode);
if (action == NavigationAction.MATCH) {
return cnode.subscriptions;
}
if (action == NavigationAction.STOP) {
return Collections.emptySet();
}
if (cnode instanceof TNode) {
return Collections.emptySet();
}
Topic remainingTopic = (ROOT.equals(cnode.token)) ? topic : topic.exceptHeadToken();
Topic remainingTopic = (ROOT.equals(cnode.getToken())) ? topic : topic.exceptHeadToken();
Set<Subscription> subscriptions = new HashSet<>();
if (remainingTopic.isEmpty()) {
subscriptions.addAll(cnode.subscriptions);
Expand Down Expand Up @@ -134,7 +134,7 @@ private INode createPathRec(Topic topic, Subscription newSubscription) {
if (!remainingTopic.isEmpty()) {
INode inode = createPathRec(remainingTopic, newSubscription);
CNode cnode = new CNode();
cnode.token = topic.headToken();
cnode.setToken(topic.headToken());
cnode.add(inode);
return new INode(cnode);
} else {
Expand All @@ -144,7 +144,7 @@ private INode createPathRec(Topic topic, Subscription newSubscription) {

private INode createLeafNodes(Token token, Subscription newSubscription) {
CNode newLeafCnode = new CNode();
newLeafCnode.token = token;
newLeafCnode.setToken(token);
newLeafCnode.addSubscription(newSubscription);

return new INode(newLeafCnode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DumpTreeVisitor implements CTrie.IVisitor<String> {
@Override
public void visit(CNode node, int deep) {
String indentTabs = indentTabs(deep);
s += indentTabs + (node.token == null ? "''" : node.token.toString()) + prettySubscriptions(node) + "\n";
s += indentTabs + (node.getToken() == null ? "''" : node.getToken().toString()) + prettySubscriptions(node) + "\n";
}

private String prettySubscriptions(CNode node) {
Expand Down
10 changes: 10 additions & 0 deletions broker/src/main/java/io/moquette/broker/subscriptions/TNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@

class TNode extends CNode {

@Override
public Token getToken() {
throw new IllegalStateException("Can't be invoked on TNode");
}

@Override
public void setToken(Token token) {
throw new IllegalStateException("Can't be invoked on TNode");
}

@Override
INode childOf(Token token) {
throw new IllegalStateException("Can't be invoked on TNode");
Expand Down