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.core.examples.mobility.ldm;
027:
028: import java.io.Serializable;
029: import java.util.Set;
030: import java.util.Collections;
031: import org.cougaar.core.mobility.Ticket;
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.core.relay.Relay;
034: import org.cougaar.core.util.UID;
035:
036: /**
037: * Package-private implementation for a step where
038: * the target ("actor") is a remote agent.
039: * <p>
040: * This allows the client on agent A to ask agent
041: * B to tell agent C to move.
042: * <p>
043: * This uses the Relay support to transfer the data
044: * between the source agent and actor agent.
045: */
046: class RemoteStepImpl extends LocalStepImpl implements Relay.Source,
047: Relay.Target {
048:
049: private transient Set _targets;
050:
051: public RemoteStepImpl(UID uid, StepOptions opt) {
052: super (uid, opt);
053: cacheTargets();
054: }
055:
056: // Relay.Source:
057:
058: private void cacheTargets() {
059: MessageAddress target = getOptions().getTarget();
060: _targets = Collections.singleton(target);
061: }
062:
063: public Set getTargets() {
064: return _targets;
065: }
066:
067: public Object getContent() {
068: return getOptions();
069: }
070:
071: public Relay.TargetFactory getTargetFactory() {
072: return RemoteStepImplFactory.INSTANCE;
073: }
074:
075: public int updateResponse(MessageAddress t, Object response) {
076: StepStatus oldS = getStatus();
077: // assert oldS != null
078: StepStatus newS = (StepStatus) response;
079: if (newS == null) {
080: newS = StepStatus.NONE;
081: }
082: // assert local-agent == getSource()
083: if (!(oldS.equals(newS))) {
084: setStatus(newS);
085: return Relay.RESPONSE_CHANGE;
086: }
087: return Relay.NO_CHANGE;
088: }
089:
090: // Relay.Target:
091:
092: public MessageAddress getSource() {
093: return getOptions().getSource();
094: }
095:
096: public Object getResponse() {
097: StepStatus ss = getStatus();
098: return ((ss != StepStatus.NONE) ? ss : null);
099: }
100:
101: public int updateContent(Object content, Token token) {
102: // currently the content (opt) is immutable
103: return Relay.NO_CHANGE;
104: }
105:
106: private void readObject(java.io.ObjectInputStream os)
107: throws ClassNotFoundException, java.io.IOException {
108: os.defaultReadObject();
109: cacheTargets();
110: }
111:
112: public String toString() {
113: return "remote-" + _toString();
114: }
115:
116: /**
117: * Simple factory implementation.
118: */
119: private static class RemoteStepImplFactory implements
120: Relay.TargetFactory, Serializable {
121:
122: public static RemoteStepImplFactory INSTANCE = new RemoteStepImplFactory();
123:
124: private RemoteStepImplFactory() {
125: }
126:
127: public Relay.Target create(UID uid, MessageAddress source,
128: Object content, Relay.Token token) {
129: StepOptions so = (StepOptions) content;
130: return new RemoteStepImpl(uid, so);
131: }
132:
133: private Object readResolve() {
134: return INSTANCE;
135: }
136: }
137: }
|