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.blackboard;
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.*;
035: import org.cougaar.core.util.UID;
036:
037: /**
038: * Implementation of a relay class that contains a BlackboardAlert
039: * object as its content.
040: * <p>
041: * Components must compare their local agent's address to the
042: * "getSource()" and "getTarget()" addresses to decide whether they
043: * are the sender or recipient.
044: */
045: public class BlackboardAlertRelay implements SimpleRelay, Relay.Source,
046: Relay.Target, Serializable {
047:
048: private final UID uid;
049: private final MessageAddress source;
050: private final MessageAddress target;
051:
052: private BlackboardAlert query;
053: private Object reply;
054:
055: private transient Set _targets;
056: private transient Relay.TargetFactory _factory;
057:
058: /**
059: * Create an instance.
060: *
061: * @param uid unique object id from the UIDService
062: * @param source the local agent's address
063: * @param target the remote agent's address
064: * @param query alert object.
065: */
066: public BlackboardAlertRelay(UID uid, MessageAddress source,
067: MessageAddress target, BlackboardAlert query) {
068: this .uid = uid;
069: this .source = source;
070: this .target = target;
071: this .query = query;
072: cacheTargets();
073: }
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: public Object getQuery() {
094: return query;
095: }
096:
097: public void setQuery(Object query) {
098: this .query = (BlackboardAlert) query;
099: }
100:
101: public Object getReply() {
102: return reply;
103: }
104:
105: public void setReply(Object reply) {
106: this .reply = reply;
107: }
108:
109: // Relay.Source:
110:
111: private void cacheTargets() {
112: _targets = Collections.singleton(target);
113: _factory = new BlackboardAlertRelayFactory(target);
114: }
115:
116: public Set getTargets() {
117: return _targets;
118: }
119:
120: public Object getContent() {
121: return query;
122: }
123:
124: public Relay.TargetFactory getTargetFactory() {
125: return _factory;
126: }
127:
128: public int updateResponse(MessageAddress target, Object response) {
129: this .reply = response;
130: return Relay.RESPONSE_CHANGE;
131: }
132:
133: // Relay.Target:
134:
135: public Object getResponse() {
136: return reply;
137: }
138:
139: public int updateContent(Object content, Token token) {
140: // assert content != null
141: this .query = (BlackboardAlert) content;
142: return Relay.CONTENT_CHANGE;
143: }
144:
145: // Object:
146:
147: public boolean equals(Object o) {
148: if (o == this ) {
149: return true;
150: } else if (o instanceof BlackboardAlertRelay) {
151: UID u = ((BlackboardAlertRelay) o).uid;
152: return uid.equals(u);
153: } else {
154: return false;
155: }
156: }
157:
158: public int hashCode() {
159: return uid.hashCode();
160: }
161:
162: private void readObject(java.io.ObjectInputStream os)
163: throws ClassNotFoundException, java.io.IOException {
164: os.defaultReadObject();
165: cacheTargets();
166: }
167:
168: public String toString() {
169: return "(BlackboardAlertRelay" + " uid=" + uid + " source="
170: + source + " target=" + target + " query=" + query
171: + " reply=" + reply + ")";
172: }
173:
174: // factory method:
175:
176: private static class BlackboardAlertRelayFactory implements
177: Relay.TargetFactory, Serializable {
178: private final MessageAddress target;
179:
180: public BlackboardAlertRelayFactory(MessageAddress target) {
181: this .target = target;
182: }
183:
184: public Relay.Target create(UID uid, MessageAddress source,
185: Object content, Relay.Token token) {
186: BlackboardAlert query = (BlackboardAlert) content;
187: return new BlackboardAlertRelay(uid, source, target, query);
188: }
189: }
190: }
|