broker/SubscriptionsStore: code formatter used
This commit is contained in:
@@ -13,13 +13,13 @@
|
|||||||
*
|
*
|
||||||
* You may elect to redistribute this code under either of these licenses.
|
* You may elect to redistribute this code under either of these licenses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.moquette.spi.impl.subscriptions;
|
package io.moquette.spi.impl.subscriptions;
|
||||||
|
|
||||||
import io.moquette.spi.ISessionsStore;
|
import io.moquette.spi.ISessionsStore;
|
||||||
import io.moquette.spi.ISessionsStore.ClientTopicCouple;
|
import io.moquette.spi.ISessionsStore.ClientTopicCouple;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.LinkedBlockingDeque;
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
@@ -33,6 +33,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
public class SubscriptionsStore {
|
public class SubscriptionsStore {
|
||||||
|
|
||||||
public static class NodeCouple {
|
public static class NodeCouple {
|
||||||
|
|
||||||
final TreeNode root;
|
final TreeNode root;
|
||||||
final TreeNode createdNode;
|
final TreeNode createdNode;
|
||||||
|
|
||||||
@@ -44,21 +45,27 @@ public class SubscriptionsStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the topic filter of the subscription is well formed
|
* Check if the topic filter of the subscription is well formed
|
||||||
* @param topicFilter the filter to validate
|
*
|
||||||
|
* @param topicFilter
|
||||||
|
* the filter to validate
|
||||||
* @return true if it's correct.
|
* @return true if it's correct.
|
||||||
* */
|
*/
|
||||||
public static boolean validate(String topicFilter) {
|
public static boolean validate(String topicFilter) {
|
||||||
try {
|
try {
|
||||||
parseTopic(topicFilter);
|
parseTopic(topicFilter);
|
||||||
return true;
|
return true;
|
||||||
} catch (ParseException pex) {
|
} catch (ParseException pex) {
|
||||||
LOG.warn("The topic filter is malformed. TopicFilter = {}, cause = {}, errorMessage = {}.", topicFilter,
|
LOG.warn(
|
||||||
pex.getCause(), pex.getMessage());
|
"The topic filter is malformed. TopicFilter = {}, cause = {}, errorMessage = {}.",
|
||||||
|
topicFilter,
|
||||||
|
pex.getCause(),
|
||||||
|
pex.getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IVisitor<T> {
|
public interface IVisitor<T> {
|
||||||
|
|
||||||
void visit(TreeNode node, int deep);
|
void visit(TreeNode node, int deep);
|
||||||
|
|
||||||
T getResult();
|
T getResult();
|
||||||
@@ -76,7 +83,8 @@ public class SubscriptionsStore {
|
|||||||
subScriptionsStr += indentTabs + couple.toString() + "\n";
|
subScriptionsStr += indentTabs + couple.toString() + "\n";
|
||||||
}
|
}
|
||||||
s += node.getToken() == null ? "" : node.getToken().toString();
|
s += node.getToken() == null ? "" : node.getToken().toString();
|
||||||
s += "\n" + (node.m_subscriptions.isEmpty() ? indentTabs : "") + subScriptionsStr /*+ "\n"*/;
|
s += "\n" + (node.m_subscriptions.isEmpty() ? indentTabs : "")
|
||||||
|
+ subScriptionsStr /* + "\n" */;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String indentTabs(int deep) {
|
private String indentTabs(int deep) {
|
||||||
@@ -99,9 +107,11 @@ public class SubscriptionsStore {
|
|||||||
private volatile ISessionsStore m_sessionsStore;
|
private volatile ISessionsStore m_sessionsStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the subscription tree with the list of subscriptions.
|
* Initialize the subscription tree with the list of subscriptions. Maintained for compatibility
|
||||||
* Maintained for compatibility reasons.
|
* reasons.
|
||||||
* @param sessionsStore to be used as backing store from the subscription store.
|
*
|
||||||
|
* @param sessionsStore
|
||||||
|
* to be used as backing store from the subscription store.
|
||||||
*/
|
*/
|
||||||
public void init(ISessionsStore sessionsStore) {
|
public void init(ISessionsStore sessionsStore) {
|
||||||
LOG.info("Initializing subscriptions store...");
|
LOG.info("Initializing subscriptions store...");
|
||||||
@@ -113,7 +123,10 @@ public class SubscriptionsStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (ClientTopicCouple clientTopic : subscriptions) {
|
for (ClientTopicCouple clientTopic : subscriptions) {
|
||||||
LOG.info("Re-subscribing client to topic. ClientId = {}, topicFilter = {}.", clientTopic.clientID, clientTopic.topicFilter);
|
LOG.info(
|
||||||
|
"Re-subscribing client to topic. ClientId = {}, topicFilter = {}.",
|
||||||
|
clientTopic.clientID,
|
||||||
|
clientTopic.topicFilter);
|
||||||
add(clientTopic);
|
add(clientTopic);
|
||||||
}
|
}
|
||||||
if (LOG.isTraceEnabled()) {
|
if (LOG.isTraceEnabled()) {
|
||||||
@@ -123,7 +136,8 @@ public class SubscriptionsStore {
|
|||||||
|
|
||||||
public void add(ClientTopicCouple newSubscription) {
|
public void add(ClientTopicCouple newSubscription) {
|
||||||
/*
|
/*
|
||||||
* The topic filters have already been validated at the ProtocolProcessor. We can assume they are valid.
|
* The topic filters have already been validated at the ProtocolProcessor. We can assume
|
||||||
|
* they are valid.
|
||||||
*/
|
*/
|
||||||
TreeNode oldRoot;
|
TreeNode oldRoot;
|
||||||
NodeCouple couple;
|
NodeCouple couple;
|
||||||
@@ -136,14 +150,16 @@ public class SubscriptionsStore {
|
|||||||
LOG.debug("A subscription has been added. Root = {}, oldRoot = {}.", couple.root, oldRoot);
|
LOG.debug("A subscription has been added. Root = {}, oldRoot = {}.", couple.root, oldRoot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected NodeCouple recreatePath(String topic, final TreeNode oldRoot) {
|
protected NodeCouple recreatePath(String topic, final TreeNode oldRoot) {
|
||||||
List<Token> tokens;
|
List<Token> tokens;
|
||||||
try {
|
try {
|
||||||
tokens = parseTopic(topic);
|
tokens = parseTopic(topic);
|
||||||
} catch (ParseException ex) {
|
} catch (ParseException ex) {
|
||||||
// TODO handle the parse exception
|
// TODO handle the parse exception
|
||||||
LOG.error("The topic is malformed. Topic = {}, cause = {}, errorMessage = {}.", topic, ex.getCause(),
|
LOG.error(
|
||||||
|
"The topic is malformed. Topic = {}, cause = {}, errorMessage = {}.",
|
||||||
|
topic,
|
||||||
|
ex.getCause(),
|
||||||
ex.getMessage());
|
ex.getMessage());
|
||||||
throw new IllegalArgumentException(ex.getMessage());
|
throw new IllegalArgumentException(ex.getMessage());
|
||||||
}
|
}
|
||||||
@@ -174,7 +190,8 @@ public class SubscriptionsStore {
|
|||||||
|
|
||||||
public void removeSubscription(String topic, String clientID) {
|
public void removeSubscription(String topic, String clientID) {
|
||||||
/*
|
/*
|
||||||
* The topic filters have already been validated at the ProtocolProcessor. We can assume they are valid.
|
* The topic filters have already been validated at the ProtocolProcessor. We can assume
|
||||||
|
* they are valid.
|
||||||
*/
|
*/
|
||||||
TreeNode oldRoot;
|
TreeNode oldRoot;
|
||||||
NodeCouple couple;
|
NodeCouple couple;
|
||||||
@@ -188,9 +205,11 @@ public class SubscriptionsStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visit the topics tree to remove matching subscriptions with clientID.
|
* Visit the topics tree to remove matching subscriptions with clientID. It's a mutating
|
||||||
* It's a mutating structure operation so create a new subscription tree (partial or total).
|
* structure operation so create a new subscription tree (partial or total).
|
||||||
* @param clientID the client ID to remove.
|
*
|
||||||
|
* @param clientID
|
||||||
|
* the client ID to remove.
|
||||||
*/
|
*/
|
||||||
public void removeForClient(String clientID) {
|
public void removeForClient(String clientID) {
|
||||||
TreeNode oldRoot;
|
TreeNode oldRoot;
|
||||||
@@ -202,13 +221,13 @@ public class SubscriptionsStore {
|
|||||||
} while (!subscriptions.compareAndSet(oldRoot, newRoot));
|
} while (!subscriptions.compareAndSet(oldRoot, newRoot));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a topic string return the clients subscriptions that matches it.
|
* Given a topic string return the clients subscriptions that matches it. Topic string can't
|
||||||
* Topic string can't contain character # and + because they are reserved to
|
* contain character # and + because they are reserved to listeners subscriptions, and not topic
|
||||||
* listeners subscriptions, and not topic publishing.
|
* publishing.
|
||||||
*
|
*
|
||||||
* @param topic to use fo searching matching subscriptions.
|
* @param topic
|
||||||
|
* to use fo searching matching subscriptions.
|
||||||
* @return the list of matching subscriptions, or empty if not matching.
|
* @return the list of matching subscriptions, or empty if not matching.
|
||||||
*/
|
*/
|
||||||
public List<Subscription> matches(String topic) {
|
public List<Subscription> matches(String topic) {
|
||||||
@@ -217,7 +236,11 @@ public class SubscriptionsStore {
|
|||||||
tokens = parseTopic(topic);
|
tokens = parseTopic(topic);
|
||||||
} catch (ParseException ex) {
|
} catch (ParseException ex) {
|
||||||
// TODO handle the parse exception
|
// TODO handle the parse exception
|
||||||
LOG.warn("The topic is malformed. Topic = {}, cause = {}, errorMessage = {}.", topic, ex.getCause(), ex.getMessage());
|
LOG.warn(
|
||||||
|
"The topic is malformed. Topic = {}, cause = {}, errorMessage = {}.",
|
||||||
|
topic,
|
||||||
|
ex.getCause(),
|
||||||
|
ex.getMessage());
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,8 +291,11 @@ public class SubscriptionsStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Verify if the 2 topics matching respecting the rules of MQTT Appendix A
|
* Verify if the 2 topics matching respecting the rules of MQTT Appendix A
|
||||||
* @param msgTopic the topic to match from the message
|
*
|
||||||
* @param subscriptionTopic the topic filter of the subscription
|
* @param msgTopic
|
||||||
|
* the topic to match from the message
|
||||||
|
* @param subscriptionTopic
|
||||||
|
* the topic filter of the subscription
|
||||||
* @return true if the two topics match.
|
* @return true if the two topics match.
|
||||||
*/
|
*/
|
||||||
// TODO reimplement with iterators or with queues
|
// TODO reimplement with iterators or with queues
|
||||||
@@ -304,8 +330,12 @@ public class SubscriptionsStore {
|
|||||||
return i == msgTokens.size();
|
return i == msgTokens.size();
|
||||||
} catch (ParseException ex) {
|
} catch (ParseException ex) {
|
||||||
LOG.error(
|
LOG.error(
|
||||||
"The message topic, the subscription topic or both are malformed. MsgTopic = {}, subscriptionTopic = {}, cause = {}, errorMessage = {}.",
|
"The message topic, the subscription topic or both are malformed. "
|
||||||
msgTopic, subscriptionTopic, ex.getCause(), ex.getMessage());
|
+ "MsgTopic = {}, subscriptionTopic = {}, cause = {}, errorMessage = {}.",
|
||||||
|
msgTopic,
|
||||||
|
subscriptionTopic,
|
||||||
|
ex.getCause(),
|
||||||
|
ex.getMessage());
|
||||||
throw new IllegalStateException(ex.getMessage());
|
throw new IllegalStateException(ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -330,13 +360,16 @@ public class SubscriptionsStore {
|
|||||||
String s = splitted[i];
|
String s = splitted[i];
|
||||||
if (s.isEmpty()) {
|
if (s.isEmpty()) {
|
||||||
// if (i != 0) {
|
// if (i != 0) {
|
||||||
// throw new ParseException("Bad format of topic, expetec topic name between separators", i);
|
// throw new ParseException("Bad format of topic, expetec topic name between
|
||||||
|
// separators", i);
|
||||||
// }
|
// }
|
||||||
res.add(Token.EMPTY);
|
res.add(Token.EMPTY);
|
||||||
} else if (s.equals("#")) {
|
} else if (s.equals("#")) {
|
||||||
// check that multi is the last symbol
|
// check that multi is the last symbol
|
||||||
if (i != splitted.length - 1) {
|
if (i != splitted.length - 1) {
|
||||||
throw new ParseException("Bad format of topic, the multi symbol (#) has to be the last one after a separator", i);
|
throw new ParseException(
|
||||||
|
"Bad format of topic, the multi symbol (#) has to be the last one after a separator",
|
||||||
|
i);
|
||||||
}
|
}
|
||||||
res.add(Token.MULTI);
|
res.add(Token.MULTI);
|
||||||
} else if (s.contains("#")) {
|
} else if (s.contains("#")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user