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:
027: package org.cougaar.core.qos.ca;
028:
029: import java.io.Serializable;
030: import java.util.Collections;
031: import java.util.Set;
032:
033: import org.cougaar.core.mts.MessageAddress;
034: import org.cougaar.core.relay.Relay;
035: import org.cougaar.core.util.UID;
036:
037: /**
038: * Implementation of a very simple Relay.
039: * <p>
040: * Components must compare their local agent's address to the
041: * "getSource()" and "getTarget()" addresses to decide whether they
042: * are the sender or recipient.
043: */
044: abstract public class SimplestRelayImpl implements Relay, Relay.Source,
045: Relay.Target, Serializable {
046:
047: private final UID uid;
048: private final MessageAddress source;
049: private final MessageAddress target;
050:
051: private Object content;
052:
053: private transient Set _targets;
054: private transient Relay.TargetFactory _factory;
055:
056: /**
057: * Create an instance.
058: *
059: * @param uid unique object id from the UIDService
060: * @param source the local agent's address
061: * @param target the remote agent's address
062: * @param content optional initial value, which can be null
063: */
064: public SimplestRelayImpl(UID uid, MessageAddress source,
065: MessageAddress target, Object content) {
066: this .uid = uid;
067: this .source = source;
068: this .target = target;
069: this .content = content;
070: cacheTargets();
071: }
072:
073: abstract Relay.TargetFactory makeFactory(MessageAddress target);
074:
075: // SimpleRelay:
076:
077: public UID getUID() {
078: return uid;
079: }
080:
081: public void setUID(UID uid) {
082: throw new UnsupportedOperationException();
083: }
084:
085: public MessageAddress getSource() {
086: return source;
087: }
088:
089: public MessageAddress getTarget() {
090: return target;
091: }
092:
093: // Relay.Source:
094:
095: private void cacheTargets() {
096: _targets = Collections.singleton(target);
097: _factory = makeFactory(target);
098: }
099:
100: public Set getTargets() {
101: return _targets;
102: }
103:
104: public Object getContent() {
105: return content;
106: }
107:
108: public Relay.TargetFactory getTargetFactory() {
109: return _factory;
110: }
111:
112: public int updateResponse(MessageAddress target, Object response) {
113: throw new RuntimeException("Attempt to update response!");
114: }
115:
116: // Relay.Target:
117:
118: public Object getResponse() {
119: return null;
120: }
121:
122: public int updateContent(Object content, Token token) {
123: this .content = content;
124: return Relay.CONTENT_CHANGE;
125: }
126:
127: // Object:
128:
129: public boolean equals(Object o) {
130: if (o == this ) {
131: return true;
132: } else if (o instanceof SimplestRelayImpl) {
133: UID u = ((SimplestRelayImpl) o).uid;
134: return uid.equals(u);
135: } else {
136: return false;
137: }
138: }
139:
140: public int hashCode() {
141: return uid.hashCode();
142: }
143:
144: private void readObject(java.io.ObjectInputStream os)
145: throws ClassNotFoundException, java.io.IOException {
146: os.defaultReadObject();
147: cacheTargets();
148: }
149:
150: public String toString() {
151: return "(SimpleRelayImpl" + " uid=" + uid + " source=" + source
152: + " target=" + target + " content=" + content + ")";
153: }
154:
155: }
|