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.util.Iterator;
030:
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.mts.MessageAttributes;
033: import org.cougaar.core.mts.MulticastMessageAddress;
034: import org.cougaar.core.mts.SimpleMessageAttributes;
035: import org.cougaar.mts.base.MessageDeliverer;
036: import org.cougaar.mts.base.MessageDelivererDelegateImplBase;
037: import org.cougaar.mts.base.MisdeliveredMessageException;
038: import org.cougaar.mts.base.SendLink;
039: import org.cougaar.mts.base.SendLinkDelegateImplBase;
040: import org.cougaar.mts.base.StandardAspect;
041:
042: /**
043: * This Aspect provides support for multicast messages.
044: */
045: public class MulticastAspect extends StandardAspect {
046:
047: private static final String MCAST = "org.cougaar.message.transport.is-multicast";
048:
049: public Object getDelegate(Object delegatee, Class type) {
050: if (type == SendLink.class) {
051: return new SendLinkDelegate((SendLink) delegatee);
052: } else {
053: return null;
054: }
055: }
056:
057: public Object getReverseDelegate(Object delegatee, Class type) {
058: if (type == MessageDeliverer.class) {
059: return new MessageDelivererDelegate(
060: (MessageDeliverer) delegatee);
061: } else {
062: return null;
063: }
064: }
065:
066: public class SendLinkDelegate extends SendLinkDelegateImplBase {
067:
068: public SendLinkDelegate(SendLink link) {
069: super (link);
070: }
071:
072: public void sendMessage(AttributedMessage msg) {
073: MessageAddress destination = msg.getTarget();
074: if (destination instanceof MulticastMessageAddress) {
075: msg.setAttribute(MCAST, destination);
076: AttributedMessage copy;
077: MessageAddress nodeAddr;
078: if (destination.equals(MessageAddress.MULTICAST_LOCAL)) {
079: if (loggingService.isDebugEnabled())
080: loggingService.debug("MCAST: Local multicast");
081: nodeAddr = getNameSupport().getNodeMessageAddress();
082: copy = new AttributedMessage(msg);
083: copy.setTarget(nodeAddr);
084: super .sendMessage(copy);
085: } else {
086: if (loggingService.isDebugEnabled())
087: loggingService
088: .debug("MCAST: Remote multicast to "
089: + destination);
090: MulticastMessageAddress dst = (MulticastMessageAddress) destination;
091: Iterator itr = getRegistry()
092: .findRemoteMulticastTransports(dst);
093: while (itr.hasNext()) {
094: nodeAddr = (MessageAddress) itr.next();
095: if (loggingService.isDebugEnabled())
096: loggingService
097: .debug("MCAST: next address = "
098: + nodeAddr);
099: copy = new AttributedMessage(msg);
100: copy.setTarget(nodeAddr);
101: super .sendMessage(copy);
102: }
103: }
104: } else {
105: super .sendMessage(msg);
106: }
107: }
108:
109: }
110:
111: public class MessageDelivererDelegate extends
112: MessageDelivererDelegateImplBase {
113:
114: public MessageDelivererDelegate(MessageDeliverer deliverer) {
115: super (deliverer);
116: }
117:
118: public MessageAttributes deliverMessage(AttributedMessage msg,
119: MessageAddress destination)
120: throws MisdeliveredMessageException {
121: MulticastMessageAddress mcastAddr = (MulticastMessageAddress) msg
122: .getAttribute(MCAST);
123:
124: if (mcastAddr != null) {
125: if (loggingService.isDebugEnabled())
126: loggingService
127: .debug("MCAST: Received multicast to "
128: + mcastAddr);
129: Iterator i = getRegistry().findLocalMulticastReceivers(
130: mcastAddr);
131: MessageAddress localDestination = null;
132: AttributedMessage copy = new AttributedMessage(msg
133: .getRawMessage(), msg);
134: while (i.hasNext()) {
135: localDestination = (MessageAddress) i.next();
136: if (loggingService.isDebugEnabled())
137: loggingService.debug("MCAST: Delivering to "
138: + localDestination);
139: super .deliverMessage(copy, localDestination);
140: }
141: // Hmm...
142: MessageAttributes meta = new SimpleMessageAttributes();
143: meta.setAttribute(MessageAttributes.DELIVERY_ATTRIBUTE,
144: MessageAttributes.DELIVERY_STATUS_DELIVERED);
145: return meta;
146: } else {
147: return super.deliverMessage(msg, destination);
148: }
149: }
150:
151: }
152:
153: }
|