Fixed the matching of topic with multi selector

This commit is contained in:
selva.andre
2012-08-07 23:38:32 +02:00
parent b9d4356487
commit 372af61076
@@ -2,8 +2,9 @@ package org.dna.mqtt.moquette.messaging.spi.impl;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -68,22 +69,33 @@ public class SubscriptionsStore {
} }
/** void matches(Queue<Token> tokens, List<Subscription> matchingSubs) {
* Return next matching node on the tree path, or null if not found any Token t = tokens.poll();
* match. if (t == null) {
*/ matchingSubs.addAll(m_subscriptions);
TreeNode matchNext(Token token) { return;
//quite similar to childWithToken }
//TODO this should take care of # and + management
//check the matching of the current token
//TODO take care of MULTI and SINGLE
List<TreeNode> matchingChildren = new ArrayList<TreeNode>();
for (TreeNode child : m_children) { for (TreeNode child : m_children) {
if (child.getToken().equals(token)) { if (child.getToken().match(t)) {
return child; matchingChildren.add(child);
} }
} }
return null; if (matchingChildren.isEmpty()) {
return;
}
for (TreeNode child : matchingChildren) {
child.matches(tokens, matchingSubs);
}
} }
/** /**
* Return the number of registered subscriptions * Return the number of registered subscriptions
*/ */
@@ -112,6 +124,26 @@ public class SubscriptionsStore {
return name; return name;
} }
/**
* Return true iff the current token is compatible with t.
*
* TODO define the rules of compatibility
* if this is a MULTI or SINGLE and the other is any string => match
* of this is a normal token and also the other, and they are equals => match
* if the other is not normal token => don't match
*/
protected boolean match(Token t) {
if (t == MULTI || t == SINGLE) {
return false;
}
if (this == MULTI || this == SINGLE) {
return true;
} else {
return equals(t);
}
}
@Override @Override
public int hashCode() { public int hashCode() {
int hash = 7; int hash = 7;
@@ -147,6 +179,7 @@ public class SubscriptionsStore {
Logger.getLogger(SubscriptionsStore.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(SubscriptionsStore.class.getName()).log(Level.SEVERE, null, ex);
} }
//Deep into the tree creating missing nodes where necessary
TreeNode current = subscriptionsRoot; TreeNode current = subscriptionsRoot;
for (Token token : tokens) { for (Token token : tokens) {
TreeNode matchingChildren; TreeNode matchingChildren;
@@ -158,6 +191,7 @@ public class SubscriptionsStore {
//create a new node for the newly inserted token //create a new node for the newly inserted token
matchingChildren = new TreeNode(current); matchingChildren = new TreeNode(current);
matchingChildren.setToken(token); matchingChildren.setToken(token);
current.addChild(matchingChildren);
current = matchingChildren; current = matchingChildren;
} }
} }
@@ -172,18 +206,21 @@ public class SubscriptionsStore {
//TODO handle the parse exception //TODO handle the parse exception
Logger.getLogger(SubscriptionsStore.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(SubscriptionsStore.class.getName()).log(Level.SEVERE, null, ex);
} }
TreeNode current = subscriptionsRoot; // TreeNode current = subscriptionsRoot;
for (Token token : tokens) { List<Subscription> matchingSubscriptions = new ArrayList<Subscription>();
current = current.matchNext(token); subscriptionsRoot.matches(new LinkedBlockingQueue<Token>(tokens), matchingSubscriptions);
if (current == null) { return matchingSubscriptions;
break; // for (Token token : tokens) {
} // current = current.matchNext(token);
} // if (current == null) {
if (current != null) { // break;
return current.subscriptions(); // }
} else { // }
return Collections.EMPTY_LIST; // if (current != null) {
} // return current.subscriptions();
// } else {
// return Collections.EMPTY_LIST;
// }
} }
public boolean contains(Subscription sub) { public boolean contains(Subscription sub) {