001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.mts.base;
028:
029: import java.util.HashMap;
030:
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.mts.MessageAttributes;
033: import org.cougaar.core.mts.MessageTransportClient;
034: import org.cougaar.mts.std.AttributedMessage;
035:
036: /**
037: * This protocol Component handles intra-node message traffic.
038: *
039: */
040: public class LoopbackLinkProtocol extends LinkProtocol {
041:
042: private HashMap links;
043:
044: public LoopbackLinkProtocol() {
045: super ();
046: links = new HashMap();
047: }
048:
049: public synchronized DestinationLink getDestinationLink(
050: MessageAddress address) {
051: DestinationLink link = (DestinationLink) links.get(address);
052: if (link == null) {
053: link = new Link(address);
054: link = (DestinationLink) attachAspects(link,
055: DestinationLink.class);
056: links.put(address, link);
057: }
058: return link;
059: }
060:
061: public void registerClient(MessageTransportClient client) {
062: // Does nothing because the Database of local clients is held
063: // by MessageTransportServerImpl
064: }
065:
066: public void unregisterClient(MessageTransportClient client) {
067: // Does nothing because the Database of local clients is held
068: // by MessageTransportServerImpl
069: }
070:
071: public boolean addressKnown(MessageAddress address) {
072: // true iff the address is local
073: return getRegistry().isLocalClient(address);
074: }
075:
076: private class Link implements DestinationLink {
077: MessageAddress address;
078:
079: Link(MessageAddress address) {
080: this .address = address;
081: }
082:
083: public int cost(AttributedMessage msg) {
084: MessageAddress addr = msg.getTarget();
085: if (getRegistry().isLocalClient(addr)) {
086: return 0;
087: } else {
088: return Integer.MAX_VALUE;
089: }
090: }
091:
092: public boolean isValid() {
093: return getRegistry().isLocalClient(address);
094: }
095:
096: public MessageAttributes forwardMessage(
097: AttributedMessage message)
098: throws MisdeliveredMessageException {
099: return getDeliverer().deliverMessage(message,
100: message.getTarget());
101: }
102:
103: public boolean retryFailedMessage(AttributedMessage message,
104: int retryCount) {
105: return true;
106: }
107:
108: public Class getProtocolClass() {
109: return LoopbackLinkProtocol.class;
110: }
111:
112: public MessageAddress getDestination() {
113: return address;
114: }
115:
116: public Object getRemoteReference() {
117: return null;
118: }
119:
120: public void addMessageAttributes(MessageAttributes attrs) {
121:
122: }
123:
124: }
125:
126: }
|