001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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: package org.cougaar.logistics.plugin.manager;
027:
028: import java.util.Collection;
029: import java.util.Collections;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.Set;
033:
034: import org.cougaar.util.log.Logging;
035:
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.relay.Relay;
038: import org.cougaar.core.util.UID;
039:
040: /**
041: * Relay used to notify logistics community of changes in load status
042: **/
043: public class LoadIndicator extends RelayAdapter {
044: public static String NORMAL_LOAD = "Normal";
045: public static String MODERATE_LOAD = "Moderate";
046: public static String SEVERE_LOAD = "Severe";
047:
048: private String myAgentName = null;
049: private String myReportingSensorClassName = null;
050: private String myLoadStatus = null;
051: private transient String myToString = null;
052:
053: public static boolean validLoadStatus(String loadStatus) {
054: return (loadStatus.equals(NORMAL_LOAD)
055: || loadStatus.equals(MODERATE_LOAD) || loadStatus
056: .equals(SEVERE_LOAD));
057: }
058:
059: public LoadIndicator(Object reportingSensor, String agentName,
060: UID uid, String loadStatus) {
061: super ();
062: setReportingSensorClassName(reportingSensor.getClass());
063: setAgentName(agentName);
064: setUID(uid);
065: setLoadStatus(loadStatus);
066: }
067:
068: /**
069: * Gets the name of the Agent whose load status is reported.
070: *
071: * @return String Name of the agent
072: */
073: public String getAgentName() {
074: return myAgentName;
075: }
076:
077: /**
078: * Sets the name of the Agent whose load status is reported.
079: *
080: * @param agentName String name of the agent
081: */
082: public void setAgentName(String agentName) {
083: if ((myAgentName != null) && (!myAgentName.equals(""))
084: && (!myAgentName.equals(agentName))) {
085: Logging.defaultLogger().warn(
086: "Attempt to reset agent name ignored.");
087: } else {
088: myAgentName = agentName;
089: myToString = null;
090: }
091: }
092:
093: /**
094: * Gets the class name of the sensor which reports the load status
095: *
096: * @return String Class name of the reporting sensor
097: */
098: public String getReportingSensorClassName() {
099: return myReportingSensorClassName;
100: }
101:
102: /**
103: * Sets the class name of the sensor which reports the load status
104: *
105: * @param reportingSensorClassName String class name of the reporting sensor
106: */
107: public void setReportingSensorClassName(
108: String reportingSensorClassName) {
109: if ((myReportingSensorClassName != null)
110: && (!myReportingSensorClassName.equals(""))
111: && (!myReportingSensorClassName
112: .equals(reportingSensorClassName))) {
113: Logging.defaultLogger().warn(
114: "Attempt to reset reporting sensor class ignored.");
115: } else {
116: myReportingSensorClassName = reportingSensorClassName;
117: myToString = null;
118: }
119: }
120:
121: /**
122: * Sets the class name of the sensor which reports the load status
123: *
124: * @param reportingSensorClass Class of the reporting sensor
125: */
126: public void setReportingSensorClassName(Class reportingSensorClass) {
127: setReportingSensorClassName(reportingSensorClass.toString());
128: }
129:
130: /**
131: * Gets the reported load status of the agent
132: *
133: * @return String Load status of the agent
134: */
135: public String getLoadStatus() {
136: return myLoadStatus;
137: }
138:
139: /**
140: * Sets the reported load status for the agent. Should be one of the
141: * defined statics.
142: *
143: * @param loadStatus Reported load status for the agent.
144: */
145: public void setLoadStatus(String loadStatus) {
146: if (!validLoadStatus(loadStatus)) {
147: Logging.defaultLogger().warn(
148: "Attempt to set load status to an unrecognised value - "
149: + loadStatus);
150: } else {
151: myLoadStatus = loadStatus;
152: myToString = null;
153: }
154: }
155:
156: protected boolean contentChanged(RelayAdapter newLoadIndicator) {
157: LoadIndicator loadIndicator = (LoadIndicator) newLoadIndicator;
158:
159: // Only the load status should actually change
160: if (!getLoadStatus().equals(loadIndicator.getLoadStatus())) {
161: setLoadStatus(loadIndicator.getLoadStatus());
162: return true;
163: } else {
164: return (super .contentChanged(newLoadIndicator));
165: }
166: }
167:
168: public String toString() {
169: if (myToString == null) {
170: myToString = getClass() + ": agent=<" + getAgentName()
171: + ">, sensor=<" + getReportingSensorClassName()
172: + ">, load status=<" + getLoadStatus() + ">, UID=<"
173: + getUID() + ">";
174: }
175:
176: return myToString;
177: }
178:
179: }
|