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.ArrayList;
030: import java.util.Iterator;
031:
032: import org.cougaar.core.mts.AgentState;
033: import org.cougaar.core.mts.Message;
034: import org.cougaar.core.mts.MessageAddress;
035: import org.cougaar.core.mts.MessageAttributes;
036: import org.cougaar.core.mts.MessageTransportClient;
037: import org.cougaar.core.service.MessageTransportService;
038: import org.cougaar.mts.std.AttributedMessage;
039:
040: /**
041: * Currently the only implementation of MessageTransportService. It
042: * does almost nothing by itself - its work is accomplished by
043: * redirecting calls to the corresponding {@link SendLink}.
044: */
045: public class MessageTransportServiceProxy implements
046: MessageTransportService {
047: private SendLink link;
048: private MessageTransportClient client;
049: private boolean registered = false;
050:
051: public MessageTransportServiceProxy(MessageTransportClient client,
052: SendLink link) {
053: this .client = client;
054: this .link = link;
055: }
056:
057: synchronized long getIncarnationNumber() {
058: return client != null ? client.getIncarnationNumber() : 0;
059: }
060:
061: void release() {
062: synchronized (this ) {
063: if (registered)
064: unregisterClient(client);
065: client = null;
066: }
067: link.release();
068: link = null;
069: }
070:
071: /**
072: * Redirects the sendMessage to the SendQueue. */
073: public void sendMessage(Message rawMessage) {
074: MessageAttributes attrs = rawMessage.getTarget()
075: .getMessageAttributes();
076: AttributedMessage message = new AttributedMessage(rawMessage,
077: attrs);
078: if (link.okToSend(message))
079: link.sendMessage(message);
080: }
081:
082: /**
083: * Wait for all queued messages for our client to be either
084: * delivered or dropped.
085: * @return the list of dropped messages, which could be null.
086: */
087: public synchronized ArrayList flushMessages() {
088: ArrayList droppedMessages = new ArrayList();
089: link.flushMessages(droppedMessages);
090: ArrayList rawMessages = new ArrayList(droppedMessages.size());
091: Iterator itr = droppedMessages.iterator();
092: while (itr.hasNext()) {
093: AttributedMessage m = (AttributedMessage) itr.next();
094: rawMessages.add(m.getRawMessage());
095: }
096: return rawMessages;
097: }
098:
099: public AgentState getAgentState() {
100: return link.getAgentState();
101: }
102:
103: /**
104: * Redirects the request to the MessageTransportRegistry. */
105: public synchronized void registerClient(
106: MessageTransportClient client) {
107: // Should throw an exception of client != this.client
108: if (!registered) {
109: link.registerClient(client);
110: registered = true;
111: } else {
112: throw new IllegalStateException("Client "
113: + client.getMessageAddress()
114: + " is already registered in the MTS");
115: }
116: }
117:
118: /**
119: * Redirects the request to the MessageTransportRegistry. */
120: public synchronized void unregisterClient(
121: MessageTransportClient client) {
122: // Should throw an exception of client != this.client
123: if (registered) {
124: link.unregisterClient(client);
125: registered = false;
126: } else {
127: throw new IllegalStateException("Client "
128: + client.getMessageAddress()
129: + " is not registed in the MTS");
130: }
131:
132: // NB: The proxy (as opposed to the client) CANNOT be
133: // unregistered here. If it were, messageDelivered callbacks
134: // wouldn't be delivered and flush could block forever.
135: // Unregistering the proxy can only happen as part of
136: // releasing the service (see release());
137:
138: }
139:
140: /**
141: * Redirects the request to the MessageTransportRegistry. */
142: public String getIdentifier() {
143: return link.getIdentifier();
144: }
145:
146: /**
147: * Redirects the request to the MessageTransportRegistry. */
148: public boolean addressKnown(MessageAddress a) {
149: return link.addressKnown(a);
150: }
151:
152: }
|