Moved selection of subscriptions out of route2Subscribers
This commit is contained in:
@@ -461,7 +461,9 @@ public class ProtocolProcessor {
|
|||||||
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
||||||
String clientID = NettyUtils.clientID(channel);
|
String clientID = NettyUtils.clientID(channel);
|
||||||
toStoreMsg.setClientID(clientID);
|
toStoreMsg.setClientID(clientID);
|
||||||
route2Subscribers(toStoreMsg);
|
|
||||||
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
||||||
|
route2Subscribers(toStoreMsg, topicMatchingSubscriptions);
|
||||||
|
|
||||||
if (msg.isRetainFlag()) {
|
if (msg.isRetainFlag()) {
|
||||||
//QoS == 0 && retain => clean old retained
|
//QoS == 0 && retain => clean old retained
|
||||||
@@ -494,7 +496,8 @@ public class ProtocolProcessor {
|
|||||||
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
||||||
String clientID = NettyUtils.clientID(channel);
|
String clientID = NettyUtils.clientID(channel);
|
||||||
toStoreMsg.setClientID(clientID);
|
toStoreMsg.setClientID(clientID);
|
||||||
route2Subscribers(toStoreMsg);
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
||||||
|
route2Subscribers(toStoreMsg, topicMatchingSubscriptions);
|
||||||
|
|
||||||
//send PUBACK
|
//send PUBACK
|
||||||
final Integer messageID = msg.getMessageID();
|
final Integer messageID = msg.getMessageID();
|
||||||
@@ -573,7 +576,8 @@ public class ProtocolProcessor {
|
|||||||
if (qos == AbstractMessage.QOSType.EXACTLY_ONCE) { //QoS2
|
if (qos == AbstractMessage.QOSType.EXACTLY_ONCE) { //QoS2
|
||||||
guid = m_messagesStore.storePublishForFuture(toStoreMsg);
|
guid = m_messagesStore.storePublishForFuture(toStoreMsg);
|
||||||
}
|
}
|
||||||
route2Subscribers(toStoreMsg);
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
||||||
|
route2Subscribers(toStoreMsg, topicMatchingSubscriptions);
|
||||||
|
|
||||||
if (!msg.isRetainFlag()) {
|
if (!msg.isRetainFlag()) {
|
||||||
return;
|
return;
|
||||||
@@ -604,14 +608,16 @@ public class ProtocolProcessor {
|
|||||||
IMessagesStore.StoredMessage tobeStored = asStoredMessage(will);
|
IMessagesStore.StoredMessage tobeStored = asStoredMessage(will);
|
||||||
tobeStored.setClientID(clientID);
|
tobeStored.setClientID(clientID);
|
||||||
tobeStored.setMessageID(messageId);
|
tobeStored.setMessageID(messageId);
|
||||||
route2Subscribers(tobeStored);
|
String topic = tobeStored.getTopic();
|
||||||
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
||||||
|
route2Subscribers(tobeStored, topicMatchingSubscriptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flood the subscribers with the message to notify. MessageID is optional and should only used for QoS 1 and 2
|
* Flood the subscribers with the message to notify. MessageID is optional and should only used for QoS 1 and 2
|
||||||
* */
|
* */
|
||||||
void route2Subscribers(IMessagesStore.StoredMessage pubMsg) {
|
void route2Subscribers(IMessagesStore.StoredMessage pubMsg, List<Subscription> topicMatchingSubscriptions) {
|
||||||
final String topic = pubMsg.getTopic();
|
final String topic = pubMsg.getTopic();
|
||||||
final AbstractMessage.QOSType publishingQos = pubMsg.getQos();
|
final AbstractMessage.QOSType publishingQos = pubMsg.getQos();
|
||||||
final ByteBuffer origMessage = pubMsg.getMessage();
|
final ByteBuffer origMessage = pubMsg.getMessage();
|
||||||
@@ -626,7 +632,6 @@ public class ProtocolProcessor {
|
|||||||
guid = m_messagesStore.storePublishForFuture(pubMsg);
|
guid = m_messagesStore.storePublishForFuture(pubMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
|
||||||
LOG.trace("Found {} matching subscriptions to <{}>", topicMatchingSubscriptions.size(), topic);
|
LOG.trace("Found {} matching subscriptions to <{}>", topicMatchingSubscriptions.size(), topic);
|
||||||
for (final Subscription sub : topicMatchingSubscriptions) {
|
for (final Subscription sub : topicMatchingSubscriptions) {
|
||||||
AbstractMessage.QOSType qos = publishingQos;
|
AbstractMessage.QOSType qos = publishingQos;
|
||||||
@@ -745,10 +750,11 @@ public class ProtocolProcessor {
|
|||||||
LOG.debug("PUB --PUBREL--> SRV processPubRel invoked for clientID {} ad messageID {}", clientID, messageID);
|
LOG.debug("PUB --PUBREL--> SRV processPubRel invoked for clientID {} ad messageID {}", clientID, messageID);
|
||||||
ClientSession targetSession = m_sessionsStore.sessionForClient(clientID);
|
ClientSession targetSession = m_sessionsStore.sessionForClient(clientID);
|
||||||
IMessagesStore.StoredMessage evt = targetSession.storedMessage(messageID);
|
IMessagesStore.StoredMessage evt = targetSession.storedMessage(messageID);
|
||||||
route2Subscribers(evt);
|
final String topic = evt.getTopic();
|
||||||
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
||||||
|
route2Subscribers(evt, topicMatchingSubscriptions);
|
||||||
|
|
||||||
if (evt.isRetained()) {
|
if (evt.isRetained()) {
|
||||||
final String topic = evt.getTopic();
|
|
||||||
if (!evt.getMessage().hasRemaining()) {
|
if (!evt.getMessage().hasRemaining()) {
|
||||||
m_messagesStore.cleanRetained(topic);
|
m_messagesStore.cleanRetained(topic);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -452,7 +452,8 @@ public class ProtocolProcessorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForwardPublishWithCorrectQos() {
|
public void testForwardPublishWithCorrectQos() {
|
||||||
StoredMessage forwardPublish = new StoredMessage("Hello world MQTT!!".getBytes(), QOSType.EXACTLY_ONCE, "a/b");
|
String topic = "a/b";
|
||||||
|
StoredMessage forwardPublish = new StoredMessage("Hello world MQTT!!".getBytes(), QOSType.EXACTLY_ONCE, topic);
|
||||||
forwardPublish.setRetained(true);
|
forwardPublish.setRetained(true);
|
||||||
forwardPublish.setMessageID(1);
|
forwardPublish.setMessageID(1);
|
||||||
|
|
||||||
@@ -463,7 +464,7 @@ public class ProtocolProcessorTest {
|
|||||||
sessionsStore.createNewSession("Sub A", false);
|
sessionsStore.createNewSession("Sub A", false);
|
||||||
sessionsStore.createNewSession("Sub B", false);
|
sessionsStore.createNewSession("Sub B", false);
|
||||||
|
|
||||||
Subscription subQos1 = new Subscription("Sub A", "a/b", QOSType.LEAST_ONE);
|
Subscription subQos1 = new Subscription("Sub A", topic, QOSType.LEAST_ONE);
|
||||||
Subscription subQos2 = new Subscription("Sub B", "a/+", QOSType.EXACTLY_ONCE);
|
Subscription subQos2 = new Subscription("Sub B", "a/+", QOSType.EXACTLY_ONCE);
|
||||||
sessionsStore.addNewSubscription(subQos1);
|
sessionsStore.addNewSubscription(subQos1);
|
||||||
sessionsStore.addNewSubscription(subQos2);
|
sessionsStore.addNewSubscription(subQos2);
|
||||||
@@ -490,7 +491,8 @@ public class ProtocolProcessorTest {
|
|||||||
processor.connectionDescriptors.put("Sub B", new ConnectionDescriptor("Sub B", null, true));
|
processor.connectionDescriptors.put("Sub B", new ConnectionDescriptor("Sub B", null, true));
|
||||||
|
|
||||||
//Exercise
|
//Exercise
|
||||||
processor.route2Subscribers(forwardPublish);
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
||||||
|
processor.route2Subscribers(forwardPublish, topicMatchingSubscriptions);
|
||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
assertEquals(2, publishedForwarded.size());
|
assertEquals(2, publishedForwarded.size());
|
||||||
|
|||||||
Reference in New Issue
Block a user