Exemplo n.º 1
0
 public NodeDescriptor getSingleSubscribedBroker(Filter filter) {
   NodeDescriptor foundBroker = null;
   // iterates over the subscribed neighbors
   Set keys = data.keySet();
   synchronized (data) {
     Iterator it = keys.iterator();
     while (it.hasNext()) {
       NodeDescriptor currentNeighbor = (NodeDescriptor) it.next();
       if (((Collection) data.get(currentNeighbor)).contains(filter)) {
         if (foundBroker != null || !currentNeighbor.isBroker()) { // more
           // than
           // one
           // broker
           // found
           // or
           // a
           // client
           // found
           return null;
         }
         foundBroker = currentNeighbor;
       }
     }
   }
   return foundBroker;
 }
Exemplo n.º 2
0
 public void addSubscription(NodeDescriptor n, Filter f) {
   List filters = (List) data.get(n);
   if (filters == null) {
     filters = new ArrayList();
     filters.add(f);
     data.put(n, filters);
   } else {
     if (!filters.contains(f)) filters.add(f);
   }
 }
Exemplo n.º 3
0
 public Collection getSubscribedNeighbors(Filter f) {
   List matchingNeighbors = new ArrayList();
   // iterates over the subscribed neighbors
   Set keys = data.keySet();
   synchronized (data) {
     Iterator it = keys.iterator();
     NodeDescriptor currentNeighbor;
     while (it.hasNext()) {
       currentNeighbor = (NodeDescriptor) it.next();
       // Check if currentNeighbor is subscribed to the message
       if (((Collection) data.get(currentNeighbor)).contains(f)) {
         matchingNeighbors.add(currentNeighbor);
       }
     }
   }
   return matchingNeighbors;
 }
Exemplo n.º 4
0
 // *** For debug purposes
 public String toString() {
   StringBuffer result = new StringBuffer(super.toString() + "\n");
   // iterates over the subscribed neighbors
   Set keys = data.keySet();
   synchronized (data) {
     Iterator it = keys.iterator();
     NodeDescriptor currentNeighbor;
     while (it.hasNext()) {
       currentNeighbor = (NodeDescriptor) it.next();
       result.append("   " + currentNeighbor + " is subscribed to:\n");
       // iterates over the set of filters for currentNeighbor
       Iterator it1 = ((Collection) data.get(currentNeighbor)).iterator();
       Filter f;
       while (it1.hasNext()) {
         f = (Filter) it1.next();
         result.append("      " + f + "\n");
       }
     }
   }
   return result.toString();
 }
Exemplo n.º 5
0
 public boolean isFilterInTable(Filter filter) {
   Collection val = data.values();
   synchronized (data) {
     Iterator it = val.iterator();
     Collection c;
     while (it.hasNext()) {
       c = (Collection) it.next();
       if (c.contains(filter)) return true;
     }
   }
   return false;
 }
Exemplo n.º 6
0
 public Collection getAllFiltersExcept(boolean duplicate, NodeDescriptor n) {
   Collection result = new ArrayList();
   Set keys = data.keySet();
   synchronized (data) {
     Iterator it = keys.iterator();
     NodeDescriptor currentNeighbor;
     while (it.hasNext()) {
       currentNeighbor = (NodeDescriptor) it.next();
       if (currentNeighbor.equals(n)) continue; // skip neighbor n
       Collection filters = (Collection) data.get(currentNeighbor);
       if (duplicate) result.addAll(filters);
       else {
         Iterator it1 = filters.iterator();
         while (it1.hasNext()) {
           Filter f = (Filter) it1.next();
           if (!result.contains(f)) result.add(f);
         }
       }
     }
   }
   return result;
 }
Exemplo n.º 7
0
 public Collection matches(Message message, NodeDescriptor senderID) {
   List matchingNeighbors = new ArrayList();
   // iterates over the subscribed neighbors
   Set keys = data.keySet();
   synchronized (data) {
     Iterator it = keys.iterator();
     NodeDescriptor currentNeighbor;
     while (it.hasNext()) {
       currentNeighbor = (NodeDescriptor) it.next();
       // Check if currentNeighbor is subscribed to the message
       // iterates over the set of filters for currentNeighbor
       if (senderID.equals(currentNeighbor)) continue;
       Iterator it1 = ((Collection) data.get(currentNeighbor)).iterator();
       while (it1.hasNext()) {
         Filter f = (Filter) it1.next();
         if (f.matches(message)) {
           matchingNeighbors.add(currentNeighbor);
           break;
         }
       }
     }
   }
   return matchingNeighbors;
 }
Exemplo n.º 8
0
 public Collection getAllFilters(NodeDescriptor n) {
   return (List) data.get(n);
 }
Exemplo n.º 9
0
 public boolean isSubscribed(NodeDescriptor n) {
   List filters = (List) data.get(n);
   return filters != null && !filters.isEmpty();
 }
Exemplo n.º 10
0
 public void clear() {
   data.clear();
 }
Exemplo n.º 11
0
 public void removeAllSubscriptions(NodeDescriptor n) {
   data.remove(n);
 }
Exemplo n.º 12
0
 public void removeSubscription(NodeDescriptor n, Filter f) {
   List filters = (List) data.get(n);
   if (filters != null) {
     filters.remove(f);
   }
 }