Refactored ProtocolProcessor.processSubscribe to use the ClientSession concept instead of directe access to map storage concepts

This commit is contained in:
andsel
2015-11-08 18:42:03 +01:00
parent c793ded63e
commit 9a18739a7f
7 changed files with 71 additions and 25 deletions
@@ -98,7 +98,7 @@ public class MemorySessionStore implements ISessionsStore {
}
LOG.debug("clientID {} is a newcome, creating it's empty subscriptions set", clientID);
m_persistentSubscriptions.put(clientID, new HashSet<Subscription>());
return new ClientSession(clientID, m_messagesStore);
return new ClientSession(clientID, m_messagesStore, this);
}
@Override
@@ -107,12 +107,12 @@ public class MemorySessionStore implements ISessionsStore {
return null;
}
return new ClientSession(clientID, m_messagesStore);
return new ClientSession(clientID, m_messagesStore, this);
}
@Override
public List<Subscription> listAllSubscriptions() {
List<Subscription> allSubscriptions = new ArrayList<Subscription>();
List<Subscription> allSubscriptions = new ArrayList<>();
for (Map.Entry<String, Set<Subscription>> entry : m_persistentSubscriptions.entrySet()) {
allSubscriptions.addAll(entry.getValue());
}
@@ -207,6 +207,7 @@ public class ProtocolProcessorTest {
msg.addSubscription(new SubscribeMessage.Couple(AbstractMessage.QOSType.MOST_ONE.byteValue(), FAKE_TOPIC));
m_session.setAttribute(NettyChannel.ATTR_KEY_CLIENTID, FAKE_CLIENT_ID);
m_session.setAttribute(NettyChannel.ATTR_KEY_CLEANSESSION, false);
m_sessionStore.createNewSession(FAKE_CLIENT_ID);
m_processor.processSubscribe(m_session, msg/*, FAKE_CLIENT_ID, false*/);
//Verify
@@ -221,6 +222,7 @@ public class ProtocolProcessorTest {
msg.addSubscription(new SubscribeMessage.Couple(AbstractMessage.QOSType.MOST_ONE.byteValue(), FAKE_TOPIC));
m_session.setAttribute(NettyChannel.ATTR_KEY_CLIENTID, FAKE_CLIENT_ID);
m_session.setAttribute(NettyChannel.ATTR_KEY_CLEANSESSION, false);
m_sessionStore.createNewSession(FAKE_CLIENT_ID);
assertEquals(0, subscriptions.size());
m_processor.processSubscribe(m_session, msg);
@@ -241,6 +243,7 @@ public class ProtocolProcessorTest {
msg.addSubscription(new SubscribeMessage.Couple(AbstractMessage.QOSType.MOST_ONE.byteValue(), BAD_FORMATTED_TOPIC));
m_session.setAttribute(NettyChannel.ATTR_KEY_CLIENTID, FAKE_CLIENT_ID);
m_session.setAttribute(NettyChannel.ATTR_KEY_CLEANSESSION, false);
m_sessionStore.createNewSession(FAKE_CLIENT_ID);
assertEquals(0, subscriptions.size());
//Exercise
@@ -238,7 +238,9 @@ public class SubscriptionsStoreTest {
private void assertMatch(String subscription, String topic) {
store = new SubscriptionsStore();
store.init(new MemoryStorageService());
MemoryStorageService memStore = new MemoryStorageService();
memStore.initStore();
store.init(memStore);
Subscription sub = new Subscription("FAKE_CLI_ID_1", subscription, AbstractMessage.QOSType.MOST_ONE, false);
store.add(sub);
assertFalse(store.matches(topic).isEmpty());
@@ -246,7 +248,9 @@ public class SubscriptionsStoreTest {
private void assertNotMatch(String subscription, String topic) {
store = new SubscriptionsStore();
store.init(new MemoryStorageService());
MemoryStorageService memStore = new MemoryStorageService();
memStore.initStore();
store.init(memStore);
Subscription sub = new Subscription("FAKE_CLI_ID_1", subscription, AbstractMessage.QOSType.MOST_ONE, false);
store.add(sub);
assertTrue(store.matches(topic).isEmpty());
@@ -345,7 +349,9 @@ public class SubscriptionsStoreTest {
@Test
public void removeSubscription_withDifferentClients_subscribedSameTopic() {
SubscriptionsStore aStore = new SubscriptionsStore();
aStore.init(new MemoryStorageService());
MemoryStorageService memStore = new MemoryStorageService();
memStore.initStore();
aStore.init(memStore);
//subscribe a not active clientID1 to /topic
Subscription slashSub = new Subscription("FAKE_CLI_ID_1", "/topic", AbstractMessage.QOSType.MOST_ONE, false);
aStore.add(slashSub);