001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.planning.plugin.completion;
028:
029: import java.util.Collections;
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.Set;
033: import java.util.SortedSet;
034: import java.util.TreeSet;
035:
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.persist.NotPersistable;
038: import org.cougaar.core.relay.Relay;
039: import org.cougaar.core.util.SimpleUniqueObject;
040: import org.cougaar.core.util.UID;
041:
042: public class CompletionRelay extends SimpleUniqueObject implements
043: Relay.Source, Relay.Target, Relay.TargetFactory, NotPersistable {
044: transient MessageAddress sourceAddress;
045: transient Set targetAddresses;
046: // Map from target to Laggard
047: transient Map laggardsByTarget;
048: transient Laggard response;
049: transient Token token;
050: private double completionThreshold;
051: private double cpuThreshold;
052: private int persistenceCount = 0;
053: transient int oldPersistenceCount = 0;
054:
055: CompletionRelay(MessageAddress source, Set targets,
056: double completionThreshold, double cpuThreshold) {
057: this .sourceAddress = source;
058: this .completionThreshold = completionThreshold;
059: this .cpuThreshold = cpuThreshold;
060: this .targetAddresses = targets;
061: laggardsByTarget = new HashMap();
062: }
063:
064: // Application Target API
065: void setResponseLaggard(Laggard laggard) {
066: response = laggard;
067: }
068:
069: Laggard getResponseLaggard() {
070: return response;
071: }
072:
073: double getCompletionThreshold() {
074: return completionThreshold;
075: }
076:
077: double getCPUThreshold() {
078: return cpuThreshold;
079: }
080:
081: boolean persistenceNeeded() {
082: return persistenceCount != oldPersistenceCount;
083: }
084:
085: void resetPersistenceNeeded() {
086: if (response != null) {
087: oldPersistenceCount = persistenceCount;
088: }
089: }
090:
091: // Application Source API
092: SortedSet getLaggards() {
093: synchronized (laggardsByTarget) {
094: return new TreeSet(laggardsByTarget.values());
095: }
096: }
097:
098: void setPersistenceNeeded() {
099: persistenceCount++;
100: }
101:
102: void setTargets(Set newTargets) {
103: this .targetAddresses = newTargets;
104: }
105:
106: // Relay.Source implementation
107: public Set getTargets() {
108: return targetAddresses == null ? Collections.EMPTY_SET
109: : targetAddresses;
110: }
111:
112: public Object getContent() {
113: return this ;
114: }
115:
116: public TargetFactory getTargetFactory() {
117: return this ;
118: }
119:
120: public int updateResponse(MessageAddress target, Object response) {
121: if (response instanceof Laggard) {
122: synchronized (laggardsByTarget) {
123: laggardsByTarget.put(target, response);
124: }
125: return Relay.RESPONSE_CHANGE;
126: } else {
127: throw new IllegalArgumentException(
128: "Not a CompletionResponse: " + response);
129: }
130: }
131:
132: // TargetFactory implementation
133: public Relay.Target create(UID uid, MessageAddress source,
134: Object content, Token token) {
135: CompletionRelay result;
136: if (targetAddresses != null) {
137: // intra-vm case, must clone
138: result = new CompletionRelay(source, null,
139: completionThreshold, cpuThreshold);
140: result.setUID(uid);
141: } else {
142: result = this ;
143: result.sourceAddress = source;
144: }
145: result.token = token;
146: return result;
147: }
148:
149: // Target implementation
150: public MessageAddress getSource() {
151: return sourceAddress;
152: }
153:
154: public Object getResponse() {
155: return response;
156: }
157:
158: public int updateContent(Object newContent, Token token) {
159: if (newContent instanceof CompletionRelay) {
160: CompletionRelay cr = (CompletionRelay) newContent;
161: if (this .completionThreshold != cr.completionThreshold
162: || this .cpuThreshold != cr.cpuThreshold
163: || this .persistenceCount != cr.persistenceCount) {
164: this .cpuThreshold = cr.cpuThreshold;
165: this .completionThreshold = cr.completionThreshold;
166: this .persistenceCount = cr.persistenceCount;
167: return Relay.CONTENT_CHANGE;
168: }
169: return Relay.NO_CHANGE;
170: }
171: throw new IllegalArgumentException("Not a CompletionRelay: "
172: + newContent);
173: }
174: }
|