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 org.cougaar.core.thread.Schedulable;
030: import org.cougaar.mts.base.SendLink;
031: import org.cougaar.mts.base.SendLinkDelegateImplBase;
032: import org.cougaar.mts.base.StandardAspect;
033:
034: /**
035: * This test Aspect scrambles the order of messages. Its main purpose
036: * is to test the effectiveness of the {@link SequenceAspect}.
037: *
038: */
039: public class ScrambleAspect extends StandardAspect {
040: public ScrambleAspect() {
041: }
042:
043: public Object getDelegate(Object delegate, Class type) {
044: if (type == SendLink.class) {
045: return new ScrambledSendLink((SendLink) delegate);
046: } else {
047: return null;
048: }
049: }
050:
051: private class ScrambledSendLink extends SendLinkDelegateImplBase {
052:
053: Schedulable sender;
054: AttributedMessage heldMessage;
055:
056: int heldMessageCount;
057: int flippedMessageCount;
058: int forcedMessageCount;
059: int messageCount;
060:
061: private ScrambledSendLink(SendLink link) {
062: super (link);
063: //long timeStarted = System.currentTimeMillis();
064: }
065:
066: private class MessageSender implements Runnable {
067: public void run() {
068: forcedHeldMessage();
069: }
070: }
071:
072: public synchronized void sendMessage(AttributedMessage message) {
073: messageCount++;
074: if (heldMessage == null)
075: holdMessage(message);
076: else
077: flipMessage(message);
078: }
079:
080: //================util methods
081: private void holdMessage(AttributedMessage message) {
082: heldMessage = message;
083: MessageSender sender_body = new MessageSender();
084: sender = threadService.getThread(this , sender_body,
085: "Scramble");
086: sender.schedule(300);
087: heldMessageCount++;
088: if (loggingService.isDebugEnabled())
089: loggingService.debug("Holding message #"
090: + printString() + " " + heldMessageCount);
091: }
092:
093: private void flipMessage(AttributedMessage message) {
094: sender.cancel();
095: // Cancelling the task doesn't guarantee that it won't
096: // run. But the only purpose of this aspect is to test
097: // weird cases (messages out of order) so it might as well
098: // test duplicates sometimes too...
099:
100: super .sendMessage(message);
101: super .sendMessage(heldMessage);
102: heldMessage = null;
103: flippedMessageCount++;
104: int previousCount = messageCount - 1;
105: if (loggingService.isDebugEnabled())
106: loggingService.debug("Flipping messages #"
107: + previousCount + " and #" + printString()
108: + " and " + message.getTarget() + " "
109: + flippedMessageCount);
110: }
111:
112: private synchronized void forcedHeldMessage() {
113: if (heldMessage != null) {
114: super .sendMessage(heldMessage);
115: forcedMessageCount++;
116: if (loggingService.isDebugEnabled())
117: loggingService
118: .debug("Forcing message #" + printString()
119: + " " + forcedMessageCount);
120: heldMessage = null;
121: }
122: }
123:
124: private String printString() {
125: return messageCount + " from "
126: + heldMessage.getOriginator() + " to "
127: + heldMessage.getTarget();
128: }
129: //===========================
130: }
131:
132: }
|