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.std;
028:
029: import java.io.ByteArrayOutputStream;
030: import java.io.ObjectOutputStream;
031: import java.util.HashMap;
032:
033: import org.cougaar.core.mts.MessageAddress;
034: import org.cougaar.core.mts.MessageAttributes;
035: import org.cougaar.core.mts.MessageTransportClient;
036:
037: import org.cougaar.mts.base.MisdeliveredMessageException;
038: import org.cougaar.mts.base.DestinationLink;
039: import org.cougaar.mts.base.LinkProtocol;
040:
041: /**
042: * This {@link LinkProtocol} is purely a debugging aid. It fails by
043: * design, throwing a MisdeliveredMessageException, after serializing.
044: */
045: class FutileSerializingLinkProtocol extends LinkProtocol
046:
047: {
048:
049: private HashMap links;
050:
051: public FutileSerializingLinkProtocol() {
052: super ();
053: links = new HashMap();
054: }
055:
056: public synchronized DestinationLink getDestinationLink(
057: MessageAddress address) {
058: DestinationLink link = (DestinationLink) links.get(address);
059: if (link == null) {
060: link = new Link(address);
061: link = (DestinationLink) attachAspects(link,
062: DestinationLink.class);
063: links.put(address, link);
064: }
065: return link;
066: }
067:
068: public void registerClient(MessageTransportClient client) {
069: // Does nothing because the Database of local clients is held
070: // by MessageTransportServerImpl
071: }
072:
073: public void unregisterClient(MessageTransportClient client) {
074: // Does nothing because the Database of local clients is held
075: // by MessageTransportServerImpl
076: }
077:
078: public boolean addressKnown(MessageAddress address) {
079: // we know everybody
080: return true;
081: }
082:
083: class Link implements DestinationLink {
084: private AttributedMessage lastMessage;
085: private MessageAddress address;
086: private int count;
087:
088: Link(MessageAddress address) {
089: this .address = address;
090: }
091:
092: public MessageAddress getDestination() {
093: return address;
094: }
095:
096: public boolean isValid() {
097: return true;
098: }
099:
100: public int cost(AttributedMessage msg) {
101: if (lastMessage != msg) {
102: lastMessage = msg;
103: count = 1;
104: return 400;
105: } else if (count < 3) {
106: count++;
107: return 400;
108: } else {
109: lastMessage = null;
110: return Integer.MAX_VALUE;
111: }
112: }
113:
114: private void serialize(AttributedMessage message) {
115: ByteArrayOutputStream baos = new ByteArrayOutputStream();
116: ObjectOutputStream oos = null;
117:
118: try {
119: oos = new ObjectOutputStream(baos);
120: oos.writeObject(message);
121: } catch (java.io.IOException ioe) {
122: if (loggingService.isErrorEnabled())
123: loggingService.error(null, ioe);
124: return;
125: }
126:
127: try {
128: oos.close();
129: } catch (java.io.IOException ioe2) {
130: if (loggingService.isErrorEnabled())
131: loggingService.error(null, ioe2);
132: }
133:
134: if (loggingService.isInfoEnabled())
135: loggingService.info("Serialized " + message);
136: }
137:
138: public MessageAttributes forwardMessage(
139: AttributedMessage message)
140: throws MisdeliveredMessageException {
141: serialize(message);
142: throw new MisdeliveredMessageException(message);
143: }
144:
145: public boolean retryFailedMessage(AttributedMessage message,
146: int retryCount) {
147: return true;
148: }
149:
150: public Class getProtocolClass() {
151: return FutileSerializingLinkProtocol.class;
152: }
153:
154: public Object getRemoteReference() {
155: return null;
156: }
157:
158: public void addMessageAttributes(MessageAttributes attrs) {
159:
160: }
161:
162: }
163:
164: }
|