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.mts.MessageAttributes;
030: import org.cougaar.mts.base.MisdeliveredMessageException;
031: import org.cougaar.mts.base.CommFailureException;
032: import org.cougaar.mts.base.UnregisteredNameException;
033: import org.cougaar.mts.base.NameLookupException;
034: import org.cougaar.mts.base.DestinationLink;
035: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
036: import org.cougaar.mts.base.StandardAspect;
037:
038: /**
039: * This debugging Aspect deliberately delays message processing by
040: * wasting CPU for pseudo-random durations.
041: */
042: public class WasteCPUAspect extends StandardAspect {
043: //This was taken from TrafficGenerator should be a math utils
044: private static class ExpRandom extends java.util.Random {
045:
046: ExpRandom() {
047: //super is uniform distribution
048: super ();
049: }
050:
051: // period is the average period,
052: // the range can go from zero to ten times the period
053: public int nextInt(int period) {
054: double raw = -(period * Math.log(super .nextDouble()));
055: // clip upper tail
056: if (raw > 10 * period) {
057: return 10 * period;
058: } else
059: return (int) Math.round(raw);
060: }
061: }
062:
063: public Object getDelegate(Object object, Class type) {
064: if (type == DestinationLink.class) {
065: DestinationLink link = (DestinationLink) object;
066: return new WasteCPUDestinationLink(link);
067: } else {
068: return null;
069: }
070: }
071:
072: ExpRandom expRandom = new ExpRandom();
073:
074: private class WasteCPUDestinationLink extends
075: DestinationLinkDelegateImplBase
076:
077: {
078:
079: WasteCPUDestinationLink(DestinationLink link) {
080: super (link);
081: }
082:
083: public synchronized MessageAttributes forwardMessage(
084: AttributedMessage message)
085: throws UnregisteredNameException, NameLookupException,
086: CommFailureException, MisdeliveredMessageException
087:
088: {
089: // Serialize into the stream rather than pushing on the
090: // queue.
091: long count = 0;
092: long startTime = System.currentTimeMillis();
093: int wasteTime = expRandom.nextInt(166);
094: while (System.currentTimeMillis() - startTime < wasteTime) {
095: count++;
096: }
097:
098: // CougaarThread.yield();
099:
100: startTime = System.currentTimeMillis();
101: while (System.currentTimeMillis() - startTime < wasteTime) {
102: count++;
103: }
104:
105: // CougaarThread.yield();
106:
107: startTime = System.currentTimeMillis();
108: while (System.currentTimeMillis() - startTime < wasteTime) {
109: count++;
110: }
111:
112: return super.forwardMessage(message);
113:
114: }
115: }
116: }
|