001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.qos.gossip;
028:
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: /**
033: * Gossip object which holds a request for Metric Keys
034: */
035: class KeyGossip extends Gossip {
036: private static class Data implements java.io.Serializable,
037: GossipPropagation {
038: int propagation_distance;
039:
040: Data(int propagation_distance) {
041: this .propagation_distance = propagation_distance;
042: }
043:
044: public int getDistance() {
045: return propagation_distance;
046: }
047:
048: public boolean equals(Object candidate) {
049: if (candidate == null || !(candidate instanceof Data))
050: return false;
051: return ((Data) candidate).propagation_distance == propagation_distance;
052: }
053:
054: }
055:
056: // clone all
057: KeyGossip cloneGossip() {
058: return cloneGossip(Integer.MAX_VALUE);
059: }
060:
061: synchronized KeyGossip cloneGossip(int max) {
062: KeyGossip result = new KeyGossip();
063: Iterator itr = iterator();
064: Map.Entry entry;
065: String key;
066: Data value;
067: int count = 0;
068: while (itr.hasNext()) {
069: if (max == count++)
070: return result;
071: entry = (Map.Entry) itr.next();
072: key = (String) entry.getKey();
073: value = (Data) entry.getValue();
074: result.addEntry(key, value); // Data is immutable [?]
075: }
076: return result;
077: }
078:
079: synchronized boolean add(String key, int propagationDistance) {
080: boolean result = true;
081: Data old = (Data) lookupValue(key);
082: if (old == null) {
083: addEntry(key, new Data(propagationDistance));
084: } else if (old.propagation_distance < propagationDistance) {
085: addEntry(key, new Data(propagationDistance));
086: } else {
087: // no changes
088: result = false;
089: }
090: return result;
091: }
092:
093: // union?
094: synchronized void add(KeyGossip gossip) {
095: Iterator itr = gossip.iterator();
096: while (itr.hasNext()) {
097: Map.Entry entry = (Map.Entry) itr.next();
098: String key = (String) entry.getKey();
099: Data value = (Data) entry.getValue();
100: add(key, value.propagation_distance);
101: }
102: }
103:
104: synchronized String prettyPrint() {
105: StringBuffer buf = new StringBuffer();
106: Iterator itr = iterator();
107: while (itr.hasNext()) {
108: Map.Entry entry = (Map.Entry) itr.next();
109: String key = (String) entry.getKey();
110: Data data = (Data) entry.getValue();
111: buf.append("\n\t");
112: buf.append(key);
113: buf.append("->");
114: buf.append(Integer.toString(data.propagation_distance));
115: }
116: return buf.toString();
117: }
118:
119: synchronized void commitChanges(KeyGossip sent) {
120: Iterator itr = sent.iterator();
121: Map.Entry entry = null;
122: String key = null;
123: GossipPropagation propagation = null;
124: GossipPropagation old_prop = null;
125: while (itr.hasNext()) {
126: entry = (Map.Entry) itr.next();
127: key = (String) entry.getKey();
128: propagation = (GossipPropagation) entry.getValue();
129: old_prop = (GossipPropagation) lookupValue(key);
130: // The reference test is intentional, because
131: // GossipPropagations are immutable.
132: if (old_prop == propagation)
133: removeEntry(key);
134: }
135: }
136:
137: }
|