Added cleanSession flag to Subscription class
This commit is contained in:
@@ -28,7 +28,7 @@ public interface IMessaging {
|
|||||||
/**
|
/**
|
||||||
* Subscribe a client to a specified topic with a defined level
|
* Subscribe a client to a specified topic with a defined level
|
||||||
*/
|
*/
|
||||||
void subscribe(String clientId, String topic, QOSType qos);
|
void subscribe(String clientId, String topic, QOSType qos, boolean cleanSession);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all subscription to any topic the client (identified by clientID)
|
* Remove all subscription to any topic the client (identified by clientID)
|
||||||
|
|||||||
@@ -128,8 +128,8 @@ public class SimpleMessaging implements IMessaging, Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void subscribe(String clientId, String topic, QOSType qos) {
|
public void subscribe(String clientId, String topic, QOSType qos, boolean cleanSession) {
|
||||||
Subscription newSubscription = new Subscription(clientId, topic, qos);
|
Subscription newSubscription = new Subscription(clientId, topic, qos, cleanSession);
|
||||||
try {
|
try {
|
||||||
LOG.debug("subscribe invoked for topic: " + topic);
|
LOG.debug("subscribe invoked for topic: " + topic);
|
||||||
m_inboundQueue.put(new SubscribeEvent(newSubscription));
|
m_inboundQueue.put(new SubscribeEvent(newSubscription));
|
||||||
|
|||||||
@@ -15,11 +15,13 @@ public class Subscription implements Serializable {
|
|||||||
QOSType requestedQos;
|
QOSType requestedQos;
|
||||||
String clientId;
|
String clientId;
|
||||||
String topic;
|
String topic;
|
||||||
|
boolean cleanSession;
|
||||||
|
|
||||||
public Subscription(String clientId, String topic, QOSType requestedQos) {
|
public Subscription(String clientId, String topic, QOSType requestedQos, boolean cleanSession) {
|
||||||
this.requestedQos = requestedQos;
|
this.requestedQos = requestedQos;
|
||||||
this.clientId = clientId;
|
this.clientId = clientId;
|
||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
|
this.cleanSession = cleanSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getClientId() {
|
public String getClientId() {
|
||||||
@@ -34,6 +36,10 @@ public class Subscription implements Serializable {
|
|||||||
return topic;
|
return topic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isCleanSession() {
|
||||||
|
return this.cleanSession;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
|
|||||||
@@ -147,8 +147,10 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
protected void handleSubscribe(IoSession session, SubscribeMessage msg) {
|
protected void handleSubscribe(IoSession session, SubscribeMessage msg) {
|
||||||
LOG.debug("handleSubscribe, registering the subscriptions");
|
LOG.debug("handleSubscribe, registering the subscriptions");
|
||||||
for (SubscribeMessage.Couple req : msg.subscriptions()) {
|
for (SubscribeMessage.Couple req : msg.subscriptions()) {
|
||||||
m_messaging.subscribe((String) session.getAttribute(Constants.ATTR_CLIENTID),
|
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
||||||
req.getTopic(), AbstractMessage.QOSType.values()[req.getQos()]);
|
boolean cleanSession = (Boolean) session.getAttribute(Constants.CLEAN_SESSION);
|
||||||
|
m_messaging.subscribe(clientID, req.getTopic(), AbstractMessage.QOSType.values()[req.getQos()],
|
||||||
|
cleanSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
//ack the client
|
//ack the client
|
||||||
|
|||||||
+5
-5
@@ -28,30 +28,30 @@ public class SimpleMessagingTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSubscribe() {
|
public void testSubscribe() {
|
||||||
//Exercise
|
//Exercise
|
||||||
SubscribeEvent evt = new SubscribeEvent(new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE));
|
SubscribeEvent evt = new SubscribeEvent(new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE, false));
|
||||||
messaging.processSubscribe(evt);
|
messaging.processSubscribe(evt);
|
||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
Subscription expectedSubscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
|
Subscription expectedSubscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE, false);
|
||||||
assertTrue(messaging.getSubscriptions().contains(expectedSubscription));
|
assertTrue(messaging.getSubscriptions().contains(expectedSubscription));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDoubleSubscribe() {
|
public void testDoubleSubscribe() {
|
||||||
SubscribeEvent evt = new SubscribeEvent(new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE));
|
SubscribeEvent evt = new SubscribeEvent(new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE, false));
|
||||||
messaging.processSubscribe(evt);
|
messaging.processSubscribe(evt);
|
||||||
|
|
||||||
//Exercise
|
//Exercise
|
||||||
messaging.processSubscribe(evt);
|
messaging.processSubscribe(evt);
|
||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
Subscription subscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
|
Subscription subscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE, false);
|
||||||
assertEquals(1, messaging.getSubscriptions().size());
|
assertEquals(1, messaging.getSubscriptions().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPublish() throws InterruptedException {
|
public void testPublish() throws InterruptedException {
|
||||||
SubscribeEvent evt = new SubscribeEvent(new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE));
|
SubscribeEvent evt = new SubscribeEvent(new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE, false));
|
||||||
messaging.processSubscribe(evt);
|
messaging.processSubscribe(evt);
|
||||||
// INotifier notifier = mock(INotifier.class);
|
// INotifier notifier = mock(INotifier.class);
|
||||||
// messaging.setNotifier(notifier);
|
// messaging.setNotifier(notifier);
|
||||||
|
|||||||
+18
-18
@@ -105,11 +105,11 @@ public class SubscriptionsStoreTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchSimple() {
|
public void testMatchSimple() {
|
||||||
Subscription slashSub = new Subscription("FAKE_CLI_ID_1", "/", AbstractMessage.QOSType.MOST_ONE);
|
Subscription slashSub = new Subscription("FAKE_CLI_ID_1", "/", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(slashSub);
|
store.add(slashSub);
|
||||||
assertTrue(store.matches("finance").isEmpty());
|
assertTrue(store.matches("finance").isEmpty());
|
||||||
|
|
||||||
Subscription slashFinanceSub = new Subscription("FAKE_CLI_ID_1", "/finance", AbstractMessage.QOSType.MOST_ONE);
|
Subscription slashFinanceSub = new Subscription("FAKE_CLI_ID_1", "/finance", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(slashFinanceSub);
|
store.add(slashFinanceSub);
|
||||||
assertTrue(store.matches("finance").isEmpty());
|
assertTrue(store.matches("finance").isEmpty());
|
||||||
|
|
||||||
@@ -119,19 +119,19 @@ public class SubscriptionsStoreTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchSimpleMulti() {
|
public void testMatchSimpleMulti() {
|
||||||
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "#", AbstractMessage.QOSType.MOST_ONE);
|
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(anySub);
|
store.add(anySub);
|
||||||
assertTrue(store.matches("finance").contains(anySub));
|
assertTrue(store.matches("finance").contains(anySub));
|
||||||
|
|
||||||
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE);
|
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(financeAnySub);
|
store.add(financeAnySub);
|
||||||
assertTrue(store.matches("finance").containsAll(Arrays.asList(financeAnySub, anySub)));
|
assertTrue(store.matches("finance").containsAll(Arrays.asList(financeAnySub, anySub)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchingDeepMulti_one_layer() {
|
public void testMatchingDeepMulti_one_layer() {
|
||||||
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "#", AbstractMessage.QOSType.MOST_ONE);
|
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE);
|
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(anySub);
|
store.add(anySub);
|
||||||
store.add(financeAnySub);
|
store.add(financeAnySub);
|
||||||
|
|
||||||
@@ -143,7 +143,7 @@ public class SubscriptionsStoreTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchingDeepMulti_two_layer() {
|
public void testMatchingDeepMulti_two_layer() {
|
||||||
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/stock/#", AbstractMessage.QOSType.MOST_ONE);
|
Subscription financeAnySub = new Subscription("FAKE_CLI_ID_1", "finance/stock/#", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(financeAnySub);
|
store.add(financeAnySub);
|
||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
@@ -152,18 +152,18 @@ public class SubscriptionsStoreTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchSimpleSingle() {
|
public void testMatchSimpleSingle() {
|
||||||
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "+", AbstractMessage.QOSType.MOST_ONE);
|
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(anySub);
|
store.add(anySub);
|
||||||
assertTrue(store.matches("finance").contains(anySub));
|
assertTrue(store.matches("finance").contains(anySub));
|
||||||
|
|
||||||
Subscription financeOne = new Subscription("FAKE_CLI_ID_1", "finance/+", AbstractMessage.QOSType.MOST_ONE);
|
Subscription financeOne = new Subscription("FAKE_CLI_ID_1", "finance/+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(financeOne);
|
store.add(financeOne);
|
||||||
assertTrue(store.matches("finance/stock").contains(financeOne));
|
assertTrue(store.matches("finance/stock").contains(financeOne));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchManySingle() {
|
public void testMatchManySingle() {
|
||||||
Subscription manySub = new Subscription("FAKE_CLI_ID_1", "+/+", AbstractMessage.QOSType.MOST_ONE);
|
Subscription manySub = new Subscription("FAKE_CLI_ID_1", "+/+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(manySub);
|
store.add(manySub);
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
@@ -173,9 +173,9 @@ public class SubscriptionsStoreTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchSlashSingle() {
|
public void testMatchSlashSingle() {
|
||||||
Subscription slashPlusSub = new Subscription("FAKE_CLI_ID_1", "/+", AbstractMessage.QOSType.MOST_ONE);
|
Subscription slashPlusSub = new Subscription("FAKE_CLI_ID_1", "/+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(slashPlusSub);
|
store.add(slashPlusSub);
|
||||||
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "+", AbstractMessage.QOSType.MOST_ONE);
|
Subscription anySub = new Subscription("FAKE_CLI_ID_1", "+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(anySub);
|
store.add(anySub);
|
||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
@@ -187,10 +187,10 @@ public class SubscriptionsStoreTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchManyDeepSingle() {
|
public void testMatchManyDeepSingle() {
|
||||||
Subscription slashPlusSub = new Subscription("FAKE_CLI_ID_1", "/finance/+/ibm", AbstractMessage.QOSType.MOST_ONE);
|
Subscription slashPlusSub = new Subscription("FAKE_CLI_ID_1", "/finance/+/ibm", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(slashPlusSub);
|
store.add(slashPlusSub);
|
||||||
|
|
||||||
Subscription slashPlusDeepSub = new Subscription("FAKE_CLI_ID_1", "/+/stock/+", AbstractMessage.QOSType.MOST_ONE);
|
Subscription slashPlusDeepSub = new Subscription("FAKE_CLI_ID_1", "/+/stock/+", AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
store.add(slashPlusDeepSub);
|
store.add(slashPlusDeepSub);
|
||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
@@ -199,7 +199,7 @@ public class SubscriptionsStoreTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchSimpleMulti_allTheTree() {
|
public void testMatchSimpleMulti_allTheTree() {
|
||||||
store.add(new Subscription("FAKE_CLI_ID_1", "#", AbstractMessage.QOSType.MOST_ONE));
|
store.add(new Subscription("FAKE_CLI_ID_1", "#", AbstractMessage.QOSType.MOST_ONE, false));
|
||||||
assertFalse(store.matches("finance").isEmpty());
|
assertFalse(store.matches("finance").isEmpty());
|
||||||
assertFalse(store.matches("finance/ibm").isEmpty());
|
assertFalse(store.matches("finance/ibm").isEmpty());
|
||||||
}
|
}
|
||||||
@@ -207,7 +207,7 @@ public class SubscriptionsStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testMatchSimpleMulti_zeroLevel() {
|
public void testMatchSimpleMulti_zeroLevel() {
|
||||||
//check MULTI in case of zero level match
|
//check MULTI in case of zero level match
|
||||||
store.add(new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE));
|
store.add(new Subscription("FAKE_CLI_ID_1", "finance/#", AbstractMessage.QOSType.MOST_ONE, false));
|
||||||
assertFalse(store.matches("finance").isEmpty());
|
assertFalse(store.matches("finance").isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +215,7 @@ public class SubscriptionsStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRemoveClientSubscriptions_existingClientID() {
|
public void testRemoveClientSubscriptions_existingClientID() {
|
||||||
String cliendID = "FAKE_CLID_1";
|
String cliendID = "FAKE_CLID_1";
|
||||||
store.add(new Subscription(cliendID, "finance/#", AbstractMessage.QOSType.MOST_ONE));
|
store.add(new Subscription(cliendID, "finance/#", AbstractMessage.QOSType.MOST_ONE, false));
|
||||||
|
|
||||||
//Exercise
|
//Exercise
|
||||||
store.removeForClient(cliendID);
|
store.removeForClient(cliendID);
|
||||||
@@ -227,7 +227,7 @@ public class SubscriptionsStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRemoveClientSubscriptions_notexistingClientID() {
|
public void testRemoveClientSubscriptions_notexistingClientID() {
|
||||||
String cliendID = "FAKE_CLID_1";
|
String cliendID = "FAKE_CLID_1";
|
||||||
store.add(new Subscription(cliendID, "finance/#", AbstractMessage.QOSType.MOST_ONE));
|
store.add(new Subscription(cliendID, "finance/#", AbstractMessage.QOSType.MOST_ONE, false));
|
||||||
|
|
||||||
//Exercise
|
//Exercise
|
||||||
store.removeForClient("FAKE_CLID_2");
|
store.removeForClient("FAKE_CLID_2");
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ public class MQTTHandlerTest {
|
|||||||
IMessaging mockedMessaging = mock(IMessaging.class);
|
IMessaging mockedMessaging = mock(IMessaging.class);
|
||||||
|
|
||||||
m_session.setAttribute(Constants.ATTR_CLIENTID, "fakeID");
|
m_session.setAttribute(Constants.ATTR_CLIENTID, "fakeID");
|
||||||
|
m_session.setAttribute(Constants.CLEAN_SESSION, false);
|
||||||
|
|
||||||
//Exercise
|
//Exercise
|
||||||
m_handler.setMessaging(mockedMessaging);
|
m_handler.setMessaging(mockedMessaging);
|
||||||
@@ -108,7 +108,7 @@ public class MQTTHandlerTest {
|
|||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
ArgumentCaptor<AbstractMessage.QOSType> argument = ArgumentCaptor.forClass(AbstractMessage.QOSType.class);
|
ArgumentCaptor<AbstractMessage.QOSType> argument = ArgumentCaptor.forClass(AbstractMessage.QOSType.class);
|
||||||
verify(mockedMessaging).subscribe(eq("fakeID"), eq(topicName), argument.capture());
|
verify(mockedMessaging).subscribe(eq("fakeID"), eq(topicName), argument.capture(), eq(false));
|
||||||
assertEquals(AbstractMessage.QOSType.EXACTLY_ONCE, argument.getValue());
|
assertEquals(AbstractMessage.QOSType.EXACTLY_ONCE, argument.getValue());
|
||||||
//TODO verify the message ACK and corresponding QoS!!
|
//TODO verify the message ACK and corresponding QoS!!
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user