001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.lib.aggagent.test;
027:
028: import java.util.Collections;
029: import java.util.Set;
030:
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.relay.Relay;
033: import org.cougaar.core.util.UID;
034:
035: /**
036: * This is an example of a simple relay object.
037: */
038: public class TestRelay implements Relay.Source, Relay.Target {
039: private MessageAddress source;
040: private MessageAddress target;
041: private UID uid;
042:
043: private Object content;
044: private Object response;
045:
046: private transient Set _targets;
047:
048: /**
049: * @param content initial content
050: * @param response initial response
051: */
052: public TestRelay(UID uid, MessageAddress source,
053: MessageAddress target, Object content, Object response) {
054: this .uid = uid;
055: this .source = source;
056: this .target = target;
057:
058: this .content = content;
059: this .response = response;
060:
061: this ._targets = ((target != null) ? Collections
062: .singleton(target) : Collections.EMPTY_SET);
063: }
064:
065: // UniqueObject interface
066:
067: public void setUID(UID uid) {
068: throw new RuntimeException("Attempt to change UID");
069: }
070:
071: public UID getUID() {
072: return uid;
073: }
074:
075: // Source interface
076:
077: public Set getTargets() {
078: return _targets;
079: }
080:
081: public Object getContent() {
082: return content;
083: }
084:
085: private static final class SimpleRelayFactory implements
086: TargetFactory, java.io.Serializable {
087:
088: public static final SimpleRelayFactory INSTANCE = new SimpleRelayFactory();
089:
090: private SimpleRelayFactory() {
091: }
092:
093: public Relay.Target create(UID uid, MessageAddress source,
094: Object content, Token token) {
095: return new TestRelay(uid, source, null, content, null);
096: }
097:
098: private Object readResolve() {
099: return INSTANCE;
100: }
101: };
102:
103: public TargetFactory getTargetFactory() {
104: return SimpleRelayFactory.INSTANCE;
105: }
106:
107: public int updateResponse(MessageAddress t, Object response) {
108: // assert response != null
109: if (!(response.equals(this .response))) {
110: this .response = response;
111: return Relay.RESPONSE_CHANGE;
112: }
113: return Relay.NO_CHANGE;
114: }
115:
116: // Target interface
117:
118: public MessageAddress getSource() {
119: return source;
120: }
121:
122: public Object getResponse() {
123: return response;
124: }
125:
126: public int updateContent(Object content, Token token) {
127: // assert content != null
128: if (!(content.equals(this .content))) {
129: this .content = content;
130: return CONTENT_CHANGE;
131: }
132: return NO_CHANGE;
133: }
134:
135: public boolean equals(Object o) {
136: if (o == this ) {
137: return true;
138: } else if (!(o instanceof TestRelay)) {
139: return false;
140: } else {
141: UID u = ((TestRelay) o).getUID();
142: return uid.equals(u);
143: }
144: }
145:
146: public int hashCode() {
147: return uid.hashCode();
148: }
149:
150: public String toString() {
151: return "(" + uid + ", " + content + ", " + response + ")";
152: }
153:
154: private void readObject(java.io.ObjectInputStream os)
155: throws ClassNotFoundException, java.io.IOException {
156: os.defaultReadObject();
157: this._targets = ((target != null) ? Collections
158: .singleton(target) : Collections.EMPTY_SET);
159: }
160: }
|