Modified the subscriptions storage to removed the subscriptions for a certain clientID

This commit is contained in:
selva.andre
2012-08-10 19:30:07 +02:00
parent e15e5721fb
commit 5be34ec932
4 changed files with 64 additions and 1 deletions
@@ -21,4 +21,10 @@ 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);
/**
* Remove all subscription to any topic the client (identified by clientID)
* was subscribed
*/
void removeSubscriptions(String clientID);
} }
@@ -47,4 +47,10 @@ public class SimpleMessaging implements IMessaging {
protected SubscriptionsStore getSubscriptions() { protected SubscriptionsStore getSubscriptions() {
return subscriptions; return subscriptions;
} }
public void removeSubscriptions(String clientID) {
rwLock.writeLock().lock();
subscriptions.removeForClient(clientID);
rwLock.writeLock().unlock();
}
} }
@@ -111,6 +111,25 @@ public class SubscriptionsStore {
} }
return res; return res;
} }
void removeClientSubscriptions(String clientID) {
//collect what to delete and then delete to avoid ConcurrentModification
List<Subscription> subsToRemove = new ArrayList<Subscription>();
for(Subscription s : m_subscriptions) {
if (s.clientId.equals(clientID)) {
subsToRemove.add(s);
}
}
for (Subscription s : subsToRemove) {
m_subscriptions.remove(s);
}
//go deep
for (TreeNode child : m_children) {
child.removeClientSubscriptions(clientID);
}
}
} }
protected static class Token { protected static class Token {
@@ -199,6 +218,13 @@ public class SubscriptionsStore {
current.addSubcription(newSubscription); current.addSubcription(newSubscription);
} }
/**
* Visit the topics tree to remove matching subscriptions with clientID
*/
public void removeForClient(String clientID) {
subscriptions.removeClientSubscriptions(clientID);
}
public List<Subscription> matches(String topic) { public List<Subscription> matches(String topic) {
List<Token> tokens = new ArrayList<Token>(); List<Token> tokens = new ArrayList<Token>();
try { try {
@@ -187,6 +187,31 @@ public class SubscriptionsStoreTest {
} }
@Test
public void testRemoveClientSubscriptions_existingClientID() {
String cliendID = "FAKE_CLID_1";
store.add(new Subscription(cliendID, "finance/#", AbstractMessage.QOSType.MOST_ONE));
//Exercise
store.removeForClient(cliendID);
//Verify
assertEquals(0, store.size());
}
@Test
public void testRemoveClientSubscriptions_notexistingClientID() {
String cliendID = "FAKE_CLID_1";
store.add(new Subscription(cliendID, "finance/#", AbstractMessage.QOSType.MOST_ONE));
//Exercise
store.removeForClient("FAKE_CLID_2");
//Verify
assertEquals(1, store.size());
}
private static Token[] asArray(Object... l) { private static Token[] asArray(Object... l) {
Token[] tokens = new Token[l.length]; Token[] tokens = new Token[l.length];
for (int i = 0; i < l.length; i++) { for (int i = 0; i < l.length; i++) {