MOved the method nextClientID from IMessagesStore to the ISessionsStore because it's session related

This commit is contained in:
andsel
2016-01-17 13:00:36 +01:00
parent 5daf1362cc
commit 4c1f36cf67
9 changed files with 73 additions and 53 deletions
@@ -34,7 +34,6 @@ public class MemoryMessagesStore implements IMessagesStore {
private Map<String, String> m_retainedStore = new HashMap<>();
private Map<String, StoredMessage> m_persistentMessageStore = new HashMap<>();
private Map<String, Map<Integer, String>> m_messageToGuids;
private Map<String, Set<Integer>> m_inflightIDs = new HashMap<>();
MemoryMessagesStore(Map<String, Map<Integer, String>> messageToGuids) {
m_messageToGuids = messageToGuids;
@@ -97,25 +96,6 @@ public class MemoryMessagesStore implements IMessagesStore {
return m_persistentMessageStore.get(guid);
}
/**
* Return the next valid packetIdentifier for the given client session.
* */
@Override
public int nextPacketID(String clientID) {
Set<Integer> inFlightForClient = m_inflightIDs.get(clientID);
if (inFlightForClient == null) {
int nextPacketId = 1;
inFlightForClient = new HashSet<>();
inFlightForClient.add(nextPacketId);
m_inflightIDs.put(clientID, inFlightForClient);
return nextPacketId;
}
int maxId = Collections.max(inFlightForClient);
int nextPacketId = (maxId + 1) % 0xFFFF;
inFlightForClient.add(nextPacketId);
return nextPacketId;
}
@Override
public void cleanRetained(String topic) {
m_retainedStore.remove(topic);
@@ -83,18 +83,18 @@ public class MapDBPersistentStoreTest {
@Test
public void testNextPacketID_notExistingClientSession() {
int packetId = m_messagesStore.nextPacketID("NOT_EXISTING_CLI");
int packetId = m_sessionsStore.nextPacketID("NOT_EXISTING_CLI");
assertEquals(1, packetId);
}
@Test
public void testNextPacketID_existingClientSession() {
//Force creation of inflight map for the CLIENT session
int packetId = m_messagesStore.nextPacketID("CLIENT");
int packetId = m_sessionsStore.nextPacketID("CLIENT");
assertEquals(1, packetId);
//request a second packetID
packetId = m_messagesStore.nextPacketID("CLIENT");
packetId = m_sessionsStore.nextPacketID("CLIENT");
assertEquals(2, packetId);
}
@@ -102,7 +102,7 @@ public class MapDBPersistentStoreTest {
public void testNextPacketID() {
//request a first ID
int packetId = m_messagesStore.nextPacketID("CLIENT");
int packetId = m_sessionsStore.nextPacketID("CLIENT");
m_sessionsStore.inFlight("CLIENT", packetId, "ABCDE"); //simulate an inflight
assertEquals(1, packetId);
@@ -110,7 +110,7 @@ public class MapDBPersistentStoreTest {
m_sessionsStore.inFlightAck("CLIENT", packetId);
//request a second packetID, counter restarts from 0
packetId = m_messagesStore.nextPacketID("CLIENT");
packetId = m_sessionsStore.nextPacketID("CLIENT");
assertEquals(1, packetId);
}
@@ -39,6 +39,7 @@ public class MemorySessionStore implements ISessionsStore {
//maps clientID->[MessageId -> guid]
private Map<String, Map<Integer, String>> m_inflightStore = new HashMap<>();
// private Map<String, Set<Integer>> m_inflightIDs = new HashMap<>();
//maps clientID->[guid*]
private Map<String, Set<String>> m_enqueuedStore = new HashMap<>();
//maps clientID->[messageID*]
@@ -169,6 +170,24 @@ public class MemorySessionStore implements ISessionsStore {
this.m_inflightStore.put(clientID, m);
}
/**
* Return the next valid packetIdentifier for the given client session.
* */
@Override
public int nextPacketID(String clientID) {
Map<Integer, String> m = this.m_inflightStore.get(clientID);
if (m == null) {
m = new HashMap<>();
int nextPacketId = 1;
m.put(nextPacketId, null);
return nextPacketId;
}
int maxId = Collections.max(m.keySet());
int nextPacketId = (maxId + 1) % 0xFFFF;
m.put(nextPacketId, null);
return nextPacketId;
}
@Override
public void bindToDeliver(String guid, String clientID) {
Set<String> guids = Utils.defaultGet(m_enqueuedStore, clientID, new HashSet<String>());